86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
|
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()
|
||
|
}
|