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

     1  package parser
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"net/http"
     8  	"net/url"
     9  	"testing"
    10  
    11  	"github.com/Asutorufa/yuhaiin/pkg/net/dns"
    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/protocol"
    15  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/subscribe"
    16  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    17  	"google.golang.org/protobuf/encoding/protojson"
    18  )
    19  
    20  func TestParseTrojan(t *testing.T) {
    21  	data := "trojan://cb60ba10-1178-3896-ba6e-69ffae322db5@1.1.1.1:443?sni=www.google.com&peer=www.google.com#zxdsdfsdf"
    22  	t.Log(Parse(subscribe.Type_trojan, []byte(data)))
    23  }
    24  
    25  func TestTrojan(t *testing.T) {
    26  	p := &point.Point{
    27  		Protocols: []*protocol.Protocol{},
    28  	}
    29  
    30  	err := protojson.Unmarshal([]byte(``), p)
    31  	assert.NoError(t, err)
    32  	z, err := point.Dialer(p)
    33  	assert.NoError(t, err)
    34  
    35  	dns, err := dns.NewDoU(dns.Config{Host: "1.1.1.1:53", Dialer: z})
    36  	assert.NoError(t, err)
    37  	t.Log(dns.LookupIP(context.TODO(), "www.google.com"))
    38  
    39  	tt := &http.Client{
    40  		Transport: &http.Transport{
    41  			DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
    42  				ad, err := netapi.ParseAddress(netapi.PaseNetwork(network), addr)
    43  				assert.NoError(t, err)
    44  				return z.Conn(ctx, ad)
    45  			},
    46  		},
    47  	}
    48  
    49  	req := http.Request{
    50  		Method: "GET",
    51  		URL: &url.URL{
    52  			Scheme: "http",
    53  			Host:   "ip.sb",
    54  		},
    55  		Header: make(http.Header),
    56  	}
    57  	req.Header.Set("User-Agent", "curl/v2.4.1")
    58  	resp, err := tt.Do(&req)
    59  	t.Error(err)
    60  	assert.NoError(t, err)
    61  	defer resp.Body.Close()
    62  	data, err := io.ReadAll(resp.Body)
    63  	assert.NoError(t, err)
    64  	t.Log(string(data))
    65  }