MCPcopy Create free account
hub / github.com/buggregator/server / handleConnect

Method handleConnect

modules/proxy/server.go:92–134  ·  view source on GitHub ↗

handleConnect handles HTTPS CONNECT tunneling with MITM.

(ctx context.Context, conn net.Conn, connectReq *http.Request)

Source from the content-addressed store, hash-verified

90
91// handleConnect handles HTTPS CONNECT tunneling with MITM.
92func (s *proxyServer) handleConnect(ctx context.Context, conn net.Conn, connectReq *http.Request) {
93 host := connectReq.Host
94 if !strings.Contains(host, ":") {
95 host += ":443"
96 }
97 hostname, _, _ := net.SplitHostPort(host)
98
99 // Respond 200 to establish the tunnel.
100 conn.Write([]byte("HTTP/1.1 200 Connection Established\r\n\r\n"))
101
102 // Get or create a TLS certificate for this host.
103 cert, err := s.certForHost(hostname)
104 if err != nil {
105 slog.Error("proxy: cert generation failed", "host", hostname, "err", err)
106 return
107 }
108
109 // TLS handshake with the client.
110 tlsConn := tls.Server(conn, &tls.Config{
111 Certificates: []tls.Certificate{*cert},
112 })
113 if err := tlsConn.Handshake(); err != nil {
114 slog.Debug("proxy: client TLS handshake failed", "host", hostname, "err", err)
115 return
116 }
117 defer tlsConn.Close()
118
119 clientReader := bufio.NewReader(tlsConn)
120
121 // Handle multiple requests over the same connection (HTTP/1.1 keep-alive).
122 for {
123 req, err := http.ReadRequest(clientReader)
124 if err != nil {
125 return
126 }
127
128 req.URL.Scheme = "https"
129 req.URL.Host = host
130 req.RequestURI = ""
131
132 s.proxyAndRecord(ctx, tlsConn, req, "https")
133 }
134}
135
136// handleHTTP handles plain HTTP proxy requests.
137func (s *proxyServer) handleHTTP(ctx context.Context, conn net.Conn, req *http.Request) {

Callers 1

handleConnMethod · 0.95

Calls 2

certForHostMethod · 0.95
proxyAndRecordMethod · 0.95

Tested by

no test coverage detected