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

     1  package quic
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"testing"
     7  
     8  	"github.com/Asutorufa/yuhaiin/pkg/net/netapi"
     9  	"github.com/Asutorufa/yuhaiin/pkg/protos/config/listener"
    10  	"github.com/Asutorufa/yuhaiin/pkg/protos/node/protocol"
    11  	"github.com/Asutorufa/yuhaiin/pkg/utils/assert"
    12  )
    13  
    14  var cert = []byte(`-----BEGIN CERTIFICATE-----
    15  MIIDJTCCAsqgAwIBAgIUUpPvsEwqcqRR08HfXOyGfAlWvKowCgYIKoZIzj0EAwIw
    16  gY8xCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1T
    17  YW4gRnJhbmNpc2NvMRkwFwYDVQQKExBDbG91ZEZsYXJlLCBJbmMuMTgwNgYDVQQL
    18  Ey9DbG91ZEZsYXJlIE9yaWdpbiBTU0wgRUNDIENlcnRpZmljYXRlIEF1dGhvcml0
    19  eTAeFw0yMzAyMDcxMjU5MDBaFw0zODAyMDMxMjU5MDBaMGIxGTAXBgNVBAoTEENs
    20  b3VkRmxhcmUsIEluYy4xHTAbBgNVBAsTFENsb3VkRmxhcmUgT3JpZ2luIENBMSYw
    21  JAYDVQQDEx1DbG91ZEZsYXJlIE9yaWdpbiBDZXJ0aWZpY2F0ZTBZMBMGByqGSM49
    22  AgEGCCqGSM49AwEHA0IABMDa0LxwazPtFxCxV297AGF1JAQTWXLbwxB8aQae+f9x
    23  mFRypG3yp9Ey3vrL0ASF/gqg5HLNDx4k5d4xwQes3DqjggEuMIIBKjAOBgNVHQ8B
    24  Af8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMAwGA1UdEwEB
    25  /wQCMAAwHQYDVR0OBBYEFG1FazlD7aG2z4tkOjF8gJ85e1L2MB8GA1UdIwQYMBaA
    26  FIUwXTsqcNTt1ZJnB/3rObQaDjinMEQGCCsGAQUFBwEBBDgwNjA0BggrBgEFBQcw
    27  AYYoaHR0cDovL29jc3AuY2xvdWRmbGFyZS5jb20vb3JpZ2luX2VjY19jYTAnBgNV
    28  HREEIDAegg4qLjEzNTQ3OTgyLnh5eoIMMTM1NDc5ODIueHl6MDwGA1UdHwQ1MDMw
    29  MaAvoC2GK2h0dHA6Ly9jcmwuY2xvdWRmbGFyZS5jb20vb3JpZ2luX2VjY19jYS5j
    30  cmwwCgYIKoZIzj0EAwIDSQAwRgIhAMDsQBnXncmISk0sqz7t+P2Qj/b1dbnTxdWH
    31  S99Gg9cvAiEAyJV2fBIr8w7LWkv7AIws2LebiNdhbQMdqmIlxWx1YI8=
    32  -----END CERTIFICATE-----
    33  `)
    34  
    35  var key = []byte(`-----BEGIN PRIVATE KEY-----
    36  MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgfFPJ3xA3HtR6OR11
    37  6b4x9zUqAB1JiCFWcnSm5SNEHuyhRANCAATA2tC8cGsz7RcQsVdvewBhdSQEE1ly
    38  28MQfGkGnvn/cZhUcqRt8qfRMt76y9AEhf4KoORyzQ8eJOXeMcEHrNw6
    39  -----END PRIVATE KEY-----
    40  `)
    41  
    42  func TestQuic(t *testing.T) {
    43  	s, err := NewServer(&listener.Inbound_Quic{
    44  		Quic: &listener.Quic2{
    45  			Host: "127.0.0.1:1090",
    46  			Tls: &listener.TlsConfig{
    47  				Certificates: []*listener.Certificate{
    48  					{
    49  						Cert: cert,
    50  						Key:  key,
    51  					},
    52  				},
    53  			},
    54  		},
    55  	})
    56  	assert.NoError(t, err)
    57  
    58  	defer s.Close()
    59  
    60  	ctx, cancel := context.WithCancel(context.Background())
    61  
    62  	go func() {
    63  		defer cancel()
    64  
    65  		spc, err := s.Packet(ctx)
    66  		assert.NoError(t, err)
    67  
    68  		buf := make([]byte, 65536)
    69  
    70  		n, addr, err := spc.ReadFrom(buf)
    71  		assert.NoError(t, err)
    72  
    73  		t.Log(string(buf[:n]), addr, bytes.Equal(buf[:n], append(cert, cert...)))
    74  	}()
    75  
    76  	qc, err := NewClient(&protocol.Protocol_Quic{
    77  		Quic: &protocol.Quic{
    78  			Host: "127.0.0.1:1090",
    79  			Tls: &protocol.TlsConfig{
    80  				Enable:             true,
    81  				InsecureSkipVerify: true,
    82  			},
    83  		},
    84  	})(nil)
    85  	assert.NoError(t, err)
    86  
    87  	pc, err := qc.PacketConn(context.TODO(), netapi.EmptyAddr)
    88  	assert.NoError(t, err)
    89  
    90  	_, err = pc.WriteTo(append(cert, cert...), nil)
    91  	assert.NoError(t, err)
    92  
    93  	<-ctx.Done()
    94  }