Clash.Meta/proxy/socks/tcp.go

170 lines
3.5 KiB
Go
Raw Normal View History

2018-06-14 01:00:58 +08:00
package socks
2018-06-10 22:50:03 +08:00
import (
2019-04-23 23:29:36 +08:00
"io"
2018-06-10 22:50:03 +08:00
"net"
2019-04-23 23:29:36 +08:00
"strconv"
2018-06-10 22:50:03 +08:00
2019-04-23 23:29:36 +08:00
adapters "github.com/Dreamacro/clash/adapters/inbound"
2018-12-05 21:13:29 +08:00
C "github.com/Dreamacro/clash/constant"
2018-12-03 23:41:40 +08:00
"github.com/Dreamacro/clash/log"
2018-06-10 22:50:03 +08:00
"github.com/Dreamacro/clash/tunnel"
"github.com/Dreamacro/go-shadowsocks2/socks"
2018-06-10 22:50:03 +08:00
)
var (
tun = tunnel.Instance()
2018-06-10 22:50:03 +08:00
)
2019-04-23 23:29:36 +08:00
// Error represents a SOCKS error
type Error byte
func (err Error) Error() string {
return "SOCKS error: " + strconv.Itoa(int(err))
}
// SOCKS request commands as defined in RFC 1928 section 4.
const (
CmdConnect = 1
CmdBind = 2
CmdUDPAssociate = 3
)
// SOCKS address types as defined in RFC 1928 section 5.
const (
AtypIPv4 = 1
AtypDomainName = 3
AtypIPv6 = 4
)
const MaxAddrLen = 1 + 1 + 255 + 2
// SOCKS errors as defined in RFC 1928 section 6.
const (
ErrGeneralFailure = Error(1)
ErrConnectionNotAllowed = Error(2)
ErrNetworkUnreachable = Error(3)
ErrHostUnreachable = Error(4)
ErrConnectionRefused = Error(5)
ErrTTLExpired = Error(6)
ErrCommandNotSupported = Error(7)
ErrAddressNotSupported = Error(8)
)
2018-12-05 21:13:29 +08:00
type SockListener struct {
net.Listener
address string
closed bool
}
2018-12-05 21:13:29 +08:00
func NewSocksProxy(addr string) (*SockListener, error) {
2018-07-15 22:23:20 +08:00
l, err := net.Listen("tcp", addr)
2018-06-10 22:50:03 +08:00
if err != nil {
return nil, err
2018-06-10 22:50:03 +08:00
}
2018-07-15 22:23:20 +08:00
2018-12-05 21:13:29 +08:00
sl := &SockListener{l, addr, false}
2018-07-15 22:23:20 +08:00
go func() {
2018-12-03 23:41:40 +08:00
log.Infoln("SOCKS proxy listening at: %s", addr)
2018-07-15 22:23:20 +08:00
for {
c, err := l.Accept()
if err != nil {
if sl.closed {
2018-07-15 22:23:20 +08:00
break
}
continue
}
go handleSocks(c)
}
}()
return sl, nil
}
2018-12-05 21:13:29 +08:00
func (l *SockListener) Close() {
l.closed = true
l.Listener.Close()
}
2018-07-15 22:23:20 +08:00
2018-12-05 21:13:29 +08:00
func (l *SockListener) Address() string {
return l.address
2018-06-10 22:50:03 +08:00
}
func handleSocks(conn net.Conn) {
2019-04-23 23:29:36 +08:00
target, command, err := handshake(conn)
2018-06-10 22:50:03 +08:00
if err != nil {
conn.Close()
return
2018-06-10 22:50:03 +08:00
}
conn.(*net.TCPConn).SetKeepAlive(true)
2019-04-23 23:29:36 +08:00
if command == CmdUDPAssociate {
tun.Add(adapters.NewSocket(target, conn, C.SOCKS, C.UDP))
return
}
tun.Add(adapters.NewSocket(target, conn, C.SOCKS, C.TCP))
}
// handshake fast-tracks SOCKS initialization to get target address to connect.
func handshake(rw io.ReadWriter) (addr socks.Addr, command int, err error) {
// Read RFC 1928 for request and reply structure and sizes.
buf := make([]byte, MaxAddrLen)
// read VER, NMETHODS, METHODS
if _, err = io.ReadFull(rw, buf[:2]); err != nil {
return
}
nmethods := buf[1]
if _, err = io.ReadFull(rw, buf[:nmethods]); err != nil {
return
}
// write VER METHOD
if _, err = rw.Write([]byte{5, 0}); err != nil {
return
}
// read VER CMD RSV ATYP DST.ADDR DST.PORT
if _, err = io.ReadFull(rw, buf[:3]); err != nil {
return
}
if buf[1] != CmdConnect && buf[1] != CmdUDPAssociate {
err = ErrCommandNotSupported
return
}
command = int(buf[1])
addr, err = readAddr(rw, buf)
if err != nil {
return
}
// write VER REP RSV ATYP BND.ADDR BND.PORT
_, err = rw.Write([]byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0})
return
}
func readAddr(r io.Reader, b []byte) (socks.Addr, error) {
if len(b) < MaxAddrLen {
return nil, io.ErrShortBuffer
}
_, err := io.ReadFull(r, b[:1]) // read 1st byte for address type
if err != nil {
return nil, err
}
switch b[0] {
case AtypDomainName:
_, err = io.ReadFull(r, b[1:2]) // read 2nd byte for domain length
if err != nil {
return nil, err
}
_, err = io.ReadFull(r, b[2:2+b[1]+2])
return b[:1+1+b[1]+2], err
case AtypIPv4:
_, err = io.ReadFull(r, b[1:1+net.IPv4len+2])
return b[:1+net.IPv4len+2], err
case AtypIPv6:
_, err = io.ReadFull(r, b[1:1+net.IPv6len+2])
return b[:1+net.IPv6len+2], err
}
return nil, ErrAddressNotSupported
2018-06-10 22:50:03 +08:00
}