1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00
v2fly/proxy/socks/server.go

200 lines
4.8 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-23 08:14:53 -04:00
"sync"
2015-10-06 11:24:57 -04:00
"time"
2015-09-09 06:13:52 -04:00
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
2016-12-27 18:53:29 -05:00
"v2ray.com/core/common"
2016-12-09 07:17:34 -05:00
"v2ray.com/core/common/buf"
"v2ray.com/core/common/bufio"
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2017-01-03 18:43:13 -05:00
proto "v2ray.com/core/common/protocol"
2016-12-15 05:51:09 -05:00
"v2ray.com/core/common/serial"
2016-12-29 18:32:20 -05:00
"v2ray.com/core/common/signal"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/udp"
2015-09-09 06:13:52 -04:00
)
2016-04-25 18:33:16 -04:00
// Server is a SOCKS 5 proxy server
type Server struct {
2016-01-31 11:01:28 -05:00
tcpMutex sync.RWMutex
udpMutex sync.RWMutex
accepting bool
packetDispatcher dispatcher.PacketDispatcher
2016-08-28 17:46:50 -04:00
config *ServerConfig
2016-06-14 16:54:08 -04:00
tcpListener *internet.TCPHub
2016-12-21 09:48:39 -05:00
udpHub *udp.Hub
2016-01-31 11:01:28 -05:00
udpAddress v2net.Destination
2016-12-21 09:37:16 -05:00
udpServer *udp.Server
2016-06-04 08:25:13 -04:00
meta *proxy.InboundHandlerMeta
2015-09-10 18:24:18 -04:00
}
2016-04-25 18:33:16 -04:00
// NewServer creates a new Server object.
2016-08-28 17:46:50 -04:00
func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) *Server {
2016-07-23 05:09:49 -04:00
s := &Server{
config: config,
meta: meta,
2015-09-16 10:27:36 -04:00
}
2016-07-23 05:09:49 -04:00
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
2016-11-21 15:13:01 -05:00
return errors.New("Socks|Server: Dispatcher is not found in the space.")
2016-07-23 05:09:49 -04:00
}
s.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return nil
})
return s
2015-09-07 08:49:40 -04:00
}
2016-02-06 12:01:27 -05:00
// Port implements InboundHandler.Port().
2016-11-27 15:39:09 -05:00
func (v *Server) Port() v2net.Port {
return v.meta.Port
2016-01-19 17:41:40 -05:00
}
2016-02-06 12:01:27 -05:00
// Close implements InboundHandler.Close().
2016-11-27 15:39:09 -05:00
func (v *Server) Close() {
v.accepting = false
if v.tcpListener != nil {
v.tcpMutex.Lock()
v.tcpListener.Close()
v.tcpListener = nil
v.tcpMutex.Unlock()
}
2016-11-27 15:39:09 -05:00
if v.udpHub != nil {
v.udpMutex.Lock()
v.udpHub.Close()
v.udpHub = nil
v.udpMutex.Unlock()
}
}
2017-01-03 18:43:13 -05:00
// Start implements InboundHandler.Start().
2016-11-27 15:39:09 -05:00
func (v *Server) Start() error {
if v.accepting {
2016-06-03 18:38:22 -04:00
return nil
2016-01-19 17:41:40 -05:00
}
2016-06-14 16:54:08 -04:00
listener, err := internet.ListenTCP(
2016-11-27 15:39:09 -05:00
v.meta.Address,
v.meta.Port,
v.handleConnection,
v.meta.StreamSettings)
2015-09-07 08:49:40 -04:00
if err != nil {
2016-11-27 15:39:09 -05:00
log.Error("Socks: failed to listen on ", v.meta.Address, ":", v.meta.Port, ": ", err)
2015-09-12 14:36:21 -04:00
return err
2015-09-07 08:49:40 -04:00
}
2016-11-27 15:39:09 -05:00
v.accepting = true
v.tcpMutex.Lock()
v.tcpListener = listener
v.tcpMutex.Unlock()
if v.config.UdpEnabled {
v.listenUDP()
2015-10-03 15:42:03 -04:00
}
2015-09-07 08:49:40 -04:00
return nil
}
2016-11-27 15:39:09 -05:00
func (v *Server) handleConnection(connection internet.Connection) {
2015-09-09 06:13:52 -04:00
defer connection.Close()
2015-09-15 18:06:22 -04:00
2017-01-03 08:53:59 -05:00
connection.SetReusable(false)
2016-11-27 15:39:09 -05:00
timedReader := v2net.NewTimeOutReader(v.config.Timeout, connection)
2016-12-09 07:17:34 -05:00
reader := bufio.NewReader(timedReader)
2017-01-03 18:43:13 -05:00
session := &ServerSession{
config: v.config,
meta: v.meta,
2015-09-09 06:13:52 -04:00
}
2016-08-14 17:20:23 -04:00
clientAddr := v2net.DestinationFromAddr(connection.RemoteAddr())
2015-09-09 06:13:52 -04:00
2017-01-03 18:43:13 -05:00
request, err := session.Handshake(reader, connection)
2015-10-03 15:42:03 -04:00
if err != nil {
2017-01-03 18:43:13 -05:00
log.Access(clientAddr, "", log.AccessRejected, err)
log.Info("Socks|Server: Failed to read request: ", err)
return
2015-10-03 18:21:06 -04:00
}
2017-01-03 18:43:13 -05:00
if request.Command == proto.RequestCommandTCP {
dest := request.Destination()
session := &proxy.SessionInfo{
Source: clientAddr,
Destination: dest,
Inbound: v.meta,
}
2017-01-03 18:43:13 -05:00
log.Info("Socks|Server: TCP Connect request to ", dest)
log.Access(clientAddr, dest, log.AccessAccepted, "")
2015-10-03 18:21:06 -04:00
2017-01-03 18:43:13 -05:00
v.transport(reader, connection, session)
return
2015-10-03 15:42:03 -04:00
}
2017-01-03 18:43:13 -05:00
if request.Command == proto.RequestCommandUDP {
v.handleUDP()
return
2016-08-14 11:08:01 -04:00
}
2015-10-03 15:42:03 -04:00
}
2017-01-03 18:43:13 -05:00
func (v *Server) handleUDP() error {
2016-11-27 15:39:09 -05:00
// The TCP connection closes after v method returns. We need to wait until
2015-10-06 11:24:57 -04:00
// 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
}
2016-11-27 15:39:09 -05:00
func (v *Server) transport(reader io.Reader, writer io.Writer, session *proxy.SessionInfo) {
ray := v.packetDispatcher.DispatchToOutbound(session)
2015-09-10 18:24:18 -04:00
input := ray.InboundInput()
output := ray.InboundOutput()
2016-12-29 18:32:20 -05:00
requestDone := signal.ExecuteAsync(func() error {
defer input.Close()
2015-09-09 06:13:52 -04:00
2016-12-09 07:17:34 -05:00
v2reader := buf.NewReader(reader)
if err := buf.PipeUntilEOF(v2reader, input); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Socks|Server: Failed to transport all TCP request: ", err)
2016-12-29 18:32:20 -05:00
return err
}
return nil
})
responseDone := signal.ExecuteAsync(func() error {
2016-12-29 18:51:39 -05:00
defer output.ForceClose()
2016-12-29 18:32:20 -05:00
v2writer := buf.NewWriter(writer)
if err := buf.PipeUntilEOF(output, v2writer); err != nil {
log.Info("Socks|Server: Failed to transport all TCP response: ", err)
return err
2016-11-21 18:17:49 -05:00
}
2016-12-29 18:32:20 -05:00
return nil
2015-09-10 18:24:18 -04:00
2016-12-29 18:32:20 -05:00
})
2016-12-29 18:32:20 -05:00
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
log.Info("Socks|Server: Connection ends with ", err)
2016-11-21 18:17:49 -05:00
}
2015-09-10 18:24:18 -04:00
}
2016-04-25 18:35:42 -04:00
2016-06-14 16:54:08 -04:00
type ServerFactory struct{}
2016-11-27 15:39:09 -05:00
func (v *ServerFactory) StreamCapability() v2net.NetworkList {
2016-10-02 17:43:58 -04:00
return v2net.NetworkList{
2017-01-03 08:53:59 -05:00
Network: []v2net.Network{v2net.Network_TCP},
2016-10-02 17:43:58 -04:00
}
2016-06-14 16:54:08 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
2016-08-28 17:46:50 -04:00
return NewServer(rawConfig.(*ServerConfig), space, meta), nil
2016-06-14 16:54:08 -04:00
}
2016-04-25 18:35:42 -04:00
func init() {
2016-12-27 18:53:29 -05:00
common.Must(proxy.RegisterInboundHandlerCreator(serial.GetMessageType(new(ServerConfig)), new(ServerFactory)))
2016-04-25 18:35:42 -04:00
}