diff --git a/cmd/client.go b/cmd/client.go
@@ -55,7 +55,7 @@ func newClientCmd() *cobra.Command {
}
log.Printf("[+] Mode: client (HTTP CONNECT proxy on %s)", listen)
- p.RegisterHandler(proxy.HttpsHandler)
+ p.Handler = proxy.HttpsHandler
return p.Run()
},
}
diff --git a/cmd/server.go b/cmd/server.go
@@ -56,7 +56,7 @@ func newServerCmd() *cobra.Command {
}
log.Printf("[+] Mode: server (TLS termination → backend %s)", backend)
- p.RegisterHandler(proxy.ServerFrontHandler(cert, key))
+ p.Handler = proxy.ServerFrontHandler(cert, key)
return p.Run()
},
}
diff --git a/proxy/proxy.go b/proxy/proxy.go
@@ -2,6 +2,7 @@ package proxy
import (
"crypto/x509"
+ "fmt"
"io"
"log"
"net"
@@ -18,16 +19,11 @@ type Proxy struct {
CAPool *x509.CertPool
Fingerprint fingerprint.TLSApplier
LocalCA *LocalCA // client-front: MitM CA for TLS interception
-
- handlers []Handler
+ Handler Handler
}
type Handler func(conn net.Conn, p *Proxy)
-func (p *Proxy) RegisterHandler(h Handler) {
- p.handlers = append(p.handlers, h)
-}
-
// dialTarget dials host (host:port) and returns a uTLS connection after a
// successful handshake. Falls back to HelloRandomized when no fingerprint is set.
func (p *Proxy) dialTarget(host string) (*utls.UConn, error) {
@@ -81,6 +77,9 @@ func pipe(client io.ReadWriteCloser, r io.Reader, target io.ReadWriteCloser) {
}
func (p *Proxy) Run() error {
+ if p.Handler == nil {
+ return fmt.Errorf("proxy: Handler is required")
+ }
ln, err := net.Listen("tcp", p.ListenAddr)
if err != nil {
return err
@@ -89,17 +88,11 @@ func (p *Proxy) Run() error {
log.Printf("[+] Starting proxy on %s", p.ListenAddr)
for {
- c, err := ln.Accept()
+ conn, err := ln.Accept()
if err != nil {
log.Printf("Failed to accept connection: %v", err)
continue
}
-
- conn := c
- go func() {
- for _, h := range p.handlers {
- h(conn, p)
- }
- }()
+ go p.Handler(conn, p)
}
}
| | |