From cce6fcf6ef13c59e0ee97cecc89cbeb0adb3577a Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Thu, 28 Jun 2018 22:57:34 +0800 Subject: [PATCH] v2ray: move the stock v2ray-core proxy to v2ray package --- main.go | 5 +- proxy/http/http.go | 2 +- proxy/v2ray/v2ray.go | 185 +++++++++++++++++++++++++++++++++++++++++++ proxy/vmess/vmess.go | 111 ++------------------------ 4 files changed, 194 insertions(+), 109 deletions(-) create mode 100644 proxy/v2ray/v2ray.go diff --git a/main.go b/main.go index d3a5a0d..c9e6496 100644 --- a/main.go +++ b/main.go @@ -18,10 +18,11 @@ import ( _ "github.com/nadoo/glider/proxy/ss" _ "github.com/nadoo/glider/proxy/ssr" _ "github.com/nadoo/glider/proxy/tcptun" - _ "github.com/nadoo/glider/proxy/tls" + // _ "github.com/nadoo/glider/proxy/tls" _ "github.com/nadoo/glider/proxy/udptun" _ "github.com/nadoo/glider/proxy/uottun" - _ "github.com/nadoo/glider/proxy/vmess" + // _ "github.com/nadoo/glider/proxy/v2ray" + // _ "github.com/nadoo/glider/proxy/vmess" ) // VERSION . diff --git a/proxy/http/http.go b/proxy/http/http.go index a013518..edf5a5a 100644 --- a/proxy/http/http.go +++ b/proxy/http/http.go @@ -251,7 +251,7 @@ func (s *HTTP) Dial(network, addr string) (net.Conn, error) { log.F("proxy-http 'CONNECT' method not allowed by proxy %s", s.addr) } - return nil, errors.New("proxy-http cound not connect remote address: " + addr + ". error code: " + code) + return nil, errors.New("proxy-http can not connect remote address: " + addr + ". error code: " + code) } // DialUDP connects to the given address via the proxy. diff --git a/proxy/v2ray/v2ray.go b/proxy/v2ray/v2ray.go new file mode 100644 index 0000000..2b58146 --- /dev/null +++ b/proxy/v2ray/v2ray.go @@ -0,0 +1,185 @@ +package v2ray + +import ( + "context" + "errors" + "net" + "net/url" + "strconv" + "strings" + + "github.com/nadoo/glider/common/log" + "github.com/nadoo/glider/proxy" + + "v2ray.com/core" + "v2ray.com/core/app/dispatcher" + "v2ray.com/core/app/proxyman" + v2net "v2ray.com/core/common/net" + "v2ray.com/core/common/protocol" + "v2ray.com/core/common/serial" + "v2ray.com/core/proxy/vmess" + "v2ray.com/core/proxy/vmess/outbound" + "v2ray.com/core/transport/internet" + "v2ray.com/core/transport/internet/tls" + + // needed + _ "v2ray.com/core/app/proxyman/outbound" + _ "v2ray.com/core/transport/internet/tcp" +) + +// V2Ray . +type V2Ray struct { + dialer proxy.Dialer + addr string + + uuid string + alertID uint32 + + outboundSecurity string + streamProtocol string + streamSecurity string + + config *core.Config + instance *core.Instance +} + +func init() { + proxy.RegisterDialer("v2ray", NewV2RayDialer) +} + +// NewV2Ray returns a v2ray proxy. +func NewV2Ray(s string, dialer proxy.Dialer) (*V2Ray, error) { + u, err := url.Parse(s) + if err != nil { + log.F("parse url err: %s", err) + return nil, err + } + + addr := u.Host + host := u.Hostname() + port, err := strconv.ParseUint(u.Port(), 10, 32) + if err != nil { + log.F("parse port err: %s", err) + return nil, err + } + + var uuid, aid string + if u.User != nil { + uuid = u.User.Username() + aid, _ = u.User.Password() + } + + alertID, err := strconv.ParseUint(aid, 10, 32) + if err != nil { + log.F("parse alertID err: %s", err) + return nil, err + } + + config := &core.Config{ + App: []*serial.TypedMessage{ + serial.ToTypedMessage(&dispatcher.Config{}), + serial.ToTypedMessage(&proxyman.OutboundConfig{}), + }, + Outbound: []*core.OutboundHandlerConfig{{ + ProxySettings: serial.ToTypedMessage(&outbound.Config{ + Receiver: []*protocol.ServerEndpoint{ + { + Address: v2net.NewIPOrDomain(v2net.ParseAddress(host)), + Port: uint32(port), + User: []*protocol.User{ + { + Account: serial.ToTypedMessage(&vmess.Account{ + Id: uuid, + AlterId: uint32(alertID), + SecuritySettings: &protocol.SecurityConfig{ + Type: protocol.SecurityType_NONE, + }, + }), + }, + }, + }, + }, + }), + SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ + StreamSettings: &internet.StreamConfig{ + Protocol: internet.TransportProtocol_TCP, + SecurityType: serial.GetMessageType(&tls.Config{}), + SecuritySettings: []*serial.TypedMessage{ + serial.ToTypedMessage(&tls.Config{ + AllowInsecure: true, + }), + }, + }, + })}, + }, + } + + v, err := core.New(config) + if err != nil { + log.Fatal("Failed to create V: ", err.Error()) + } + + p := &V2Ray{ + dialer: dialer, + addr: addr, + + uuid: uuid, + alertID: uint32(alertID), + + outboundSecurity: "auto", + streamProtocol: "tcp", + streamSecurity: "tls", + + config: config, + instance: v, + } + + return p, nil +} + +// NewV2RayDialer returns a v2ray proxy dialer. +func NewV2RayDialer(s string, dialer proxy.Dialer) (proxy.Dialer, error) { + return NewV2Ray(s, dialer) +} + +// Addr returns forwarder's address +func (s *V2Ray) Addr() string { return s.addr } + +// NextDialer returns the next dialer +func (s *V2Ray) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDialer(dstAddr) } + +// Dial connects to the address addr on the network net via the proxy. +func (s *V2Ray) Dial(network, addr string) (net.Conn, error) { + + // c, err := s.dialer.Dial("tcp", s.addr) + + d := strings.Split(addr, ":") + host, portStr := d[0], d[1] + port, err := strconv.ParseUint(portStr, 10, 32) + if err != nil { + log.F("parse portStr err: %s", err) + return nil, err + } + + // TODO: does not support upstream dialer now + c, err := core.Dial(context.Background(), + s.instance, + v2net.TCPDestination(v2net.ParseAddress(host), v2net.Port(port))) + + if err != nil { + log.F("proxy-v2ray dial to %s error: %s", s.addr, err) + return nil, err + } + + if c, ok := c.(*net.TCPConn); ok { + c.SetKeepAlive(true) + } + + return c, err + +} + +// DialUDP connects to the given address via the proxy. +func (s *V2Ray) DialUDP(network, addr string) (net.PacketConn, net.Addr, error) { + return nil, nil, errors.New("v2ray client does not support udp now") +} diff --git a/proxy/vmess/vmess.go b/proxy/vmess/vmess.go index 524091f..aeea8d2 100644 --- a/proxy/vmess/vmess.go +++ b/proxy/vmess/vmess.go @@ -1,30 +1,13 @@ package vmess import ( - "context" "errors" "net" "net/url" "strconv" - "strings" "github.com/nadoo/glider/common/log" "github.com/nadoo/glider/proxy" - - "v2ray.com/core" - "v2ray.com/core/app/dispatcher" - "v2ray.com/core/app/proxyman" - v2net "v2ray.com/core/common/net" - "v2ray.com/core/common/protocol" - "v2ray.com/core/common/serial" - "v2ray.com/core/proxy/vmess" - "v2ray.com/core/proxy/vmess/outbound" - "v2ray.com/core/transport/internet" - "v2ray.com/core/transport/internet/tls" - - // needed - _ "v2ray.com/core/app/proxyman/outbound" - _ "v2ray.com/core/transport/internet/tcp" ) // VMess . @@ -38,9 +21,6 @@ type VMess struct { outboundSecurity string streamProtocol string streamSecurity string - - config *core.Config - instance *core.Instance } func init() { @@ -56,12 +36,6 @@ func NewVMess(s string, dialer proxy.Dialer) (*VMess, error) { } addr := u.Host - host := u.Hostname() - port, err := strconv.ParseUint(u.Port(), 10, 32) - if err != nil { - log.F("parse port err: %s", err) - return nil, err - } var uuid, aid string if u.User != nil { @@ -75,63 +49,14 @@ func NewVMess(s string, dialer proxy.Dialer) (*VMess, error) { return nil, err } - config := &core.Config{ - App: []*serial.TypedMessage{ - serial.ToTypedMessage(&dispatcher.Config{}), - serial.ToTypedMessage(&proxyman.OutboundConfig{}), - }, - Outbound: []*core.OutboundHandlerConfig{{ - ProxySettings: serial.ToTypedMessage(&outbound.Config{ - Receiver: []*protocol.ServerEndpoint{ - { - Address: v2net.NewIPOrDomain(v2net.ParseAddress(host)), - Port: uint32(port), - User: []*protocol.User{ - { - Account: serial.ToTypedMessage(&vmess.Account{ - Id: uuid, - AlterId: uint32(alertID), - SecuritySettings: &protocol.SecurityConfig{ - Type: protocol.SecurityType_NONE, - }, - }), - }, - }, - }, - }, - }), - SenderSettings: serial.ToTypedMessage(&proxyman.SenderConfig{ - StreamSettings: &internet.StreamConfig{ - Protocol: internet.TransportProtocol_TCP, - SecurityType: serial.GetMessageType(&tls.Config{}), - SecuritySettings: []*serial.TypedMessage{ - serial.ToTypedMessage(&tls.Config{ - AllowInsecure: true, - }), - }, - }, - })}, - }, - } - - v, err := core.New(config) - if err != nil { - log.Fatal("Failed to create V: ", err.Error()) - } - p := &VMess{ - dialer: dialer, - addr: addr, - - uuid: uuid, - alertID: uint32(alertID), - + dialer: dialer, + addr: addr, + uuid: uuid, + alertID: uint32(alertID), outboundSecurity: "auto", streamProtocol: "tcp", streamSecurity: "tls", - - config: config, - instance: v, } return p, nil @@ -150,33 +75,7 @@ func (s *VMess) NextDialer(dstAddr string) proxy.Dialer { return s.dialer.NextDi // Dial connects to the address addr on the network net via the proxy. func (s *VMess) Dial(network, addr string) (net.Conn, error) { - - // c, err := s.dialer.Dial("tcp", s.addr) - - d := strings.Split(addr, ":") - host, portStr := d[0], d[1] - port, err := strconv.ParseUint(portStr, 10, 32) - if err != nil { - log.F("parse portStr err: %s", err) - return nil, err - } - - // TODO: does not support upstream dialer now - c, err := core.Dial(context.Background(), - s.instance, - v2net.TCPDestination(v2net.ParseAddress(host), v2net.Port(port))) - - if err != nil { - log.F("proxy-vmess dial to %s error: %s", s.addr, err) - return nil, err - } - - if c, ok := c.(*net.TCPConn); ok { - c.SetKeepAlive(true) - } - - return c, err - + return nil, nil } // DialUDP connects to the given address via the proxy.