-
Notifications
You must be signed in to change notification settings - Fork 0
/
youproxy.go
131 lines (117 loc) · 3.01 KB
/
youproxy.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"flag"
"fmt"
"github.com/dragonsinth/slither/main/slitherserver/impl"
"html"
"io/ioutil"
"log"
"net/http"
"net/url"
"runtime"
"strings"
"time"
)
func main() {
addr := flag.String("addr", ":8080", "http service address")
flag.Parse()
if runtime.GOOS == "darwin" {
register("")
} else {
impl.Register("slither.dragonsinth.com")
register("y.dragonsinth.com")
}
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func register(hostname string) {
http.HandleFunc(hostname+"/", func(w http.ResponseWriter, r *http.Request) {
v := r.URL.Query()
vid := v.Get("v")
if vid == "" {
http.NotFound(w, r)
return
}
rsp, err := http.Get("https://www.youtube.com/get_video_info?video_id=" + url.QueryEscape(vid))
if err != nil {
http.Error(w, fmt.Sprintf("failed to get: %v", err), http.StatusInternalServerError)
return
}
if rsp.StatusCode < 200 || rsp.StatusCode > 299 {
http.Error(w, "proxy fetch failed", rsp.StatusCode)
return
}
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("failed to read: %v", err), http.StatusInternalServerError)
return
}
vals, err := url.ParseQuery(string(body))
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse: %v", err), http.StatusInternalServerError)
return
}
for k, v := range vals {
log.Printf("%s: %s", k, v)
}
u := ""
streams := append(vals["url_encoded_fmt_stream_map"], vals["adaptive_fmts"]...)
for _, s := range streams {
log.Println()
log.Printf("%+v", s)
svals, err := url.ParseQuery(s)
if err != nil {
log.Printf("failed to svals: %v", err)
continue
}
itags := svals["itag"]
urls := svals["url"]
for i := range itags {
switch itags[i] {
case "139":
u = urls[i]
break
case "140":
u = urls[i]
break
case "141":
u = urls[i]
break
}
}
}
if u == "" {
http.Error(w, fmt.Sprintf("no valid streams: %v", err), http.StatusBadRequest)
return
}
title := vals.Get("title")
thumbnail := vals.Get("thumbnail_url")
log.Printf("serving %s (%s): %s", vid, title, u)
httpGet(fmt.Sprintf(tmpl, html.EscapeString(title), html.EscapeString(u), html.EscapeString(thumbnail)), "text/html; charset=utf-8", w, r)
})
}
const tmpl = `
<html>
<head>
<title>%s</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
</head
<body style="zoom: 200%%;">
<audio style="width: 100%%;" src="%s" autoplay controls></audio>
<img style="width: 100%%;" src="%s"></img>
</body>
</html>
`
func httpGet(data string, contentType string, w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
httpError(w, http.StatusMethodNotAllowed)
return
}
w.Header().Add("Content-Type", contentType)
http.ServeContent(w, r, "", time.Now(), strings.NewReader(data))
}
func httpError(w http.ResponseWriter, code int) {
http.Error(w, http.StatusText(code), code)
}