Skip to content

Remote: Proxy For Http Injector

destConn, err := net.Dial("tcp", dest) if err != nil { log.Printf("Failed to connect to %s: %v", dest, err) http.Error(w, err.Error(), http.StatusBadGateway) return } defer destConn.Close()

clientConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))

package main import ( "io" "log" "net" "net/http" )

h, ok := w.(http.Hijacker) if !ok { http.Error(w, "No hijack", 500) return } clientConn, _, err := h.Hijack() if err != nil { return } defer clientConn.Close() remote proxy for http injector

// Ensure port is present if !strings.Contains(dest, ":") { dest = dest + ":80" // default to HTTP }

// Send 200 Connection Established clientConn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))

clientConn.Write([]byte("HTTP/1.1 200 OK\r\n\r\n")) destConn, err := net

// Bidirectional copy go func() { io.Copy(destConn, clientConn) }() io.Copy(clientConn, destConn) }

func handleInjectorTunnel(w http.ResponseWriter, r *http.Request) { dest, err := extractDestination(r) if err != nil { http.Error(w, "Missing destination", http.StatusBadRequest) return }

go func() { io.Copy(destConn, clientConn) }() io.Copy(clientConn, destConn) } err := net.Dial("tcp"

func main() { flag.Parse() http.HandleFunc("/", handle) log.Printf("Remote proxy listening on %s", *listenAddr) log.Fatal(http.ListenAndServe(*listenAddr, nil)) } Build:

go func() { io.Copy(destConn, clientConn) }() io.Copy(clientConn, destConn) }

func handleTunnel(w http.ResponseWriter, r *http.Request) { // Extract destination from CONNECT request destConn, err := net.Dial("tcp", r.Host) if err != nil { http.Error(w, err.Error(), http.StatusServiceUnavailable) return } defer destConn.Close()

type connPool struct { sync.Mutex conns map[string][]net.Conn } func (p *connPool) Get(addr string) net.Conn { p.Lock() defer p.Unlock() if pool, ok := p.conns[addr]; ok && len(pool) > 0 { conn := pool[len(pool)-1] p.conns[addr] = pool[:len(pool)-1] return conn } return nil }