1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00
v2fly/proxy/shadowsocks/client.go

179 lines
4.6 KiB
Go
Raw Normal View History

2016-10-31 14:24:28 +00:00
package shadowsocks
import (
"context"
"errors"
"v2ray.com/core/common"
2016-12-09 10:35:27 +00:00
"v2ray.com/core/common/buf"
2016-12-09 12:17:34 +00:00
"v2ray.com/core/common/bufio"
2016-10-31 14:24:28 +00:00
"v2ray.com/core/common/log"
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"
2016-12-29 23:32:20 +00:00
"v2ray.com/core/common/signal"
2016-10-31 14:24:28 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
)
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 {
serverPicker protocol.ServerPicker
meta *proxy.OutboundHandlerMeta
}
2016-12-21 22:07:35 +00:00
// NewClient create a new Shadowsocks client.
func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
meta := proxy.OutboundMetaFromContext(ctx)
if meta == nil {
return nil, errors.New("Shadowsocks|Client: No outbound meta in context.")
}
2016-11-02 15:33:04 +00:00
serverList := protocol.NewServerList()
for _, rec := range config.Server {
serverList.AddServer(protocol.NewServerSpecFromPB(*rec))
}
client := &Client{
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
meta: meta,
}
return client, nil
}
2016-12-21 22:07:35 +00:00
// Dispatch implements OutboundHandler.Dispatch().
2017-01-13 22:42:39 +00:00
func (v *Client) Dispatch(destination net.Destination, ray ray.OutboundRay) {
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 {
2016-11-27 20:39:09 +00:00
server = v.serverPicker.PickServer()
2016-10-31 14:24:28 +00:00
dest := server.Destination()
dest.Network = network
2016-11-27 20:39:09 +00:00
rawConn, err := internet.Dial(v.meta.Address, dest, v.meta.GetDialerOptions())
2016-10-31 14:24:28 +00:00
if err != nil {
return err
}
conn = rawConn
return nil
})
if err != nil {
log.Warning("Shadowsocks|Client: Failed to find an available destination:", err)
return
2016-10-31 14:24:28 +00:00
}
log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
2016-11-02 21:18:25 +00:00
conn.SetReusable(false)
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()
rawAccount, err := user.GetTypedAccount()
if err != nil {
log.Warning("Shadowsocks|Client: Failed to get a valid user account: ", err)
return
2016-10-31 15:35:18 +00:00
}
account := rawAccount.(*ShadowsocksAccount)
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
}
if request.Command == protocol.RequestCommandTCP {
2016-12-09 12:17:34 +00:00
bufferedWriter := bufio.NewWriter(conn)
2016-10-31 15:35:18 +00:00
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
if err != nil {
2016-12-21 22:07:35 +00:00
log.Info("Shadowsocks|Client: Failed to write request: ", err)
return
2016-10-31 15:35:18 +00:00
}
2016-12-29 23:32:20 +00:00
bufferedWriter.SetBuffered(false)
requestDone := signal.ExecuteAsync(func() error {
if err := buf.PipeUntilEOF(ray.OutboundInput(), bodyWriter); err != nil {
return err
}
return nil
})
2016-10-31 15:35:18 +00:00
2016-12-29 23:32:20 +00:00
responseDone := signal.ExecuteAsync(func() error {
defer ray.OutboundOutput().Close()
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
}
2016-12-09 12:17:34 +00:00
if err := buf.PipeUntilEOF(responseReader, ray.OutboundOutput()); err != nil {
2016-12-29 23:32:20 +00:00
return err
2016-11-21 22:06:26 +00:00
}
2016-10-31 15:35:18 +00:00
2016-12-29 23:32:20 +00:00
return nil
})
2016-11-05 00:00:09 +00:00
2016-12-29 23:32:20 +00:00
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
log.Info("Shadowsocks|Client: Connection ends with ", err)
2017-01-10 13:22:42 +00:00
ray.OutboundInput().CloseError()
ray.OutboundOutput().CloseError()
2016-12-29 23:32:20 +00:00
}
2016-10-31 15:35:18 +00:00
}
if request.Command == protocol.RequestCommandUDP {
2016-11-05 00:00:09 +00:00
writer := &UDPWriter{
Writer: conn,
Request: request,
}
2016-12-29 23:32:20 +00:00
requestDone := signal.ExecuteAsync(func() error {
if err := buf.PipeUntilEOF(ray.OutboundInput(), writer); err != nil {
log.Info("Shadowsocks|Client: Failed to transport all UDP request: ", err)
return err
}
return nil
})
2017-01-13 22:42:39 +00:00
timedReader := net.NewTimeOutReader(16, conn)
2016-12-29 23:32:20 +00:00
responseDone := signal.ExecuteAsync(func() error {
defer ray.OutboundOutput().Close()
reader := &UDPReader{
Reader: timedReader,
User: user,
}
if err := buf.PipeUntilEOF(reader, ray.OutboundOutput()); err != nil {
log.Info("Shadowsocks|Client: Failed to transport all UDP response: ", err)
return err
}
return nil
})
2017-01-10 13:22:42 +00:00
if err := signal.ErrorOrFinish2(requestDone, responseDone); err != nil {
log.Info("Shadowsocks|Client: Connection ends with ", err)
ray.OutboundInput().CloseError()
ray.OutboundOutput().CloseError()
}
2016-10-31 15:35:18 +00:00
}
2016-10-31 14:24:28 +00:00
}
2016-11-02 15:33:04 +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
}