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