1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-15 16:56:08 -05:00
v2fly/proxy/shadowsocks/server.go

225 lines
6.1 KiB
Go
Raw Normal View History

2016-01-27 06:46:40 -05:00
// R.I.P Shadowsocks
package shadowsocks
import (
2016-01-28 06:33:58 -05:00
"sync"
2016-11-02 11:18:29 -04:00
"errors"
2016-08-20 14:55:45 -04:00
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"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
udpHub *udp.UDPHub
udpServer *udp.UDPServer
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 {
return nil, errors.New("Shadowsocks|Server: Failed to get user account: " + err.Error())
}
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-05-12 13:42:08 -04:00
func (this *Server) Port() v2net.Port {
2016-06-04 08:25:13 -04:00
return this.meta.Port
2016-01-27 06:46:40 -05:00
}
2016-05-12 13:42:08 -04:00
func (this *Server) Close() {
this.accepting = false
2016-01-30 16:50:36 -05:00
// TODO: synchronization
if this.tcpHub != nil {
this.tcpHub.Close()
this.tcpHub = nil
}
if this.udpHub != nil {
this.udpHub.Close()
this.udpHub = nil
}
}
2016-06-03 18:38:22 -04:00
func (this *Server) Start() error {
if this.accepting {
2016-06-03 18:38:22 -04:00
return nil
}
2016-06-14 16:54:08 -04:00
tcpHub, err := internet.ListenTCP(this.meta.Address, this.meta.Port, this.handleConnection, this.meta.StreamSettings)
if err != nil {
2016-06-04 08:25:13 -04:00
log.Error("Shadowsocks: Failed to listen TCP on ", this.meta.Address, ":", this.meta.Port, ": ", err)
return err
}
2016-01-28 15:30:05 -05:00
this.tcpHub = tcpHub
2016-09-17 18:41:21 -04:00
if this.config.UdpEnabled {
2016-11-13 08:33:00 -05:00
this.udpServer = udp.NewUDPServer(this.packetDispatcher)
2016-08-15 11:44:46 -04:00
udpHub, err := udp.ListenUDP(this.meta.Address, this.meta.Port, udp.ListenOption{Callback: this.handlerUDPPayload})
2016-01-28 15:30:05 -05:00
if err != nil {
2016-06-04 08:25:13 -04:00
log.Error("Shadowsocks: Failed to listen UDP on ", this.meta.Address, ":", this.meta.Port, ": ", err)
2016-02-03 15:44:20 -05:00
return err
2016-01-28 15:30:05 -05:00
}
this.udpHub = udpHub
}
2016-02-16 08:35:24 -05:00
this.accepting = true
2016-01-27 06:46:40 -05:00
return nil
}
2016-08-15 11:44:46 -04:00
func (this *Server) handlerUDPPayload(payload *alloc.Buffer, session *proxy.SessionInfo) {
source := session.Source
2016-10-31 10:24:28 -04:00
request, data, err := DecodeUDPPacket(this.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-02 11:17:57 -04:00
if request.Option.Has(RequestOptionOneTimeAuth) && this.account.OneTimeAuth == Account_Disabled {
log.Info("Shadowsocks|Server: Client payload enables OTA but server doesn't allow it.")
payload.Release()
return
}
if !request.Option.Has(RequestOptionOneTimeAuth) && this.account.OneTimeAuth == Account_Enabled {
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-11-13 08:33:00 -05:00
this.udpServer.Dispatch(&proxy.SessionInfo{Source: source, Destination: dest, User: request.User, Inbound: this.meta}, data, func(destination v2net.Destination, payload *alloc.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-10-31 10:24:28 -04:00
this.udpHub.WriteTo(data.Value, source)
2016-02-01 15:34:07 -05:00
})
2016-01-28 15:30:05 -05:00
}
2016-06-14 16:54:08 -04:00
func (this *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()
bufferedReader := v2io.NewBufferedReader(timedReader)
defer bufferedReader.Release()
2016-10-31 10:24:28 -04:00
request, bodyReader, err := ReadTCPSession(this.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-10-31 10:24:28 -04:00
userSettings := this.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-13 08:33:00 -05:00
ray := this.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-13 08:33:00 -05:00
Inbound: this.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-10-31 10:24:28 -04:00
bufferedWriter := v2io.NewBufferedWriter(conn)
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-10-31 10:24:28 -04:00
v2io.Pipe(ray.InboundOutput(), responseWriter)
2016-01-28 07:40:00 -05:00
}
2016-01-28 06:33:58 -05:00
}()
2016-10-31 10:24:28 -04:00
v2io.Pipe(bodyReader, ray.InboundInput())
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-10-02 17:43:58 -04:00
func (this *ServerFactory) StreamCapability() v2net.NetworkList {
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
}
func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.InboundHandlerMeta) (proxy.InboundHandler, error) {
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
}