fix mock tls config

This commit is contained in:
wweir
2018-11-24 23:59:20 +08:00
parent 325d1b98b5
commit be646215e3
4 changed files with 34 additions and 2 deletions
+4
View File
@@ -16,3 +16,7 @@
.vscode
.idea
sower
cert.pem
**/cert.pem
privkey.pem
**/privkey.pem
+2 -1
View File
@@ -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)
}
+1 -1
View File
@@ -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)
}
+27
View File
@@ -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}}
}