From be646215e326ce8c353df77aa43c2f5c66755fc9 Mon Sep 17 00:00:00 2001 From: wweir Date: Sat, 24 Nov 2018 23:59:20 +0800 Subject: [PATCH] fix mock tls config --- .gitignore | 4 ++++ proxy/client.go | 3 ++- proxy/server.go | 2 +- proxy/util.go | 27 +++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 9ae07b4..1572851 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,7 @@ .vscode .idea sower +cert.pem +**/cert.pem +privkey.pem +**/privkey.pem diff --git a/proxy/client.go b/proxy/client.go index 1c31631..b3caf44 100644 --- a/proxy/client.go +++ b/proxy/client.go @@ -1,6 +1,7 @@ package proxy import ( + "crypto/tls" "net" "github.com/golang/glog" @@ -9,7 +10,7 @@ import ( func StartClient(server string) { connCh := listenLocal([]string{":80", ":443"}) - sess, err := quic.DialAddr(server, nil, nil) + sess, err := quic.DialAddr(server, &tls.Config{InsecureSkipVerify: true}, nil) if err != nil { glog.Fatalf("connect to remote(%s) fail:%s\n", server, err) } diff --git a/proxy/server.go b/proxy/server.go index 340dddf..51f9164 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -9,7 +9,7 @@ import ( ) func StartServer(port string) { - ln, err := quic.ListenAddr(":"+port, nil, nil) + ln, err := quic.ListenAddr(":"+port, mockTlsPem(), nil) if err != nil { glog.Fatalln(err) } diff --git a/proxy/util.go b/proxy/util.go index 7d58220..26d6d0f 100644 --- a/proxy/util.go +++ b/proxy/util.go @@ -1,7 +1,13 @@ package proxy import ( + "crypto/rand" + "crypto/rsa" + "crypto/tls" + "crypto/x509" + "encoding/pem" "io" + "math/big" "net" "sync" "sync/atomic" @@ -45,3 +51,24 @@ func redirect(conn1, conn2 net.Conn, wg *sync.WaitGroup, exitFlag *int32) { conn2.SetDeadline(now) wg.Done() } + +func mockTlsPem() *tls.Config { + key, err := rsa.GenerateKey(rand.Reader, 1024) + if err != nil { + glog.Fatalln(err) + } + template := x509.Certificate{SerialNumber: big.NewInt(1)} + certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key) + if err != nil { + glog.Fatalln(err) + } + + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}) + keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)}) + + tlsCert, err := tls.X509KeyPair(certPEM, keyPEM) + if err != nil { + glog.Fatalln(err) + } + return &tls.Config{Certificates: []tls.Certificate{tlsCert}} +}