github.com/laof/lite-speed-test@v0.0.0-20230930011949-1f39b7037845/proxy/shadowsocks/shadowsocks.go (about)

     1  package shadowsocks
     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  	shadowsocks *outbound.ShadowSocks
    16  }
    17  
    18  func (c Client) DialConn(addr *tunnel.Address, _ tunnel.Tunnel) (net.Conn, error) {
    19  	meta := &C.Metadata{
    20  		NetWork: 0,
    21  		Type:    0,
    22  		SrcPort: "",
    23  		DstPort: fmt.Sprintf("%d", addr.Port),
    24  	}
    25  	switch addr.AddressType {
    26  	case tunnel.IPv4:
    27  	case tunnel.IPv6:
    28  		meta.DstIP = addr.IP
    29  	case tunnel.DomainName:
    30  		meta.Host = addr.DomainName
    31  	}
    32  	return c.shadowsocks.DialContext(c.ctx, meta)
    33  }
    34  
    35  // for http transport
    36  func (c *Client) Dial(network, address string) (net.Conn, error) {
    37  	addr, err := tunnel.NewAddressFromAddr(network, address)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return c.DialConn(addr, nil)
    42  }
    43  
    44  func (c Client) Close() error {
    45  	return nil
    46  }
    47  
    48  func NewClient(ctx context.Context, shadowsocks *outbound.ShadowSocks) Client {
    49  	return Client{
    50  		ctx:         ctx,
    51  		shadowsocks: shadowsocks,
    52  	}
    53  }