1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 18:06:15 -05:00
v2fly/proxy/shadowsocks/client.go

179 lines
4.6 KiB
Go
Raw Normal View History

2016-10-31 10:24:28 -04:00
package shadowsocks
import (
2016-10-31 11:35:18 -04:00
"sync"
2016-12-09 07:17:34 -05:00
2016-11-02 11:33:04 -04:00
"v2ray.com/core/app"
2016-12-09 05:35:27 -05:00
"v2ray.com/core/common/buf"
2016-12-09 07:17:34 -05:00
"v2ray.com/core/common/bufio"
2016-10-31 10:24:28 -04:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
)
type Client struct {
serverPicker protocol.ServerPicker
meta *proxy.OutboundHandlerMeta
}
2016-11-02 11:33:04 -04:00
func NewClient(config *ClientConfig, space app.Space, meta *proxy.OutboundHandlerMeta) (*Client, error) {
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-09 05:35:27 -05:00
func (v *Client) Dispatch(destination v2net.Destination, payload *buf.Buffer, ray ray.OutboundRay) {
2016-10-31 10:24:28 -04:00
defer payload.Release()
defer ray.OutboundInput().Release()
defer ray.OutboundOutput().Close()
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
2016-11-27 15:39:09 -05:00
rawConn, err := internet.Dial(v.meta.Address, dest, v.meta.GetDialerOptions())
2016-10-31 10:24:28 -04: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 10:24:28 -04:00
}
log.Info("Shadowsocks|Client: Tunneling request to ", destination, " via ", server.Destination())
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,
}
if destination.Network == v2net.Network_TCP {
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 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
}
if request.Command == protocol.RequestCommandTCP {
2016-12-09 07:17:34 -05:00
bufferedWriter := bufio.NewWriter(conn)
2016-10-31 11:35:18 -04:00
defer bufferedWriter.Release()
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
defer bodyWriter.Release()
if err != nil {
log.Info("Shadowsock|Client: Failed to write request: ", err)
return
2016-10-31 11:35:18 -04:00
}
if err := bodyWriter.Write(payload); err != nil {
log.Info("Shadowsocks|Client: Failed to write payload: ", err)
return
2016-10-31 11:35:18 -04:00
}
var responseMutex sync.Mutex
responseMutex.Lock()
go func() {
defer responseMutex.Unlock()
responseReader, err := ReadTCPResponse(user, conn)
if err != nil {
2016-12-04 03:10:47 -05:00
log.Warning("Shadowsocks|Client: Failed to read response: ", err)
2016-10-31 11:35:18 -04:00
return
}
2016-12-09 07:17:34 -05:00
if err := buf.PipeUntilEOF(responseReader, ray.OutboundOutput()); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Shadowsocks|Client: Failed to transport all TCP response: ", err)
2016-11-21 17:06:26 -05:00
}
2016-10-31 11:35:18 -04:00
}()
2016-11-04 20:00:09 -04:00
bufferedWriter.SetCached(false)
2016-12-09 07:17:34 -05:00
if err := buf.PipeUntilEOF(ray.OutboundInput(), bodyWriter); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Shadowsocks|Client: Failed to trasnport all TCP request: ", err)
2016-11-21 17:06:26 -05:00
}
2016-11-04 20:00:09 -04:00
2016-10-31 11:35:18 -04:00
responseMutex.Lock()
}
if request.Command == protocol.RequestCommandUDP {
timedReader := v2net.NewTimeOutReader(16, conn)
var responseMutex sync.Mutex
responseMutex.Lock()
go func() {
defer responseMutex.Unlock()
reader := &UDPReader{
Reader: timedReader,
User: user,
}
2016-12-09 07:17:34 -05:00
if err := buf.PipeUntilEOF(reader, ray.OutboundOutput()); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Shadowsocks|Client: Failed to transport all UDP response: ", err)
2016-11-21 17:06:26 -05:00
}
2016-10-31 11:35:18 -04:00
}()
2016-11-04 20:00:09 -04:00
writer := &UDPWriter{
Writer: conn,
Request: request,
}
2016-11-19 16:40:14 -05:00
if !payload.IsEmpty() {
if err := writer.Write(payload); err != nil {
log.Info("Shadowsocks|Client: Failed to write payload: ", err)
return
2016-11-19 16:40:14 -05:00
}
2016-11-04 20:00:09 -04:00
}
2016-12-09 07:17:34 -05:00
if err := buf.PipeUntilEOF(ray.OutboundInput(), writer); err != nil {
2016-11-21 18:17:49 -05:00
log.Info("Shadowsocks|Client: Failed to transport all UDP request: ", err)
2016-11-21 17:06:26 -05:00
}
2016-11-04 20:00:09 -04:00
2016-10-31 11:35:18 -04:00
responseMutex.Lock()
}
2016-10-31 10:24:28 -04:00
}
2016-11-02 11:33:04 -04:00
type ClientFactory struct{}
2016-11-27 15:39:09 -05:00
func (v *ClientFactory) StreamCapability() v2net.NetworkList {
2016-11-02 11:33:04 -04:00
return v2net.NetworkList{
Network: []v2net.Network{v2net.Network_TCP, v2net.Network_RawTCP},
}
}
2016-11-27 15:39:09 -05:00
func (v *ClientFactory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
2016-11-02 11:33:04 -04:00
return NewClient(rawConfig.(*ClientConfig), space, meta)
}