From 398469ed4ffc77d8aee87086bd7388fddfa20cca Mon Sep 17 00:00:00 2001 From: dingjun Date: Wed, 1 Jul 2020 15:01:00 +0800 Subject: [PATCH] update test case --- conf_test.go | 3 +-- main_test.go | 20 ++++++++++---------- proto_test.go | 14 +++----------- server.crt | 22 ++++++++++++++++++++++ server.key | 27 +++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 server.crt create mode 100644 server.key diff --git a/conf_test.go b/conf_test.go index 7cf5547..53c1ffb 100644 --- a/conf_test.go +++ b/conf_test.go @@ -1,7 +1,6 @@ package main import ( - "fmt" "io/ioutil" "testing" @@ -17,7 +16,7 @@ func TestConf(t *testing.T) { if err := yaml.Unmarshal(data, &c); err != nil { t.Fatal(err) } - fmt.Printf("%+v\n", c) + // fmt.Printf("%+v\n", c) var testdata = map[string]string{ "www.example.com": "127.0.0.1:8443 proxy-v2", "b.example.com": "127.0.0.1:8542", diff --git a/main_test.go b/main_test.go index 925a498..c7fe872 100644 --- a/main_test.go +++ b/main_test.go @@ -2,7 +2,7 @@ package main import "testing" -func Test_getSNIServerName(t *testing.T) { +func TestGetSNIServerName(t *testing.T) { type args struct { buf []byte } @@ -12,19 +12,19 @@ func Test_getSNIServerName(t *testing.T) { want string }{ { - args: args{buf: []uint8{22, 3, 1, 1, 2, 1, 0, 0, 254, 3, 3, 247, 68, 239, 62, 76, 12, 172, 30, 61, 229, 84, 250, 148, 64, 71, 81, 45, 164, 200, 3, 109, 97, 229, 137, 70, 33, 240, 4, 89, 224, 176, 190, 0, 0, 110, 192, 47, 192, 43, 192, 48, 192, 44, 0, 158, 192, 39, 0, 103, 192, 40, 0, 107, 192, 36, 192, 20, 192, 10, 0, 165, 0, 163, 0, 161, 0, 159, 0, 106, 0, 105, 0, 104, 0, 57, 0, 56, 0, 55, 0, 54, 192, 50, 192, 46, 192, 42, 192, 38, 192, 15, 192, 5, 0, 157, 0, 61, 0, 53, 192, 35, 192, 19, 192, 9, 0, 164, 0, 162, 0, 160, 0, 64, 0, 63, 0, 62, 0, 51, 0, 50, 0, 49, 0, 48, 192, 49, 192, 45, 192, 41, 192, 37, 192, 14, 192, 4, 0, 156, 0, 60, 0, 47, 0, 255, 1, 0, 0, 103, 0, 0, 0, 15, 0, 13, 0, 0, 10, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 0, 11, 0, 4, 3, 0, 1, 2, 0, 10, 0, 28, 0, 26, 0, 23, 0, 25, 0, 28, 0, 27, 0, 24, 0, 26, 0, 22, 0, 14, 0, 13, 0, 11, 0, 12, 0, 9, 0, 10, 0, 35, 0, 0, 0, 13, 0, 32, 0, 30, 6, 1, 6, 2, 6, 3, 5, 1, 5, 2, 5, 3, 4, 1, 4, 2, 4, 3, 3, 1, 3, 2, 3, 3, 2, 1, 2, 2, 2, 3, 51, 116, 0, 0,}}, - name:"google", - want:"google.com", + args: args{buf: []uint8{22, 3, 1, 1, 2, 1, 0, 0, 254, 3, 3, 247, 68, 239, 62, 76, 12, 172, 30, 61, 229, 84, 250, 148, 64, 71, 81, 45, 164, 200, 3, 109, 97, 229, 137, 70, 33, 240, 4, 89, 224, 176, 190, 0, 0, 110, 192, 47, 192, 43, 192, 48, 192, 44, 0, 158, 192, 39, 0, 103, 192, 40, 0, 107, 192, 36, 192, 20, 192, 10, 0, 165, 0, 163, 0, 161, 0, 159, 0, 106, 0, 105, 0, 104, 0, 57, 0, 56, 0, 55, 0, 54, 192, 50, 192, 46, 192, 42, 192, 38, 192, 15, 192, 5, 0, 157, 0, 61, 0, 53, 192, 35, 192, 19, 192, 9, 0, 164, 0, 162, 0, 160, 0, 64, 0, 63, 0, 62, 0, 51, 0, 50, 0, 49, 0, 48, 192, 49, 192, 45, 192, 41, 192, 37, 192, 14, 192, 4, 0, 156, 0, 60, 0, 47, 0, 255, 1, 0, 0, 103, 0, 0, 0, 15, 0, 13, 0, 0, 10, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 0, 11, 0, 4, 3, 0, 1, 2, 0, 10, 0, 28, 0, 26, 0, 23, 0, 25, 0, 28, 0, 27, 0, 24, 0, 26, 0, 22, 0, 14, 0, 13, 0, 11, 0, 12, 0, 9, 0, 10, 0, 35, 0, 0, 0, 13, 0, 32, 0, 30, 6, 1, 6, 2, 6, 3, 5, 1, 5, 2, 5, 3, 4, 1, 4, 2, 4, 3, 3, 1, 3, 2, 3, 3, 2, 1, 2, 2, 2, 3, 51, 116, 0, 0}}, + name: "google", + want: "google.com", }, { - args: args{buf: []uint8{22,3,1,1,1,1,1,1,1,1,1,1}}, - name:"empty", - want:"", + args: args{buf: []uint8{22, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}}, + name: "empty", + want: "", }, { - args: args{buf: []uint8{22,3,1,1,1}}, - name:"no panic", - want:"", + args: args{buf: []uint8{22, 3, 1, 1, 1}}, + name: "no panic", + want: "", }, } for _, tt := range tests { diff --git a/proto_test.go b/proto_test.go index 78b7b26..2f97a47 100644 --- a/proto_test.go +++ b/proto_test.go @@ -23,7 +23,7 @@ func TestProxyProto(t *testing.T) { log.Fatal(err) } - listener, err := net.Listen("tcp", "0.0.0.0:0") + listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { t.Fatal(err) } @@ -34,7 +34,6 @@ func TestProxyProto(t *testing.T) { for { conn, err := listener.Accept() if err != nil { - log.Errorln(err) return } go serve(context.Background(), conn) @@ -44,7 +43,7 @@ func TestProxyProto(t *testing.T) { if err != nil { t.Fatal(err) } - listener2, err := net.Listen("tcp", "0.0.0.0:8443") + listener2, err := net.Listen("tcp", "127.0.0.1:8443") if err != nil { t.Fatal(err) } @@ -68,18 +67,11 @@ func TestProxyProto(t *testing.T) { log.Errorf("handshake error: %s", err) return } - log.Debugf("%+v", _conn.ConnectionState()) - log.Debugf("addr: %s", addr.String()) - //fmt.Fprintf(conn, "%s", addr.String()) - //conn.Close() conn.Write([]byte(addr.String())) - // time.Sleep(1 * time.Second) - log.Debugln("reply addr") - // conn.Close() }(conn) } }() - //time.Sleep(1 * time.Second) + conn, err := tls.Dial("tcp", listener.Addr().String(), &tls.Config{ ServerName: "www.example.com", InsecureSkipVerify: true, diff --git a/server.crt b/server.crt new file mode 100644 index 0000000..ec4aab7 --- /dev/null +++ b/server.crt @@ -0,0 +1,22 @@ +-----BEGIN CERTIFICATE----- +MIIDlDCCAnygAwIBAgIJAKqJbywk2bLPMA0GCSqGSIb3DQEBCwUAMF8xCzAJBgNV +BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX +aWRnaXRzIFB0eSBMdGQxGDAWBgNVBAMMD3d3dy5leGFtcGxlLmNvbTAeFw0xOTA0 +MTUwNjIxNTdaFw0xOTA1MTUwNjIxNTdaMF8xCzAJBgNVBAYTAkFVMRMwEQYDVQQI +DApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQx +GDAWBgNVBAMMD3d3dy5leGFtcGxlLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEP +ADCCAQoCggEBANQa4OK5aS7faja2yxwV2k7xIhHKFlz71p09brIO7ZY/TEtl7NOl +bvJ/MXSh6bHZDiYgtVlvkdFqM53aUKosSItp+hlWsp34FoIcvaVLWre3g6WPPi/O +vWNCnVMdXAyGzt/M0Kj6jpCNPxLedsRHVMokrqQLMfjPLDh8OiE9SuyEf5cnqF94 +8LL0CJ3R1XIWsHe1Ya3MRgF1xHK7VhV9B9YwVUrcYYmmFabHdqsvvCjwiP4uTsJl +97QgxLGy666AKJsHoy5IqCUfD63UEej5IRzGKG1IEjkIrm33uoBAG+sH4lZQqcEc +ExrkIM9xrGg4Ypt4oDSXICcIoVsngcCX6pMCAwEAAaNTMFEwHQYDVR0OBBYEFL7i +Pwgyo6hJtuwBw441/G586xJIMB8GA1UdIwQYMBaAFL7iPwgyo6hJtuwBw441/G58 +6xJIMA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAG707ZV5eVSE +feWUabdRU7cJbE/Nyn+E1Y5obrVGNF1YIORJn3/CqzfC0GrqmVn3sHJG8q0H+HE6 +7tWmRFq4vE7sON0fMa0RSLBqe0JTwCR/O/8qJA1HQAGdjeYwzT8nTNkAzxD2t4s8 +6miZSjGbBpEvhDYRUWAeCCLHUfSA/jPuCX1FiZxrcdeTiNJDNN3SGooB32zdlEn2 +jBbtNLtvPzXkNT6CjKX4LBux5trlIJYYiosxV6joUKWMCX52bpipy9NbF5kgH0Q1 +P68B9b8mKCDimKM4Xs64ywarnaIY+FbGi5jiUYNcJaX7yehhK6wYOBMRSjubUfbA +Do0Z7SwnnVY= +-----END CERTIFICATE----- diff --git a/server.key b/server.key new file mode 100644 index 0000000..9b6f436 --- /dev/null +++ b/server.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEA1Brg4rlpLt9qNrbLHBXaTvEiEcoWXPvWnT1usg7tlj9MS2Xs +06Vu8n8xdKHpsdkOJiC1WW+R0WozndpQqixIi2n6GVaynfgWghy9pUtat7eDpY8+ +L869Y0KdUx1cDIbO38zQqPqOkI0/Et52xEdUyiSupAsx+M8sOHw6IT1K7IR/lyeo +X3jwsvQIndHVchawd7VhrcxGAXXEcrtWFX0H1jBVStxhiaYVpsd2qy+8KPCI/i5O +wmX3tCDEsbLrroAomwejLkioJR8PrdQR6PkhHMYobUgSOQiubfe6gEAb6wfiVlCp +wRwTGuQgz3GsaDhim3igNJcgJwihWyeBwJfqkwIDAQABAoIBAAI9VmqOcqHgEgsJ +2xMs1+VFfoqZi6rnrdhzMZwHtFKF0JRrOxjxAj7lQOxuWT1bgV69sayEFRIVAP6F +s5xYY1u1fsKNu80XNYWiMA5ywT53XZh97rYqhK1roqWiBbrMIZsTWpCuNJsqIqRc +XPytlCOeR4nL5HcwGNNv3F1FfGkK6C3H5rr2oJnh5etVX+J/Y1ipG+VSYvChokcz +CbpCqAwfnm3LTk0qTRAOXmFiREgZM7mM/tv+e/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