-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrtn.go
67 lines (64 loc) · 1.52 KB
/
grtn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
log "github.com/tominescu/double-golang/simplelog"
)
func grtnHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Request URL:%s", r.URL)
err := r.ParseForm()
if err != nil {
http.Error(w, http.StatusText(503), 503)
return
}
id := r.Form.Get("id")
if id == "" {
http.Error(w, http.StatusText(503), 503)
return
}
uri := "http://www.gdtv.cn/m2o/channel/channel_info.php?id=" + id
client := &http.Client{}
req, _ := http.NewRequest("GET", uri, nil)
req.Header.Set("User-Agent", "curl/7.52.1")
resp, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), 503)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, err.Error(), 503)
return
}
re := regexp.MustCompile(`http:[^\"]*\.m3u8\?_upt=\w*`)
hls := re.Find(body)
dst := strings.Replace(string(hls), "\\", "", -1)
req, _ = http.NewRequest("GET", dst, nil)
req.Header.Set("User-Agent", "curl/7.52.1")
resp2, err := client.Do(req)
if err != nil {
http.Error(w, err.Error(), 503)
return
}
defer resp2.Body.Close()
body, err = ioutil.ReadAll(resp2.Body)
re = regexp.MustCompile(`.*\.m3u8\?_upt=.*`)
hls = re.Find(body)
u, err := url.Parse(string(hls))
if err != nil {
http.Error(w, err.Error(), 503)
return
}
base, err := url.Parse(dst)
if err != nil {
http.Error(w, err.Error(), 503)
return
}
dst = base.ResolveReference(u).String()
w.Header().Set("Location", dst)
http.Error(w, dst, 302)
}