commit bb80e8f66ffabf0944542dac2bbe03f1fb181540 Author: Daniel Ponte Date: Thu Apr 4 19:11:20 2024 -0400 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed8cd3c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +welhproxy diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..493247b --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +all: + go build welhproxy.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..53725ed --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.dynatron.me/amigan/welhproxy + +go 1.21.6 diff --git a/welhproxy.go b/welhproxy.go new file mode 100644 index 0000000..48e8912 --- /dev/null +++ b/welhproxy.go @@ -0,0 +1,85 @@ +package main + +import ( + "bufio" + "errors" + "fmt" + "log" + "net/http" + "strings" +) + +type WELHProxy struct { + Listen string +} + +func (p *WELHProxy) String() string { + return p.Listen +} + +func (p *WELHProxy) fetchSite(_ http.ResponseWriter, r *http.Request) (url string, err error) { + req, err := http.NewRequestWithContext(r.Context(), "GET", "https://welh.radio12345.com/openfire.ajax.php?radio_id=1172158", nil) + if err != nil { + return "", err + } + + getResp, err := http.DefaultClient.Do(req) + if err != nil { + return "", err + } + + getResp.Body.Close() + req, err = http.NewRequestWithContext(r.Context(), "GET", "https://welh.radio12345.com/", nil) + if err != nil { + return "", err + } + + getResp, err = http.DefaultClient.Do(req) + if err != nil { + return "", err + } + + defer getResp.Body.Close() + + if getResp.StatusCode < 300 && getResp.StatusCode >= 200 { + s := bufio.NewScanner(getResp.Body) + for s.Scan() { + t := strings.TrimSpace(s.Text()) + if strings.Contains(t, `", "")), nil + } + } + return "", fmt.Errorf("could not find stream URL") + } + + return "", fmt.Errorf("failed to get url: %+v", getResp) +} + +func (p *WELHProxy) handle(w http.ResponseWriter, r *http.Request) { + log.Println(r) + url, err := p.fetchSite(w, r) + if err != nil { + log.Println(err) + } + http.Redirect(w, r, url, http.StatusFound) +} + +func (p *WELHProxy) Go() { + http.HandleFunc("/", p.handle) + log.Fatal(http.ListenAndServe(p.Listen, nil)) +} + +func NewWELHProxy(addr string) *WELHProxy { + return &WELHProxy{ + Listen: addr, + } +} + +func main() { + server := NewWELHProxy(":8534") + log.Printf("welhproxy starting on %s", server) + server.Go() +}