-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path4gtv.go
94 lines (89 loc) · 2.5 KB
/
4gtv.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"bytes"
"io/ioutil"
"net/http"
"regexp"
"strings"
log "github.com/tominescu/double-golang/simplelog"
)
func fourgtvTsHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Request URL:%s", r.URL)
url := "https://4gtvfreemobile-cds.cdn.hinet.net" + r.URL.Path
log.Debug("Curl url: %s", url)
resp, err := http.Get(url)
if err != nil {
http.Error(w, err.Error(), 503)
return
}
defer resp.Body.Close()
MyCopy(w, resp.Body)
}
func fourgtvApiHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Request URL:%s", r.URL)
url := "https://p-sirona-yond-4gtv.svc.litv.tv" + r.URL.Path
log.Debug("Curl url: %s", url)
resp, err := http.Get(url)
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
}
body = bytes.Replace(body, []byte("https://4gtvfreemobile-cds.cdn.hinet.net"), []byte("http://"+r.Host), -1)
w.Write(body)
}
func fourgtvHandler(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
}
jsonStr := `{"jsonrpc":"2.0","id":12,"method":"LoadService.GetURLsNoAuth","params":{"AssetId":"` + id + `","DeviceType":"mobile","MediaType":"channel"}}`
req, err := http.NewRequest("POST", "https://twproxy02.svc.litv.tv/cdi/4gtv/rpc", bytes.NewBufferString(jsonStr))
req.Header.Set("Content-Type", "application/json; charset=utf-8")
resp, err := gclient.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`)
hls := re.Find(body)
if len(hls) < 1 {
http.Error(w, "Cant't find m3u8 url", 503)
return
}
resp2, err := http.Get(string(hls))
if err != nil {
http.Error(w, err.Error(), 503)
return
}
defer resp2.Body.Close()
body, err = ioutil.ReadAll(resp2.Body)
re = regexp.MustCompile(`.*\.m3u8`)
m3u8 := re.FindAll(body, -1)
if len(m3u8) < 1 {
http.Error(w, "Cant't find m3u8 url", 503)
return
}
dst := string(bytes.Replace(hls, []byte("https://p-sirona-yond-4gtv.svc.litv.tv"), []byte("http://"+r.Host), -1))
dst = strings.Replace(dst, "master.m3u8", string(m3u8[len(m3u8)-1]), -1)
w.Header().Set("Location", dst)
http.Error(w, dst, 302)
}