mirror of
https://github.com/TeamPiped/http3-ytproxy.git
synced 2024-08-14 23:56:43 +00:00
Working release.
This commit is contained in:
parent
27ffcc7eee
commit
4cdba58f8e
2 changed files with 103 additions and 13 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -11,5 +11,8 @@
|
||||||
# Output of the go coverage tool, specifically when used with LiteIDE
|
# Output of the go coverage tool, specifically when used with LiteIDE
|
||||||
*.out
|
*.out
|
||||||
|
|
||||||
|
# Unix Domain Socket
|
||||||
|
*.sock
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
# vendor/
|
||||||
|
|
119
main.go
119
main.go
|
@ -2,32 +2,119 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/lucas-clemente/quic-go/http3"
|
"github.com/lucas-clemente/quic-go/http3"
|
||||||
)
|
)
|
||||||
|
|
||||||
var hclient = &http.Client{
|
// http/3 client
|
||||||
|
var h3client = &http.Client{
|
||||||
Transport: &http3.RoundTripper{},
|
Transport: &http3.RoundTripper{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// http/2 client
|
||||||
|
var h2client = &http.Client{}
|
||||||
|
|
||||||
|
// user agent to use
|
||||||
|
var ua = "Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101"
|
||||||
|
|
||||||
|
func genericHTTPProxy(w http.ResponseWriter, req *http.Request) {
|
||||||
|
|
||||||
|
q := req.URL.Query()
|
||||||
|
host := q.Get("host")
|
||||||
|
q.Del("host")
|
||||||
|
|
||||||
|
if len(host) <= 0 {
|
||||||
|
host = getHost(req.URL.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(host) <= 0 {
|
||||||
|
log.Panic("No host in query parameters.")
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyURL, err := url.Parse("https://" + host + strings.Replace(req.URL.Path, "/ggpht", "", 1))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
proxyURL.RawQuery = q.Encode()
|
||||||
|
|
||||||
|
request, err := http.NewRequest("GET", proxyURL.String(), nil)
|
||||||
|
|
||||||
|
copyHeaders(req.Header, request.Header)
|
||||||
|
request.Header.Set("User-Agent", ua)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var client *http.Client
|
||||||
|
|
||||||
|
if strings.HasPrefix(req.URL.Path, "/videoplayback") { // https://github.com/lucas-clemente/quic-go/issues/2836
|
||||||
|
client = h2client
|
||||||
|
} else {
|
||||||
|
client = h3client
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(request)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
copyHeaders(resp.Header, w.Header())
|
||||||
|
|
||||||
|
w.WriteHeader(resp.StatusCode)
|
||||||
|
|
||||||
|
io.Copy(w, resp.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = ""
|
||||||
|
|
||||||
|
if strings.HasPrefix(path, "/vi/") {
|
||||||
|
host = "i.ytimg.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(path, "/ggpht/") {
|
||||||
|
host = "yt3.ggpht.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(path, "/a/") {
|
||||||
|
host = "yt3.ggpht.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
return host
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("Sending QUIC request to YouTube...")
|
http.HandleFunc("/videoplayback", genericHTTPProxy)
|
||||||
|
http.HandleFunc("/vi/", genericHTTPProxy)
|
||||||
resp, err := hclient.Get("https://www.youtube.com/")
|
http.HandleFunc("/a/", genericHTTPProxy)
|
||||||
|
http.HandleFunc("/ggpht/", genericHTTPProxy)
|
||||||
|
listener, err := net.Listen("unix", "http-proxy.sock")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
fmt.Println("Failed to bind to UDS, falling back to TCP/IP")
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
http.ListenAndServe(":8080", nil)
|
||||||
|
} else {
|
||||||
|
http.Serve(listener, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(string(body))
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue