|
|
|
@ -11,6 +11,7 @@ import ( |
|
|
|
"github.com/go-log/log" |
|
|
|
"github.com/shadowsocks/go-shadowsocks2/core" |
|
|
|
"github.com/songgao/water" |
|
|
|
"github.com/songgao/water/waterutil" |
|
|
|
"golang.org/x/net/ipv4" |
|
|
|
"golang.org/x/net/ipv6" |
|
|
|
) |
|
|
|
@ -227,58 +228,253 @@ func (h *tunHandler) transportTun(tun net.Conn, conn net.PacketConn, raddr net.A |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
type tunConn struct { |
|
|
|
ifce *water.Interface |
|
|
|
addr net.Addr |
|
|
|
type tunListener struct { |
|
|
|
addr net.Addr |
|
|
|
conns chan net.Conn |
|
|
|
closed chan struct{} |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) Read(b []byte) (n int, err error) { |
|
|
|
return c.ifce.Read(b) |
|
|
|
// TunListener creates a listener for tun tunnel.
|
|
|
|
func TunListener(addr string) (Listener, error) { |
|
|
|
laddr, err := net.ResolveUDPAddr("udp", addr) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
threads := 1 |
|
|
|
ln := &tunListener{ |
|
|
|
addr: laddr, |
|
|
|
conns: make(chan net.Conn, threads), |
|
|
|
closed: make(chan struct{}), |
|
|
|
} |
|
|
|
|
|
|
|
for i := 0; i < threads; i++ { |
|
|
|
conn, err := net.ListenUDP("udp", laddr) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
ln.conns <- conn |
|
|
|
} |
|
|
|
|
|
|
|
return ln, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) Write(b []byte) (n int, err error) { |
|
|
|
return c.ifce.Write(b) |
|
|
|
func (l *tunListener) Accept() (net.Conn, error) { |
|
|
|
select { |
|
|
|
case conn := <-l.conns: |
|
|
|
return conn, nil |
|
|
|
case <-l.closed: |
|
|
|
} |
|
|
|
|
|
|
|
return nil, errors.New("accept on closed listener") |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) Close() (err error) { |
|
|
|
return c.ifce.Close() |
|
|
|
func (l *tunListener) Addr() net.Addr { |
|
|
|
return l.addr |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) LocalAddr() net.Addr { |
|
|
|
return c.addr |
|
|
|
func (l *tunListener) Close() error { |
|
|
|
select { |
|
|
|
case <-l.closed: |
|
|
|
return errors.New("listener has been closed") |
|
|
|
default: |
|
|
|
close(l.closed) |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) RemoteAddr() net.Addr { |
|
|
|
return &net.IPAddr{} |
|
|
|
type TapConfig struct { |
|
|
|
Name string |
|
|
|
Addr string |
|
|
|
MTU int |
|
|
|
Routes []string |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) SetDeadline(t time.Time) error { |
|
|
|
return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
|
|
|
type tapHandler struct { |
|
|
|
raddr string |
|
|
|
options *HandlerOptions |
|
|
|
ipNet *net.IPNet |
|
|
|
routes sync.Map |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) SetReadDeadline(t time.Time) error { |
|
|
|
return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
|
|
|
// TapHandler creates a handler for tap tunnel.
|
|
|
|
func TapHandler(raddr string, opts ...HandlerOption) Handler { |
|
|
|
h := &tapHandler{ |
|
|
|
raddr: raddr, |
|
|
|
options: &HandlerOptions{}, |
|
|
|
} |
|
|
|
for _, opt := range opts { |
|
|
|
opt(h.options) |
|
|
|
} |
|
|
|
|
|
|
|
return h |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunConn) SetWriteDeadline(t time.Time) error { |
|
|
|
return &net.OpError{Op: "set", Net: "tun", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
|
|
|
func (h *tapHandler) Init(options ...HandlerOption) { |
|
|
|
if h.options == nil { |
|
|
|
h.options = &HandlerOptions{} |
|
|
|
} |
|
|
|
for _, opt := range options { |
|
|
|
opt(h.options) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type tunListener struct { |
|
|
|
func (h *tapHandler) Handle(conn net.Conn) { |
|
|
|
defer os.Exit(0) |
|
|
|
defer conn.Close() |
|
|
|
|
|
|
|
uc, ok := conn.(net.PacketConn) |
|
|
|
if !ok { |
|
|
|
log.Log("[tap] wrong connection type, must be PacketConn") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
tc, err := h.createTap() |
|
|
|
if err != nil { |
|
|
|
log.Logf("[tap] %s create tap: %v", conn.LocalAddr(), err) |
|
|
|
return |
|
|
|
} |
|
|
|
defer tc.Close() |
|
|
|
|
|
|
|
log.Logf("[tap] %s - %s: tap creation successful", tc.LocalAddr(), conn.LocalAddr()) |
|
|
|
|
|
|
|
var raddr net.Addr |
|
|
|
if h.raddr != "" { |
|
|
|
raddr, err = net.ResolveUDPAddr("udp", h.raddr) |
|
|
|
if err != nil { |
|
|
|
log.Logf("[tap] %s - %s remote addr: %v", tc.LocalAddr(), conn.LocalAddr(), err) |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if len(h.options.Users) > 0 && h.options.Users[0] != nil { |
|
|
|
passwd, _ := h.options.Users[0].Password() |
|
|
|
cipher, err := core.PickCipher(h.options.Users[0].Username(), nil, passwd) |
|
|
|
if err != nil { |
|
|
|
log.Logf("[tap] %s - %s cipher: %v", tc.LocalAddr(), conn.LocalAddr(), err) |
|
|
|
return |
|
|
|
} |
|
|
|
uc = cipher.PacketConn(uc) |
|
|
|
} |
|
|
|
|
|
|
|
h.transportTap(tc, uc, raddr) |
|
|
|
} |
|
|
|
|
|
|
|
func (h *tapHandler) createTap() (conn net.Conn, err error) { |
|
|
|
conn, h.ipNet, err = createTap(h.options.TapConfig) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
func (h *tapHandler) transportTap(tap net.Conn, conn net.PacketConn, raddr net.Addr) error { |
|
|
|
errc := make(chan error, 1) |
|
|
|
|
|
|
|
go func() { |
|
|
|
for { |
|
|
|
err := func() error { |
|
|
|
b := sPool.Get().([]byte) |
|
|
|
defer sPool.Put(b) |
|
|
|
|
|
|
|
n, err := tap.Read(b) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
macSrc := waterutil.MACSource(b[:n]) |
|
|
|
macDst := waterutil.MACDestination(b[:n]) |
|
|
|
|
|
|
|
addr := raddr |
|
|
|
if v, ok := h.routes.Load(macDst.String()); ok { |
|
|
|
addr = v.(net.Addr) |
|
|
|
} |
|
|
|
if addr == nil { |
|
|
|
log.Logf("[tap] %s: no route for %s -> %s %d %d", |
|
|
|
tap.LocalAddr(), macSrc, macDst, n, waterutil.MACEthertype(b[:n])) |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
if Debug { |
|
|
|
log.Logf("[tap] %s >>> %s: %s -> %s %d %d", |
|
|
|
tap.LocalAddr(), addr, macSrc, macDst, n, waterutil.MACEthertype(b[:n])) |
|
|
|
} |
|
|
|
|
|
|
|
if _, err := conn.WriteTo(b[:n], addr); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
return nil |
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
errc <- err |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
go func() { |
|
|
|
for { |
|
|
|
err := func() error { |
|
|
|
b := sPool.Get().([]byte) |
|
|
|
defer mPool.Put(b) |
|
|
|
|
|
|
|
n, addr, err := conn.ReadFrom(b) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
macSrc := waterutil.MACSource(b[:n]) |
|
|
|
macDst := waterutil.MACDestination(b[:n]) |
|
|
|
|
|
|
|
if Debug { |
|
|
|
log.Logf("[tap] %s <<< %s: %s -> %s %d %d", |
|
|
|
tap.LocalAddr(), addr, macSrc, macDst, n, waterutil.MACEthertype(b[:n])) |
|
|
|
} |
|
|
|
|
|
|
|
if actual, loaded := h.routes.LoadOrStore(macSrc.String(), addr); loaded { |
|
|
|
if actual.(net.Addr).String() != addr.String() { |
|
|
|
log.Logf("[tap] %s <- %s: update route: %s -> %s (old %s)", |
|
|
|
tap.LocalAddr(), addr, macSrc, addr, actual.(net.Addr)) |
|
|
|
h.routes.Store(macSrc.String(), addr) |
|
|
|
} |
|
|
|
} else { |
|
|
|
log.Logf("[tap] %s: new route: %s -> %s", tap.LocalAddr(), macSrc, addr) |
|
|
|
} |
|
|
|
|
|
|
|
if _, err := tap.Write(b[:n]); err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
return nil |
|
|
|
}() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
errc <- err |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
err := <-errc |
|
|
|
if err != nil && err == io.EOF { |
|
|
|
err = nil |
|
|
|
} |
|
|
|
log.Logf("[tap] %s - %s: %v", tap.LocalAddr(), conn.LocalAddr(), err) |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
type tapListener struct { |
|
|
|
addr net.Addr |
|
|
|
conns chan net.Conn |
|
|
|
closed chan struct{} |
|
|
|
} |
|
|
|
|
|
|
|
// TunListener creates a listener for tun tunnel.
|
|
|
|
func TunListener(addr string) (Listener, error) { |
|
|
|
// TapListener creates a listener for tap tunnel.
|
|
|
|
func TapListener(addr string) (Listener, error) { |
|
|
|
laddr, err := net.ResolveUDPAddr("udp", addr) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
|
|
|
|
threads := 1 |
|
|
|
ln := &tunListener{ |
|
|
|
ln := &tapListener{ |
|
|
|
addr: laddr, |
|
|
|
conns: make(chan net.Conn, threads), |
|
|
|
closed: make(chan struct{}), |
|
|
|
@ -295,7 +491,7 @@ func TunListener(addr string) (Listener, error) { |
|
|
|
return ln, nil |
|
|
|
} |
|
|
|
|
|
|
|
func (l *tunListener) Accept() (net.Conn, error) { |
|
|
|
func (l *tapListener) Accept() (net.Conn, error) { |
|
|
|
select { |
|
|
|
case conn := <-l.conns: |
|
|
|
return conn, nil |
|
|
|
@ -305,11 +501,11 @@ func (l *tunListener) Accept() (net.Conn, error) { |
|
|
|
return nil, errors.New("accept on closed listener") |
|
|
|
} |
|
|
|
|
|
|
|
func (l *tunListener) Addr() net.Addr { |
|
|
|
func (l *tapListener) Addr() net.Addr { |
|
|
|
return l.addr |
|
|
|
} |
|
|
|
|
|
|
|
func (l *tunListener) Close() error { |
|
|
|
func (l *tapListener) Close() error { |
|
|
|
select { |
|
|
|
case <-l.closed: |
|
|
|
return errors.New("listener has been closed") |
|
|
|
@ -318,3 +514,40 @@ func (l *tunListener) Close() error { |
|
|
|
} |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
type tunTapConn struct { |
|
|
|
ifce *water.Interface |
|
|
|
addr net.Addr |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) Read(b []byte) (n int, err error) { |
|
|
|
return c.ifce.Read(b) |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) Write(b []byte) (n int, err error) { |
|
|
|
return c.ifce.Write(b) |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) Close() (err error) { |
|
|
|
return c.ifce.Close() |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) LocalAddr() net.Addr { |
|
|
|
return c.addr |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) RemoteAddr() net.Addr { |
|
|
|
return &net.IPAddr{} |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) SetDeadline(t time.Time) error { |
|
|
|
return &net.OpError{Op: "set", Net: "tuntap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) SetReadDeadline(t time.Time) error { |
|
|
|
return &net.OpError{Op: "set", Net: "tuntap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
|
|
|
} |
|
|
|
|
|
|
|
func (c *tunTapConn) SetWriteDeadline(t time.Time) error { |
|
|
|
return &net.OpError{Op: "set", Net: "tuntap", Source: nil, Addr: nil, Err: errors.New("deadline not supported")} |
|
|
|
} |