RemoteAddr support

addon-dailer
lqqyt2423 3 years ago
parent a669ac1be1
commit 9f6ef6b320

@ -2,14 +2,15 @@ package proxy
import ( import (
"net" "net"
"net/http"
) )
// 拦截 https 流量通用接口 // 拦截 https 流量通用接口
type Interceptor interface { type Interceptor interface {
// 初始化 // 初始化
Start() error Start() error
// 针对每个 host 连接 // 传入当前客户端 req
Dial(host string) (net.Conn, error) Dial(req *http.Request) (net.Conn, error)
} }
// 直接转发 https 流量 // 直接转发 https 流量
@ -19,6 +20,6 @@ func (i *Forward) Start() error {
return nil return nil
} }
func (i *Forward) Dial(host string) (net.Conn, error) { func (i *Forward) Dial(req *http.Request) (net.Conn, error) {
return net.Dial("tcp", host) return net.Dial("tcp", req.Host)
} }

@ -21,10 +21,17 @@ func (l *listener) Accept() (net.Conn, error) { return <-l.connChan, nil }
func (l *listener) Close() error { return nil } func (l *listener) Close() error { return nil }
func (l *listener) Addr() net.Addr { return nil } func (l *listener) Addr() net.Addr { return nil }
type pipeAddr struct {
remoteAddr string
}
func (pipeAddr) Network() string { return "pipe" }
func (a *pipeAddr) String() string { return a.remoteAddr }
// 建立客户端和服务端通信的通道 // 建立客户端和服务端通信的通道
func newPipes(host string) (net.Conn, *connBuf) { func newPipes(req *http.Request) (net.Conn, *connBuf) {
client, srv := net.Pipe() client, srv := net.Pipe()
server := newConnBuf(srv, host) server := newConnBuf(srv, req)
return client, server return client, server
} }
@ -33,13 +40,15 @@ type connBuf struct {
net.Conn net.Conn
r *bufio.Reader r *bufio.Reader
host string host string
remoteAddr string
} }
func newConnBuf(c net.Conn, host string) *connBuf { func newConnBuf(c net.Conn, req *http.Request) *connBuf {
return &connBuf{ return &connBuf{
Conn: c, Conn: c,
r: bufio.NewReader(c), r: bufio.NewReader(c),
host: host, host: req.Host,
remoteAddr: req.RemoteAddr,
} }
} }
@ -51,6 +60,10 @@ func (b *connBuf) Read(data []byte) (int, error) {
return b.r.Read(data) return b.r.Read(data)
} }
func (b *connBuf) RemoteAddr() net.Addr {
return &pipeAddr{remoteAddr: b.remoteAddr}
}
// Middle: man-in-the-middle // Middle: man-in-the-middle
type Middle struct { type Middle struct {
Proxy *Proxy Proxy *Proxy
@ -91,8 +104,8 @@ func (m *Middle) Start() error {
return m.Server.ServeTLS(m.Listener, "", "") return m.Server.ServeTLS(m.Listener, "", "")
} }
func (m *Middle) Dial(host string) (net.Conn, error) { func (m *Middle) Dial(req *http.Request) (net.Conn, error) {
clientConn, serverConn := newPipes(host) clientConn, serverConn := newPipes(req)
go m.intercept(serverConn) go m.intercept(serverConn)
return clientConn, nil return clientConn, nil
} }

@ -262,7 +262,7 @@ func (proxy *Proxy) handleConnect(res http.ResponseWriter, req *http.Request) {
log.Debug("receive connect") log.Debug("receive connect")
conn, err := proxy.Interceptor.Dial(req.Host) conn, err := proxy.Interceptor.Dial(req)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
res.WriteHeader(502) res.WriteHeader(502)

Loading…
Cancel
Save