You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/app/proxy/proxy.go

157 lines
3.5 KiB

8 years ago
package proxy
import (
"io"
"net"
"time"
8 years ago
"context"
8 years ago
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors"
8 years ago
"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
}
8 years ago
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.")
}
8 years ago
proxy := new(OutboundProxy)
space.OnInitialize(func() error {
proxy.outboundManager = proxyman.OutboundHandlerManagerFromSpace(space)
if proxy.outboundManager == nil {
return errors.New("Proxy: Outbound handler manager not found in space.")
8 years ago
}
return nil
})
8 years ago
return proxy, nil
}
func (OutboundProxy) Interface() interface{} {
return (*OutboundProxy)(nil)
8 years ago
}
8 years ago
func (v *OutboundProxy) RegisterDialer() {
internet.ProxyDialer = v.Dial
}
8 years ago
// Dial implements internet.Dialer.
8 years ago
func (v *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
handler := v.outboundManager.GetHandler(options.Proxy.Tag)
8 years ago
if handler == nil {
log.Warning("Proxy: Failed to get outbound handler with tag: ", options.Proxy.Tag)
8 years ago
return internet.Dial(src, dest, internet.DialerOptions{
Stream: options.Stream,
})
}
8 years ago
log.Info("Proxy: Dialing to ", dest)
8 years ago
stream := ray.NewRay()
go handler.Dispatch(dest, stream)
return NewConnection(src, dest, stream), nil
8 years ago
}
type Connection struct {
8 years ago
stream ray.Ray
closed bool
localAddr net.Addr
remoteAddr net.Addr
reader *buf.BufferToBytesReader
writer *buf.BytesToBufferWriter
8 years ago
}
func NewConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *Connection {
return &Connection{
8 years ago
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()),
8 years ago
}
}
8 years ago
// Read implements net.Conn.Read().
func (v *Connection) Read(b []byte) (int, error) {
8 years ago
if v.closed {
8 years ago
return 0, io.EOF
}
8 years ago
return v.reader.Read(b)
8 years ago
}
8 years ago
// Write implements net.Conn.Write().
func (v *Connection) Write(b []byte) (int, error) {
8 years ago
if v.closed {
8 years ago
return 0, io.ErrClosedPipe
8 years ago
}
8 years ago
return v.writer.Write(b)
8 years ago
}
8 years ago
// Close implements net.Conn.Close().
func (v *Connection) Close() error {
8 years ago
v.closed = true
v.stream.InboundInput().Close()
v.stream.InboundOutput().CloseError()
8 years ago
return nil
}
8 years ago
// LocalAddr implements net.Conn.LocalAddr().
func (v *Connection) LocalAddr() net.Addr {
8 years ago
return v.localAddr
8 years ago
}
8 years ago
// RemoteAddr implements net.Conn.RemoteAddr().
func (v *Connection) RemoteAddr() net.Addr {
8 years ago
return v.remoteAddr
8 years ago
}
func (v *Connection) SetDeadline(t time.Time) error {
8 years ago
return nil
}
func (v *Connection) SetReadDeadline(t time.Time) error {
8 years ago
return nil
}
func (v *Connection) SetWriteDeadline(t time.Time) error {
8 years ago
return nil
}
func (v *Connection) Reusable() bool {
8 years ago
return false
}
func (v *Connection) SetReusable(bool) {
8 years ago
}
func OutboundProxyFromSpace(space app.Space) *OutboundProxy {
8 years ago
app := space.GetApplication((*OutboundProxy)(nil))
if app == nil {
return nil
}
return app.(*OutboundProxy)
}
func init() {
8 years ago
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return NewOutboundProxy(ctx, config.(*Config))
}))
}