|
|
@ -21,25 +21,34 @@ 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// add Peek method for conn
|
|
|
|
// add Peek method for conn
|
|
|
|
type connBuf struct {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|