2019-02-01 19:08:21 +00:00
|
|
|
// +build !confonly
|
|
|
|
|
2016-10-31 14:24:28 +00:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2017-01-12 23:56:21 +00:00
|
|
|
"context"
|
2017-01-31 11:42:05 +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-01-13 22:42:39 +00:00
|
|
|
"v2ray.com/core/common/net"
|
2016-10-31 14:24:28 +00:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/retry"
|
2018-10-21 08:27:13 +00:00
|
|
|
"v2ray.com/core/common/session"
|
2016-12-29 23:32:20 +00:00
|
|
|
"v2ray.com/core/common/signal"
|
2018-10-21 08:27:13 +00:00
|
|
|
"v2ray.com/core/common/task"
|
|
|
|
"v2ray.com/core/features/policy"
|
2018-11-03 11:36:29 +00:00
|
|
|
"v2ray.com/core/transport"
|
2016-10-31 14:24:28 +00:00
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
)
|
|
|
|
|
2016-12-21 22:07:35 +00:00
|
|
|
// Client is a inbound handler for Shadowsocks protocol
|
2016-10-31 14:24:28 +00:00
|
|
|
type Client struct {
|
2018-10-21 08:27:13 +00:00
|
|
|
serverPicker protocol.ServerPicker
|
|
|
|
policyManager policy.Manager
|
2016-10-31 14:24:28 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 22:07:35 +00:00
|
|
|
// NewClient create a new Shadowsocks client.
|
2017-01-12 23:56:21 +00:00
|
|
|
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
2016-11-02 15:33:04 +00:00
|
|
|
serverList := protocol.NewServerList()
|
|
|
|
for _, rec := range config.Server {
|
2018-08-26 22:11:32 +00:00
|
|
|
s, err := protocol.NewServerSpecFromPB(*rec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse server spec").Base(err)
|
|
|
|
}
|
|
|
|
serverList.AddServer(s)
|
2016-11-02 15:33:04 +00:00
|
|
|
}
|
2017-04-11 11:42:10 +00:00
|
|
|
if serverList.Size() == 0 {
|
|
|
|
return nil, newError("0 server")
|
|
|
|
}
|
2018-10-21 08:27:13 +00:00
|
|
|
|
|
|
|
v := core.MustFromContext(ctx)
|
2016-11-02 15:33:04 +00:00
|
|
|
client := &Client{
|
2018-10-21 08:27:13 +00:00
|
|
|
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
|
|
|
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
|
2016-11-02 15:33:04 +00:00
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +00:00
|
|
|
// Process implements OutboundHandler.Process().
|
2018-11-03 11:36:29 +00:00
|
|
|
func (c *Client) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
|
2018-09-18 21:09:54 +00:00
|
|
|
outbound := session.OutboundFromContext(ctx)
|
|
|
|
if outbound == nil || !outbound.Target.IsValid() {
|
2017-04-08 22:54:23 +00:00
|
|
|
return newError("target not specified")
|
2017-02-09 21:49:38 +00:00
|
|
|
}
|
2018-09-18 21:09:54 +00:00
|
|
|
destination := outbound.Target
|
2016-10-31 14:24:28 +00:00
|
|
|
network := destination.Network
|
|
|
|
|
|
|
|
var server *protocol.ServerSpec
|
|
|
|
var conn internet.Connection
|
|
|
|
|
2016-11-20 20:47:51 +00:00
|
|
|
err := retry.ExponentialBackoff(5, 100).On(func() error {
|
2018-04-01 07:58:45 +00:00
|
|
|
server = c.serverPicker.PickServer()
|
2016-10-31 14:24:28 +00:00
|
|
|
dest := server.Destination()
|
|
|
|
dest.Network = network
|
2017-01-26 19:46:44 +00:00
|
|
|
rawConn, err := dialer.Dial(ctx, dest)
|
2016-10-31 14:24:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = rawConn
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-04-08 22:54:23 +00:00
|
|
|
return newError("failed to find an available destination").AtWarning().Base(err)
|
2016-10-31 14:24:28 +00:00
|
|
|
}
|
2018-06-24 23:09:02 +00:00
|
|
|
newError("tunneling request to ", destination, " via ", server.Destination()).WriteToLog(session.ExportIDToError(ctx))
|
2016-10-31 14:24:28 +00:00
|
|
|
|
2017-01-28 20:24:46 +00:00
|
|
|
defer conn.Close()
|
2016-11-02 21:18:25 +00:00
|
|
|
|
2016-10-31 15:35:18 +00:00
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Address: destination.Address,
|
|
|
|
Port: destination.Port,
|
|
|
|
}
|
2017-01-13 22:42:39 +00:00
|
|
|
if destination.Network == net.Network_TCP {
|
2016-10-31 15:35:18 +00:00
|
|
|
request.Command = protocol.RequestCommandTCP
|
|
|
|
} else {
|
|
|
|
request.Command = protocol.RequestCommandUDP
|
|
|
|
}
|
|
|
|
|
|
|
|
user := server.PickUser()
|
2018-08-26 22:11:32 +00:00
|
|
|
account, ok := user.Account.(*MemoryAccount)
|
|
|
|
if !ok {
|
|
|
|
return newError("user account is not valid")
|
2016-10-31 15:35:18 +00:00
|
|
|
}
|
|
|
|
request.User = user
|
|
|
|
|
2016-11-02 15:17:57 +00:00
|
|
|
if account.OneTimeAuth == Account_Auto || account.OneTimeAuth == Account_Enabled {
|
2016-10-31 15:35:18 +00:00
|
|
|
request.Option |= RequestOptionOneTimeAuth
|
|
|
|
}
|
|
|
|
|
2018-10-21 08:27:13 +00:00
|
|
|
sessionPolicy := c.policyManager.ForLevel(user.Level)
|
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)
|
2017-01-31 11:42:05 +00:00
|
|
|
|
2016-10-31 15:35:18 +00:00
|
|
|
if request.Command == protocol.RequestCommandTCP {
|
2017-11-09 21:33:15 +00:00
|
|
|
bufferedWriter := buf.NewBufferedWriter(buf.NewWriter(conn))
|
2016-10-31 15:35:18 +00:00
|
|
|
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
|
|
|
|
if err != nil {
|
2017-04-08 22:54:23 +00:00
|
|
|
return newError("failed to write request").Base(err)
|
2016-10-31 15:35:18 +00:00
|
|
|
}
|
|
|
|
|
2017-03-27 09:26:44 +00:00
|
|
|
if err := bufferedWriter.SetBuffered(false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-12-29 23:32:20 +00:00
|
|
|
|
2018-04-11 14:45:09 +00:00
|
|
|
requestDone := func() error {
|
2018-01-10 11:22:37 +00:00
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
|
2018-04-16 22:31:10 +00:00
|
|
|
return buf.Copy(link.Reader, bodyWriter, buf.UpdateActivity(timer))
|
2018-04-11 14:45:09 +00:00
|
|
|
}
|
2016-10-31 15:35:18 +00:00
|
|
|
|
2018-04-11 14:45:09 +00:00
|
|
|
responseDone := func() error {
|
2018-01-10 11:22:37 +00:00
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
|
2016-10-31 15:35:18 +00:00
|
|
|
|
|
|
|
responseReader, err := ReadTCPResponse(user, conn)
|
|
|
|
if err != nil {
|
2016-12-29 23:32:20 +00:00
|
|
|
return err
|
2016-10-31 15:35:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 22:31:10 +00:00
|
|
|
return buf.Copy(responseReader, link.Writer, buf.UpdateActivity(timer))
|
2018-04-11 14:45:09 +00:00
|
|
|
}
|
2016-11-05 00:00:09 +00:00
|
|
|
|
2018-12-06 10:35:02 +00:00
|
|
|
var responseDoneAndCloseWriter = task.OnSuccess(responseDone, task.Close(link.Writer))
|
|
|
|
if err := task.Run(ctx, requestDone, responseDoneAndCloseWriter); err != nil {
|
2017-04-08 22:54:23 +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-10-31 15:35:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Command == protocol.RequestCommandUDP {
|
|
|
|
|
2018-07-31 11:43:27 +00:00
|
|
|
writer := &buf.SequentialWriter{Writer: &UDPWriter{
|
2016-11-05 00:00:09 +00:00
|
|
|
Writer: conn,
|
|
|
|
Request: request,
|
2018-07-31 11:43:27 +00:00
|
|
|
}}
|
2016-11-05 00:00:09 +00:00
|
|
|
|
2018-04-11 14:45:09 +00:00
|
|
|
requestDone := func() error {
|
2018-02-12 11:28:18 +00:00
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
|
|
|
|
|
2018-04-16 22:31:10 +00:00
|
|
|
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
|
2017-04-08 22:54:23 +00:00
|
|
|
return newError("failed to transport all UDP request").Base(err)
|
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
|
|
|
responseDone := func() error {
|
2018-02-12 11:28:18 +00:00
|
|
|
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
|
2016-12-29 23:32:20 +00:00
|
|
|
|
|
|
|
reader := &UDPReader{
|
2017-01-31 11:42:05 +00:00
|
|
|
Reader: conn,
|
2016-12-29 23:32:20 +00:00
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:31:10 +00:00
|
|
|
if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
|
2017-04-08 22:54:23 +00:00
|
|
|
return newError("failed to transport all UDP response").Base(err)
|
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-12-06 10:35:02 +00:00
|
|
|
var responseDoneAndCloseWriter = task.OnSuccess(responseDone, task.Close(link.Writer))
|
|
|
|
if err := task.Run(ctx, requestDone, responseDoneAndCloseWriter); err != nil {
|
2017-04-08 22:54:23 +00:00
|
|
|
return newError("connection ends").Base(err)
|
2017-01-10 13:22:42 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
|
|
|
|
return nil
|
2016-10-31 15:35:18 +00:00
|
|
|
}
|
2017-01-26 19:46:44 +00:00
|
|
|
|
|
|
|
return nil
|
2016-10-31 14:24:28 +00:00
|
|
|
}
|
2016-11-02 15:33:04 +00:00
|
|
|
|
2017-01-12 23:56:21 +00:00
|
|
|
func init() {
|
|
|
|
common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
|
|
|
|
return NewClient(ctx, config.(*ClientConfig))
|
|
|
|
}))
|
2016-11-02 15:33:04 +00:00
|
|
|
}
|