github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/proxy/vmess/client.go (about) 1 package vmess 2 3 import ( 4 "context" 5 "fmt" 6 "net" 7 8 C "github.com/laof/lite-speed-test/constant" 9 "github.com/laof/lite-speed-test/outbound" 10 "github.com/laof/lite-speed-test/tunnel" 11 ) 12 13 type Client struct { 14 ctx context.Context 15 vmess *outbound.Vmess 16 } 17 18 func (c Client) DialConn(addr *tunnel.Address, _ tunnel.Tunnel) (net.Conn, error) { 19 networkType := C.TCP 20 if addr.NetworkType == "udp" { 21 networkType = C.UDP 22 } 23 meta := &C.Metadata{ 24 NetWork: networkType, 25 Type: 0, 26 SrcPort: "", 27 DstPort: fmt.Sprintf("%d", addr.Port), 28 } 29 switch addr.AddressType { 30 case tunnel.IPv4: 31 case tunnel.IPv6: 32 meta.DstIP = addr.IP 33 case tunnel.DomainName: 34 meta.Host = addr.DomainName 35 } 36 return c.vmess.DialContext(c.ctx, meta) 37 } 38 39 func (c Client) Close() error { 40 return nil 41 } 42 43 // for http transport 44 func (c *Client) Dial(network, address string) (net.Conn, error) { 45 addr, err := tunnel.NewAddressFromAddr(network, address) 46 if err != nil { 47 return nil, err 48 } 49 return c.DialConn(addr, nil) 50 } 51 52 func NewClient(ctx context.Context, vmess *outbound.Vmess) Client { 53 return Client{ 54 ctx: ctx, 55 vmess: vmess, 56 } 57 }