2020-10-24 15:47:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-10-25 12:41:17 +00:00
|
|
|
"io"
|
2020-10-24 15:47:41 +00:00
|
|
|
"log"
|
2020-10-25 12:41:17 +00:00
|
|
|
"net"
|
2020-10-24 15:47:41 +00:00
|
|
|
"net/http"
|
2020-10-25 12:41:17 +00:00
|
|
|
"net/url"
|
2020-10-25 14:01:23 +00:00
|
|
|
"os"
|
2020-10-25 12:41:17 +00:00
|
|
|
"strings"
|
2020-10-25 12:52:02 +00:00
|
|
|
"syscall"
|
2021-03-12 06:59:53 +00:00
|
|
|
"time"
|
2020-10-24 15:47:41 +00:00
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go/http3"
|
|
|
|
)
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
// http/3 client
|
|
|
|
var h3client = &http.Client{
|
2020-10-24 15:47:41 +00:00
|
|
|
Transport: &http3.RoundTripper{},
|
|
|
|
}
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
// http/2 client
|
2021-03-12 06:59:53 +00:00
|
|
|
var h2client = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
Dial: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
}).Dial,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ResponseHeaderTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
2021-04-09 08:50:14 +00:00
|
|
|
IdleConnTimeout: 30 * time.Second,
|
2021-03-12 06:59:53 +00:00
|
|
|
},
|
|
|
|
}
|
2020-10-25 12:41:17 +00:00
|
|
|
|
|
|
|
// user agent to use
|
|
|
|
var ua = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101"
|
|
|
|
|
2021-03-04 08:57:42 +00:00
|
|
|
type requesthandler struct{}
|
|
|
|
|
|
|
|
func (*requesthandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2020-10-25 12:41:17 +00:00
|
|
|
|
|
|
|
q := req.URL.Query()
|
|
|
|
host := q.Get("host")
|
|
|
|
q.Del("host")
|
|
|
|
|
2020-12-10 13:37:10 +00:00
|
|
|
if len(host) <= 0 {
|
|
|
|
host = q.Get("hls_chunk_host")
|
|
|
|
}
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
if len(host) <= 0 {
|
|
|
|
host = getHost(req.URL.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(host) <= 0 {
|
2021-01-20 06:17:24 +00:00
|
|
|
io.WriteString(w, "No host in query parameters.")
|
|
|
|
return
|
2020-10-25 12:41:17 +00:00
|
|
|
}
|
2020-10-24 15:47:41 +00:00
|
|
|
|
2020-11-12 09:11:17 +00:00
|
|
|
path := req.URL.Path
|
|
|
|
|
|
|
|
path = strings.Replace(path, "/ggpht", "", 1)
|
|
|
|
path = strings.Replace(path, "/i/", "/", 1)
|
|
|
|
|
|
|
|
proxyURL, err := url.Parse("https://" + host + path)
|
2020-10-24 15:47:41 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-10-25 12:41:17 +00:00
|
|
|
log.Panic(err)
|
2020-10-24 15:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
proxyURL.RawQuery = q.Encode()
|
|
|
|
|
2020-10-27 09:59:49 +00:00
|
|
|
if strings.HasSuffix(proxyURL.Path, "maxres.jpg") {
|
|
|
|
proxyURL.Path = getBestThumbnail(proxyURL.Path)
|
|
|
|
}
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
request, err := http.NewRequest("GET", proxyURL.String(), nil)
|
|
|
|
|
|
|
|
copyHeaders(req.Header, request.Header)
|
|
|
|
request.Header.Set("User-Agent", ua)
|
2020-10-24 15:47:41 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2020-10-25 12:41:17 +00:00
|
|
|
log.Panic(err)
|
2020-10-24 15:47:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
var client *http.Client
|
|
|
|
|
2020-10-25 15:57:05 +00:00
|
|
|
// https://github.com/lucas-clemente/quic-go/issues/2836
|
|
|
|
client = h2client
|
2020-10-25 12:41:17 +00:00
|
|
|
|
|
|
|
resp, err := client.Do(request)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2021-03-04 08:57:42 +00:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
copyHeaders(resp.Header, w.Header())
|
|
|
|
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
|
2021-04-09 08:49:38 +00:00
|
|
|
go io.Copy(w, resp.Body)
|
2020-10-25 12:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func copyHeaders(from http.Header, to http.Header) {
|
|
|
|
// Loop over header names
|
|
|
|
for name, values := range from {
|
|
|
|
// Loop over all values for the name.
|
|
|
|
for _, value := range values {
|
|
|
|
to.Set(name, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getHost(path string) (host string) {
|
|
|
|
|
|
|
|
host = ""
|
|
|
|
|
2020-11-13 07:45:51 +00:00
|
|
|
if strings.HasPrefix(path, "/vi/") || strings.HasPrefix(path, "/vi_webp/") || strings.HasPrefix(path, "/sb/") {
|
2020-10-25 12:41:17 +00:00
|
|
|
host = "i.ytimg.com"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(path, "/ggpht/") {
|
|
|
|
host = "yt3.ggpht.com"
|
|
|
|
}
|
|
|
|
|
2020-11-13 19:36:08 +00:00
|
|
|
if strings.HasPrefix(path, "/a/") || strings.HasPrefix(path, "/ytc/") {
|
2020-10-25 12:41:17 +00:00
|
|
|
host = "yt3.ggpht.com"
|
|
|
|
}
|
|
|
|
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
2020-10-27 09:59:49 +00:00
|
|
|
func getBestThumbnail(path string) (newpath string) {
|
|
|
|
|
|
|
|
formats := [4]string{"maxresdefault.jpg", "sddefault.jpg", "hqdefault.jpg", "mqdefault.jpg"}
|
|
|
|
|
|
|
|
for _, format := range formats {
|
|
|
|
newpath = strings.Replace(path, "maxres.jpg", format, 1)
|
|
|
|
url := "https://i.ytimg.com" + newpath
|
|
|
|
resp, _ := h2client.Head(url)
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
return newpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Replace(path, "maxres.jpg", "mqdefault.jpg", 1)
|
|
|
|
}
|
|
|
|
|
2020-10-25 12:41:17 +00:00
|
|
|
func main() {
|
2020-10-25 14:01:23 +00:00
|
|
|
socket := "socket" + string(os.PathSeparator) + "http-proxy.sock"
|
|
|
|
syscall.Unlink(socket)
|
|
|
|
listener, err := net.Listen("unix", socket)
|
2021-03-12 06:59:53 +00:00
|
|
|
srv := &http.Server{
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
Addr: ":8080",
|
|
|
|
Handler: &requesthandler{},
|
|
|
|
}
|
2020-10-25 12:41:17 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to bind to UDS, falling back to TCP/IP")
|
|
|
|
fmt.Println(err.Error())
|
2021-03-12 06:59:53 +00:00
|
|
|
srv.ListenAndServe()
|
2020-10-25 12:41:17 +00:00
|
|
|
} else {
|
2021-03-04 08:57:42 +00:00
|
|
|
defer listener.Close()
|
2021-03-12 06:59:53 +00:00
|
|
|
srv.Serve(listener)
|
2020-10-25 12:41:17 +00:00
|
|
|
}
|
2020-10-24 15:47:41 +00:00
|
|
|
}
|