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
7 changed files
with
102 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package listen | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
) | ||
|
||
var listeners = make(map[string]func(network, address string) (net.Listener, error)) | ||
|
||
var ErrUnsupported = errors.New("unsupported") | ||
|
||
func Listen(kind, network, address string) (net.Listener, error) { | ||
f, ok := listeners[kind] | ||
if ok { | ||
return f(network, address) | ||
} | ||
return nil, ErrUnsupported | ||
} | ||
|
||
type strAddr string | ||
|
||
func (a strAddr) Network() string { return "tcp" } | ||
func (a strAddr) String() string { return string(a) } | ||
|
||
type targetConn struct { | ||
net.Conn | ||
target net.Addr | ||
} | ||
|
||
func (c *targetConn) LocalAddr() net.Addr { return c.target } | ||
|
||
type targetListener struct { | ||
net.Listener | ||
target net.Addr | ||
} | ||
|
||
func ListenTo(network, address, target string) (net.Listener, error) { | ||
l, err := net.Listen(network, address) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &targetListener{l, strAddr(target)}, err | ||
} | ||
|
||
func (l *targetListener) Accept() (net.Conn, error) { | ||
c, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &targetConn{c, l.target}, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package main | ||
package listen | ||
|
||
import ( | ||
"net" | ||
|
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,39 @@ | ||
package listen | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/riobard/go-shadowsocks2/socks" | ||
) | ||
|
||
func init() { | ||
listeners["socks"] = socksListen | ||
} | ||
|
||
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 was deleted.
Oops, something went wrong.
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