You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.0 KiB
Go

4 years ago
package proxy
import (
"bufio"
"context"
4 years ago
"crypto/tls"
"net"
"net/http"
"strings"
"github.com/lqqyt2423/go-mitmproxy/cert"
2 years ago
log "github.com/sirupsen/logrus"
4 years ago
)
// 模拟了标准库中 server 运行,目的是仅通过当前进程内存转发 socket 数据,不需要经过 tcp 或 unix socket
type pipeAddr struct {
remoteAddr string
}
func (pipeAddr) Network() string { return "pipe" }
func (a *pipeAddr) String() string { return a.remoteAddr }
4 years ago
// add Peek method for conn
type pipeConn struct {
4 years ago
net.Conn
r *bufio.Reader
host string
remoteAddr string
2 years ago
connContext *ConnContext
4 years ago
}
func newPipeConn(c net.Conn, req *http.Request) *pipeConn {
return &pipeConn{
Conn: c,
r: bufio.NewReader(c),
host: req.Host,
remoteAddr: req.RemoteAddr,
2 years ago
connContext: req.Context().Value(connContextKey).(*ConnContext),
4 years ago
}
4 years ago
}
func (c *pipeConn) Peek(n int) ([]byte, error) {
return c.r.Peek(n)
4 years ago
}
func (c *pipeConn) Read(data []byte) (int, error) {
return c.r.Read(data)
4 years ago
}
func (c *pipeConn) RemoteAddr() net.Addr {
return &pipeAddr{remoteAddr: c.remoteAddr}
}
2 years ago
// 建立客户端和服务端通信的通道
func newPipes(req *http.Request) (net.Conn, *pipeConn) {
client, srv := net.Pipe()
server := newPipeConn(srv, req)
return client, server
}
// mock net.Listener
type middleListener struct {
connChan chan net.Conn
4 years ago
}
2 years ago
func (l *middleListener) Accept() (net.Conn, error) { return <-l.connChan, nil }
func (l *middleListener) Close() error { return nil }
func (l *middleListener) Addr() net.Addr { return nil }
// middle: man-in-the-middle server
type middle struct {
proxy *Proxy
ca *cert.CA
listener *middleListener
server *http.Server
}
func newMiddle(proxy *Proxy) (interceptor, error) {
ca, err := cert.NewCA(proxy.Opts.CaRootPath)
4 years ago
if err != nil {
return nil, err
}
2 years ago
m := &middle{
proxy: proxy,
ca: ca,
listener: &middleListener{
connChan: make(chan net.Conn),
},
4 years ago
}
server := &http.Server{
Handler: m,
ConnContext: func(ctx context.Context, c net.Conn) context.Context {
2 years ago
return context.WithValue(ctx, connContextKey, c.(*tls.Conn).NetConn().(*pipeConn).connContext)
},
4 years ago
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler)), // disable http2
TLSConfig: &tls.Config{
GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
log.Debugf("Middle GetCertificate ServerName: %v\n", chi.ServerName)
return ca.GetCert(chi.ServerName)
},
},
}
2 years ago
m.server = server
4 years ago
return m, nil
}
2 years ago
func (m *middle) Start() error {
return m.server.ServeTLS(m.listener, "", "")
4 years ago
}
// todo: should block until ServerConnected
2 years ago
func (m *middle) Dial(req *http.Request) (net.Conn, error) {
pipeClientConn, pipeServerConn := newPipes(req)
go m.intercept(pipeServerConn)
return pipeClientConn, nil
4 years ago
}
2 years ago
func (m *middle) ServeHTTP(res http.ResponseWriter, req *http.Request) {
4 years ago
if strings.EqualFold(req.Header.Get("Connection"), "Upgrade") && strings.EqualFold(req.Header.Get("Upgrade"), "websocket") {
// wss
2 years ago
defaultWebSocket.wss(res, req)
4 years ago
return
}
if req.URL.Scheme == "" {
req.URL.Scheme = "https"
}
if req.URL.Host == "" {
req.URL.Host = req.Host
}
2 years ago
m.proxy.ServeHTTP(res, req)
4 years ago
}
// 解析 connect 流量
// 如果是 tls 流量,则进入 listener.Accept => Middle.ServeHTTP
// 否则很可能是 ws 流量
2 years ago
func (m *middle) intercept(pipeServerConn *pipeConn) {
log := log.WithField("in", "middle.intercept").WithField("host", pipeServerConn.host)
4 years ago
buf, err := pipeServerConn.Peek(3)
4 years ago
if err != nil {
log.Errorf("Peek error: %v\n", err)
pipeServerConn.Close()
4 years ago
return
}
// https://github.com/mitmproxy/mitmproxy/blob/main/mitmproxy/net/tls.py is_tls_record_magic
2 years ago
if buf[0] == 0x16 && buf[1] == 0x03 && buf[2] <= 0x03 {
4 years ago
// tls
2 years ago
pipeServerConn.connContext.ClientConn.Tls = true
2 years ago
pipeServerConn.connContext.initHttpsServerConn()
2 years ago
m.listener.connChan <- pipeServerConn
4 years ago
} else {
// ws
2 years ago
defaultWebSocket.ws(pipeServerConn, pipeServerConn.host)
4 years ago
}
}