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

     1  package wireguard
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"net/http"
     8  	"testing"
     9  
    10  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    11  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/protocol"
    12  	"github.com/Asutorufa/yuhaiin/pkg/protos/statistic"
    13  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    14  )
    15  
    16  func TestWireguard(t *testing.T) {
    17  	r, err := NewClient(&protocol.Protocol_Wireguard{
    18  		Wireguard: &protocol.Wireguard{
    19  			SecretKey: "OD0YfReLPYBSL/vV+1JSBPpeBurGFLNA4wQCfD+yDFA=",
    20  			Endpoint: []string{
    21  				"10.0.0.2/32",
    22  			},
    23  			Mtu:      1500,
    24  			Reserved: []byte{0, 0, 0},
    25  			Peers: []*protocol.WireguardPeerConfig{
    26  				{
    27  					PublicKey: "2HWI3cW1HlAyQk1xiu+4QBL1KISMxSo4VQgCz+wCjmo=",
    28  					Endpoint:  "192.168.122.20:51820",
    29  					AllowedIps: []string{
    30  						"0.0.0.0/0",
    31  					},
    32  				},
    33  			},
    34  		},
    35  	})(nil)
    36  
    37  	assert.NoError(t, err)
    38  
    39  	hc := &http.Client{
    40  		Transport: &http.Transport{
    41  			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    42  				addrd, err := netapi.ParseAddress(statistic.Type_tcp, addr)
    43  				if err != nil {
    44  					return nil, err
    45  				}
    46  				return r.Conn(ctx, addrd)
    47  			},
    48  		},
    49  	}
    50  
    51  	t.Run("httpbin", func(t *testing.T) {
    52  		resp, err := hc.Get("https://httpbin.org/ip")
    53  		assert.NoError(t, err)
    54  		defer resp.Body.Close()
    55  
    56  		data, err := io.ReadAll(resp.Body)
    57  		assert.NoError(t, err)
    58  
    59  		t.Log(string(data))
    60  	})
    61  }