-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathneu.go
69 lines (63 loc) · 1.69 KB
/
neu.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
package main
import (
"bytes"
"crypto/tls"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
log "github.com/tominescu/double-golang/simplelog"
)
const PROXY_PREFIX = "https://6proxy.6box.cn/index.php?q="
func neutsHandler(w http.ResponseWriter, r *http.Request) {
log.Info("Request URL:%s", r.URL)
url := "https://ts.neu6.edu.cn" + strings.TrimPrefix(r.URL.Path, "/neu")
bt := time.Now()
resp, err := ProxyGet(url)
if err != nil {
http.Error(w, err.Error(), 503)
return
}
defer resp.Body.Close()
n, err := io.Copy(w, resp.Body)
et := time.Now()
cost := et.Sub(bt).Nanoseconds()
log.Info("Curl url: %s download: %.2fMB cost: %.2fs speed:%.2fMbps threadNum:%d", url, float64(n)/1024/1024, float64(cost)/1e9, float64(n)/float64(cost)*1e9/1024/1024*8, DefaultThreadNum)
}
func neuHandler(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
}
url := "https://media2.neu6.edu.cn/hls/" + id + ".m3u8"
resp, err := ProxyGet(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
}
newBody := bytes.Replace(body, []byte("https://ts.neu6.edu.cn"), []byte("http://"+r.Host+"/neu"), -1)
w.Write(newBody)
}
func ProxyGet(u string) (resp *http.Response, err error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
u = PROXY_PREFIX + url.QueryEscape(u)
return client.Get(u)
}