2015-12-04 06:07:32 -05:00
|
|
|
package outbound
|
2015-09-10 18:24:18 -04:00
|
|
|
|
|
|
|
import (
|
2016-06-01 20:20:53 -04:00
|
|
|
"io"
|
2015-09-23 08:14:53 -04:00
|
|
|
"sync"
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2015-12-06 05:00:10 -05:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2016-04-25 18:13:26 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2016-01-29 08:39:55 -05:00
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
2015-09-19 18:50:21 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 17:54:36 -04:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-05-07 14:26:29 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/protocol"
|
2016-06-05 18:10:51 -04:00
|
|
|
"github.com/v2ray/v2ray-core/common/retry"
|
2016-01-02 17:32:18 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-02 17:08:36 -05:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
2016-07-23 07:17:51 -04:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/vmess/encoding"
|
2016-02-01 06:22:29 -05:00
|
|
|
vmessio "github.com/v2ray/v2ray-core/proxy/vmess/io"
|
2016-06-14 16:54:08 -04:00
|
|
|
"github.com/v2ray/v2ray-core/transport/internet"
|
2015-10-14 08:51:19 -04:00
|
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
2015-09-10 18:24:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type VMessOutboundHandler struct {
|
2016-07-25 10:48:09 -04:00
|
|
|
serverList *protocol.ServerList
|
|
|
|
serverPicker protocol.ServerPicker
|
|
|
|
meta *proxy.OutboundHandlerMeta
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:13:26 -04:00
|
|
|
func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
2016-05-06 18:19:06 -04:00
|
|
|
defer ray.OutboundInput().Release()
|
|
|
|
defer ray.OutboundOutput().Close()
|
|
|
|
|
2016-07-25 10:48:09 -04:00
|
|
|
var rec *protocol.ServerSpec
|
2016-06-14 16:54:08 -04:00
|
|
|
var conn internet.Connection
|
2016-06-05 18:10:51 -04:00
|
|
|
|
|
|
|
err := retry.Timed(5, 100).On(func() error {
|
2016-07-25 10:48:09 -04:00
|
|
|
rec = this.serverPicker.PickServer()
|
|
|
|
rawConn, err := internet.Dial(this.meta.Address, rec.Destination(), this.meta.StreamSettings)
|
2016-06-05 18:10:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
conn = rawConn
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-07-11 18:12:35 -04:00
|
|
|
log.Error("VMess|Outbound: Failed to find an available destination:", err)
|
2016-06-05 18:10:51 -04:00
|
|
|
return err
|
|
|
|
}
|
2016-08-09 17:17:02 -04:00
|
|
|
log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination())
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2016-05-07 14:26:29 -04:00
|
|
|
command := protocol.RequestCommandTCP
|
2016-04-25 18:13:26 -04:00
|
|
|
if target.IsUDP() {
|
2016-05-07 14:26:29 -04:00
|
|
|
command = protocol.RequestCommandUDP
|
2015-09-20 12:22:29 -04:00
|
|
|
}
|
2016-05-07 14:26:29 -04:00
|
|
|
request := &protocol.RequestHeader{
|
2016-07-23 07:17:51 -04:00
|
|
|
Version: encoding.Version,
|
2016-06-05 18:10:51 -04:00
|
|
|
User: rec.PickUser(),
|
2015-09-20 12:22:29 -04:00
|
|
|
Command: command,
|
2016-04-25 18:13:26 -04:00
|
|
|
Address: target.Address(),
|
|
|
|
Port: target.Port(),
|
2016-05-07 14:26:29 -04:00
|
|
|
Option: protocol.RequestOptionChunkStream,
|
2016-02-01 06:22:29 -05:00
|
|
|
}
|
2015-10-07 08:50:17 -04:00
|
|
|
|
2015-09-10 18:24:18 -04:00
|
|
|
defer conn.Close()
|
2016-06-02 15:34:25 -04:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
conn.SetReusable(true)
|
|
|
|
if conn.Reusable() { // Conn reuse may be disabled on transportation layer
|
2016-06-02 15:34:25 -04:00
|
|
|
request.Option.Set(protocol.RequestOptionConnectionReuse)
|
2016-05-30 18:21:41 -04:00
|
|
|
}
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2015-09-22 08:45:03 -04:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
2016-01-05 06:08:16 -05:00
|
|
|
|
2015-09-23 08:14:53 -04:00
|
|
|
var requestFinish, responseFinish sync.Mutex
|
|
|
|
requestFinish.Lock()
|
|
|
|
responseFinish.Lock()
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2016-07-23 07:17:51 -04:00
|
|
|
session := encoding.NewClientSession(protocol.DefaultIDHash)
|
2016-02-27 10:41:21 -05:00
|
|
|
|
2016-04-25 18:13:26 -04:00
|
|
|
go this.handleRequest(session, conn, request, payload, input, &requestFinish)
|
2016-07-25 10:48:09 -04:00
|
|
|
go this.handleResponse(session, conn, request, rec.Destination(), output, &responseFinish)
|
2015-09-15 15:17:06 -04:00
|
|
|
|
2015-09-23 08:14:53 -04:00
|
|
|
requestFinish.Lock()
|
|
|
|
responseFinish.Lock()
|
2015-09-15 15:17:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-23 07:17:51 -04:00
|
|
|
func (this *VMessOutboundHandler) handleRequest(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) {
|
2015-09-23 08:14:53 -04:00
|
|
|
defer finish.Unlock()
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2016-02-27 10:41:21 -05:00
|
|
|
writer := v2io.NewBufferedWriter(conn)
|
2016-04-12 15:56:36 -04:00
|
|
|
defer writer.Release()
|
2016-02-27 10:41:21 -05:00
|
|
|
session.EncodeRequestHeader(request, writer)
|
2015-09-17 17:26:09 -04:00
|
|
|
|
2016-02-27 10:41:21 -05:00
|
|
|
bodyWriter := session.EncodeRequestBody(writer)
|
2016-04-25 18:13:26 -04:00
|
|
|
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
|
2016-06-02 15:34:25 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2016-04-25 18:13:26 -04:00
|
|
|
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
|
2015-09-15 15:17:06 -04:00
|
|
|
}
|
2016-06-05 18:10:51 -04:00
|
|
|
if err := streamWriter.Write(payload); err != nil {
|
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
2016-05-12 20:20:07 -04:00
|
|
|
writer.SetCached(false)
|
|
|
|
|
2016-06-01 16:09:34 -04:00
|
|
|
err := v2io.Pipe(input, streamWriter)
|
2016-06-01 20:20:53 -04:00
|
|
|
if err != io.EOF {
|
2016-06-01 16:09:34 -04:00
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
|
|
|
|
2016-06-02 15:34:25 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2016-07-29 17:18:58 -04:00
|
|
|
err := streamWriter.Write(alloc.NewLocalBuffer(32).Clear())
|
2016-06-02 15:34:25 -04:00
|
|
|
if err != nil {
|
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
2016-04-28 15:14:00 -04:00
|
|
|
}
|
2016-04-25 18:13:26 -04:00
|
|
|
streamWriter.Release()
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-15 15:17:06 -04:00
|
|
|
}
|
2015-09-10 18:24:18 -04:00
|
|
|
|
2016-07-23 07:17:51 -04:00
|
|
|
func (this *VMessOutboundHandler) handleResponse(session *encoding.ClientSession, conn internet.Connection, request *protocol.RequestHeader, dest v2net.Destination, output v2io.Writer, finish *sync.Mutex) {
|
2015-09-23 08:14:53 -04:00
|
|
|
defer finish.Unlock()
|
2015-10-06 03:35:02 -04:00
|
|
|
|
2016-02-27 10:41:21 -05:00
|
|
|
reader := v2io.NewBufferedReader(conn)
|
2016-04-12 15:56:36 -04:00
|
|
|
defer reader.Release()
|
2016-02-01 06:22:29 -05:00
|
|
|
|
2016-02-27 10:41:21 -05:00
|
|
|
header, err := session.DecodeResponseHeader(reader)
|
2015-09-10 18:24:18 -04:00
|
|
|
if err != nil {
|
2016-06-05 18:10:51 -04:00
|
|
|
conn.SetReusable(false)
|
2016-07-24 05:29:59 -04:00
|
|
|
log.Warning("VMess|Outbound: Failed to read response from ", request.Destination(), ": ", err)
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2016-02-27 10:41:21 -05:00
|
|
|
go this.handleCommand(dest, header.Command)
|
2015-09-15 18:06:22 -04:00
|
|
|
|
2016-06-02 15:34:25 -04:00
|
|
|
if !header.Option.Has(protocol.ResponseOptionConnectionReuse) {
|
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
|
|
|
|
2016-02-27 10:41:21 -05:00
|
|
|
reader.SetCached(false)
|
2016-05-12 20:20:07 -04:00
|
|
|
decryptReader := session.DecodeResponseBody(reader)
|
2015-12-04 06:42:56 -05:00
|
|
|
|
2016-02-27 10:41:21 -05:00
|
|
|
var bodyReader v2io.Reader
|
2016-06-02 15:34:25 -04:00
|
|
|
if request.Option.Has(protocol.RequestOptionChunkStream) {
|
2016-02-27 10:41:21 -05:00
|
|
|
bodyReader = vmessio.NewAuthChunkReader(decryptReader)
|
2016-02-01 06:22:29 -05:00
|
|
|
} else {
|
2016-02-27 10:41:21 -05:00
|
|
|
bodyReader = v2io.NewAdaptiveReader(decryptReader)
|
2015-10-03 05:34:01 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 16:09:34 -04:00
|
|
|
err = v2io.Pipe(bodyReader, output)
|
2016-06-01 20:20:53 -04:00
|
|
|
if err != io.EOF {
|
2016-06-01 16:09:34 -04:00
|
|
|
conn.SetReusable(false)
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:43:13 -04:00
|
|
|
bodyReader.Release()
|
2016-02-01 06:22:29 -05:00
|
|
|
|
2015-09-18 07:19:12 -04:00
|
|
|
return
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
|
|
|
|
type Factory struct{}
|
|
|
|
|
|
|
|
func (this *Factory) StreamCapability() internet.StreamConnectionType {
|
2016-08-13 09:33:34 -04:00
|
|
|
return internet.StreamConnectionTypeRawTCP | internet.StreamConnectionTypeTCP | internet.StreamConnectionTypeKCP | internet.StreamConnectionTypeWebSocket
|
2016-06-12 01:52:29 -04:00
|
|
|
}
|
2016-06-12 01:57:08 -04:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
func (this *Factory) Create(space app.Space, rawConfig interface{}, meta *proxy.OutboundHandlerMeta) (proxy.OutboundHandler, error) {
|
|
|
|
vOutConfig := rawConfig.(*Config)
|
2016-06-12 01:57:08 -04:00
|
|
|
|
2016-07-25 10:48:09 -04:00
|
|
|
serverList := protocol.NewServerList()
|
|
|
|
for _, rec := range vOutConfig.Receivers {
|
|
|
|
serverList.AddServer(rec)
|
|
|
|
}
|
2016-06-14 16:54:08 -04:00
|
|
|
handler := &VMessOutboundHandler{
|
2016-07-25 10:48:09 -04:00
|
|
|
serverList: serverList,
|
|
|
|
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
|
|
|
|
meta: meta,
|
2016-06-14 16:54:08 -04:00
|
|
|
}
|
2016-06-12 01:57:08 -04:00
|
|
|
|
2016-06-14 16:54:08 -04:00
|
|
|
return handler, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
internal.MustRegisterOutboundHandlerCreator("vmess", new(Factory))
|
2015-09-10 18:24:18 -04:00
|
|
|
}
|