1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-02 17:27:50 -04:00
v2fly/proxy/shadowsocks/server.go

228 lines
6.1 KiB
Go
Raw Normal View History

2016-01-27 06:46:40 -05:00
package shadowsocks
import (
2016-01-28 06:33:58 -05:00
"sync"
2016-12-09 07:17:34 -05:00
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-12-09 07:17:34 -05:00
"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"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/internet/udp"
2016-01-27 06:46:40 -05:00
)
2016-05-12 13:42:08 -04:00
type Server struct {
2016-01-31 11:01:28 -05:00
packetDispatcher dispatcher.PacketDispatcher
2016-09-25 16:07:32 -04:00
config *ServerConfig
2016-10-31 10:24:28 -04:00
user *protocol.User
2016-11-02 11:17:57 -04:00
account *ShadowsocksAccount
2016-06-04 08:25:13 -04:00
meta *proxy.InboundHandlerMeta
2016-01-31 11:01:28 -05:00
accepting bool
2016-06-14 16:54:08 -04:00
tcpHub *internet.TCPHub
2016-12-21 09:48:39 -05:00
udpHub *udp.Hub
2016-12-21 09:37:16 -05:00
udpServer *udp.Server
2016-01-31 11:01:28 -05:00
}
2016-09-25 16:07:32 -04:00
func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) (*Server, error) {
2016-09-17 18:41:21 -04:00
if config.GetUser() == nil {
return nil, protocol.ErrUserMissing
2016-01-31 11:01:28 -05:00
}
2016-10-31 10:24:28 -04:00
2016-11-02 11:18:29 -04:00
rawAccount, err := config.User.GetTypedAccount()
2016-11-02 11:17:57 -04:00
if err != nil {
2016-12-04 03:43:33 -05:00
return nil, errors.Base(err).Message("Shadowsocks|Server: Failed to get user account.")
2016-11-02 11:17:57 -04:00
}
account := rawAccount.(*ShadowsocksAccount)
2016-09-17 18:41:21 -04:00
s := &Server{
2016-11-02 11:17:57 -04:00
config: config,
meta: meta,
user: config.GetUser(),
account: account,
2016-09-17 18:41:21 -04:00
}
space.InitializeApplication(func() error {
if !space.HasApp(dispatcher.APP_ID) {
2016-11-21 15:13:01 -05:00
return errors.New("Shadowsocks|Server: Dispatcher is not found in space.")
2016-09-17 18:41:21 -04:00
}
s.packetDispatcher = space.GetApp(dispatcher.APP_ID).(dispatcher.PacketDispatcher)
return nil
})
return s, nil
2016-01-27 06:46:40 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *Server) Port() v2net.Port {
return v.meta.Port
2016-01-27 06:46:40 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *Server) Close() {
v.accepting = false
2016-01-30 16:50:36 -05:00
// TODO: synchronization
2016-11-27 15:39:09 -05:00
if v.tcpHub != nil {
v.tcpHub.Close()
v.tcpHub = nil
}
2016-11-27 15:39:09 -05:00
if v.udpHub != nil {
v.udpHub.Close()
v.udpHub = nil
}
}
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-11-27 15:39:09 -05:00
tcpHub, err := internet.ListenTCP(v.meta.Address, v.meta.Port, v.handleConnection, v.meta.StreamSettings)
if err != nil {
2016-11-27 15:39:09 -05:00
log.Error("Shadowsocks: Failed to listen TCP on ", v.meta.Address, ":", v.meta.Port, ": ", err)
return err
}
2016-11-27 15:39:09 -05:00
v.tcpHub = tcpHub
2016-01-28 15:30:05 -05:00
2016-11-27 15:39:09 -05:00
if v.config.UdpEnabled {
2016-12-21 09:37:16 -05:00
v.udpServer = udp.NewServer(v.packetDispatcher)
2016-11-27 15:39:09 -05:00
udpHub, err := udp.ListenUDP(v.meta.Address, v.meta.Port, udp.ListenOption{Callback: v.handlerUDPPayload})
2016-01-28 15:30:05 -05:00
if err != nil {
2016-11-27 15:39:09 -05:00
log.Error("Shadowsocks: Failed to listen UDP on ", v.meta.Address, ":", v.meta.Port, ": ", err)
2016-02-03 15:44:20 -05:00
return err
2016-01-28 15:30:05 -05:00
}
2016-11-27 15:39:09 -05:00
v.udpHub = udpHub
2016-01-28 15:30:05 -05:00
}
2016-11-27 15:39:09 -05:00
v.accepting = true
2016-02-16 08:35:24 -05:00
2016-01-27 06:46:40 -05:00
return nil
}
2016-12-09 05:35:27 -05:00
func (v *Server) handlerUDPPayload(payload *buf.Buffer, session *proxy.SessionInfo) {
2016-08-15 11:44:46 -04:00
source := session.Source
2016-11-27 15:39:09 -05:00
request, data, err := DecodeUDPPacket(v.user, payload)
2016-01-28 15:30:05 -05:00
if err != nil {
2016-10-31 10:24:28 -04:00
log.Info("Shadowsocks|Server: Skipping invalid UDP packet from: ", source, ": ", err)
log.Access(source, "", log.AccessRejected, err)
payload.Release()
2016-01-28 15:30:05 -05:00
return
}
2016-11-27 15:39:09 -05:00
if request.Option.Has(RequestOptionOneTimeAuth) && v.account.OneTimeAuth == Account_Disabled {
2016-11-02 11:17:57 -04:00
log.Info("Shadowsocks|Server: Client payload enables OTA but server doesn't allow it.")
payload.Release()
return
}
2016-11-27 15:39:09 -05:00
if !request.Option.Has(RequestOptionOneTimeAuth) && v.account.OneTimeAuth == Account_Enabled {
2016-11-02 11:17:57 -04:00
log.Info("Shadowsocks|Server: Client payload disables OTA but server forces it.")
payload.Release()
return
}
2016-10-31 10:24:28 -04:00
dest := request.Destination()
2016-05-24 16:41:51 -04:00
log.Access(source, dest, log.AccessAccepted, "")
2016-10-31 10:24:28 -04:00
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
2016-01-30 16:57:20 -05:00
2016-12-09 05:35:27 -05:00
v.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: v.meta}, data, func(destination v2net.Destination, payload *buf.Buffer) {
2016-04-25 18:13:26 -04:00
defer payload.Release()
2016-01-28 15:30:05 -05:00
2016-10-31 10:24:28 -04:00
data, err := EncodeUDPPacket(request, payload)
2016-01-28 15:30:05 -05:00
if err != nil {
2016-10-31 10:24:28 -04:00
log.Warning("Shadowsocks|Server: Failed to encode UDP packet: ", err)
2016-01-28 15:30:05 -05:00
return
}
2016-10-31 10:24:28 -04:00
defer data.Release()
2016-01-28 15:30:05 -05:00
2016-12-05 09:19:14 -05:00
v.udpHub.WriteTo(data.Bytes(), source)
2016-02-01 15:34:07 -05:00
})
2016-01-28 15:30:05 -05:00
}
2016-11-27 15:39:09 -05:00
func (v *Server) handleConnection(conn internet.Connection) {
defer conn.Close()
2016-11-02 17:18:25 -04:00
conn.SetReusable(false)
timedReader := v2net.NewTimeOutReader(16, conn)
2016-05-01 11:18:02 -04:00
defer timedReader.Release()
2016-12-09 07:17:34 -05:00
bufferedReader := bufio.NewReader(timedReader)
2016-05-01 11:18:02 -04:00
defer bufferedReader.Release()
2016-11-27 15:39:09 -05:00
request, bodyReader, err := ReadTCPSession(v.user, bufferedReader)
2016-01-28 06:33:58 -05:00
if err != nil {
2016-05-24 16:41:51 -04:00
log.Access(conn.RemoteAddr(), "", log.AccessRejected, err)
2016-10-31 10:24:28 -04:00
log.Info("Shadowsocks|Server: Failed to create request from: ", conn.RemoteAddr(), ": ", err)
2016-01-28 06:33:58 -05:00
return
}
2016-10-31 10:24:28 -04:00
defer bodyReader.Release()
2016-05-01 11:18:02 -04:00
bufferedReader.SetCached(false)
2016-01-28 06:33:58 -05:00
2016-11-27 15:39:09 -05:00
userSettings := v.user.GetSettings()
2016-02-03 06:18:28 -05:00
timedReader.SetTimeOut(userSettings.PayloadReadTimeout)
2016-10-31 10:24:28 -04:00
dest := request.Destination()
2016-05-24 16:41:51 -04:00
log.Access(conn.RemoteAddr(), dest, log.AccessAccepted, "")
2016-10-31 10:24:28 -04:00
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
2016-01-30 16:57:20 -05:00
2016-11-27 15:39:09 -05:00
ray := v.packetDispatcher.DispatchToOutbound(&proxy.SessionInfo{
2016-08-14 17:20:23 -04:00
Source: v2net.DestinationFromAddr(conn.RemoteAddr()),
2016-08-14 11:08:01 -04:00
Destination: dest,
2016-11-02 11:22:29 -04:00
User: request.User,
2016-11-27 15:39:09 -05:00
Inbound: v.meta,
2016-08-14 11:08:01 -04:00
})
2016-05-07 03:53:15 -04:00
defer ray.InboundOutput().Release()
2016-01-28 06:33:58 -05:00
var writeFinish sync.Mutex
writeFinish.Lock()
go func() {
2016-10-31 10:24:28 -04:00
defer writeFinish.Unlock()
2016-01-28 07:40:00 -05:00
2016-12-09 07:17:34 -05:00
bufferedWriter := bufio.NewWriter(conn)
2016-10-31 10:24:28 -04:00
defer bufferedWriter.Release()
2016-01-28 07:40:00 -05:00
2016-10-31 10:24:28 -04:00
responseWriter, err := WriteTCPResponse(request, bufferedWriter)
if err != nil {
log.Warning("Shadowsocks|Server: Failed to write response: ", err)
return
}
defer responseWriter.Release()
2016-02-23 12:16:13 -05:00
2016-10-31 10:24:28 -04:00
if payload, err := ray.InboundOutput().Read(); err == nil {
responseWriter.Write(payload)
bufferedWriter.SetCached(false)
2016-12-09 07:17:34 -05:00
if err := buf.PipeUntilEOF(ray.InboundOutput(), responseWriter); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Shadowsocks|Server: Failed to transport all TCP response: ", err)
}
2016-01-28 07:40:00 -05:00
}
2016-01-28 06:33:58 -05:00
}()
2016-12-09 07:17:34 -05:00
if err := buf.PipeUntilEOF(bodyReader, ray.InboundInput()); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Shadowsocks|Server: Failed to transport all TCP request: ", err)
}
2016-04-18 12:44:10 -04:00
ray.InboundInput().Close()
2016-01-28 06:33:58 -05:00
writeFinish.Lock()
}
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{
2016-11-02 17:19:01 -04:00
Network: []v2net.Network{v2net.Network_TCP, v2net.Network_RawTCP},
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-06-14 16:54:08 -04:00
if !space.HasApp(dispatcher.APP_ID) {
2016-08-18 02:21:20 -04:00
return nil, common.ErrBadConfiguration
2016-06-14 16:54:08 -04:00
}
2016-09-25 16:07:32 -04:00
return NewServer(rawConfig.(*ServerConfig), space, meta)
2016-06-14 16:54:08 -04:00
}