|
|
@ -64,92 +64,113 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|
|
|
|
|
|
|
|
|
|
|
reader := bufio.NewReader(connection)
|
|
|
|
reader := bufio.NewReader(connection)
|
|
|
|
|
|
|
|
|
|
|
|
auth, err := socksio.ReadAuthentication(reader)
|
|
|
|
auth, auth4, err := socksio.ReadAuthentication(reader)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil && err != socksio.ErrorSocksVersion4 {
|
|
|
|
log.Error("Error on reading authentication: %v", err)
|
|
|
|
log.Error("Error on reading authentication: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
expectedAuthMethod := socksio.AuthNotRequired
|
|
|
|
var dest v2net.Address
|
|
|
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
|
|
|
|
|
|
|
expectedAuthMethod = socksio.AuthUserPass
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !auth.HasAuthMethod(expectedAuthMethod) {
|
|
|
|
// TODO refactor this part
|
|
|
|
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNoMatchingMethod)
|
|
|
|
if err == socksio.ErrorSocksVersion4 {
|
|
|
|
err = socksio.WriteAuthentication(connection, authResponse)
|
|
|
|
result := socksio.Socks4RequestGranted
|
|
|
|
if err != nil {
|
|
|
|
if auth4.Command == socksio.CmdBind {
|
|
|
|
log.Error("Error on socksio write authentication: %v", err)
|
|
|
|
result = socksio.Socks4RequestRejected
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Warning("Client doesn't support allowed any auth methods.")
|
|
|
|
socks4Response := socksio.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
|
|
|
|
return ErrorAuthenticationFailed
|
|
|
|
socksio.WriteSocks4AuthenticationResponse(connection, socks4Response)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
authResponse := socksio.NewAuthenticationResponse(expectedAuthMethod)
|
|
|
|
if result == socksio.Socks4RequestRejected {
|
|
|
|
err = socksio.WriteAuthentication(connection, authResponse)
|
|
|
|
return ErrorCommandNotSupported
|
|
|
|
if err != nil {
|
|
|
|
}
|
|
|
|
log.Error("Error on socksio write authentication: %v", err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
dest = v2net.IPAddress(auth4.IP[:], auth4.Port)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
|
|
|
expectedAuthMethod := socksio.AuthNotRequired
|
|
|
|
upRequest, err := socksio.ReadUserPassRequest(reader)
|
|
|
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
|
|
|
|
|
|
|
expectedAuthMethod = socksio.AuthUserPass
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !auth.HasAuthMethod(expectedAuthMethod) {
|
|
|
|
|
|
|
|
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNoMatchingMethod)
|
|
|
|
|
|
|
|
err = socksio.WriteAuthentication(connection, authResponse)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
authResponse := socksio.NewAuthenticationResponse(expectedAuthMethod)
|
|
|
|
|
|
|
|
err = socksio.WriteAuthentication(connection, authResponse)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Failed to read username and password: %v", err)
|
|
|
|
log.Error("Error on socksio write authentication: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status := byte(0)
|
|
|
|
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
|
|
|
if !upRequest.IsValid(server.config.Username, server.config.Password) {
|
|
|
|
upRequest, err := socksio.ReadUserPassRequest(reader)
|
|
|
|
status = byte(0xFF)
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
upResponse := socksio.NewSocks5UserPassResponse(status)
|
|
|
|
|
|
|
|
err = socksio.WriteUserPassResponse(connection, upResponse)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error("Error on socksio write user pass response: %v", err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if status != byte(0) {
|
|
|
|
|
|
|
|
return ErrorInvalidUser
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
upResponse := socksio.NewSocks5UserPassResponse(status)
|
|
|
|
|
|
|
|
err = socksio.WriteUserPassResponse(connection, upResponse)
|
|
|
|
request, err := socksio.ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error on socksio write user pass response: %v", err)
|
|
|
|
log.Error("Error on reading socks request: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if status != byte(0) {
|
|
|
|
|
|
|
|
return ErrorInvalidUser
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request, err := socksio.ReadRequest(reader)
|
|
|
|
response := socksio.NewSocks5Response()
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error("Error on reading socks request: %v", err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response := socksio.NewSocks5Response()
|
|
|
|
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
|
|
|
|
|
|
|
response := socksio.NewSocks5Response()
|
|
|
|
|
|
|
|
response.Error = socksio.ErrorCommandNotSupported
|
|
|
|
|
|
|
|
err = socksio.WriteResponse(connection, response)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
log.Error("Error on socksio write response: %v", err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Warning("Unsupported socks command %d", request.Command)
|
|
|
|
|
|
|
|
return ErrorCommandNotSupported
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
|
|
|
response.Error = socksio.ErrorSuccess
|
|
|
|
response := socksio.NewSocks5Response()
|
|
|
|
response.Port = request.Port
|
|
|
|
response.Error = socksio.ErrorCommandNotSupported
|
|
|
|
response.AddrType = request.AddrType
|
|
|
|
|
|
|
|
switch response.AddrType {
|
|
|
|
|
|
|
|
case socksio.AddrTypeIPv4:
|
|
|
|
|
|
|
|
copy(response.IPv4[:], request.IPv4[:])
|
|
|
|
|
|
|
|
case socksio.AddrTypeIPv6:
|
|
|
|
|
|
|
|
copy(response.IPv6[:], request.IPv6[:])
|
|
|
|
|
|
|
|
case socksio.AddrTypeDomain:
|
|
|
|
|
|
|
|
response.Domain = request.Domain
|
|
|
|
|
|
|
|
}
|
|
|
|
err = socksio.WriteResponse(connection, response)
|
|
|
|
err = socksio.WriteResponse(connection, response)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Error on socksio write response: %v", err)
|
|
|
|
log.Error("Error on socksio write response: %v", err)
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Warning("Unsupported socks command %d", request.Command)
|
|
|
|
|
|
|
|
return ErrorCommandNotSupported
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
response.Error = socksio.ErrorSuccess
|
|
|
|
dest = request.Destination()
|
|
|
|
response.Port = request.Port
|
|
|
|
|
|
|
|
response.AddrType = request.AddrType
|
|
|
|
|
|
|
|
switch response.AddrType {
|
|
|
|
|
|
|
|
case socksio.AddrTypeIPv4:
|
|
|
|
|
|
|
|
copy(response.IPv4[:], request.IPv4[:])
|
|
|
|
|
|
|
|
case socksio.AddrTypeIPv6:
|
|
|
|
|
|
|
|
copy(response.IPv6[:], request.IPv6[:])
|
|
|
|
|
|
|
|
case socksio.AddrTypeDomain:
|
|
|
|
|
|
|
|
response.Domain = request.Domain
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = socksio.WriteResponse(connection, response)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
ray := server.vPoint.NewInboundConnectionAccepted(dest)
|
|
|
|
log.Error("Error on socksio write response: %v", err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ray := server.vPoint.NewInboundConnectionAccepted(request.Destination())
|
|
|
|
|
|
|
|
input := ray.InboundInput()
|
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
|
|
|
output := ray.InboundOutput()
|
|
|
|
readFinish := make(chan bool)
|
|
|
|
readFinish := make(chan bool)
|
|
|
|