2015-09-08 07:18:55 -04:00
|
|
|
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-10-06 11:24:57 -04:00
|
|
|
"time"
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-10-08 08:46:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2015-09-24 18:17:44 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/errors"
|
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"
|
2015-09-25 15:00:51 -04:00
|
|
|
jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
2015-09-24 18:17:44 -04:00
|
|
|
"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
|
2015-10-06 17:11:08 -04:00
|
|
|
config *jsonconfig.SocksConfig
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-06 17:11:08 -04:00
|
|
|
func NewSocksServer(vp *core.Point, config *jsonconfig.SocksConfig) *SocksServer {
|
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-10-03 15:42:03 -04:00
|
|
|
if server.config.UDPEnabled {
|
|
|
|
server.ListenUDP(port)
|
|
|
|
}
|
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-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-10-07 09:24:34 -04:00
|
|
|
reader := v2net.NewTimeOutReader(120, connection)
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-19 17:54:36 -04:00
|
|
|
auth, auth4, err := protocol.ReadAuthentication(reader)
|
2015-09-24 18:17:44 -04:00
|
|
|
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-10-03 15:42:03 -04:00
|
|
|
if err != nil && errors.HasCode(err, 1000) {
|
|
|
|
return server.handleSocks4(reader, connection, auth4)
|
2015-09-17 11:37:04 -04:00
|
|
|
} else {
|
2015-10-03 15:42:03 -04:00
|
|
|
return server.handleSocks5(reader, connection, auth)
|
|
|
|
}
|
|
|
|
}
|
2015-09-17 11:37:04 -04:00
|
|
|
|
2015-10-03 18:21:06 -04:00
|
|
|
func (server *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Writer, auth protocol.Socks5AuthenticationRequest) error {
|
2015-10-03 15:42:03 -04:00
|
|
|
expectedAuthMethod := protocol.AuthNotRequired
|
|
|
|
if server.config.IsPassword() {
|
|
|
|
expectedAuthMethod = protocol.AuthUserPass
|
|
|
|
}
|
2015-09-17 11:37:04 -04:00
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
if !auth.HasAuthMethod(expectedAuthMethod) {
|
|
|
|
authResponse := protocol.NewAuthenticationResponse(protocol.AuthNoMatchingMethod)
|
|
|
|
err := protocol.WriteAuthentication(writer, 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
|
|
|
|
}
|
2015-10-03 15:42:03 -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
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
authResponse := protocol.NewAuthenticationResponse(expectedAuthMethod)
|
|
|
|
err := protocol.WriteAuthentication(writer, authResponse)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks failed to write authentication: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if server.config.IsPassword() {
|
|
|
|
upRequest, err := protocol.ReadUserPassRequest(reader)
|
2015-09-16 11:07:05 -04:00
|
|
|
if err != nil {
|
2015-10-03 15:42:03 -04:00
|
|
|
log.Error("Socks failed to read username and password: %v", err)
|
2015-09-16 11:07:05 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-10-03 15:42:03 -04:00
|
|
|
status := byte(0)
|
2015-10-10 09:51:35 -04:00
|
|
|
if server.config.HasAccount(upRequest.Username(), upRequest.Password()) {
|
2015-10-03 15:42:03 -04:00
|
|
|
status = byte(0xFF)
|
|
|
|
}
|
|
|
|
upResponse := protocol.NewSocks5UserPassResponse(status)
|
|
|
|
err = protocol.WriteUserPassResponse(writer, upResponse)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks failed to write user pass response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if status != byte(0) {
|
|
|
|
err = errors.NewAuthenticationError(upRequest.AuthDetail())
|
|
|
|
log.Warning(err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
request, err := protocol.ReadRequest(reader)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks failed to read request: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2015-09-11 08:12:26 -04:00
|
|
|
|
2015-10-03 18:21:06 -04:00
|
|
|
if request.Command == protocol.CmdUdpAssociate && server.config.UDPEnabled {
|
|
|
|
return server.handleUDP(reader, writer)
|
|
|
|
}
|
|
|
|
|
2015-10-04 10:53:50 -04:00
|
|
|
response := protocol.NewSocks5Response()
|
2015-10-03 18:21:06 -04:00
|
|
|
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
|
2015-10-03 15:42:03 -04:00
|
|
|
response := protocol.NewSocks5Response()
|
|
|
|
response.Error = protocol.ErrorCommandNotSupported
|
2015-10-08 17:06:12 -04:00
|
|
|
|
|
|
|
responseBuffer := alloc.NewSmallBuffer().Clear()
|
|
|
|
response.Write(responseBuffer)
|
|
|
|
_, err = writer.Write(responseBuffer.Value)
|
2015-09-16 11:07:05 -04:00
|
|
|
if err != nil {
|
2015-09-25 11:59:45 -04:00
|
|
|
log.Error("Socks failed to write response: %v", err)
|
2015-09-16 11:07:05 -04:00
|
|
|
return err
|
|
|
|
}
|
2015-10-03 15:42:03 -04:00
|
|
|
log.Warning("Unsupported socks command %d", request.Command)
|
|
|
|
return errors.NewInvalidOperationError("Socks command " + strconv.Itoa(int(request.Command)))
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Error = protocol.ErrorSuccess
|
2015-10-03 18:21:06 -04:00
|
|
|
|
2015-10-05 11:09:57 -04:00
|
|
|
// Some SOCKS software requires a value other than dest. Let's fake one:
|
2015-10-06 05:57:26 -04:00
|
|
|
response.Port = uint16(1717)
|
2015-10-05 11:09:44 -04:00
|
|
|
response.AddrType = protocol.AddrTypeIPv4
|
2015-10-06 05:57:26 -04:00
|
|
|
response.IPv4[0] = 0
|
2015-10-05 11:09:57 -04:00
|
|
|
response.IPv4[1] = 0
|
|
|
|
response.IPv4[2] = 0
|
2015-10-06 05:57:26 -04:00
|
|
|
response.IPv4[3] = 0
|
2015-10-05 11:09:44 -04:00
|
|
|
|
2015-10-08 17:06:12 -04:00
|
|
|
responseBuffer := alloc.NewSmallBuffer().Clear()
|
|
|
|
response.Write(responseBuffer)
|
|
|
|
_, err = writer.Write(responseBuffer.Value)
|
2015-10-03 15:42:03 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks failed to write response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := request.Destination()
|
2015-10-08 08:46:18 -04:00
|
|
|
data, err := v2net.ReadFrom(reader, nil)
|
2015-10-03 15:42:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-11 08:12:26 -04:00
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
packet := v2net.NewPacket(dest, data, true)
|
|
|
|
server.transport(reader, writer, packet)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-03 18:21:06 -04:00
|
|
|
func (server *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer) error {
|
|
|
|
response := protocol.NewSocks5Response()
|
|
|
|
response.Error = protocol.ErrorSuccess
|
|
|
|
|
|
|
|
udpAddr := server.getUDPAddr()
|
|
|
|
|
|
|
|
response.Port = udpAddr.Port()
|
|
|
|
switch {
|
|
|
|
case udpAddr.IsIPv4():
|
|
|
|
response.AddrType = protocol.AddrTypeIPv4
|
|
|
|
copy(response.IPv4[:], udpAddr.IP())
|
|
|
|
case udpAddr.IsIPv6():
|
|
|
|
response.AddrType = protocol.AddrTypeIPv6
|
|
|
|
copy(response.IPv6[:], udpAddr.IP())
|
|
|
|
case udpAddr.IsDomain():
|
|
|
|
response.AddrType = protocol.AddrTypeDomain
|
|
|
|
response.Domain = udpAddr.Domain()
|
|
|
|
}
|
2015-10-08 17:06:12 -04:00
|
|
|
|
|
|
|
responseBuffer := alloc.NewSmallBuffer()
|
|
|
|
response.Write(responseBuffer)
|
|
|
|
_, err := writer.Write(responseBuffer.Value)
|
|
|
|
responseBuffer.Release()
|
|
|
|
|
2015-10-03 18:21:06 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Socks failed to write response: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
reader.SetTimeOut(300) /* 5 minutes */
|
|
|
|
v2net.ReadFrom(reader, nil) // Just in case of anything left in the socket
|
2015-10-06 11:24:57 -04:00
|
|
|
// The TCP connection closes after this method returns. We need to wait until
|
|
|
|
// the client closes it.
|
|
|
|
// TODO: get notified from UDP part
|
2015-10-06 05:57:26 -04:00
|
|
|
<-time.After(5 * time.Minute)
|
2015-10-03 18:21:06 -04:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
func (server *SocksServer) handleSocks4(reader io.Reader, writer io.Writer, auth protocol.Socks4AuthenticationRequest) error {
|
|
|
|
result := protocol.Socks4RequestGranted
|
|
|
|
if auth.Command == protocol.CmdBind {
|
|
|
|
result = protocol.Socks4RequestRejected
|
2015-09-11 08:12:26 -04:00
|
|
|
}
|
2015-10-03 15:42:03 -04:00
|
|
|
socks4Response := protocol.NewSocks4AuthenticationResponse(result, auth.Port, auth.IP[:])
|
2015-10-08 17:06:12 -04:00
|
|
|
|
|
|
|
responseBuffer := alloc.NewSmallBuffer().Clear()
|
|
|
|
socks4Response.Write(responseBuffer)
|
|
|
|
writer.Write(responseBuffer.Value)
|
|
|
|
responseBuffer.Release()
|
2015-09-17 11:37:04 -04:00
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
if result == protocol.Socks4RequestRejected {
|
|
|
|
return errors.NewInvalidOperationError("Socks4 command " + strconv.Itoa(int(auth.Command)))
|
|
|
|
}
|
|
|
|
|
|
|
|
dest := v2net.NewTCPDestination(v2net.IPAddress(auth.IP[:], auth.Port))
|
2015-10-08 08:46:18 -04:00
|
|
|
data, err := v2net.ReadFrom(reader, nil)
|
2015-10-03 15:42:03 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
packet := v2net.NewPacket(dest, data, true)
|
|
|
|
server.transport(reader, writer, packet)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *SocksServer) transport(reader io.Reader, writer io.Writer, firstPacket v2net.Packet) {
|
|
|
|
ray := server.vPoint.DispatchToOutbound(firstPacket)
|
2015-09-10 18:24:18 -04:00
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
var inputFinish, outputFinish sync.Mutex
|
|
|
|
inputFinish.Lock()
|
|
|
|
outputFinish.Lock()
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-10-03 15:42:03 -04:00
|
|
|
go dumpInput(reader, input, &inputFinish)
|
|
|
|
go dumpOutput(writer, output, &outputFinish)
|
|
|
|
outputFinish.Lock()
|
2015-09-07 08:49:40 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-10-08 08:46:18 -04:00
|
|
|
func dumpInput(reader io.Reader, input chan<- *alloc.Buffer, 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-10-08 08:46:18 -04:00
|
|
|
func dumpOutput(writer io.Writer, output <-chan *alloc.Buffer, 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
|
|
|
}
|