| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- package proxy
- import (
- "io"
- "net"
- "time"
- "v2ray.com/core/app"
- "v2ray.com/core/app/proxyman"
- "v2ray.com/core/common/errors"
- v2io "v2ray.com/core/common/io"
- "v2ray.com/core/common/log"
- v2net "v2ray.com/core/common/net"
- "v2ray.com/core/transport/internet"
- "v2ray.com/core/transport/ray"
- )
- const (
- APP_ID = 7
- )
- type OutboundProxy struct {
- outboundManager proxyman.OutboundHandlerManager
- }
- func NewOutboundProxy(space app.Space) *OutboundProxy {
- proxy := new(OutboundProxy)
- space.InitializeApplication(func() error {
- if !space.HasApp(proxyman.APP_ID_OUTBOUND_MANAGER) {
- return errors.New("Proxy: Outbound handler manager not found.")
- }
- proxy.outboundManager = space.GetApp(proxyman.APP_ID_OUTBOUND_MANAGER).(proxyman.OutboundHandlerManager)
- return nil
- })
- return proxy
- }
- func (v *OutboundProxy) RegisterDialer() {
- internet.ProxyDialer = v.Dial
- }
- func (v *OutboundProxy) Dial(src v2net.Address, dest v2net.Destination, options internet.DialerOptions) (internet.Connection, error) {
- handler := v.outboundManager.GetHandler(options.Proxy.Tag)
- if handler == nil {
- log.Warning("Proxy: Failed to get outbound handler with tag: ", options.Proxy.Tag)
- return internet.Dial(src, dest, internet.DialerOptions{
- Stream: options.Stream,
- })
- }
- log.Info("Proxy: Dialing to ", dest)
- stream := ray.NewRay()
- go handler.Dispatch(dest, nil, stream)
- return NewProxyConnection(src, dest, stream), nil
- }
- func (v *OutboundProxy) Release() {
- }
- type ProxyConnection struct {
- stream ray.Ray
- closed bool
- localAddr net.Addr
- remoteAddr net.Addr
- reader *v2io.ChanReader
- writer *v2io.ChainWriter
- }
- func NewProxyConnection(src v2net.Address, dest v2net.Destination, stream ray.Ray) *ProxyConnection {
- return &ProxyConnection{
- 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: v2io.NewChanReader(stream.InboundOutput()),
- writer: v2io.NewChainWriter(stream.InboundInput()),
- }
- }
- func (v *ProxyConnection) Read(b []byte) (int, error) {
- if v.closed {
- return 0, io.EOF
- }
- return v.reader.Read(b)
- }
- func (v *ProxyConnection) Write(b []byte) (int, error) {
- if v.closed {
- return 0, io.ErrClosedPipe
- }
- return v.writer.Write(b)
- }
- func (v *ProxyConnection) Close() error {
- v.closed = true
- v.stream.InboundInput().Close()
- v.stream.InboundOutput().Release()
- v.reader.Release()
- v.writer.Release()
- return nil
- }
- func (v *ProxyConnection) LocalAddr() net.Addr {
- return v.localAddr
- }
- func (v *ProxyConnection) RemoteAddr() net.Addr {
- return v.remoteAddr
- }
- func (v *ProxyConnection) SetDeadline(t time.Time) error {
- return nil
- }
- func (v *ProxyConnection) SetReadDeadline(t time.Time) error {
- return nil
- }
- func (v *ProxyConnection) SetWriteDeadline(t time.Time) error {
- return nil
- }
- func (v *ProxyConnection) Reusable() bool {
- return false
- }
- func (v *ProxyConnection) SetReusable(bool) {
- }
|