Created
May 14, 2023 00:09
-
-
Save ARACOOOL/9895a66551092fb3e3a004533188fb42 to your computer and use it in GitHub Desktop.
proxy.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"fmt" | |
"io" | |
"log" | |
"net" | |
"net/http" | |
"time" | |
) | |
func handleTunneling(w http.ResponseWriter, r *http.Request) { | |
destConn, err := net.DialTimeout("tcp", r.Host, 10*time.Second) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusServiceUnavailable) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
hijacker, ok := w.(http.Hijacker) | |
if !ok { | |
http.Error(w, "Hijacking not supported", http.StatusInternalServerError) | |
return | |
} | |
clientConn, _, err := hijacker.Hijack() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusServiceUnavailable) | |
} | |
go transfer(destConn, clientConn) | |
go transfer(clientConn, destConn) | |
} | |
func transfer(destination io.WriteCloser, source io.ReadCloser) { | |
defer destination.Close() | |
defer source.Close() | |
io.Copy(destination, source) | |
} | |
func handleHTTP(w http.ResponseWriter, req *http.Request) { | |
resp, err := http.DefaultTransport.RoundTrip(req) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusServiceUnavailable) | |
return | |
} | |
defer resp.Body.Close() | |
copyHeader(w.Header(), resp.Header) | |
w.WriteHeader(resp.StatusCode) | |
io.Copy(w, resp.Body) | |
} | |
func copyHeader(dst, src http.Header) { | |
for k, vv := range src { | |
for _, v := range vv { | |
dst.Add(k, v) | |
} | |
} | |
} | |
func main() { | |
server := &http.Server{ | |
Addr: ":8888", | |
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
fmt.Printf("Req: %+v %s\n", r.URL, r.URL.Path) | |
fmt.Println("GET params were:", r.URL.Query()) | |
if r.Method == http.MethodConnect { | |
handleTunneling(w, r) | |
} else { | |
handleHTTP(w, r) | |
} | |
}), | |
// Disable HTTP/2. | |
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), | |
} | |
log.Fatal(server.ListenAndServe()) | |
} |
@iamwavecut Thank you for suggestion. fmt.Printf("%s://%s%s", r.URL.Scheme, r.Host, r.RequestURI)
This is what it outputs
://www.youtube.com:443www.youtube.com:443
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
fmt.Sprintf("%s://%s%s", r.URL.Scheme, r.Host, r.RequestURI())