2016-01-27 11:46:40 +00:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2017-01-12 23:56:21 +00:00
|
|
|
"context"
|
2017-01-31 11:42:05 +00:00
|
|
|
"runtime"
|
2017-02-03 21:35:09 +00:00
|
|
|
"time"
|
2017-01-12 23:56:21 +00:00
|
|
|
|
2016-08-20 18:55:45 +00:00
|
|
|
"v2ray.com/core/app"
|
|
|
|
"v2ray.com/core/app/dispatcher"
|
2017-02-03 21:35:09 +00:00
|
|
|
"v2ray.com/core/app/log"
|
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-04 08:10:47 +00:00
|
|
|
"v2ray.com/core/common/errors"
|
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"
|
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 {
|
2017-02-03 21:35:09 +00:00
|
|
|
config *ServerConfig
|
|
|
|
user *protocol.User
|
|
|
|
account *ShadowsocksAccount
|
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.")
|
|
|
|
}
|
2016-09-17 22:41:21 +00:00
|
|
|
if config.GetUser() == nil {
|
2017-02-12 12:25:30 +00:00
|
|
|
return nil, errors.New("Shadowsocks|Server: User is not specified.")
|
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,
|
|
|
|
user: config.GetUser(),
|
|
|
|
account: account,
|
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{
|
|
|
|
Network: []net.Network{net.Network_TCP},
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2017-02-03 21:35:09 +00:00
|
|
|
func (s *Server) Process(ctx context.Context, network net.Network, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
2017-01-28 20:24:46 +00:00
|
|
|
conn.SetReusable(false)
|
|
|
|
|
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:
|
|
|
|
return errors.New("Shadowsocks|Server: Unknown network: ", network)
|
2016-01-29 15:43:45 +00:00
|
|
|
}
|
2016-01-27 21:11:31 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 21:35:09 +00:00
|
|
|
func (v *Server) handlerUDPPayload(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
|
|
|
udpServer := udp.NewDispatcher(dispatcher)
|
2016-01-28 20:30:05 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
reader := buf.NewReader(conn)
|
|
|
|
for {
|
|
|
|
payload, err := reader.Read()
|
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-01-26 19:46:44 +00:00
|
|
|
request, data, err := DecodeUDPPacket(v.user, payload)
|
|
|
|
if err != nil {
|
2017-02-09 21:49:38 +00:00
|
|
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
|
|
|
log.Info("Shadowsocks|Server: Skipping invalid UDP packet from: ", source, ": ", err)
|
|
|
|
log.Access(source, "", log.AccessRejected, err)
|
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
payload.Release()
|
|
|
|
continue
|
|
|
|
}
|
2016-01-27 21:11:31 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
if request.Option.Has(RequestOptionOneTimeAuth) && v.account.OneTimeAuth == Account_Disabled {
|
|
|
|
log.Info("Shadowsocks|Server: Client payload enables OTA but server doesn't allow it.")
|
|
|
|
payload.Release()
|
|
|
|
continue
|
|
|
|
}
|
2016-01-28 20:30:05 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
if !request.Option.Has(RequestOptionOneTimeAuth) && v.account.OneTimeAuth == Account_Enabled {
|
|
|
|
log.Info("Shadowsocks|Server: Client payload disables OTA but server forces it.")
|
|
|
|
payload.Release()
|
|
|
|
continue
|
|
|
|
}
|
2016-11-02 15:17:57 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
dest := request.Destination()
|
2017-02-09 21:49:38 +00:00
|
|
|
if source, ok := proxy.SourceFromContext(ctx); ok {
|
|
|
|
log.Access(source, dest, log.AccessAccepted, "")
|
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
log.Info("Shadowsocks|Server: Tunnelling request to ", dest)
|
2016-11-02 15:17:57 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
2017-02-03 21:35:09 +00:00
|
|
|
udpServer.Dispatch(ctx, dest, data, func(payload *buf.Buffer) {
|
2017-01-26 19:46:44 +00:00
|
|
|
defer payload.Release()
|
2016-01-30 21:57:20 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
data, err := EncodeUDPPacket(request, payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Warning("Shadowsocks|Server: Failed to encode UDP packet: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer data.Release()
|
2016-01-28 20:30:05 +00:00
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
conn.Write(data.Bytes())
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-02-03 21:35:09 +00:00
|
|
|
func (s *Server) handleConnection(ctx context.Context, conn internet.Connection, dispatcher dispatcher.Interface) error {
|
2017-01-31 15:49:59 +00:00
|
|
|
conn.SetReadDeadline(time.Now().Add(time.Second * 8))
|
2017-02-15 21:51:01 +00:00
|
|
|
bufferedReader := buf.NewBufferedReader(conn)
|
2017-01-26 19:46:44 +00:00
|
|
|
request, bodyReader, err := ReadTCPSession(s.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)
|
2017-01-26 19:46:44 +00:00
|
|
|
return 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
|
|
|
|
2016-12-27 20:41:44 +00:00
|
|
|
bufferedReader.SetBuffered(false)
|
2016-01-28 11:33:58 +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
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
ctx = protocol.ContextWithUser(ctx, request.User)
|
2017-01-31 11:42:05 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-01-31 15:49:59 +00:00
|
|
|
userSettings := s.user.GetSettings()
|
2017-01-31 16:46:39 +00:00
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, userSettings.PayloadTimeout)
|
2017-02-03 21:35:09 +00:00
|
|
|
ray, err := dispatcher.Dispatch(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-01-28 11:33:58 +00:00
|
|
|
|
2016-12-29 23:32:20 +00:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-02-15 21:51:01 +00:00
|
|
|
bufferedWriter := buf.NewBufferedWriter(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
|
|
|
|
2017-01-31 11:42:05 +00:00
|
|
|
if err := buf.PipeUntilEOF(timer, ray.InboundOutput(), responseWriter); err != nil {
|
2016-12-29 23:32:20 +00:00
|
|
|
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()
|
|
|
|
|
2017-01-31 11:42:05 +00:00
|
|
|
if err := buf.PipeUntilEOF(timer, bodyReader, ray.InboundInput()); err != nil {
|
2016-12-29 23:32:20 +00:00
|
|
|
log.Info("Shadowsocks|Server: Failed to transport all TCP request: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2016-01-28 11:33:58 +00:00
|
|
|
|
2017-01-28 20:24:46 +00:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2016-12-29 23:32:20 +00:00
|
|
|
log.Info("Shadowsocks|Server: Connection ends with ", err)
|
2017-03-31 19:10:33 +00:00
|
|
|
cancel()
|
2017-01-10 13:22:42 +00:00
|
|
|
ray.InboundInput().CloseError()
|
|
|
|
ray.InboundOutput().CloseError()
|
2017-01-26 19:46:44 +00:00
|
|
|
return err
|
2016-12-29 23:32:20 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
|
2017-01-31 11:42:05 +00:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
|
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
|
|
|
}
|