2015-09-08 07:18:55 -04:00
|
|
|
package socks
|
2015-09-07 08:49:40 -04:00
|
|
|
|
|
|
|
import (
|
2015-09-17 16:05:47 -04:00
|
|
|
_ "bufio"
|
2015-09-09 06:13:52 -04:00
|
|
|
"errors"
|
2015-09-15 18:06:22 -04:00
|
|
|
"io"
|
2015-09-07 08:49:40 -04:00
|
|
|
"net"
|
2015-09-11 08:12:26 -04:00
|
|
|
"strconv"
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
|
|
|
protocol "github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
2015-09-09 06:13:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrorAuthenticationFailed = errors.New("None of the authentication methods is allowed.")
|
|
|
|
ErrorCommandNotSupported = errors.New("Client requested an unsupported command.")
|
2015-09-15 18:06:22 -04:00
|
|
|
ErrorInvalidUser = errors.New("Invalid username or password.")
|
2015-09-07 08:49:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SocksServer is a SOCKS 5 proxy server
|
|
|
|
type SocksServer struct {
|
|
|
|
accepting bool
|
2015-09-12 16:11:54 -04:00
|
|
|
vPoint *core.Point
|
2015-09-12 05:51:42 -04:00
|
|
|
config SocksConfig
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-12 16:11:54 -04:00
|
|
|
func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer {
|
2015-09-12 05:51:42 -04:00
|
|
|
config, err := loadConfig(rawConfig)
|
|
|
|
if err != nil {
|
|
|
|
panic(log.Error("Unable to load socks config: %v", err))
|
|
|
|
}
|
2015-09-16 10:27:36 -04:00
|
|
|
return &SocksServer{
|
|
|
|
vPoint: vp,
|
|
|
|
config: config,
|
|
|
|
}
|
2015-09-07 08:49:40 -04:00
|
|
|
}
|
|
|
|
|
2015-09-11 08:12:09 -04:00
|
|
|
func (server *SocksServer) Listen(port uint16) error {
|
|
|
|
listener, err := net.Listen("tcp", ":"+strconv.Itoa(int(port)))
|
2015-09-07 08:49:40 -04:00
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Error("Error on listening port %d: %v", port, err)
|
|
|
|
return err
|
2015-09-07 08:49:40 -04:00
|
|
|
}
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Debug("Working on tcp:%d", port)
|
2015-09-07 08:49:40 -04:00
|
|
|
server.accepting = true
|
2015-09-07 11:46:17 -04:00
|
|
|
go server.AcceptConnections(listener)
|
2015-09-07 08:49:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-16 11:07:05 -04:00
|
|
|
func (server *SocksServer) AcceptConnections(listener net.Listener) {
|
2015-09-07 08:49:40 -04:00
|
|
|
for server.accepting {
|
|
|
|
connection, err := listener.Accept()
|
|
|
|
if err != nil {
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Error("Error on accepting socks connection: %v", err)
|
2015-09-07 08:49:40 -04:00
|
|
|
}
|
|
|
|
go server.HandleConnection(connection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 11:46:17 -04:00
|
|
|
func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
2015-09-09 06:13:52 -04:00
|
|
|
defer connection.Close()
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-09-17 16:05:47 -04:00
|
|
|
reader := connection.(io.Reader)
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
auth, auth4, err := protocol.ReadAuthentication(reader)
|
|
|
|
if err != nil && err != protocol.ErrorSocksVersion4 {
|
2015-09-12 14:36:21 -04:00
|
|
|
log.Error("Error on reading authentication: %v", err)
|
|
|
|
return err
|
2015-09-09 06:13:52 -04:00
|
|
|
}
|
|
|
|
|
2015-09-20 12:22:29 -04:00
|
|
|
var dest v2net.Destination
|
2015-09-12 05:51:42 -04:00
|
|
|
|
2015-09-17 11:37:04 -04:00
|
|
|
// TODO refactor this part
|
2015-09-19 17:54:36 -04:00
|
|
|
if err == protocol.ErrorSocksVersion4 {
|
|
|
|
result := protocol.Socks4RequestGranted
|
|
|
|
if auth4.Command == protocol.CmdBind {
|
|
|
|
result = protocol.Socks4RequestRejected
|
2015-09-16 11:07:05 -04:00
|
|
|
}
|
2015-09-19 17:54:36 -04:00
|
|
|
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
|
|
|
|
protocol.WriteSocks4AuthenticationResponse(connection, socks4Response)
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
if result == protocol.Socks4RequestRejected {
|
2015-09-17 11:37:04 -04:00
|
|
|
return ErrorCommandNotSupported
|
|
|
|
}
|
|
|
|
|
2015-09-20 12:22:29 -04:00
|
|
|
dest = v2net.NewTCPDestination(v2net.IPAddress(auth4.IP[:], auth4.Port))
|
2015-09-17 11:37:04 -04:00
|
|
|
} else {
|
2015-09-19 17:54:36 -04:00
|
|
|
expectedAuthMethod := protocol.AuthNotRequired
|
2015-09-17 11:37:04 -04:00
|
|
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
2015-09-19 17:54:36 -04:00
|
|
|
expectedAuthMethod = protocol.AuthUserPass
|
2015-09-17 11:37:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !auth.HasAuthMethod(expectedAuthMethod) {
|
2015-09-19 17:54:36 -04:00
|
|
|
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
|
|
|
|
err = protocol.WriteAuthentication(connection, authResponse)
|
2015-09-17 11:37:04 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error on socksio write authentication: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warning("Client doesn't support allowed any auth methods.")
|
|
|
|
return ErrorAuthenticationFailed
|
|
|
|
}
|
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
|
|
|
|
err = protocol.WriteAuthentication(connection, authResponse)
|
2015-09-15 18:06:22 -04:00
|
|
|
if err != nil {
|
2015-09-17 11:37:04 -04:00
|
|
|
log.Error("Error on socksio write authentication: %v", err)
|
2015-09-15 18:06:22 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-17 11:37:04 -04:00
|
|
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
2015-09-19 17:54:36 -04:00
|
|
|
upRequest, err := protocol.ReadUserPassRequest(reader)
|
2015-09-17 11:37:04 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read username and password: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
status := byte(0)
|
|
|
|
if !upRequest.IsValid(server.config.Username, server.config.Password) {
|
|
|
|
status = byte(0xFF)
|
|
|
|
}
|
2015-09-19 17:54:36 -04:00
|
|
|
upResponse := protocol.NewSocks5UserPassResponse(status)
|
|
|
|
err = protocol.WriteUserPassResponse(connection, upResponse)
|
2015-09-17 11:37:04 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error on socksio write user pass response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != byte(0) {
|
|
|
|
return ErrorInvalidUser
|
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
}
|
2015-09-17 11:37:04 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
request, err := protocol.ReadRequest(reader)
|
2015-09-16 11:07:05 -04:00
|
|
|
if err != nil {
|
2015-09-17 11:37:04 -04:00
|
|
|
log.Error("Error on reading socks request: %v", err)
|
2015-09-16 11:07:05 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
response := protocol.NewSocks5Response()
|
2015-09-11 08:12:26 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
|
|
|
|
response := protocol.NewSocks5Response()
|
|
|
|
response.Error = protocol.ErrorCommandNotSupported
|
|
|
|
err = protocol.WriteResponse(connection, response)
|
2015-09-17 11:37:04 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error on socksio write response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Warning("Unsupported socks command %d", request.Command)
|
|
|
|
return ErrorCommandNotSupported
|
|
|
|
}
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
response.Error = protocol.ErrorSuccess
|
2015-09-17 11:37:04 -04:00
|
|
|
response.Port = request.Port
|
|
|
|
response.AddrType = request.AddrType
|
|
|
|
switch response.AddrType {
|
2015-09-19 17:54:36 -04:00
|
|
|
case protocol.AddrTypeIPv4:
|
2015-09-17 11:37:04 -04:00
|
|
|
copy(response.IPv4[:], request.IPv4[:])
|
2015-09-19 17:54:36 -04:00
|
|
|
case protocol.AddrTypeIPv6:
|
2015-09-17 11:37:04 -04:00
|
|
|
copy(response.IPv6[:], request.IPv6[:])
|
2015-09-19 17:54:36 -04:00
|
|
|
case protocol.AddrTypeDomain:
|
2015-09-17 11:37:04 -04:00
|
|
|
response.Domain = request.Domain
|
|
|
|
}
|
2015-09-19 17:54:36 -04:00
|
|
|
err = protocol.WriteResponse(connection, response)
|
2015-09-16 11:07:05 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Error on socksio write response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-09-11 08:12:26 -04:00
|
|
|
|
2015-09-17 11:37:04 -04:00
|
|
|
dest = request.Destination()
|
2015-09-11 08:12:26 -04:00
|
|
|
}
|
2015-09-17 11:37:04 -04:00
|
|
|
|
|
|
|
ray := server.vPoint.NewInboundConnectionAccepted(dest)
|
2015-09-10 18:24:18 -04:00
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
2015-09-14 07:51:30 -04:00
|
|
|
readFinish := make(chan bool)
|
2015-09-14 12:19:17 -04:00
|
|
|
writeFinish := make(chan bool)
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-15 15:40:51 -04:00
|
|
|
go server.dumpInput(reader, input, readFinish)
|
2015-09-14 07:51:30 -04:00
|
|
|
go server.dumpOutput(connection, output, writeFinish)
|
|
|
|
<-writeFinish
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-07 08:49:40 -04:00
|
|
|
return nil
|
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-09-15 15:45:04 -04:00
|
|
|
func (server *SocksServer) dumpInput(reader io.Reader, input chan<- []byte, finish chan<- bool) {
|
|
|
|
v2net.ReaderToChan(input, reader)
|
2015-09-13 14:01:50 -04:00
|
|
|
close(input)
|
2015-09-14 12:19:17 -04:00
|
|
|
log.Debug("Socks input closed")
|
2015-09-13 14:01:50 -04:00
|
|
|
finish <- true
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-09-15 15:45:04 -04:00
|
|
|
func (server *SocksServer) dumpOutput(writer io.Writer, output <-chan []byte, finish chan<- bool) {
|
|
|
|
v2net.ChanToWriter(writer, output)
|
2015-09-14 12:19:17 -04:00
|
|
|
log.Debug("Socks output closed")
|
2015-09-13 14:01:50 -04:00
|
|
|
finish <- true
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|