1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-03 06:30:42 +00:00
v2fly/app/proxyman/outbound/handler.go

177 lines
4.0 KiB
Go
Raw Normal View History

2017-01-14 23:48:37 +00:00
package outbound
import (
"context"
"errors"
"io"
"net"
"time"
2017-01-14 23:48:37 +00:00
"v2ray.com/core/app"
2017-01-14 23:48:37 +00:00
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2017-01-14 23:48:37 +00:00
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
2017-01-14 23:48:37 +00:00
)
type Handler struct {
config *proxyman.OutboundHandlerConfig
senderSettings *proxyman.SenderConfig
2017-01-26 19:57:18 +00:00
proxy proxy.Outbound
outboundManager proxyman.OutboundHandlerManager
2017-01-14 23:48:37 +00:00
}
func NewHandler(ctx context.Context, config *proxyman.OutboundHandlerConfig) (*Handler, error) {
h := &Handler{
config: config,
}
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("Proxyman|OutboundHandler: No space in context.")
}
space.OnInitialize(func() error {
ohm := proxyman.OutboundHandlerManagerFromSpace(space)
if ohm == nil {
return errors.New("Proxyman|OutboundHandler: No OutboundManager in space.")
}
h.outboundManager = ohm
return nil
})
if config.SenderSettings != nil {
senderSettings, err := config.SenderSettings.GetInstance()
2017-01-14 23:48:37 +00:00
if err != nil {
return nil, err
}
switch s := senderSettings.(type) {
case *proxyman.SenderConfig:
h.senderSettings = s
2017-01-14 23:48:37 +00:00
default:
return nil, errors.New("Proxyman|DefaultOutboundHandler: settings is not SenderConfig.")
2017-01-14 23:48:37 +00:00
}
}
2017-01-26 21:47:31 +00:00
proxyHandler, err := config.GetProxyHandler(ctx)
2017-01-14 23:48:37 +00:00
if err != nil {
return nil, err
}
h.proxy = proxyHandler
return h, nil
}
func (h *Handler) Dispatch(ctx context.Context, outboundRay ray.OutboundRay) {
ctx = proxy.ContextWithDialer(ctx, h)
h.proxy.Process(ctx, outboundRay)
2017-01-14 23:48:37 +00:00
}
func (h *Handler) Dial(ctx context.Context, dest v2net.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-01-26 21:47:31 +00:00
log.Info("Proxyman|OutboundHandler: Proxying to ", tag)
ctx = proxy.ContextWithDestination(ctx, dest)
stream := ray.NewRay(ctx)
go handler.Dispatch(ctx, stream)
return NewConnection(stream), nil
}
log.Warning("Proxyman|OutboundHandler: Failed to get outbound handler with tag: ", tag)
}
if h.senderSettings.Via != nil {
ctx = internet.ContextWithDialerSource(ctx, h.senderSettings.Via.AsAddress())
}
if h.senderSettings != nil {
ctx = internet.ContextWithStreamSettings(ctx, h.senderSettings.StreamSettings)
}
2017-01-14 23:48:37 +00:00
}
return internet.Dial(ctx, dest)
}
type Connection struct {
stream ray.Ray
closed bool
localAddr net.Addr
remoteAddr net.Addr
reader *buf.BufferToBytesReader
writer *buf.BytesToBufferWriter
}
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,
},
reader: buf.NewBytesReader(stream.InboundOutput()),
writer: buf.NewBytesWriter(stream.InboundInput()),
2017-01-14 23:48:37 +00:00
}
}
// Read implements net.Conn.Read().
func (v *Connection) Read(b []byte) (int, error) {
if v.closed {
return 0, io.EOF
2017-01-14 23:48:37 +00:00
}
return v.reader.Read(b)
}
// Write implements net.Conn.Write().
func (v *Connection) Write(b []byte) (int, error) {
if v.closed {
return 0, io.ErrClosedPipe
2017-01-14 23:48:37 +00:00
}
return v.writer.Write(b)
}
// 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
}
func (v *Connection) SetDeadline(t time.Time) error {
return nil
}
func (v *Connection) SetReadDeadline(t time.Time) error {
return nil
}
func (v *Connection) SetWriteDeadline(t time.Time) error {
return nil
}
func (v *Connection) Reusable() bool {
return false
}
func (v *Connection) SetReusable(bool) {
2017-01-14 23:48:37 +00:00
}