transparent-proxy/server/ipv4.go

49 lines
1.0 KiB
Go
Raw Normal View History

2017-08-07 15:57:17 +08:00
package server
import (
"net"
)
type IPv4TransparentListener struct {
listener *net.TCPListener
}
type IPv4TransparentConnection struct {
conn *net.TCPConn
remoteAddr *net.TCPAddr
}
func NewIPv4TransparentListener(addr string) (*IPv4TransparentListener, error) {
tcpaddr, err := net.ResolveTCPAddr("tcp4", addr)
if err != nil {
return nil, err
}
listener, err := net.ListenTCP("tcp4", tcpaddr)
if err != nil {
return nil, err
}
return &IPv4TransparentListener{listener: listener}, nil
}
func (l *IPv4TransparentListener) Close() error {
return l.listener.Close()
}
func (l *IPv4TransparentListener) Accept() (*IPv4TransparentConnection, error) {
conn, err := l.listener.AcceptTCP()
if err != nil {
return nil, err
}
fd := GetFDFromTCPConn(conn)
remoteAddr := GetAddr4FromFD(fd)
return &IPv4TransparentConnection{conn: conn, remoteAddr: remoteAddr}, nil
}
func (c *IPv4TransparentConnection) TCPConn() *net.TCPConn {
return c.conn
}
func (c *IPv4TransparentConnection) RealAddr() *net.TCPAddr {
return c.remoteAddr
}