forked from riobard/go-shadowsocks2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
218 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
|
||
"github.com/riobard/go-shadowsocks2/socks" | ||
) | ||
|
||
var listeners map[string]func(network, address string) (net.Listener, error) | ||
|
||
func init() { | ||
listeners = make(map[string]func(network, address string) (net.Listener, error)) | ||
} | ||
|
||
func listen(kind, network, address string) (net.Listener, error) { | ||
f, ok := listeners[kind] | ||
if ok { | ||
return f(network, address) | ||
} | ||
return nil, errors.New("unsupported listener " + kind) | ||
} | ||
|
||
type strAddr string | ||
|
||
func (a strAddr) Network() string { return "tcp" } | ||
func (a strAddr) String() string { return string(a) } | ||
|
||
type tunConn struct { | ||
net.Conn | ||
target net.Addr | ||
} | ||
|
||
func (c *tunConn) LocalAddr() net.Addr { return c.target } | ||
|
||
type tunListener struct { | ||
net.Listener | ||
target net.Addr | ||
} | ||
|
||
func tunListen(network, address, target string) (net.Listener, error) { | ||
l, err := net.Listen(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tunListener{l, strAddr(target)}, err | ||
} | ||
|
||
func (l *tunListener) Accept() (net.Conn, error) { | ||
c, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &tunConn{c, l.target}, nil | ||
} | ||
|
||
type socksConn struct{ net.Conn } | ||
|
||
func (sc socksConn) LocalAddr() net.Addr { | ||
addr, err := socks.Handshake(sc.Conn) | ||
if err != nil { | ||
return nil | ||
} | ||
return strAddr(addr.String()) | ||
} | ||
|
||
type socksListener struct{ net.Listener } | ||
|
||
func (l *socksListener) Accept() (net.Conn, error) { | ||
c, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return socksConn{c}, nil | ||
} | ||
|
||
func socksListen(network, addr string) (net.Listener, error) { | ||
l, err := net.Listen(network, addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &socksListener{l}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/riobard/go-shadowsocks2/pfutil" | ||
) | ||
|
||
func init() { | ||
listeners["redir"] = pfListen | ||
} | ||
|
||
type pfConn struct{ net.Conn } | ||
|
||
func (c *pfConn) LocalAddr() net.Addr { | ||
tc, ok := c.Conn.(*net.TCPConn) | ||
if !ok { | ||
return nil | ||
} | ||
addr, err := pfutil.NatLookup(tc) | ||
if err != nil { | ||
return nil | ||
} | ||
return addr | ||
} | ||
|
||
type pfListener struct{ net.Listener } | ||
|
||
func (l *pfListener) Accept() (net.Conn, error) { | ||
c, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &pfConn{c}, nil | ||
} | ||
|
||
func pfListen(network, address string) (net.Listener, error) { | ||
l, err := net.Listen(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &pfListener{l}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"syscall" | ||
|
||
"github.com/riobard/go-shadowsocks2/nfutil" | ||
) | ||
|
||
func init() { | ||
listeners["redir"] = nfListen | ||
listeners["tproxy"] = tproxyListen | ||
} | ||
|
||
type nfConn struct{ net.Conn } | ||
|
||
func (c *nfConn) LocalAddr() net.Addr { | ||
tc, ok := c.Conn.(*net.TCPConn) | ||
if !ok { | ||
return nil | ||
} | ||
addr, err := nfutil.GetOrigDst(tc, false) // TODO: detect if c is ipv6 | ||
if err != nil { | ||
return nil | ||
} | ||
return addr | ||
} | ||
|
||
type nfListener struct{ net.Listener } | ||
|
||
func (l *nfListener) Accept() (net.Conn, error) { | ||
c, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &nfConn{c}, nil | ||
} | ||
|
||
func nfListen(network, address string) (net.Listener, error) { | ||
l, err := net.Listen(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &nfListener{l}, nil | ||
} | ||
|
||
func tproxyListen(network, address string) (net.Listener, error) { | ||
lcfg := net.ListenConfig{Control: func(network, address string, rc syscall.RawConn) error { | ||
var err1, err2 error | ||
err2 = rc.Control(func(fd uintptr) { err1 = syscall.SetsockoptInt(int(fd), syscall.SOL_IP, syscall.IP_TRANSPARENT, 1) }) | ||
if err1 != nil { | ||
return err1 | ||
} | ||
return err2 | ||
}} | ||
l, err := lcfg.Listen(context.Background(), network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return l, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.