1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-06 10:20:44 -05:00
v2fly/app/proxyman/outbound/handler.go

217 lines
5.3 KiB
Go
Raw Normal View History

2017-01-14 18:48:37 -05:00
package outbound
import (
"context"
"io"
"time"
2017-01-14 18:48:37 -05:00
"v2ray.com/core/app"
"v2ray.com/core/app/log"
2017-01-14 18:48:37 -05:00
"v2ray.com/core/app/proxyman"
2017-04-02 15:30:21 -04:00
"v2ray.com/core/app/proxyman/mux"
"v2ray.com/core/common/buf"
2017-01-28 17:34:55 -05:00
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
2017-01-14 18:48:37 -05:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2017-01-14 18:48:37 -05:00
)
type Handler struct {
config *proxyman.OutboundHandlerConfig
senderSettings *proxyman.SenderConfig
2017-01-26 14:57:18 -05:00
proxy proxy.Outbound
outboundManager proxyman.OutboundHandlerManager
2017-04-02 15:30:21 -04:00
mux *mux.ClientManager
2017-01-14 18:48:37 -05:00
}
func NewHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) (*Handler, error) {
h := &Handler{
config: config,
}
space := app.SpaceFromContext(ctx)
if space == nil {
2017-04-08 19:43:25 -04:00
return nil, newError("no space in context")
}
space.OnInitialize(func() error {
ohm := proxyman.OutboundHandlerManagerFromSpace(space)
if ohm == nil {
2017-04-08 19:43:25 -04:00
return newError("no OutboundManager in space")
}
h.outboundManager = ohm
return nil
})
if config.SenderSettings != nil {
senderSettings, err := config.SenderSettings.GetInstance()
2017-01-14 18:48:37 -05:00
if err != nil {
return nil, err
}
switch s := senderSettings.(type) {
case *proxyman.SenderConfig:
h.senderSettings = s
2017-01-14 18:48:37 -05:00
default:
2017-04-08 19:43:25 -04:00
return nil, newError("settings is not SenderConfig")
2017-01-14 18:48:37 -05:00
}
}
2017-01-26 16:47:31 -05:00
proxyHandler, err := config.GetProxyHandler(ctx)
2017-01-14 18:48:37 -05:00
if err != nil {
return nil, err
}
2017-04-02 19:07:18 -04:00
if h.senderSettings != nil && h.senderSettings.MultiplexSettings != nil && h.senderSettings.MultiplexSettings.Enabled {
2017-04-12 04:40:21 -04:00
config := h.senderSettings.MultiplexSettings
if config.Concurrency < 1 || config.Concurrency > 1024 {
return nil, newError("invalid mux concurrency: ", config.Concurrency)
}
h.mux = mux.NewClientManager(proxyHandler, h, config)
2017-04-02 19:07:18 -04:00
}
2017-01-14 18:48:37 -05:00
h.proxy = proxyHandler
return h, nil
}
2017-04-12 04:40:21 -04:00
// Dispatch implements proxy.Outbound.Dispatch.
func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
2017-04-02 15:30:21 -04:00
if h.mux != nil {
err := h.mux.Dispatch(ctx, outboundRay)
if err != nil {
2017-04-08 19:43:25 -04:00
log.Trace(newError("failed to process outbound traffic").Base(err))
2017-10-24 11:33:28 -04:00
outboundRay.OutboundOutput().CloseError()
2017-02-21 17:14:07 -05:00
}
2017-02-10 06:01:52 -05:00
} else {
2017-04-02 15:30:21 -04:00
err := h.proxy.Process(ctx, outboundRay, h)
// Ensure outbound ray is properly closed.
if err != nil && errors.Cause(err) != io.EOF {
2017-04-08 19:43:25 -04:00
log.Trace(newError("failed to process outbound traffic").Base(err))
2017-04-02 15:30:21 -04:00
outboundRay.OutboundOutput().CloseError()
} else {
outboundRay.OutboundOutput().Close()
}
outboundRay.OutboundInput().CloseError()
}
2017-01-14 18:48:37 -05:00
}
2017-02-15 16:51:01 -05:00
// Dial implements proxy.Dialer.Dial().
func (h *Handler) Dial(ctx context.Context, dest net.Destination) (internet.Connection, error) {
if h.senderSettings != nil {
if h.senderSettings.ProxySettings.HasTag() {
tag := h.senderSettings.ProxySettings.Tag
handler := h.outboundManager.GetHandler(tag)
if handler != nil {
2017-04-08 19:43:25 -04:00
log.Trace(newError("proxying to ", tag).AtDebug())
2017-02-09 16:49:38 -05:00
ctx = proxy.ContextWithTarget(ctx, dest)
stream := ray.NewRay(ctx)
go handler.Dispatch(ctx, stream)
return NewConnection(stream), nil
}
2017-04-08 19:43:25 -04:00
log.Trace(newError("failed to get outbound handler with tag: ", tag).AtWarning())
}
if h.senderSettings.Via != nil {
ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
}
2017-02-03 18:27:16 -05:00
if h.senderSettings.StreamSettings != nil {
ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
}
2017-01-14 18:48:37 -05:00
}
return internet.Dial(ctx, dest)
}
2017-04-23 07:41:52 -04:00
var (
_ buf.MultiBufferReader = (*Connection)(nil)
_ buf.MultiBufferWriter = (*Connection)(nil)
)
type Connection struct {
stream ray.Ray
closed bool
localAddr net.Addr
remoteAddr net.Addr
2017-04-15 16:22:29 -04:00
bytesReader io.Reader
reader buf.Reader
writer buf.Writer
}
func NewConnection(stream ray.Ray) *Connection {
return &Connection{
stream: stream,
localAddr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
},
remoteAddr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
},
2017-04-15 16:22:29 -04:00
bytesReader: buf.ToBytesReader(stream.InboundOutput()),
reader: stream.InboundOutput(),
writer: stream.InboundInput(),
2017-01-14 18:48:37 -05:00
}
}
// Read implements net.Conn.Read().
func (v *Connection) Read(b []byte) (int, error) {
if v.closed {
return 0, io.EOF
2017-01-14 18:48:37 -05:00
}
2017-04-15 16:22:29 -04:00
return v.bytesReader.Read(b)
}
func (v *Connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
return v.reader.Read()
}
// Write implements net.Conn.Write().
func (v *Connection) Write(b []byte) (int, error) {
if v.closed {
return 0, io.ErrClosedPipe
2017-01-14 18:48:37 -05:00
}
2017-04-15 15:07:23 -04:00
return buf.ToBytesWriter(v.writer).Write(b)
}
2017-04-23 07:41:52 -04:00
func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
2017-04-15 15:07:23 -04:00
if v.closed {
2017-04-23 07:41:52 -04:00
return io.ErrClosedPipe
2017-04-15 15:07:23 -04:00
}
2017-04-23 07:41:52 -04:00
return v.writer.Write(mb)
}
// Close implements net.Conn.Close().
func (v *Connection) Close() error {
v.closed = true
v.stream.InboundInput().Close()
v.stream.InboundOutput().CloseError()
return nil
}
// LocalAddr implements net.Conn.LocalAddr().
func (v *Connection) LocalAddr() net.Addr {
return v.localAddr
}
// RemoteAddr implements net.Conn.RemoteAddr().
func (v *Connection) RemoteAddr() net.Addr {
return v.remoteAddr
}
2017-02-15 16:51:01 -05:00
// SetDeadline implements net.Conn.SetDeadline().
func (v *Connection) SetDeadline(t time.Time) error {
return nil
}
2017-02-15 16:51:01 -05:00
// SetReadDeadline implements net.Conn.SetReadDeadline().
func (v *Connection) SetReadDeadline(t time.Time) error {
return nil
}
2017-02-15 16:51:01 -05:00
// SetWriteDeadline implement net.Conn.SetWriteDeadline().
func (v *Connection) SetWriteDeadline(t time.Time) error {
return nil
}