This commit is contained in:
Daniel Ponte 2024-04-04 19:11:20 -04:00
commit bb80e8f66f
4 changed files with 91 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
welhproxy

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
go build welhproxy.go

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.dynatron.me/amigan/welhproxy
go 1.21.6

85
welhproxy.go Normal file
View File

@ -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, `<div style="display:none" id="urladdress">`) {
if !s.Scan() {
return "", errors.New("scan")
}
return strings.TrimSpace(strings.ReplaceAll(s.Text(), "</div>", "")), 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()
}