v2ray-core/app/proxy/proxy.go

152 lines
3.4 KiB
Go
Raw Normal View History

2016-11-08 23:17:09 +00:00
package proxy
import (
"io"
"net"
"time"
"v2ray.com/core/app"
"v2ray.com/core/app/proxyman"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common"
2016-12-09 12:17:34 +00:00
"v2ray.com/core/common/buf"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-11-08 23:17:09 +00:00
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2017-01-06 14:32:36 +00:00
"v2ray.com/core/common/serial"
2016-11-08 23:17:09 +00:00
"v2ray.com/core/transport/internet"
"v2ray.com/core/transport/ray"
)
type OutboundProxy struct {
outboundManager proxyman.OutboundHandlerManager
}
func NewOutboundProxy(space app.Space) *OutboundProxy {
proxy := new(OutboundProxy)
2017-01-06 14:32:36 +00: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-08 23:17:09 +00:00
}
return nil
})
return proxy
}
2016-11-27 20:39:09 +00:00
func (v *OutboundProxy) RegisterDialer() {
internet.ProxyDialer = v.Dial
2016-11-10 22:41:28 +00:00
}
2016-12-29 00:05:16 +00:00
// Dial implements internet.Dialer.
2016-11-27 20:39:09 +00: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-08 23:17:09 +00:00
if handler == nil {
2016-11-10 22:41:28 +00:00
log.Warning("Proxy: Failed to get outbound handler with tag: ", options.Proxy.Tag)
2016-11-08 23:17:09 +00:00
return internet.Dial(src, dest, internet.DialerOptions{
Stream: options.Stream,
})
}
2016-11-19 21:38:50 +00:00
log.Info("Proxy: Dialing to ", dest)
2016-11-08 23:17:09 +00:00
stream := ray.NewRay()
2017-01-04 11:34:01 +00:00
go handler.Dispatch(dest, stream)
2016-12-28 23:58:00 +00:00
return NewConnection(src, dest, stream), nil
2016-11-08 23:17:09 +00:00
}
2016-12-28 23:58:00 +00:00
type Connection struct {
2016-11-08 23:17:09 +00:00
stream ray.Ray
closed bool
localAddr net.Addr
remoteAddr net.Addr
2016-12-09 12:17:34 +00:00
reader *buf.BufferToBytesReader
writer *buf.BytesToBufferWriter
2016-11-08 23:17:09 +00:00
}
2016-12-28 23:58:00 +00:00
func NewConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *Connection {
return &Connection{
2016-11-08 23:17:09 +00: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 12:17:34 +00:00
reader: buf.NewBytesReader(stream.InboundOutput()),
writer: buf.NewBytesWriter(stream.InboundInput()),
2016-11-08 23:17:09 +00:00
}
}
2016-12-29 00:05:16 +00:00
// Read implements net.Conn.Read().
2016-12-28 23:58:00 +00:00
func (v *Connection) Read(b []byte) (int, error) {
2016-11-27 20:39:09 +00:00
if v.closed {
2016-11-08 23:17:09 +00:00
return 0, io.EOF
}
2016-11-27 20:39:09 +00:00
return v.reader.Read(b)
2016-11-08 23:17:09 +00:00
}
2016-12-29 00:05:16 +00:00
// Write implements net.Conn.Write().
2016-12-28 23:58:00 +00:00
func (v *Connection) Write(b []byte) (int, error) {
2016-11-27 20:39:09 +00:00
if v.closed {
2016-11-18 23:37:11 +00:00
return 0, io.ErrClosedPipe
2016-11-08 23:17:09 +00:00
}
2016-11-27 20:39:09 +00:00
return v.writer.Write(b)
2016-11-08 23:17:09 +00:00
}
2016-12-29 00:05:16 +00:00
// Close implements net.Conn.Close().
2016-12-28 23:58:00 +00:00
func (v *Connection) Close() error {
2016-11-27 20:39:09 +00:00
v.closed = true
v.stream.InboundInput().Close()
2017-01-04 11:52:24 +00:00
v.stream.InboundOutput().ForceClose()
2016-11-08 23:17:09 +00:00
return nil
}
2016-12-29 00:05:16 +00:00
// LocalAddr implements net.Conn.LocalAddr().
2016-12-28 23:58:00 +00:00
func (v *Connection) LocalAddr() net.Addr {
2016-11-27 20:39:09 +00:00
return v.localAddr
2016-11-08 23:17:09 +00:00
}
2016-12-29 00:05:16 +00:00
// RemoteAddr implements net.Conn.RemoteAddr().
2016-12-28 23:58:00 +00:00
func (v *Connection) RemoteAddr() net.Addr {
2016-11-27 20:39:09 +00:00
return v.remoteAddr
2016-11-08 23:17:09 +00:00
}
2016-12-28 23:58:00 +00:00
func (v *Connection) SetDeadline(t time.Time) error {
2016-11-08 23:17:09 +00:00
return nil
}
2016-12-28 23:58:00 +00:00
func (v *Connection) SetReadDeadline(t time.Time) error {
2016-11-08 23:17:09 +00:00
return nil
}
2016-12-28 23:58:00 +00:00
func (v *Connection) SetWriteDeadline(t time.Time) error {
2016-11-08 23:17:09 +00:00
return nil
}
2016-12-28 23:58:00 +00:00
func (v *Connection) Reusable() bool {
2016-11-08 23:17:09 +00:00
return false
}
2016-12-28 23:58:00 +00:00
func (v *Connection) SetReusable(bool) {
2016-11-08 23:17:09 +00:00
}
2017-01-06 14:32:36 +00:00
type OutboundProxyFactory struct{}
func (OutboundProxyFactory) Create(space app.Space, config interface{}) (app.Application, error) {
return NewOutboundProxy(space), nil
}
func OutboundProxyFromSpace(space app.Space) *OutboundProxy {
app := space.(app.AppGetter).GetApp(serial.GetMessageType((*Config)(nil)))
if app == nil {
return nil
}
return app.(*OutboundProxy)
}
func init() {
common.Must(app.RegisterApplicationFactory((*Config)(nil), OutboundProxyFactory{}))
}