2015-12-04 11:07:32 +00:00
|
|
|
package outbound
|
2015-09-11 00:24:18 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2015-09-23 14:14:53 +02:00
|
|
|
"sync"
|
2015-09-11 00:24:18 +02:00
|
|
|
|
2015-12-06 11:00:10 +01:00
|
|
|
"github.com/v2ray/v2ray-core/app"
|
2016-04-26 00:13:26 +02:00
|
|
|
"github.com/v2ray/v2ray-core/common/alloc"
|
2016-01-29 13:39:55 +00:00
|
|
|
v2io "github.com/v2ray/v2ray-core/common/io"
|
2015-09-20 00:50:21 +02:00
|
|
|
"github.com/v2ray/v2ray-core/common/log"
|
2015-09-19 23:54:36 +02:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-05-07 20:26:29 +02:00
|
|
|
"github.com/v2ray/v2ray-core/common/protocol"
|
|
|
|
"github.com/v2ray/v2ray-core/common/protocol/raw"
|
2016-01-02 23:32:18 +01:00
|
|
|
"github.com/v2ray/v2ray-core/proxy"
|
2016-01-02 23:08:36 +01:00
|
|
|
"github.com/v2ray/v2ray-core/proxy/internal"
|
2016-02-01 12:22:29 +01:00
|
|
|
vmessio "github.com/v2ray/v2ray-core/proxy/vmess/io"
|
2016-04-26 00:13:26 +02:00
|
|
|
"github.com/v2ray/v2ray-core/transport/dialer"
|
2015-10-14 14:51:19 +02:00
|
|
|
"github.com/v2ray/v2ray-core/transport/ray"
|
2015-09-11 00:24:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type VMessOutboundHandler struct {
|
2015-12-05 01:16:21 +01:00
|
|
|
receiverManager *ReceiverManager
|
2015-09-11 00:24:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-26 00:13:26 +02:00
|
|
|
func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error {
|
2016-05-07 00:19:06 +02:00
|
|
|
defer ray.OutboundInput().Release()
|
|
|
|
defer ray.OutboundOutput().Close()
|
|
|
|
|
2016-04-26 00:13:26 +02:00
|
|
|
destination, vNextUser := this.receiverManager.PickReceiver()
|
2015-09-11 00:24:18 +02:00
|
|
|
|
2016-05-07 20:26:29 +02:00
|
|
|
command := protocol.RequestCommandTCP
|
2016-04-26 00:13:26 +02:00
|
|
|
if target.IsUDP() {
|
2016-05-07 20:26:29 +02:00
|
|
|
command = protocol.RequestCommandUDP
|
2015-09-20 18:22:29 +02:00
|
|
|
}
|
2016-05-07 20:26:29 +02:00
|
|
|
request := &protocol.RequestHeader{
|
2016-02-27 22:37:22 +01:00
|
|
|
Version: raw.Version,
|
2015-10-31 14:08:13 +01:00
|
|
|
User: vNextUser,
|
2015-09-20 18:22:29 +02:00
|
|
|
Command: command,
|
2016-04-26 00:13:26 +02:00
|
|
|
Address: target.Address(),
|
|
|
|
Port: target.Port(),
|
2016-05-07 20:26:29 +02:00
|
|
|
Option: protocol.RequestOptionChunkStream,
|
2016-02-01 12:22:29 +01:00
|
|
|
}
|
2015-10-07 14:50:17 +02:00
|
|
|
|
2016-04-26 00:13:26 +02:00
|
|
|
conn, err := dialer.Dial(destination)
|
2015-09-11 00:24:18 +02:00
|
|
|
if err != nil {
|
2016-04-26 00:13:26 +02:00
|
|
|
log.Error("Failed to open ", destination, ": ", err)
|
2015-09-11 00:24:18 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-04-26 00:13:26 +02:00
|
|
|
log.Info("VMessOut: Tunneling request to ", request.Address, " via ", destination)
|
2015-09-17 23:00:17 +02:00
|
|
|
|
2015-09-11 00:24:18 +02:00
|
|
|
defer conn.Close()
|
2015-09-16 00:06:22 +02:00
|
|
|
|
2015-09-22 14:45:03 +02:00
|
|
|
input := ray.OutboundInput()
|
|
|
|
output := ray.OutboundOutput()
|
2016-01-05 12:08:16 +01:00
|
|
|
|
2015-09-23 14:14:53 +02:00
|
|
|
var requestFinish, responseFinish sync.Mutex
|
|
|
|
requestFinish.Lock()
|
|
|
|
responseFinish.Lock()
|
2015-09-16 00:06:22 +02:00
|
|
|
|
2016-05-07 20:26:29 +02:00
|
|
|
session := raw.NewClientSession(protocol.DefaultIDHash)
|
2016-02-27 16:41:21 +01:00
|
|
|
|
2016-04-26 00:13:26 +02:00
|
|
|
go this.handleRequest(session, conn, request, payload, input, &requestFinish)
|
|
|
|
go this.handleResponse(session, conn, request, destination, output, &responseFinish)
|
2015-09-15 21:17:06 +02:00
|
|
|
|
2015-09-23 14:14:53 +02:00
|
|
|
requestFinish.Lock()
|
|
|
|
responseFinish.Lock()
|
2015-09-15 21:17:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-07 20:26:29 +02:00
|
|
|
func (this *VMessOutboundHandler) handleRequest(session *raw.ClientSession, conn net.Conn, request *protocol.RequestHeader, payload *alloc.Buffer, input v2io.Reader, finish *sync.Mutex) {
|
2015-09-23 14:14:53 +02:00
|
|
|
defer finish.Unlock()
|
2016-04-26 00:13:26 +02:00
|
|
|
defer payload.Release()
|
2015-09-16 00:06:22 +02:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
writer := v2io.NewBufferedWriter(conn)
|
2016-04-12 21:56:36 +02:00
|
|
|
defer writer.Release()
|
2016-02-27 16:41:21 +01:00
|
|
|
session.EncodeRequestHeader(request, writer)
|
2015-09-17 23:26:09 +02:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
if request.Option.IsChunkStream() {
|
2016-04-26 00:13:26 +02:00
|
|
|
vmessio.Authenticate(payload)
|
2016-02-01 12:22:29 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
bodyWriter := session.EncodeRequestBody(writer)
|
2016-04-26 00:13:26 +02:00
|
|
|
bodyWriter.Write(payload.Value)
|
2016-01-05 12:08:16 +01:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
writer.SetCached(false)
|
2015-09-18 12:31:42 +02:00
|
|
|
|
2016-04-26 00:13:26 +02:00
|
|
|
var streamWriter v2io.Writer = v2io.NewAdaptiveWriter(bodyWriter)
|
|
|
|
if request.Option.IsChunkStream() {
|
|
|
|
streamWriter = vmessio.NewAuthChunkWriter(streamWriter)
|
2015-09-15 21:17:06 +02:00
|
|
|
}
|
2016-04-26 00:13:26 +02:00
|
|
|
v2io.Pipe(input, streamWriter)
|
2016-04-28 21:14:00 +02:00
|
|
|
if request.Option.IsChunkStream() {
|
|
|
|
streamWriter.Write(alloc.NewSmallBuffer().Clear())
|
|
|
|
}
|
2016-04-26 00:13:26 +02:00
|
|
|
streamWriter.Release()
|
2015-09-18 13:19:12 +02:00
|
|
|
return
|
2015-09-15 21:17:06 +02:00
|
|
|
}
|
2015-09-11 00:24:18 +02:00
|
|
|
|
2016-05-07 20:26:29 +02:00
|
|
|
func (this *VMessOutboundHandler) handleResponse(session *raw.ClientSession, conn net.Conn, request *protocol.RequestHeader, dest v2net.Destination, output v2io.Writer, finish *sync.Mutex) {
|
2015-09-23 14:14:53 +02:00
|
|
|
defer finish.Unlock()
|
2015-10-06 09:35:02 +02:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
reader := v2io.NewBufferedReader(conn)
|
2016-04-12 21:56:36 +02:00
|
|
|
defer reader.Release()
|
2016-02-01 12:22:29 +01:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
header, err := session.DecodeResponseHeader(reader)
|
2015-09-11 00:24:18 +02:00
|
|
|
if err != nil {
|
2016-02-27 16:41:21 +01:00
|
|
|
log.Warning("VMessOut: Failed to read response: ", err)
|
2015-09-18 13:19:12 +02:00
|
|
|
return
|
2015-09-11 00:24:18 +02:00
|
|
|
}
|
2016-02-27 16:41:21 +01:00
|
|
|
go this.handleCommand(dest, header.Command)
|
2015-09-16 00:06:22 +02:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
reader.SetCached(false)
|
|
|
|
decryptReader := session.DecodeResponseBody(conn)
|
2015-12-04 11:42:56 +00:00
|
|
|
|
2016-02-27 16:41:21 +01:00
|
|
|
var bodyReader v2io.Reader
|
|
|
|
if request.Option.IsChunkStream() {
|
|
|
|
bodyReader = vmessio.NewAuthChunkReader(decryptReader)
|
2016-02-01 12:22:29 +01:00
|
|
|
} else {
|
2016-02-27 16:41:21 +01:00
|
|
|
bodyReader = v2io.NewAdaptiveReader(decryptReader)
|
2015-10-03 11:34:01 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 18:44:10 +02:00
|
|
|
v2io.Pipe(bodyReader, output)
|
2016-04-12 21:43:13 +02:00
|
|
|
bodyReader.Release()
|
2016-02-01 12:22:29 +01:00
|
|
|
|
2015-09-18 13:19:12 +02:00
|
|
|
return
|
2015-09-11 00:24:18 +02:00
|
|
|
}
|
|
|
|
|
2015-09-12 20:36:21 +02:00
|
|
|
func init() {
|
2016-01-25 17:29:26 +01:00
|
|
|
internal.MustRegisterOutboundHandlerCreator("vmess",
|
2016-01-25 17:18:24 +01:00
|
|
|
func(space app.Space, rawConfig interface{}) (proxy.OutboundHandler, error) {
|
2016-01-15 12:43:06 +01:00
|
|
|
vOutConfig := rawConfig.(*Config)
|
2016-01-06 16:23:54 +01:00
|
|
|
return &VMessOutboundHandler{
|
2016-01-15 12:43:06 +01:00
|
|
|
receiverManager: NewReceiverManager(vOutConfig.Receivers),
|
2016-01-06 16:23:54 +01:00
|
|
|
}, nil
|
|
|
|
})
|
2015-09-11 00:24:18 +02:00
|
|
|
}
|