2017-07-13 21:55:41 +08:00
|
|
|
// socks5 client:
|
|
|
|
// https://github.com/golang/net/tree/master/proxy
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// socks5 server:
|
|
|
|
// https://github.com/shadowsocks/go-shadowsocks2/tree/master/socks
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2018-01-20 22:13:04 +08:00
|
|
|
"sync"
|
|
|
|
"time"
|
2017-07-13 21:55:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const socks5Version = 5
|
|
|
|
|
|
|
|
const (
|
|
|
|
socks5AuthNone = 0
|
|
|
|
socks5AuthPassword = 2
|
|
|
|
)
|
|
|
|
|
|
|
|
// SOCKS request commands as defined in RFC 1928 section 4.
|
|
|
|
const (
|
|
|
|
socks5Connect = 1
|
|
|
|
socks5Bind = 2
|
|
|
|
socks5UDPAssociate = 3
|
|
|
|
)
|
|
|
|
|
|
|
|
// SOCKS address types as defined in RFC 1928 section 5.
|
|
|
|
const (
|
|
|
|
socks5IP4 = 1
|
|
|
|
socks5Domain = 3
|
|
|
|
socks5IP6 = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaxAddrLen is the maximum size of SOCKS address in bytes.
|
|
|
|
const MaxAddrLen = 1 + 1 + 255 + 2
|
|
|
|
|
|
|
|
// Addr represents a SOCKS address as defined in RFC 1928 section 5.
|
|
|
|
type Addr []byte
|
|
|
|
|
2018-01-20 22:13:04 +08:00
|
|
|
var socks5Errors = []error{
|
|
|
|
errors.New(""),
|
|
|
|
errors.New("general failure"),
|
|
|
|
errors.New("connection forbidden"),
|
|
|
|
errors.New("network unreachable"),
|
|
|
|
errors.New("host unreachable"),
|
|
|
|
errors.New("connection refused"),
|
|
|
|
errors.New("TTL expired"),
|
|
|
|
errors.New("command not supported"),
|
|
|
|
errors.New("address type not supported"),
|
|
|
|
errors.New("socks5UDPAssociate"),
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-10 20:33:35 +08:00
|
|
|
// SOCKS5 struct
|
2017-08-23 16:35:39 +08:00
|
|
|
type SOCKS5 struct {
|
|
|
|
*Forwarder
|
|
|
|
sDialer Dialer
|
|
|
|
|
2017-07-29 23:20:27 +08:00
|
|
|
user string
|
|
|
|
password string
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
2017-08-23 16:35:39 +08:00
|
|
|
// NewSOCKS5 returns a Proxy that makes SOCKSv5 connections to the given address
|
2017-07-13 21:55:41 +08:00
|
|
|
// with an optional username and password. See RFC 1928.
|
2017-09-07 23:50:10 +08:00
|
|
|
func NewSOCKS5(addr, user, pass string, cDialer Dialer, sDialer Dialer) (*SOCKS5, error) {
|
2017-08-23 16:35:39 +08:00
|
|
|
s := &SOCKS5{
|
|
|
|
Forwarder: NewForwarder(addr, cDialer),
|
|
|
|
sDialer: sDialer,
|
|
|
|
user: user,
|
|
|
|
password: pass,
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
2018-01-20 22:13:04 +08:00
|
|
|
// ListenAndServe serves socks5 requests.
|
2017-08-23 16:35:39 +08:00
|
|
|
func (s *SOCKS5) ListenAndServe() {
|
2018-01-20 22:13:04 +08:00
|
|
|
go s.ListenAndServeUDP()
|
|
|
|
s.ListenAndServeTCP()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenAndServeTCP .
|
|
|
|
func (s *SOCKS5) ListenAndServeTCP() {
|
2017-07-13 21:55:41 +08:00
|
|
|
l, err := net.Listen("tcp", s.addr)
|
|
|
|
if err != nil {
|
2017-10-18 21:47:32 +08:00
|
|
|
logf("proxy-socks5 failed to listen on %s: %v", s.addr, err)
|
2017-07-13 21:55:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:47:32 +08:00
|
|
|
logf("proxy-socks5 listening TCP on %s", s.addr)
|
2017-07-13 21:55:41 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
c, err := l.Accept()
|
|
|
|
if err != nil {
|
2017-10-18 21:47:32 +08:00
|
|
|
logf("proxy-socks5 failed to accept: %v", err)
|
2017-07-13 21:55:41 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-01-20 22:13:04 +08:00
|
|
|
go s.ServeTCP(c)
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 22:13:04 +08:00
|
|
|
// ServeTCP .
|
|
|
|
func (s *SOCKS5) ServeTCP(c net.Conn) {
|
2017-07-13 21:55:41 +08:00
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
if c, ok := c.(*net.TCPConn); ok {
|
|
|
|
c.SetKeepAlive(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
tgt, err := s.handshake(c)
|
|
|
|
if err != nil {
|
2018-01-20 22:13:04 +08:00
|
|
|
// UDP: keep the connection until disconnect then free the UDP socket
|
|
|
|
if err == socks5Errors[9] {
|
|
|
|
buf := []byte{}
|
|
|
|
// block here
|
|
|
|
for {
|
|
|
|
_, err := c.Read(buf)
|
|
|
|
if err, ok := err.(net.Error); ok && err.Timeout() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logf("UDP Associate End.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:47:32 +08:00
|
|
|
logf("proxy-socks5 failed to get target address: %v", err)
|
2017-07-13 21:55:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:35:39 +08:00
|
|
|
rc, err := s.sDialer.Dial("tcp", tgt.String())
|
2017-07-13 21:55:41 +08:00
|
|
|
if err != nil {
|
2017-10-18 21:47:32 +08:00
|
|
|
logf("proxy-socks5 failed to connect to target: %v", err)
|
2017-07-13 21:55:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer rc.Close()
|
|
|
|
|
|
|
|
logf("proxy-socks5 %s <-> %s", c.RemoteAddr(), tgt)
|
|
|
|
|
|
|
|
_, _, err = relay(c, rc)
|
|
|
|
if err != nil {
|
|
|
|
if err, ok := err.(net.Error); ok && err.Timeout() {
|
|
|
|
return // ignore i/o timeout
|
|
|
|
}
|
2018-01-08 18:14:57 +08:00
|
|
|
logf("proxy-socks5 relay error: %v", err)
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-20 22:13:04 +08:00
|
|
|
// ListenAndServeUDP serves udp requests.
|
|
|
|
func (s *SOCKS5) ListenAndServeUDP() {
|
|
|
|
lc, err := net.ListenPacket("udp", s.addr)
|
|
|
|
if err != nil {
|
|
|
|
logf("proxy-socks5-udp failed to listen on %s: %v", s.addr, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer lc.Close()
|
|
|
|
|
|
|
|
logf("proxy-socks5-udp listening UDP on %s", s.addr)
|
|
|
|
|
|
|
|
var nm sync.Map
|
|
|
|
buf := make([]byte, udpBufSize)
|
|
|
|
|
|
|
|
for {
|
|
|
|
c := NewSocks5PktConn(lc, nil, nil, true)
|
|
|
|
|
|
|
|
n, raddr, err := c.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
logf("proxy-socks5-udp remote read error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var pc *Socks5PktConn
|
|
|
|
v, ok := nm.Load(raddr.String())
|
|
|
|
if !ok && v == nil {
|
|
|
|
lpc, nextHop, err := s.sDialer.DialUDP("udp", c.tgtAddr.String())
|
|
|
|
if err != nil {
|
|
|
|
logf("proxy-socks5-udp remote dial error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
pc = NewSocks5PktConn(lpc, nextHop, nil, false)
|
|
|
|
nm.Store(raddr.String(), pc)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
timedCopy(c, raddr, pc, 2*time.Minute)
|
|
|
|
pc.Close()
|
|
|
|
nm.Delete(raddr.String())
|
|
|
|
}()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
pc = v.(*Socks5PktConn)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = pc.WriteTo(buf[:n], pc.writeAddr)
|
|
|
|
if err != nil {
|
|
|
|
logf("proxy-socks5-udp remote write error: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logf("proxy-socks5-udp %s <-> %s", raddr, c.tgtAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:55:41 +08:00
|
|
|
// Dial connects to the address addr on the network net via the SOCKS5 proxy.
|
2017-08-23 16:35:39 +08:00
|
|
|
func (s *SOCKS5) Dial(network, addr string) (net.Conn, error) {
|
2017-07-13 21:55:41 +08:00
|
|
|
switch network {
|
|
|
|
case "tcp", "tcp6", "tcp4":
|
|
|
|
default:
|
2017-09-07 23:50:10 +08:00
|
|
|
return nil, errors.New("proxy-socks5: no support for connection type " + network)
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
2017-09-04 23:32:12 +08:00
|
|
|
c, err := s.cDialer.Dial(network, s.addr)
|
2017-07-13 21:55:41 +08:00
|
|
|
if err != nil {
|
|
|
|
logf("dial to %s error: %s", s.addr, err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c, ok := c.(*net.TCPConn); ok {
|
|
|
|
c.SetKeepAlive(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.connect(c, addr); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:26:38 +08:00
|
|
|
// DialUDP connects to the given address via the proxy.
|
|
|
|
func (s *SOCKS5) DialUDP(network, addr string) (pc net.PacketConn, writeTo net.Addr, err error) {
|
2018-01-20 22:13:04 +08:00
|
|
|
return nil, nil, errors.New("sock5 client does not support udp now")
|
2018-01-17 00:26:38 +08:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:55:41 +08:00
|
|
|
// connect takes an existing connection to a socks5 proxy server,
|
|
|
|
// and commands the server to extend that connection to target,
|
|
|
|
// which must be a canonical address with a host and port.
|
2017-08-23 16:35:39 +08:00
|
|
|
func (s *SOCKS5) connect(conn net.Conn, target string) error {
|
2017-07-13 21:55:41 +08:00
|
|
|
host, portStr, err := net.SplitHostPort(target)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
port, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("proxy: failed to parse port number: " + portStr)
|
|
|
|
}
|
|
|
|
if port < 1 || port > 0xffff {
|
|
|
|
return errors.New("proxy: port number out of range: " + portStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// the size here is just an estimate
|
|
|
|
buf := make([]byte, 0, 6+len(host))
|
|
|
|
|
|
|
|
buf = append(buf, socks5Version)
|
|
|
|
if len(s.user) > 0 && len(s.user) < 256 && len(s.password) < 256 {
|
|
|
|
buf = append(buf, 2 /* num auth methods */, socks5AuthNone, socks5AuthPassword)
|
|
|
|
} else {
|
|
|
|
buf = append(buf, 1 /* num auth methods */, socks5AuthNone)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := conn.Write(buf); err != nil {
|
|
|
|
return errors.New("proxy: failed to write greeting to SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
|
|
|
|
return errors.New("proxy: failed to read greeting from SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
if buf[0] != 5 {
|
|
|
|
return errors.New("proxy: SOCKS5 proxy at " + s.addr + " has unexpected version " + strconv.Itoa(int(buf[0])))
|
|
|
|
}
|
|
|
|
if buf[1] == 0xff {
|
|
|
|
return errors.New("proxy: SOCKS5 proxy at " + s.addr + " requires authentication")
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf[1] == socks5AuthPassword {
|
|
|
|
buf = buf[:0]
|
|
|
|
buf = append(buf, 1 /* password protocol version */)
|
|
|
|
buf = append(buf, uint8(len(s.user)))
|
|
|
|
buf = append(buf, s.user...)
|
|
|
|
buf = append(buf, uint8(len(s.password)))
|
|
|
|
buf = append(buf, s.password...)
|
|
|
|
|
|
|
|
if _, err := conn.Write(buf); err != nil {
|
|
|
|
return errors.New("proxy: failed to write authentication request to SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
|
|
|
|
return errors.New("proxy: failed to read authentication reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf[1] != 0 {
|
|
|
|
return errors.New("proxy: SOCKS5 proxy at " + s.addr + " rejected username/password")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = buf[:0]
|
|
|
|
buf = append(buf, socks5Version, socks5Connect, 0 /* reserved */)
|
|
|
|
|
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
buf = append(buf, socks5IP4)
|
|
|
|
ip = ip4
|
|
|
|
} else {
|
|
|
|
buf = append(buf, socks5IP6)
|
|
|
|
}
|
|
|
|
buf = append(buf, ip...)
|
|
|
|
} else {
|
|
|
|
if len(host) > 255 {
|
|
|
|
return errors.New("proxy: destination hostname too long: " + host)
|
|
|
|
}
|
|
|
|
buf = append(buf, socks5Domain)
|
|
|
|
buf = append(buf, byte(len(host)))
|
|
|
|
buf = append(buf, host...)
|
|
|
|
}
|
|
|
|
buf = append(buf, byte(port>>8), byte(port))
|
|
|
|
|
|
|
|
if _, err := conn.Write(buf); err != nil {
|
|
|
|
return errors.New("proxy: failed to write connect request to SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.ReadFull(conn, buf[:4]); err != nil {
|
|
|
|
return errors.New("proxy: failed to read connect reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
failure := "unknown error"
|
|
|
|
if int(buf[1]) < len(socks5Errors) {
|
2018-01-20 22:13:04 +08:00
|
|
|
failure = socks5Errors[buf[1]].Error()
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(failure) > 0 {
|
|
|
|
return errors.New("proxy: SOCKS5 proxy at " + s.addr + " failed to connect: " + failure)
|
|
|
|
}
|
|
|
|
|
|
|
|
bytesToDiscard := 0
|
|
|
|
switch buf[3] {
|
|
|
|
case socks5IP4:
|
|
|
|
bytesToDiscard = net.IPv4len
|
|
|
|
case socks5IP6:
|
|
|
|
bytesToDiscard = net.IPv6len
|
|
|
|
case socks5Domain:
|
|
|
|
_, err := io.ReadFull(conn, buf[:1])
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("proxy: failed to read domain length from SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
bytesToDiscard = int(buf[0])
|
|
|
|
default:
|
|
|
|
return errors.New("proxy: got unknown address type " + strconv.Itoa(int(buf[3])) + " from SOCKS5 proxy at " + s.addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cap(buf) < bytesToDiscard {
|
|
|
|
buf = make([]byte, bytesToDiscard)
|
|
|
|
} else {
|
|
|
|
buf = buf[:bytesToDiscard]
|
|
|
|
}
|
|
|
|
if _, err := io.ReadFull(conn, buf); err != nil {
|
|
|
|
return errors.New("proxy: failed to read address from SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Also need to discard the port number
|
|
|
|
if _, err := io.ReadFull(conn, buf[:2]); err != nil {
|
|
|
|
return errors.New("proxy: failed to read port from SOCKS5 proxy at " + s.addr + ": " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handshake fast-tracks SOCKS initialization to get target address to connect.
|
2017-08-23 16:35:39 +08:00
|
|
|
func (s *SOCKS5) handshake(rw io.ReadWriter) (Addr, error) {
|
2017-07-13 21:55:41 +08:00
|
|
|
// 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 nil, err
|
|
|
|
}
|
|
|
|
nmethods := buf[1]
|
|
|
|
if _, err := io.ReadFull(rw, buf[:nmethods]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// write VER METHOD
|
|
|
|
if _, err := rw.Write([]byte{5, 0}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// read VER CMD RSV ATYP DST.ADDR DST.PORT
|
|
|
|
if _, err := io.ReadFull(rw, buf[:3]); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-20 22:13:04 +08:00
|
|
|
cmd := buf[1]
|
2017-07-13 21:55:41 +08:00
|
|
|
addr, err := readAddr(rw, buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-01-20 22:13:04 +08:00
|
|
|
switch cmd {
|
|
|
|
case socks5Connect:
|
|
|
|
_, err = rw.Write([]byte{5, 0, 0, 1, 0, 0, 0, 0, 0, 0}) // SOCKS v5, reply succeeded
|
|
|
|
case socks5UDPAssociate:
|
|
|
|
listenAddr := ParseAddr(rw.(net.Conn).LocalAddr().String())
|
|
|
|
_, err = rw.Write(append([]byte{5, 0, 0}, listenAddr...)) // SOCKS v5, reply succeeded
|
|
|
|
if err != nil {
|
|
|
|
return nil, socks5Errors[7]
|
|
|
|
}
|
|
|
|
err = socks5Errors[9]
|
|
|
|
default:
|
|
|
|
return nil, socks5Errors[7]
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr, err // skip VER, CMD, RSV fields
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// String serializes SOCKS address a to string form.
|
|
|
|
func (a Addr) String() string {
|
|
|
|
var host, port string
|
|
|
|
|
2017-09-06 18:52:22 +08:00
|
|
|
switch ATYP(a[0]) { // address type
|
2017-07-13 21:55:41 +08:00
|
|
|
case socks5Domain:
|
|
|
|
host = string(a[2 : 2+int(a[1])])
|
|
|
|
port = strconv.Itoa((int(a[2+int(a[1])]) << 8) | int(a[2+int(a[1])+1]))
|
|
|
|
case socks5IP4:
|
|
|
|
host = net.IP(a[1 : 1+net.IPv4len]).String()
|
|
|
|
port = strconv.Itoa((int(a[1+net.IPv4len]) << 8) | int(a[1+net.IPv4len+1]))
|
|
|
|
case socks5IP6:
|
|
|
|
host = net.IP(a[1 : 1+net.IPv6len]).String()
|
|
|
|
port = strconv.Itoa((int(a[1+net.IPv6len]) << 8) | int(a[1+net.IPv6len+1]))
|
|
|
|
}
|
|
|
|
|
|
|
|
return net.JoinHostPort(host, port)
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:52:22 +08:00
|
|
|
// UoT udp over tcp
|
|
|
|
func UoT(b byte) bool {
|
|
|
|
return b&0x8 == 0x8
|
|
|
|
}
|
|
|
|
|
|
|
|
// ATYP return the address type
|
|
|
|
func ATYP(b byte) int {
|
|
|
|
return int(b &^ 0x8)
|
|
|
|
}
|
|
|
|
|
2017-07-13 21:55:41 +08:00
|
|
|
func readAddr(r io.Reader, b []byte) (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
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:52:22 +08:00
|
|
|
switch ATYP(b[0]) {
|
2017-07-13 21:55:41 +08:00
|
|
|
case socks5Domain:
|
|
|
|
_, 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+int(b[1])+2])
|
|
|
|
return b[:1+1+int(b[1])+2], err
|
|
|
|
case socks5IP4:
|
|
|
|
_, err = io.ReadFull(r, b[1:1+net.IPv4len+2])
|
|
|
|
return b[:1+net.IPv4len+2], err
|
|
|
|
case socks5IP6:
|
|
|
|
_, err = io.ReadFull(r, b[1:1+net.IPv6len+2])
|
|
|
|
return b[:1+net.IPv6len+2], err
|
|
|
|
}
|
|
|
|
|
2018-01-20 22:13:04 +08:00
|
|
|
return nil, socks5Errors[8]
|
2017-07-13 21:55:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAddr reads just enough bytes from r to get a valid Addr.
|
|
|
|
func ReadAddr(r io.Reader) (Addr, error) {
|
|
|
|
return readAddr(r, make([]byte, MaxAddrLen))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitAddr slices a SOCKS address from beginning of b. Returns nil if failed.
|
|
|
|
func SplitAddr(b []byte) Addr {
|
|
|
|
addrLen := 1
|
|
|
|
if len(b) < addrLen {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-06 18:52:22 +08:00
|
|
|
switch ATYP(b[0]) {
|
2017-07-13 21:55:41 +08:00
|
|
|
case socks5Domain:
|
|
|
|
if len(b) < 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
addrLen = 1 + 1 + int(b[1]) + 2
|
|
|
|
case socks5IP4:
|
|
|
|
addrLen = 1 + net.IPv4len + 2
|
|
|
|
case socks5IP6:
|
|
|
|
addrLen = 1 + net.IPv6len + 2
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b) < addrLen {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b[:addrLen]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseAddr parses the address in string s. Returns nil if failed.
|
|
|
|
func ParseAddr(s string) Addr {
|
|
|
|
var addr Addr
|
|
|
|
host, port, err := net.SplitHostPort(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ip := net.ParseIP(host); ip != nil {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
addr = make([]byte, 1+net.IPv4len+2)
|
|
|
|
addr[0] = socks5IP4
|
|
|
|
copy(addr[1:], ip4)
|
|
|
|
} else {
|
|
|
|
addr = make([]byte, 1+net.IPv6len+2)
|
|
|
|
addr[0] = socks5IP6
|
|
|
|
copy(addr[1:], ip)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(host) > 255 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
addr = make([]byte, 1+1+len(host)+2)
|
|
|
|
addr[0] = socks5Domain
|
|
|
|
addr[1] = byte(len(host))
|
|
|
|
copy(addr[2:], host)
|
|
|
|
}
|
|
|
|
|
|
|
|
portnum, err := strconv.ParseUint(port, 10, 16)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addr[len(addr)-2], addr[len(addr)-1] = byte(portnum>>8), byte(portnum)
|
|
|
|
|
|
|
|
return addr
|
|
|
|
}
|
2018-01-20 22:13:04 +08:00
|
|
|
|
|
|
|
// Socks5PktConn .
|
|
|
|
type Socks5PktConn struct {
|
|
|
|
net.PacketConn
|
|
|
|
|
|
|
|
writeAddr net.Addr // write to and read from addr
|
|
|
|
|
|
|
|
tgtAddr Addr
|
|
|
|
tgtHeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewSocks5PktConn returns a Socks5PktConn
|
|
|
|
func NewSocks5PktConn(c net.PacketConn, writeAddr net.Addr, tgtAddr Addr, tgtHeader bool) *Socks5PktConn {
|
|
|
|
pc := &Socks5PktConn{
|
|
|
|
PacketConn: c,
|
|
|
|
writeAddr: writeAddr,
|
|
|
|
tgtAddr: tgtAddr,
|
|
|
|
tgtHeader: tgtHeader}
|
|
|
|
return pc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *Socks5PktConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
if !pc.tgtHeader {
|
|
|
|
return pc.PacketConn.ReadFrom(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, len(b))
|
|
|
|
n, raddr, err := pc.PacketConn.ReadFrom(buf)
|
|
|
|
if err != nil {
|
|
|
|
return n, raddr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://tools.ietf.org/html/rfc1928#section-7
|
|
|
|
// +----+------+------+----------+----------+----------+
|
|
|
|
// |RSV | FRAG | ATYP | DST.ADDR | DST.PORT | DATA |
|
|
|
|
// +----+------+------+----------+----------+----------+
|
|
|
|
// | 2 | 1 | 1 | Variable | 2 | Variable |
|
|
|
|
// +----+------+------+----------+----------+----------+
|
|
|
|
tgtAddr := SplitAddr(buf[3:])
|
|
|
|
copy(b, buf[3+len(tgtAddr):])
|
|
|
|
|
|
|
|
//test
|
|
|
|
if pc.writeAddr == nil {
|
|
|
|
pc.writeAddr = raddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if pc.tgtAddr == nil {
|
|
|
|
pc.tgtAddr = tgtAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
return n - len(tgtAddr), raddr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *Socks5PktConn) WriteTo(b []byte, addr net.Addr) (int, error) {
|
|
|
|
if !pc.tgtHeader {
|
|
|
|
return pc.PacketConn.WriteTo(b, addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := append(append([]byte{0, 0, 0}, b[:]...))
|
|
|
|
return pc.PacketConn.WriteTo(buf, pc.writeAddr)
|
|
|
|
}
|