1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-21 09:36:34 -05:00
v2fly/proxy/shadowsocks/client.go

210 lines
6.2 KiB
Go
Raw Normal View History

2016-10-31 10:24:28 -04:00
package shadowsocks
import (
"context"
2017-01-31 06:42:05 -05:00
core "github.com/v2fly/v2ray-core/v5"
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/buf"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/common/net/packetaddr"
"github.com/v2fly/v2ray-core/v5/common/protocol"
"github.com/v2fly/v2ray-core/v5/common/retry"
"github.com/v2fly/v2ray-core/v5/common/session"
"github.com/v2fly/v2ray-core/v5/common/signal"
"github.com/v2fly/v2ray-core/v5/common/task"
"github.com/v2fly/v2ray-core/v5/features/policy"
"github.com/v2fly/v2ray-core/v5/proxy"
"github.com/v2fly/v2ray-core/v5/transport"
"github.com/v2fly/v2ray-core/v5/transport/internet"
"github.com/v2fly/v2ray-core/v5/transport/internet/udp"
2016-10-31 10:24:28 -04:00
)
// Client is a inbound handler for Shadowsocks protocol
2016-10-31 10:24:28 -04:00
type Client struct {
2018-10-21 04:27:13 -04:00
serverPicker protocol.ServerPicker
policyManager policy.Manager
2016-10-31 10:24:28 -04:00
}
2016-12-21 17:07:35 -05:00
// NewClient create a new Shadowsocks client.
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 {
2020-08-26 07:35:33 -04:00
s, err := protocol.NewServerSpecFromPB(rec)
2018-08-26 18:11:32 -04:00
if err != nil {
return nil, newError("failed to parse server spec").Base(err)
}
serverList.AddServer(s)
2016-11-02 11:33:04 -04:00
}
2017-04-11 07:42:10 -04:00
if serverList.Size() == 0 {
return nil, newError("0 server")
}
2018-10-21 04:27:13 -04:00
v := core.MustFromContext(ctx)
2016-11-02 11:33:04 -04:00
client := &Client{
2018-10-21 04:27:13 -04:00
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
2016-11-02 11:33:04 -04:00
}
return client, nil
}
// Process implements OutboundHandler.Process().
2018-11-03 07:36:29 -04:00
func (c *Client) Process(ctx context.Context, link *transport.Link, dialer internet.Dialer) error {
outbound := session.OutboundFromContext(ctx)
if outbound == nil || !outbound.Target.IsValid() {
2017-04-08 18:54:23 -04:00
return newError("target not specified")
2017-02-09 16:49:38 -05:00
}
destination := outbound.Target
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 {
2018-04-01 03:58:45 -04:00
server = c.serverPicker.PickServer()
2016-10-31 10:24:28 -04:00
dest := server.Destination()
dest.Network = network
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-04-08 18:54:23 -04:00
return newError("failed to find an available destination").AtWarning().Base(err)
2016-10-31 10:24:28 -04:00
}
newError("tunneling request to ", destination, " via ", network, ":", server.Destination().NetAddr()).WriteToLog(session.ExportIDToError(ctx))
2016-10-31 10:24:28 -04:00
defer conn.Close()
2016-11-02 17:18:25 -04:00
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()
_, ok := user.Account.(*MemoryAccount)
2018-08-26 18:11:32 -04:00
if !ok {
return newError("user account is not valid")
2016-10-31 11:35:18 -04:00
}
request.User = user
2018-10-21 04:27:13 -04:00
sessionPolicy := c.policyManager.ForLevel(user.Level)
2017-11-14 18:36:14 -05:00
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, sessionPolicy.Timeouts.ConnectionIdle)
2017-01-31 06:42:05 -05:00
if packetConn, err := packetaddr.ToPacketAddrConn(link, destination); err == nil {
requestDone := func() error {
protocolWriter := &UDPWriter{
Writer: conn,
Request: request,
}
return udp.CopyPacketConn(protocolWriter, packetConn, udp.UpdateActivity(timer))
}
responseDone := func() error {
protocolReader := &UDPReader{
Reader: conn,
User: user,
}
return udp.CopyPacketConn(packetConn, protocolReader, udp.UpdateActivity(timer))
}
responseDoneAndCloseWriter := task.OnSuccess(responseDone, task.Close(link.Writer))
if err := task.Run(ctx, requestDone, responseDoneAndCloseWriter); err != nil {
return newError("connection ends").Base(err)
}
return nil
}
2016-10-31 11:35:18 -04:00
if request.Command == protocol.RequestCommandTCP {
2018-04-11 10:45:09 -04:00
requestDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
bufferedWriter := buf.NewBufferedWriter(buf.NewWriter(conn))
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
if err != nil {
return newError("failed to write request").Base(err)
}
if err = buf.CopyOnceTimeout(link.Reader, bodyWriter, proxy.FirstPayloadTimeout); err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return newError("failed to write A request payload").Base(err).AtWarning()
}
if err := bufferedWriter.SetBuffered(false); err != nil {
return err
}
2018-04-16 18:31:10 -04:00
return buf.Copy(link.Reader, bodyWriter, buf.UpdateActivity(timer))
2018-04-11 10:45:09 -04:00
}
2016-10-31 11:35:18 -04:00
2018-04-11 10:45:09 -04:00
responseDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
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
}
2018-04-16 18:31:10 -04:00
return buf.Copy(responseReader, link.Writer, buf.UpdateActivity(timer))
2018-04-11 10:45:09 -04:00
}
2016-11-04 20:00:09 -04:00
2021-05-19 17:28:52 -04:00
responseDoneAndCloseWriter := task.OnSuccess(responseDone, task.Close(link.Writer))
2018-12-06 05:35:02 -05:00
if err := task.Run(ctx, requestDone, responseDoneAndCloseWriter); err != nil {
2017-04-08 18:54:23 -04:00
return newError("connection ends").Base(err)
2016-12-29 18:32:20 -05:00
}
return nil
2016-10-31 11:35:18 -04:00
}
if request.Command == protocol.RequestCommandUDP {
2018-07-31 07:43:27 -04:00
writer := &buf.SequentialWriter{Writer: &UDPWriter{
2016-11-04 20:00:09 -04:00
Writer: conn,
Request: request,
2018-07-31 07:43:27 -04:00
}}
2016-11-04 20:00:09 -04:00
2018-04-11 10:45:09 -04:00
requestDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.DownlinkOnly)
2018-04-16 18:31:10 -04:00
if err := buf.Copy(link.Reader, writer, buf.UpdateActivity(timer)); err != nil {
2017-04-08 18:54:23 -04:00
return newError("failed to transport all UDP request").Base(err)
2016-12-29 18:32:20 -05:00
}
return nil
2018-04-11 10:45:09 -04:00
}
2016-12-29 18:32:20 -05:00
2018-04-11 10:45:09 -04:00
responseDone := func() error {
defer timer.SetTimeout(sessionPolicy.Timeouts.UplinkOnly)
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,
}
2018-04-16 18:31:10 -04:00
if err := buf.Copy(reader, link.Writer, buf.UpdateActivity(timer)); err != nil {
2017-04-08 18:54:23 -04:00
return newError("failed to transport all UDP response").Base(err)
2016-12-29 18:32:20 -05:00
}
return nil
2018-04-11 10:45:09 -04:00
}
2016-12-29 18:32:20 -05:00
2021-05-19 17:28:52 -04:00
responseDoneAndCloseWriter := task.OnSuccess(responseDone, task.Close(link.Writer))
2018-12-06 05:35:02 -05:00
if err := task.Run(ctx, requestDone, responseDoneAndCloseWriter); err != nil {
2017-04-08 18:54:23 -04:00
return newError("connection ends").Base(err)
2017-01-10 08:22:42 -05:00
}
return nil
2016-10-31 11:35:18 -04:00
}
return nil
2016-10-31 10:24:28 -04:00
}
2016-11-02 11:33:04 -04: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
}