1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-07 13:54:27 -04:00
v2fly/proxy/socks/socks.go

195 lines
5.5 KiB
Go
Raw Normal View History

package socks
2015-09-07 08:49:40 -04:00
import (
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-23 08:14:53 -04:00
"sync"
2015-09-09 06:13:52 -04:00
2015-09-10 18:24:18 -04:00
"github.com/v2ray/v2ray-core"
"github.com/v2ray/v2ray-core/common/errors"
2015-09-19 18:50:21 -04:00
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
2015-09-09 06:13:52 -04:00
)
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
config jsonconfig.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 {
config, err := jsonconfig.Load(rawConfig)
2015-09-12 05:51:42 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Unable to load socks config: %v", err)
panic(errors.NewConfigurationError())
2015-09-12 05:51:42 -04:00
}
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-25 11:59:45 -04:00
log.Error("Socks failed to listen on port %d: %v", port, err)
2015-09-12 14:36:21 -04:00
return err
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
}
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-25 11:59:45 -04:00
log.Error("Socks failed to accept new connection %v", err)
return
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-24 08:51:19 -04:00
reader := v2net.NewTimeOutReader(4, connection)
2015-09-09 06:13:52 -04:00
auth, auth4, err := protocol.ReadAuthentication(reader)
if err != nil && !errors.HasCode(err, 1000) {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to read authentication: %v", err)
2015-09-12 14:36:21 -04:00
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
if errors.HasCode(err, 1000) {
result := protocol.Socks4RequestGranted
if auth4.Command == protocol.CmdBind {
result = protocol.Socks4RequestRejected
}
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth4.Port, auth4.IP[:])
2015-09-25 11:59:45 -04:00
connection.Write(socks4Response.ToBytes(nil))
2015-09-09 06:13:52 -04:00
if result == protocol.Socks4RequestRejected {
2015-09-25 11:59:45 -04:00
return errors.NewInvalidOperationError("Socks4 command " + strconv.Itoa(int(auth4.Command)))
2015-09-17 11:37:04 -04:00
}
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 {
expectedAuthMethod := protocol.AuthNotRequired
if server.config.IsPassword() {
expectedAuthMethod = protocol.AuthUserPass
2015-09-17 11:37:04 -04:00
}
if !auth.HasAuthMethod(expectedAuthMethod) {
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
err = protocol.WriteAuthentication(connection, authResponse)
2015-09-17 11:37:04 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to write authentication: %v", err)
2015-09-17 11:37:04 -04:00
return err
}
2015-09-25 11:59:45 -04:00
log.Warning("Socks client doesn't support allowed any auth methods.")
return errors.NewInvalidOperationError("Unsupported auth methods.")
2015-09-17 11:37:04 -04:00
}
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
err = protocol.WriteAuthentication(connection, authResponse)
2015-09-15 18:06:22 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to write authentication: %v", err)
2015-09-15 18:06:22 -04:00
return err
}
if server.config.IsPassword() {
upRequest, err := protocol.ReadUserPassRequest(reader)
2015-09-17 11:37:04 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to read username and password: %v", err)
2015-09-17 11:37:04 -04:00
return err
}
status := byte(0)
if !upRequest.IsValid(server.config.Username, server.config.Password) {
status = byte(0xFF)
}
upResponse := protocol.NewSocks5UserPassResponse(status)
err = protocol.WriteUserPassResponse(connection, upResponse)
2015-09-17 11:37:04 -04:00
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to write user pass response: %v", err)
2015-09-17 11:37:04 -04:00
return err
}
if status != byte(0) {
2015-09-25 11:59:45 -04:00
err = errors.NewAuthenticationError(upRequest.AuthDetail())
log.Warning(err.Error())
return err
2015-09-17 11:37:04 -04:00
}
2015-09-15 18:06:22 -04:00
}
2015-09-17 11:37:04 -04:00
request, err := protocol.ReadRequest(reader)
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to read request: %v", err)
return err
}
2015-09-09 06:13:52 -04:00
response := protocol.NewSocks5Response()
2015-09-11 08:12:26 -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 {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to write response: %v", err)
2015-09-17 11:37:04 -04:00
return err
}
log.Warning("Unsupported socks command %d", request.Command)
2015-09-25 11:59:45 -04:00
return errors.NewInvalidOperationError("Socks command " + strconv.Itoa(int(request.Command)))
2015-09-17 11:37:04 -04:00
}
2015-09-09 06:13:52 -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 {
case protocol.AddrTypeIPv4:
2015-09-17 11:37:04 -04:00
copy(response.IPv4[:], request.IPv4[:])
case protocol.AddrTypeIPv6:
2015-09-17 11:37:04 -04:00
copy(response.IPv6[:], request.IPv6[:])
case protocol.AddrTypeDomain:
2015-09-17 11:37:04 -04:00
response.Domain = request.Domain
}
err = protocol.WriteResponse(connection, response)
if err != nil {
2015-09-25 11:59:45 -04:00
log.Error("Socks failed to 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
2015-10-02 09:32:26 -04:00
ray := server.vPoint.DispatchToOutbound(v2net.NewPacket(dest, nil, true))
2015-09-10 18:24:18 -04:00
input := ray.InboundInput()
output := ray.InboundOutput()
2015-09-23 08:14:53 -04:00
var readFinish, writeFinish sync.Mutex
readFinish.Lock()
writeFinish.Lock()
2015-09-10 18:24:18 -04:00
2015-09-23 11:13:50 -04:00
go dumpInput(reader, input, &readFinish)
go dumpOutput(connection, output, &writeFinish)
2015-09-23 08:14:53 -04:00
writeFinish.Lock()
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-23 11:13:50 -04:00
func dumpInput(reader io.Reader, input chan<- []byte, finish *sync.Mutex) {
2015-09-15 15:45:04 -04:00
v2net.ReaderToChan(input, reader)
2015-09-23 08:14:53 -04:00
finish.Unlock()
2015-09-13 14:01:50 -04:00
close(input)
2015-09-10 18:24:18 -04:00
}
2015-09-23 11:13:50 -04:00
func dumpOutput(writer io.Writer, output <-chan []byte, finish *sync.Mutex) {
2015-09-15 15:45:04 -04:00
v2net.ChanToWriter(writer, output)
2015-09-23 08:14:53 -04:00
finish.Unlock()
2015-09-10 18:24:18 -04:00
}