2015-09-08 07:18:55 -04:00
|
|
|
package socks
|
2015-09-07 08:49:40 -04:00
|
|
|
|
|
|
|
import (
|
2015-09-09 06:13:52 -04:00
|
|
|
"errors"
|
2015-09-10 18:24:18 -04:00
|
|
|
"io"
|
2015-09-07 08:49:40 -04:00
|
|
|
"net"
|
2015-09-09 06:13:52 -04:00
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
"github.com/v2ray/v2ray-core"
|
2015-09-09 06:13:52 -04:00
|
|
|
socksio "github.com/v2ray/v2ray-core/io/socks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrorAuthenticationFailed = errors.New("None of the authentication methods is allowed.")
|
|
|
|
ErrorCommandNotSupported = errors.New("Client requested an unsupported command.")
|
2015-09-07 08:49:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// SocksServer is a SOCKS 5 proxy server
|
|
|
|
type SocksServer struct {
|
|
|
|
accepting bool
|
2015-09-10 18:24:18 -04:00
|
|
|
vPoint *core.VPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSocksServer(vp *core.VPoint) *SocksServer {
|
|
|
|
server := new(SocksServer)
|
|
|
|
server.vPoint = vp
|
|
|
|
return server
|
2015-09-07 08:49:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (server *SocksServer) Listen(port uint8) error {
|
|
|
|
listener, err := net.Listen("tcp", ":"+string(port))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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) error {
|
|
|
|
for server.accepting {
|
|
|
|
connection, err := listener.Accept()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go server.HandleConnection(connection)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
auth, err := socksio.ReadAuthentication(connection)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if auth.HasAuthMethod(socksio.AuthNotRequired) {
|
|
|
|
return ErrorAuthenticationFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
authResponse := socksio.NewAuthenticationResponse(socksio.AuthNotRequired)
|
|
|
|
socksio.WriteAuthentication(connection, authResponse)
|
|
|
|
|
|
|
|
request, err := socksio.ReadRequest(connection)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate {
|
|
|
|
response := socksio.NewSocks5Response()
|
|
|
|
response.Error = socksio.ErrorCommandNotSupported
|
|
|
|
socksio.WriteResponse(connection, response)
|
|
|
|
return ErrorCommandNotSupported
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
ray := server.vPoint.NewInboundConnectionAccepted(request.Destination())
|
|
|
|
input := ray.InboundInput()
|
|
|
|
output := ray.InboundOutput()
|
|
|
|
finish := make(chan bool, 2)
|
|
|
|
|
|
|
|
go server.dumpInput(connection, input, finish)
|
|
|
|
go server.dumpOutput(connection, output, finish)
|
|
|
|
server.waitForFinish(finish)
|
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
|
|
|
|
|
|
|
func (server *SocksServer) dumpInput(conn net.Conn, input chan<- []byte, finish chan<- bool) {
|
|
|
|
for {
|
|
|
|
buffer := make([]byte, 256)
|
|
|
|
nBytes, err := conn.Read(buffer)
|
|
|
|
if err == io.EOF {
|
|
|
|
finish <- true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
input <- buffer[:nBytes]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *SocksServer) dumpOutput(conn net.Conn, output <-chan []byte, finish chan<- bool) {
|
|
|
|
for {
|
|
|
|
buffer, open := <-output
|
|
|
|
if !open {
|
|
|
|
finish <- true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
conn.Write(buffer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *SocksServer) waitForFinish(finish <-chan bool) {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
<-finish
|
|
|
|
}
|
|
|
|
}
|