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.

60 lines
1.0 KiB
Go

9 years ago
package socks
import (
"io"
"log"
"net"
"time"
9 years ago
)
const (
socks4Version = 0x04
socks5Version = 0x05
cmdConnect = 0x01
addrTypeIPv4 = 0x01
addrTypeDomain = 0x03
addrTypeIPv6 = 0x04
)
7 years ago
// DialFunc the function dial to remote
type DialFunc func(network, addr string) (net.Conn, error)
// Conn present a client connection
type Conn struct {
net.Conn
// the function to dial to upstream server
// when nil, use net.Dial
7 years ago
Dial DialFunc
// username for socks5 server
Username string
// password
Password string
9 years ago
}
8 years ago
// Serve serve the client
func (s *Conn) Serve() {
9 years ago
buf := make([]byte, 1)
// read version
io.ReadFull(s.Conn, buf)
9 years ago
dial := s.Dial
if s.Dial == nil {
d := net.Dialer{Timeout: 10 * time.Second}
dial = d.Dial
}
9 years ago
switch buf[0] {
case socks4Version:
s4 := socks4Conn{clientConn: s.Conn, dial: dial}
9 years ago
s4.Serve()
case socks5Version:
s5 := socks5Conn{clientConn: s.Conn, dial: dial,
username: s.Username, password: s.Password}
9 years ago
s5.Serve()
default:
log.Printf("error version %d", buf[0])
s.Conn.Close()
9 years ago
}
}