Created
February 14, 2023 11:10
-
-
Save lsongdev/2d48c5d237dd02f81bed0d1ab10febea to your computer and use it in GitHub Desktop.
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 ( | |
"io" | |
"log" | |
"net" | |
"net/http" | |
"time" | |
) | |
func handleTunneling(w http.ResponseWriter, r *http.Request) { | |
dest_conn, err := net.DialTimeout("tcp", r.Host, 10*time.Second) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusServiceUnavailable) | |
return | |
} | |
hijacker, ok := w.(http.Hijacker) | |
if !ok { | |
http.Error(w, "Hijacking not supported", http.StatusInternalServerError) | |
return | |
} | |
w.WriteHeader(http.StatusOK) | |
client_conn, _, err := hijacker.Hijack() | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusServiceUnavailable) | |
} | |
go transfer(dest_conn, client_conn) | |
go transfer(client_conn, dest_conn) | |
} | |
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) | |
} | |
// Hop-by-hop headers. These are removed when sent to the backend. | |
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html | |
var hopHeaders = []string{ | |
"Connection", | |
"Keep-Alive", | |
"Proxy-Authenticate", | |
"Proxy-Authorization", | |
"Te", // canonicalized version of "TE" | |
"Trailers", | |
"Transfer-Encoding", | |
"Upgrade", | |
} | |
func copyHeader(dst, src http.Header) { | |
for k, vv := range src { | |
for _, v := range vv { | |
dst.Add(k, v) | |
} | |
} | |
} | |
func delHopHeaders(header http.Header) { | |
for _, h := range hopHeaders { | |
header.Del(h) | |
} | |
} | |
func main() { | |
server := &http.Server{ | |
Addr: ":1088", | |
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
log.Println(r.Method, r.URL) | |
if r.Method == http.MethodConnect { | |
handleTunneling(w, r) | |
} else { | |
handleHTTP(w, r) | |
} | |
}), | |
} | |
server.ListenAndServe() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment