transparent-proxy/server/ipv6.go

49 lines
1.0 KiB
Go
Raw Permalink Normal View History

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