2016-01-27 06:46:40 -05:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
2017-02-03 16:35:09 -05:00
|
|
|
"time"
|
2017-01-12 18:56:21 -05:00
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
"v2ray.com/core"
|
2017-01-12 18:56:21 -05:00
|
|
|
"v2ray.com/core/common"
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2017-12-19 15:28:12 -05:00
|
|
|
"v2ray.com/core/common/log"
|
2017-01-13 17:42:39 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-08-20 14:55:45 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
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"
|
2016-01-27 06:46:40 -05:00
|
|
|
)
|
|
|
|
|
2016-05-12 13:42:08 -04:00
|
|
|
type Server struct {
|
2018-01-10 06:22:37 -05:00
|
|
|
config *ServerConfig
|
|
|
|
user *protocol.User
|
|
|
|
account *MemoryAccount
|
|
|
|
v *core.Instance
|
2016-01-31 11:01:28 -05:00
|
|
|
}
|
|
|
|
|
2017-04-07 16:45:07 -04:00
|
|
|
// NewServer create a new Shadowsocks server.
|
2017-01-12 18:56:21 -05:00
|
|
|
func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
|
2016-09-17 18:41:21 -04:00
|
|
|
if config.GetUser() == nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("user is not specified")
|
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 {
|
2017-04-08 19:43:25 -04:00
|
|
|
return nil, newError("failed to get user account").Base(err)
|
2016-11-02 11:17:57 -04:00
|
|
|
}
|
2017-11-29 16:57:18 -05:00
|
|
|
account := rawAccount.(*MemoryAccount)
|
2016-11-02 11:17:57 -04:00
|
|
|
|
2016-09-17 18:41:21 -04:00
|
|
|
s := &Server{
|
2016-11-02 11:17:57 -04:00
|
|
|
config: config,
|
|
|
|
user: config.GetUser(),
|
|
|
|
account: account,
|
2018-01-10 06:22:37 -05:00
|
|
|
v: core.FromContext(ctx),
|
2016-09-17 18:41:21 -04:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
if s.v == nil {
|
|
|
|
return nil, newError("V is not in context.")
|
|
|
|
}
|
2017-11-27 16:09:30 -05:00
|
|
|
|
2016-09-17 18:41:21 -04:00
|
|
|
return s, nil
|
2016-01-27 06:46:40 -05:00
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
func (s *Server) Network() net.NetworkList {
|
2017-01-14 18:57:06 -05:00
|
|
|
list := net.NetworkList{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
if s.config.UdpEnabled {
|
2017-01-14 18:57:06 -05:00
|
|
|
list.Network = append(list.Network, net.Network_UDP)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher core.Dispatcher) error {
|
2017-01-26 14:46:44 -05:00
|
|
|
switch network {
|
|
|
|
case net.Network_TCP:
|
2017-02-03 16:35:09 -05:00
|
|
|
return s.handleConnection(ctx, conn, dispatcher)
|
2017-01-26 14:46:44 -05:00
|
|
|
case net.Network_UDP:
|
2017-02-03 16:35:09 -05:00
|
|
|
return s.handlerUDPPayload(ctx, conn, dispatcher)
|
2017-01-26 14:46:44 -05:00
|
|
|
default:
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("unknown network: ", network)
|
2016-01-29 10:43:45 -05:00
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection, dispatcher core.Dispatcher) error {
|
2017-02-03 16:35:09 -05:00
|
|
|
udpServer := udp.NewDispatcher(dispatcher)
|
2016-01-28 15:30:05 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
reader := buf.NewReader(conn)
|
|
|
|
for {
|
2017-11-09 16:33:15 -05:00
|
|
|
mpayload, err := reader.ReadMultiBuffer()
|
2016-01-28 15:30:05 -05:00
|
|
|
if err != nil {
|
2017-01-26 14:46:44 -05:00
|
|
|
break
|
2016-01-28 15:30:05 -05:00
|
|
|
}
|
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
for _, payload := range mpayload {
|
2017-09-19 17:27:49 -04:00
|
|
|
request, data, err := DecodeUDPPacket(s.user, payload)
|
2017-04-15 15:07:23 -04:00
|
|
|
if err != nil {
|
|
|
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("dropping invalid UDP packet from: ", source).Base(err).WriteToLog()
|
|
|
|
log.Record(&log.AccessMessage{
|
|
|
|
From: source,
|
|
|
|
To: "",
|
|
|
|
Status: log.AccessRejected,
|
|
|
|
Reason: err,
|
|
|
|
})
|
2017-04-15 15:07:23 -04:00
|
|
|
}
|
|
|
|
payload.Release()
|
|
|
|
continue
|
2017-02-09 16:49:38 -05:00
|
|
|
}
|
2016-01-27 16:11:31 -05:00
|
|
|
|
2017-09-19 17:27:49 -04:00
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) && s.account.OneTimeAuth == Account_Disabled {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("client payload enables OTA but server doesn't allow it").WriteToLog()
|
2017-04-15 15:07:23 -04:00
|
|
|
payload.Release()
|
|
|
|
continue
|
|
|
|
}
|
2016-01-28 15:30:05 -05:00
|
|
|
|
2017-09-19 17:27:49 -04:00
|
|
|
if !request.Option.Has(RequestOptionOneTimeAuth) && s.account.OneTimeAuth == Account_Enabled {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("client payload disables OTA but server forces it").WriteToLog()
|
2017-04-15 15:07:23 -04:00
|
|
|
payload.Release()
|
|
|
|
continue
|
|
|
|
}
|
2016-11-02 11:17:57 -04:00
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
dest := request.Destination()
|
|
|
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
2017-12-19 15:28:12 -05:00
|
|
|
log.Record(&log.AccessMessage{
|
|
|
|
From: source,
|
|
|
|
To: dest,
|
|
|
|
Status: log.AccessAccepted,
|
|
|
|
Reason: "",
|
|
|
|
})
|
2017-04-15 15:07:23 -04:00
|
|
|
}
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("tunnelling request to ", dest).WriteToLog()
|
2016-11-02 11:17:57 -04:00
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
|
|
|
udpServer.Dispatch(ctx, dest, data, func(payload *buf.Buffer) {
|
|
|
|
defer payload.Release()
|
2016-01-30 16:57:20 -05:00
|
|
|
|
2017-04-21 08:51:09 -04:00
|
|
|
data, err := EncodeUDPPacket(request, payload.Bytes())
|
2017-04-15 15:07:23 -04:00
|
|
|
if err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
newError("failed to encode UDP packet").Base(err).AtWarning().WriteToLog()
|
2017-04-15 15:07:23 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer data.Release()
|
2016-01-28 15:30:05 -05:00
|
|
|
|
2017-04-15 15:07:23 -04:00
|
|
|
conn.Write(data.Bytes())
|
|
|
|
})
|
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
2016-01-28 15:30:05 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
return nil
|
2016-01-28 15:30:05 -05:00
|
|
|
}
|
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, dispatcher core.Dispatcher) error {
|
|
|
|
sessionPolicy := s.v.PolicyManager().ForLevel(s.user.Level)
|
|
|
|
conn.SetReadDeadline(time.Now().Add(sessionPolicy.Timeouts.Handshake))
|
2017-11-09 16:33:15 -05:00
|
|
|
bufferedReader := buf.NewBufferedReader(buf.NewReader(conn))
|
2017-01-26 14:46:44 -05:00
|
|
|
request, bodyReader, err := ReadTCPSession(s.user, bufferedReader)
|
2016-01-28 06:33:58 -05:00
|
|
|
if err != nil {
|
2017-12-19 15:28:12 -05:00
|
|
|
log.Record(&log.AccessMessage{
|
|
|
|
From: conn.RemoteAddr(),
|
|
|
|
To: "",
|
|
|
|
Status: log.AccessRejected,
|
|
|
|
Reason: err,
|
|
|
|
})
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to create request from: ", conn.RemoteAddr()).Base(err)
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
2017-01-31 10:49:59 -05:00
|
|
|
conn.SetReadDeadline(time.Time{})
|
2016-10-31 10:24:28 -04:00
|
|
|
|
2016-12-27 15:41:44 -05:00
|
|
|
bufferedReader.SetBuffered(false)
|
2016-01-28 06:33:58 -05:00
|
|
|
|
2016-10-31 10:24:28 -04:00
|
|
|
dest := request.Destination()
|
2017-12-19 15:28:12 -05:00
|
|
|
log.Record(&log.AccessMessage{
|
|
|
|
From: conn.RemoteAddr(),
|
|
|
|
To: dest,
|
|
|
|
Status: log.AccessAccepted,
|
|
|
|
Reason: "",
|
|
|
|
})
|
|
|
|
newError("tunnelling request to ", dest).WriteToLog()
|
2016-01-30 16:57:20 -05:00
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
2017-01-31 06:42:05 -05:00
|
|
|
|
2017-11-14 18:36:14 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2018-01-10 06:22:37 -05:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
|
2017-02-03 16:35:09 -05:00
|
|
|
ray, err := dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-28 06:33:58 -05:00
|
|
|
|
2017-04-07 16:29:31 -04:00
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-11-09 16:33:15 -05:00
|
|
|
bufferedWriter := buf.NewBufferedWriter(buf.NewWriter(conn))
|
2016-10-31 10:24:28 -04:00
|
|
|
responseWriter, err := WriteTCPResponse(request, bufferedWriter)
|
|
|
|
if err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to write response").Base(err)
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
2016-02-23 12:16:13 -05:00
|
|
|
|
2017-11-09 16:33:15 -05:00
|
|
|
payload, err := ray.InboundOutput().ReadMultiBuffer()
|
2016-12-29 18:32:20 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-09 16:33:15 -05:00
|
|
|
if err := responseWriter.WriteMultiBuffer(payload); err != nil {
|
2017-01-06 05:40:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
payload.Release()
|
|
|
|
|
|
|
|
if err := bufferedWriter.SetBuffered(false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-18 13:01:24 -04:00
|
|
|
|
2017-04-27 16:30:48 -04:00
|
|
|
if err := buf.Copy(ray.InboundOutput(), responseWriter, buf.UpdateActivity(timer)); err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to transport all TCP response").Base(err)
|
2016-01-28 07:40:00 -05:00
|
|
|
}
|
2016-01-28 06:33:58 -05:00
|
|
|
|
2018-01-10 06:22:37 -05:00
|
|
|
timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
|
2017-09-27 09:29:00 -04:00
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2017-04-07 16:29:31 -04:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2016-12-29 18:32:20 -05:00
|
|
|
defer ray.InboundInput().Close()
|
|
|
|
|
2017-04-27 16:30:48 -04:00
|
|
|
if err := buf.Copy(bodyReader, ray.InboundInput(), buf.UpdateActivity(timer)); err != nil {
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("failed to transport all TCP request").Base(err)
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
2018-01-10 06:22:37 -05:00
|
|
|
timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
|
2016-12-29 18:32:20 -05:00
|
|
|
return nil
|
|
|
|
})
|
2016-01-28 06:33:58 -05:00
|
|
|
|
2017-01-28 15:24:46 -05:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2017-01-10 08:22:42 -05:00
|
|
|
ray.InboundInput().CloseError()
|
|
|
|
ray.InboundOutput().CloseError()
|
2017-04-08 19:43:25 -04:00
|
|
|
return newError("connection ends").Base(err)
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
return nil
|
2016-01-28 06:33:58 -05:00
|
|
|
}
|
|
|
|
|
2017-01-12 18:56:21 -05: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 16:54:08 -04:00
|
|
|
}
|