mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 17:35:40 +08:00
v2ray: move the stock v2ray-core proxy to v2ray package
This commit is contained in:
parent
20273b48be
commit
cce6fcf6ef
5
main.go
5
main.go
@ -18,10 +18,11 @@ import (
|
|||||||
_ "github.com/nadoo/glider/proxy/ss"
|
_ "github.com/nadoo/glider/proxy/ss"
|
||||||
_ "github.com/nadoo/glider/proxy/ssr"
|
_ "github.com/nadoo/glider/proxy/ssr"
|
||||||
_ "github.com/nadoo/glider/proxy/tcptun"
|
_ "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/udptun"
|
||||||
_ "github.com/nadoo/glider/proxy/uottun"
|
_ "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 .
|
// VERSION .
|
||||||
|
@ -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)
|
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.
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
185
proxy/v2ray/v2ray.go
Normal file
185
proxy/v2ray/v2ray.go
Normal file
@ -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")
|
||||||
|
}
|
@ -1,30 +1,13 @@
|
|||||||
package vmess
|
package vmess
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/nadoo/glider/common/log"
|
"github.com/nadoo/glider/common/log"
|
||||||
"github.com/nadoo/glider/proxy"
|
"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 .
|
// VMess .
|
||||||
@ -38,9 +21,6 @@ type VMess struct {
|
|||||||
outboundSecurity string
|
outboundSecurity string
|
||||||
streamProtocol string
|
streamProtocol string
|
||||||
streamSecurity string
|
streamSecurity string
|
||||||
|
|
||||||
config *core.Config
|
|
||||||
instance *core.Instance
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -56,12 +36,6 @@ func NewVMess(s string, dialer proxy.Dialer) (*VMess, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addr := u.Host
|
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
|
var uuid, aid string
|
||||||
if u.User != nil {
|
if u.User != nil {
|
||||||
@ -75,63 +49,14 @@ func NewVMess(s string, dialer proxy.Dialer) (*VMess, error) {
|
|||||||
return nil, 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 := &VMess{
|
p := &VMess{
|
||||||
dialer: dialer,
|
dialer: dialer,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
|
uuid: uuid,
|
||||||
uuid: uuid,
|
alertID: uint32(alertID),
|
||||||
alertID: uint32(alertID),
|
|
||||||
|
|
||||||
outboundSecurity: "auto",
|
outboundSecurity: "auto",
|
||||||
streamProtocol: "tcp",
|
streamProtocol: "tcp",
|
||||||
streamSecurity: "tls",
|
streamSecurity: "tls",
|
||||||
|
|
||||||
config: config,
|
|
||||||
instance: v,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return p, nil
|
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.
|
// Dial connects to the address addr on the network net via the proxy.
|
||||||
func (s *VMess) Dial(network, addr string) (net.Conn, error) {
|
func (s *VMess) Dial(network, addr string) (net.Conn, error) {
|
||||||
|
return nil, nil
|
||||||
// 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
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DialUDP connects to the given address via the proxy.
|
// DialUDP connects to the given address via the proxy.
|
||||||
|
Loading…
Reference in New Issue
Block a user