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