2016-10-31 10:24:28 -04:00
|
|
|
package shadowsocks
|
|
|
|
|
|
|
|
import (
|
2017-01-12 18:56:21 -05:00
|
|
|
"context"
|
2017-01-31 06:42:05 -05:00
|
|
|
"runtime"
|
2017-02-09 16:49:38 -05:00
|
|
|
"time"
|
2017-01-31 06:42:05 -05:00
|
|
|
|
2017-02-03 16:35:09 -05:00
|
|
|
"v2ray.com/core/app/log"
|
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-02-10 05:41:50 -05:00
|
|
|
"v2ray.com/core/common/errors"
|
2017-01-13 17:42:39 -05:00
|
|
|
"v2ray.com/core/common/net"
|
2016-10-31 10:24:28 -04:00
|
|
|
"v2ray.com/core/common/protocol"
|
|
|
|
"v2ray.com/core/common/retry"
|
2016-12-29 18:32:20 -05:00
|
|
|
"v2ray.com/core/common/signal"
|
2016-10-31 10:24:28 -04:00
|
|
|
"v2ray.com/core/proxy"
|
|
|
|
"v2ray.com/core/transport/internet"
|
|
|
|
"v2ray.com/core/transport/ray"
|
|
|
|
)
|
|
|
|
|
2016-12-21 17:07:35 -05:00
|
|
|
// Client is a inbound handler for Shadowsocks protocol
|
2016-10-31 10:24:28 -04:00
|
|
|
type Client struct {
|
|
|
|
serverPicker protocol.ServerPicker
|
|
|
|
}
|
|
|
|
|
2016-12-21 17:07:35 -05:00
|
|
|
// NewClient create a new Shadowsocks client.
|
2017-01-12 18:56:21 -05:00
|
|
|
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
|
2016-11-02 11:33:04 -04:00
|
|
|
serverList := protocol.NewServerList()
|
|
|
|
for _, rec := range config.Server {
|
|
|
|
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
|
|
|
|
}
|
|
|
|
client := &Client{
|
|
|
|
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
|
|
|
}
|
|
|
|
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
// Process implements OutboundHandler.Process().
|
2017-02-03 16:35:09 -05:00
|
|
|
func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
|
2017-02-09 16:49:38 -05:00
|
|
|
destination, ok := proxy.TargetFromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("Shadowsocks|Client: Target not specified.")
|
|
|
|
}
|
2016-10-31 10:24:28 -04:00
|
|
|
network := destination.Network
|
|
|
|
|
|
|
|
var server *protocol.ServerSpec
|
|
|
|
var conn internet.Connection
|
|
|
|
|
2016-11-20 15:47:51 -05:00
|
|
|
err := retry.ExponentialBackoff(5, 100).On(func() error {
|
2016-11-27 15:39:09 -05:00
|
|
|
server = v.serverPicker.PickServer()
|
2016-10-31 10:24:28 -04:00
|
|
|
dest := server.Destination()
|
|
|
|
dest.Network = network
|
2017-01-26 14:46:44 -05:00
|
|
|
rawConn, err := dialer.Dial(ctx, dest)
|
2016-10-31 10:24:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = rawConn
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2017-02-21 17:14:07 -05:00
|
|
|
return errors.Base(err).RequireUserAction().Message("Shadowsocks|Client: Failed to find an available destination.")
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
|
|
|
log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
|
|
|
|
|
2017-01-28 15:24:46 -05:00
|
|
|
defer conn.Close()
|
2016-11-02 17:18:25 -04:00
|
|
|
conn.SetReusable(false)
|
|
|
|
|
2016-10-31 11:35:18 -04:00
|
|
|
request := &protocol.RequestHeader{
|
|
|
|
Version: Version,
|
|
|
|
Address: destination.Address,
|
|
|
|
Port: destination.Port,
|
|
|
|
}
|
2017-01-13 17:42:39 -05:00
|
|
|
if destination.Network == net.Network_TCP {
|
2016-10-31 11:35:18 -04:00
|
|
|
request.Command = protocol.RequestCommandTCP
|
|
|
|
} else {
|
|
|
|
request.Command = protocol.RequestCommandUDP
|
|
|
|
}
|
|
|
|
|
|
|
|
user := server.PickUser()
|
|
|
|
rawAccount, err := user.GetTypedAccount()
|
|
|
|
if err != nil {
|
2016-11-27 17:57:19 -05:00
|
|
|
log.Warning("Shadowsocks|Client: Failed to get a valid user account: ", err)
|
2017-01-26 14:46:44 -05:00
|
|
|
return err
|
2016-10-31 11:35:18 -04:00
|
|
|
}
|
|
|
|
account := rawAccount.(*ShadowsocksAccount)
|
|
|
|
request.User = user
|
|
|
|
|
2016-11-02 11:17:57 -04:00
|
|
|
if account.OneTimeAuth == Account_Auto || account.OneTimeAuth == Account_Enabled {
|
2016-10-31 11:35:18 -04:00
|
|
|
request.Option |= RequestOptionOneTimeAuth
|
|
|
|
}
|
|
|
|
|
2017-01-31 06:42:05 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
timer := signal.CancelAfterInactivity(ctx, cancel, time.Minute*2)
|
|
|
|
|
2016-10-31 11:35:18 -04:00
|
|
|
if request.Command == protocol.RequestCommandTCP {
|
2017-02-15 16:51:01 -05:00
|
|
|
bufferedWriter := buf.NewBufferedWriter(conn)
|
2016-10-31 11:35:18 -04:00
|
|
|
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
|
|
|
|
if err != nil {
|
2016-12-21 17:07:35 -05:00
|
|
|
log.Info("Shadowsocks|Client: Failed to write request: ", err)
|
2017-01-26 14:46:44 -05:00
|
|
|
return err
|
2016-10-31 11:35:18 -04:00
|
|
|
}
|
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
bufferedWriter.SetBuffered(false)
|
|
|
|
|
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-01-31 06:42:05 -05:00
|
|
|
if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), bodyWriter); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2016-10-31 11:35:18 -04:00
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-01-26 14:46:44 -05:00
|
|
|
defer outboundRay.OutboundOutput().Close()
|
2016-10-31 11:35:18 -04:00
|
|
|
|
|
|
|
responseReader, err := ReadTCPResponse(user, conn)
|
|
|
|
if err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
return err
|
2016-10-31 11:35:18 -04:00
|
|
|
}
|
|
|
|
|
2017-01-31 06:42:05 -05:00
|
|
|
if err := buf.PipeUntilEOF(timer, responseReader, outboundRay.OutboundOutput()); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
return err
|
2016-11-21 17:06:26 -05:00
|
|
|
}
|
2016-10-31 11:35:18 -04:00
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
return nil
|
|
|
|
})
|
2016-11-04 20:00:09 -04:00
|
|
|
|
2017-01-28 15:24:46 -05:00
|
|
|
if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
log.Info("Shadowsocks|Client: Connection ends with ", err)
|
2017-01-26 14:46:44 -05:00
|
|
|
return err
|
2016-12-29 18:32:20 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
return nil
|
2016-10-31 11:35:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if request.Command == protocol.RequestCommandUDP {
|
|
|
|
|
2016-11-04 20:00:09 -04:00
|
|
|
writer := &UDPWriter{
|
|
|
|
Writer: conn,
|
|
|
|
Request: request,
|
|
|
|
}
|
|
|
|
|
2016-12-29 18:32:20 -05:00
|
|
|
requestDone := signal.ExecuteAsync(func() error {
|
2017-01-31 06:42:05 -05:00
|
|
|
if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), writer); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
log.Info("Shadowsocks|Client: Failed to transport all UDP request: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
responseDone := signal.ExecuteAsync(func() error {
|
2017-01-26 14:46:44 -05:00
|
|
|
defer outboundRay.OutboundOutput().Close()
|
2016-12-29 18:32:20 -05:00
|
|
|
|
|
|
|
reader := &UDPReader{
|
2017-01-31 06:42:05 -05:00
|
|
|
Reader: conn,
|
2016-12-29 18:32:20 -05:00
|
|
|
User: user,
|
|
|
|
}
|
|
|
|
|
2017-01-31 06:42:05 -05:00
|
|
|
if err := buf.PipeUntilEOF(timer, reader, outboundRay.OutboundOutput()); err != nil {
|
2016-12-29 18:32:20 -05:00
|
|
|
log.Info("Shadowsocks|Client: Failed to transport all UDP response: ", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
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
|
|
|
log.Info("Shadowsocks|Client: Connection ends with ", err)
|
2017-01-26 14:46:44 -05:00
|
|
|
return err
|
2017-01-10 08:22:42 -05:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
|
|
|
return nil
|
2016-10-31 11:35:18 -04:00
|
|
|
}
|
2017-01-26 14:46:44 -05:00
|
|
|
|
2017-01-31 06:42:05 -05:00
|
|
|
runtime.KeepAlive(timer)
|
|
|
|
|
2017-01-26 14:46:44 -05:00
|
|
|
return nil
|
2016-10-31 10:24:28 -04:00
|
|
|
}
|
2016-11-02 11:33:04 -04:00
|
|
|
|
2017-01-12 18:56:21 -05: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 11:33:04 -04:00
|
|
|
}
|