You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
2.3 KiB
Go

9 years ago
package socks
import (
"fmt"
"io"
"log"
"net"
//"strconv"
"encoding/binary"
)
type socks5Conn struct {
//addr string
8 years ago
clientConn net.Conn
serverConn net.Conn
dial dialFunc
9 years ago
}
func (s5 *socks5Conn) Serve() {
defer s5.Close()
if err := s5.handshake(); err != nil {
log.Println(err)
return
}
if err := s5.processRequest(); err != nil {
log.Println(err)
return
}
}
func (s5 *socks5Conn) handshake() error {
// version has already readed by socksConn.Serve()
// only process auth methods here
buf := make([]byte, 258)
8 years ago
n, err := io.ReadAtLeast(s5.clientConn, buf, 1)
9 years ago
if err != nil {
return err
}
l := int(buf[0]) + 1
if n < l {
8 years ago
_, err := io.ReadFull(s5.clientConn, buf[n:l])
9 years ago
if err != nil {
return err
}
}
// no auth required
8 years ago
s5.clientConn.Write([]byte{0x05, 0x00})
9 years ago
return nil
}
func (s5 *socks5Conn) processRequest() error {
buf := make([]byte, 258)
8 years ago
n, err := io.ReadAtLeast(s5.clientConn, buf, 10)
9 years ago
if err != nil {
return err
}
if buf[0] != socks5Version {
return fmt.Errorf("error version %s", buf[0])
}
// only support connect
if buf[1] != cmdConnect {
return fmt.Errorf("unsupported command %s", buf[1])
}
hlen := 0
host := ""
msglen := 0
switch buf[3] {
case addrTypeIPv4:
hlen = 4
case addrTypeDomain:
hlen = int(buf[4]) + 1
case addrTypeIPv6:
hlen = 16
}
msglen = 6 + hlen
if n < msglen {
8 years ago
_, err := io.ReadFull(s5.clientConn, buf[n:msglen])
9 years ago
if err != nil {
return err
}
}
// get target address
addr := buf[4 : 4+hlen]
if buf[3] == addrTypeDomain {
host = string(addr[1:])
} else {
host = net.IP(addr).String()
}
port := binary.BigEndian.Uint16(buf[msglen-2 : msglen])
target := net.JoinHostPort(host, fmt.Sprintf("%d", port))
// reply user with connect success
// if dial to target failed, user will receive connection reset
8 years ago
s5.clientConn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01})
9 years ago
log.Printf("connecing to %s", target)
// connect to the target
8 years ago
s5.serverConn, err = s5.dial("tcp", target)
9 years ago
if err != nil {
return err
}
// enter data exchange
s5.forward()
return nil
}
func (s5 *socks5Conn) forward() {
8 years ago
go io.Copy(s5.clientConn, s5.serverConn)
io.Copy(s5.serverConn, s5.clientConn)
9 years ago
}
func (s5 *socks5Conn) Close() {
8 years ago
if s5.serverConn != nil {
s5.serverConn.Close()
9 years ago
}
8 years ago
if s5.clientConn != nil {
s5.clientConn.Close()
9 years ago
}
}