github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/proxy/shadowsocks/client_test.go (about)

     1  package shadowsocks
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"io"
     8  	"net"
     9  	"net/http"
    10  	"testing"
    11  
    12  	"github.com/Asutorufa/yuhaiin/pkg/net/dialer"
    13  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    14  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/simple"
    15  	"github.com/Asutorufa/yuhaiin/pkg/net/proxy/websocket"
    16  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/point"
    17  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/protocol"
    18  	"github.com/Asutorufa/yuhaiin/pkg/protos/statistic"
    19  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    20  	"github.com/Asutorufa/yuhaiin/pkg/utils/yerror"
    21  )
    22  
    23  func ExampleNew() {
    24  	simple := simple.NewClient(&protocol.Protocol_Simple{
    25  		Simple: &protocol.Simple{
    26  			Host: "127.0.0.1",
    27  			Port: 1080,
    28  		},
    29  	})
    30  	ws := websocket.NewClient(&protocol.Protocol_Websocket{
    31  		Websocket: &protocol.Websocket{
    32  			Host: "localhost",
    33  		},
    34  	})
    35  
    36  	ss := NewClient(&protocol.Protocol_Shadowsocks{
    37  		Shadowsocks: &protocol.Shadowsocks{
    38  			Method:   "aes-128-gcm",
    39  			Password: "test",
    40  		},
    41  	})
    42  
    43  	var err error
    44  	var conn netapi.Proxy
    45  	for _, wrap := range []point.WrapProxy{simple, ws, ss} {
    46  		conn, err = wrap(conn)
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  	}
    51  }
    52  
    53  func TestConn(t *testing.T) {
    54  	p := yerror.Must(simple.NewClient(
    55  		&protocol.Protocol_Simple{
    56  			Simple: &protocol.Simple{
    57  				Host: "127.0.0.1",
    58  				Port: 1080,
    59  			},
    60  		})(nil))
    61  	z, err := websocket.NewClient(&protocol.Protocol_Websocket{Websocket: &protocol.Websocket{Host: "localhost:1090"}})(p)
    62  	assert.NoError(t, err)
    63  	z, err = NewClient(
    64  		&protocol.Protocol_Shadowsocks{
    65  			Shadowsocks: &protocol.Shadowsocks{
    66  				Method:   "aes-128-gcm",
    67  				Password: "test",
    68  			},
    69  		},
    70  		// "v2ray",
    71  		// "tls;cert=/mnt/data/program/go-shadowsocks/ca.crt;host=localhost:1090",
    72  	)(z)
    73  	if err != nil {
    74  		t.Error(err)
    75  		return
    76  	}
    77  
    78  	cc := &http.Client{
    79  		Transport: &http.Transport{
    80  			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    81  				switch network {
    82  				default:
    83  					return dialer.DialContext(ctx, network, addr)
    84  				case "tcp":
    85  					ad, err := netapi.ParseAddress(netapi.PaseNetwork(network), addr)
    86  					if err != nil {
    87  						return nil, fmt.Errorf("parse address failed: %v", err)
    88  					}
    89  					return z.Conn(ctx, ad)
    90  				}
    91  			},
    92  		},
    93  	}
    94  
    95  	resp, err := cc.Get("http://ip.sb")
    96  	assert.NoError(t, err)
    97  
    98  	data, err := io.ReadAll(resp.Body)
    99  	assert.NoError(t, err)
   100  
   101  	t.Log(string(data))
   102  }
   103  
   104  func TestUDPConn(t *testing.T) {
   105  	p := yerror.Must(simple.NewClient(
   106  		&protocol.Protocol_Simple{
   107  			Simple: &protocol.Simple{
   108  				Host: "127.0.0.1",
   109  				Port: 1090,
   110  			},
   111  		})(nil))
   112  	s, err := NewClient(
   113  		&protocol.Protocol_Shadowsocks{
   114  			Shadowsocks: &protocol.Shadowsocks{
   115  				Method:   "aes-128-gcm",
   116  				Password: "test",
   117  			},
   118  		})(p)
   119  	assert.NoError(t, err)
   120  
   121  	ad, _ := netapi.ParseAddress(statistic.Type_udp, "223.5.5.5:53")
   122  	c, err := s.PacketConn(context.TODO(), ad)
   123  	assert.NoError(t, err)
   124  
   125  	req := "ev4BAAABAAAAAAAAA3d3dwZnb29nbGUDY29tAAABAAE="
   126  
   127  	data, err := base64.StdEncoding.DecodeString(req)
   128  	assert.NoError(t, err)
   129  	x, err := c.WriteTo([]byte(data), nil)
   130  	assert.NoError(t, err)
   131  
   132  	t.Log(x)
   133  
   134  	y := make([]byte, 32*1024)
   135  
   136  	x, addr, err := c.ReadFrom(y)
   137  	assert.NoError(t, err)
   138  	t.Log(addr, y[:x])
   139  
   140  }