1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-02-20 23:47:21 -05:00

157 lines
3.5 KiB
Go
Raw Normal View History

2016-11-09 00:17:09 +01:00
package proxy
import (
"io"
"net"
"time"
2017-01-13 13:41:40 +01:00
"context"
2016-11-09 00:17:09 +01:00
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
2017-01-06 15:32:36 +01:00
"v2ray.com/core/common"
2016-12-09 13:17:34 +01:00
"v2ray.com/core/common/buf"
2016-12-04 09:10:47 +01:00
"v2ray.com/core/common/errors"
2016-11-09 00:17:09 +01:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
)
type OutboundProxy struct {
outboundManager proxyman.OutboundHandlerManager
}
2017-01-13 13:41:40 +01:00
func NewOutboundProxy(ctx context.Context, config *Config) (*OutboundProxy, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("OutboundProxy: No space in context.")
}
2016-11-09 00:17:09 +01:00
proxy := new(OutboundProxy)
2017-01-06 15:32:36 +01:00
space.OnInitialize(func() error {
proxy.outboundManager = proxyman.OutboundHandlerManagerFromSpace(space)
if proxy.outboundManager == nil {
return errors.New("Proxy: Outbound handler manager not found in space.")
2016-11-09 00:17:09 +01:00
}
return nil
})
2017-01-13 13:41:40 +01:00
return proxy, nil
}
func (OutboundProxy) Interface() interface{} {
return (*OutboundProxy)(nil)
2016-11-09 00:17:09 +01:00
}
2016-11-27 21:39:09 +01:00
func (v *OutboundProxy) RegisterDialer() {
internet.ProxyDialer = v.Dial
2016-11-10 23:41:28 +01:00
}
2016-12-29 01:05:16 +01:00
// Dial implements internet.Dialer.
2016-11-27 21:39:09 +01:00
func (v *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
handler := v.outboundManager.GetHandler(options.Proxy.Tag)
2016-11-09 00:17:09 +01:00
if handler == nil {
2016-11-10 23:41:28 +01:00
log.Warning("Proxy: Failed to get outbound handler with tag: ", options.Proxy.Tag)
2016-11-09 00:17:09 +01:00
return internet.Dial(src, dest, internet.DialerOptions{
Stream: options.Stream,
})
}
2016-11-19 22:38:50 +01:00
log.Info("Proxy: Dialing to ", dest)
2016-11-09 00:17:09 +01:00
stream := ray.NewRay()
2017-01-04 12:34:01 +01:00
go handler.Dispatch(dest, stream)
2016-12-29 00:58:00 +01:00
return NewConnection(src, dest, stream), nil
2016-11-09 00:17:09 +01:00
}
2016-12-29 00:58:00 +01:00
type Connection struct {
2016-11-09 00:17:09 +01:00
stream ray.Ray
closed bool
localAddr net.Addr
remoteAddr net.Addr
2016-12-09 13:17:34 +01:00
reader *buf.BufferToBytesReader
writer *buf.BytesToBufferWriter
2016-11-09 00:17:09 +01:00
}
2016-12-29 00:58:00 +01:00
func NewConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *Connection {
return &Connection{
2016-11-09 00:17:09 +01:00
stream: stream,
localAddr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
},
remoteAddr: &net.TCPAddr{
IP: []byte{0, 0, 0, 0},
Port: 0,
},
2016-12-09 13:17:34 +01:00
reader: buf.NewBytesReader(stream.InboundOutput()),
writer: buf.NewBytesWriter(stream.InboundInput()),
2016-11-09 00:17:09 +01:00
}
}
2016-12-29 01:05:16 +01:00
// Read implements net.Conn.Read().
2016-12-29 00:58:00 +01:00
func (v *Connection) Read(b []byte) (int, error) {
2016-11-27 21:39:09 +01:00
if v.closed {
2016-11-09 00:17:09 +01:00
return 0, io.EOF
}
2016-11-27 21:39:09 +01:00
return v.reader.Read(b)
2016-11-09 00:17:09 +01:00
}
2016-12-29 01:05:16 +01:00
// Write implements net.Conn.Write().
2016-12-29 00:58:00 +01:00
func (v *Connection) Write(b []byte) (int, error) {
2016-11-27 21:39:09 +01:00
if v.closed {
2016-11-19 00:37:11 +01:00
return 0, io.ErrClosedPipe
2016-11-09 00:17:09 +01:00
}
2016-11-27 21:39:09 +01:00
return v.writer.Write(b)
2016-11-09 00:17:09 +01:00
}
2016-12-29 01:05:16 +01:00
// Close implements net.Conn.Close().
2016-12-29 00:58:00 +01:00
func (v *Connection) Close() error {
2016-11-27 21:39:09 +01:00
v.closed = true
v.stream.InboundInput().Close()
2017-01-10 14:22:42 +01:00
v.stream.InboundOutput().CloseError()
2016-11-09 00:17:09 +01:00
return nil
}
2016-12-29 01:05:16 +01:00
// LocalAddr implements net.Conn.LocalAddr().
2016-12-29 00:58:00 +01:00
func (v *Connection) LocalAddr() net.Addr {
2016-11-27 21:39:09 +01:00
return v.localAddr
2016-11-09 00:17:09 +01:00
}
2016-12-29 01:05:16 +01:00
// RemoteAddr implements net.Conn.RemoteAddr().
2016-12-29 00:58:00 +01:00
func (v *Connection) RemoteAddr() net.Addr {
2016-11-27 21:39:09 +01:00
return v.remoteAddr
2016-11-09 00:17:09 +01:00
}
2016-12-29 00:58:00 +01:00
func (v *Connection) SetDeadline(t time.Time) error {
2016-11-09 00:17:09 +01:00
return nil
}
2016-12-29 00:58:00 +01:00
func (v *Connection) SetReadDeadline(t time.Time) error {
2016-11-09 00:17:09 +01:00
return nil
}
2016-12-29 00:58:00 +01:00
func (v *Connection) SetWriteDeadline(t time.Time) error {
2016-11-09 00:17:09 +01:00
return nil
}
2016-12-29 00:58:00 +01:00
func (v *Connection) Reusable() bool {
2016-11-09 00:17:09 +01:00
return false
}
2016-12-29 00:58:00 +01:00
func (v *Connection) SetReusable(bool) {
2016-11-09 00:17:09 +01:00
}
2017-01-06 15:32:36 +01:00
func OutboundProxyFromSpace(space app.Space) *OutboundProxy {
2017-01-13 13:41:40 +01:00
app := space.GetApplication((*OutboundProxy)(nil))
2017-01-06 15:32:36 +01:00
if app == nil {
return nil
}
return app.(*OutboundProxy)
}
func init() {
2017-01-13 13:41:40 +01:00
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewOutboundProxy(ctx, config.(*Config))
}))
2017-01-06 15:32:36 +01:00
}