github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/node/parser/vmess_test.go (about)

     1  package parser
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"net"
     8  	"net/http"
     9  	"net/url"
    10  	"testing"
    11  
    12  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
    13  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/point"
    14  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/subscribe"
    15  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    16  	"google.golang.org/protobuf/encoding/protojson"
    17  )
    18  
    19  //{
    20  //"host":"",
    21  //"path":"",
    22  //"tls":"",
    23  //"verify_cert":true,
    24  //"add":"127.0.0.1",
    25  //"port":0,
    26  //"aid":2,
    27  //"net":"tcp",
    28  //"type":"none",
    29  //"v":"2",
    30  //"ps":"name",
    31  //"id":"cccc-cccc-dddd-aaa-46a1aaaaaa",
    32  //"class":1
    33  //}
    34  
    35  func TestGetVmess(t *testing.T) {
    36  	data := "vmess://eyJob3N0Ijoid3d3LmV4YW1wbGUuY29tIiwicGF0aCI6Ii90ZXN0IiwidGxzIjoiIiwidmVyaWZ5X2NlcnQiOnRydWUsImFkZCI6ImV4YW1wbGUuY29tIiwicG9ydCI6IjQ0MyIsImFpZCI6IjEiLCJuZXQiOiJ3cyIsInR5cGUiOiJub25lIiwidiI6IjIiLCJwcyI6ImV4YW1wbGUiLCJ1dWlkIjoiMmYzYjJiYjktYjJhZS0zOTE5LTk1ZDQtNzAyY2U3YzAyMjYyIiwiY2xhc3MiOjB9Cg=="
    37  	t.Log(Parse(subscribe.Type_vmess, []byte(data)))
    38  
    39  	data = "vmess://eyJob3N0Ijoid3d3LmV4YW1wbGUuY29tIiwicGF0aCI6Ii90ZXN0IiwidGxzIjoiIiwidmVyaWZ5X2NlcnQiOnRydWUsImFkZCI6ImV4YW1wbGUuY29tIiwicG9ydCI6NDQzLCJhaWQiOjEsIm5ldCI6IndzIiwidHlwZSI6Im5vbmUiLCJ2IjoiMiIsInBzIjoiZXhhbXBsZSIsInV1aWQiOiIyZjNiMmJiOS1iMmFlLTM5MTktOTVkNC03MDJjZTdjMDIyNjIiLCJjbGFzcyI6MH0K"
    40  	t.Log(Parse(subscribe.Type_vmess, []byte(data)))
    41  }
    42  
    43  func TestVmess(t *testing.T) {
    44  	z := &point.Point{}
    45  
    46  	err := protojson.Unmarshal([]byte(``), z)
    47  	assert.NoError(t, err)
    48  
    49  	x, err := point.Dialer(z)
    50  	assert.NoError(t, err)
    51  
    52  	tt := &http.Client{
    53  		Transport: &http.Transport{
    54  			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    55  				ad, err := netapi.ParseAddress(netapi.PaseNetwork(network), addr)
    56  				if err != nil {
    57  					return nil, fmt.Errorf("parse address failed: %w", err)
    58  				}
    59  				return x.Conn(ctx, ad)
    60  			},
    61  		},
    62  	}
    63  
    64  	req := http.Request{
    65  		Method: "GET",
    66  		URL: &url.URL{
    67  			Scheme: "http",
    68  			Host:   "ip.sb",
    69  		},
    70  		Header: make(http.Header),
    71  	}
    72  	req.Header.Set("User-Agent", "curl/v2.4.1")
    73  	resp, err := tt.Do(&req)
    74  	assert.NoError(t, err)
    75  	defer resp.Body.Close()
    76  	data, err := io.ReadAll(resp.Body)
    77  	assert.NoError(t, err)
    78  	t.Log(string(data))
    79  }