2019-03-05 01:23:18 +00:00
|
|
|
package crypt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2019-08-10 03:10:01 +00:00
|
|
|
"github.com/astaxie/beego/logs"
|
2019-03-05 01:23:18 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-03-23 14:19:59 +00:00
|
|
|
var pemPath, keyPath string
|
|
|
|
|
|
|
|
func InitTls(pem, key string) {
|
|
|
|
pemPath = pem
|
|
|
|
keyPath = key
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:23:18 +00:00
|
|
|
func NewTlsServerConn(conn net.Conn) net.Conn {
|
2019-03-23 14:19:59 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair(pemPath, keyPath)
|
2019-03-05 01:23:18 +00:00
|
|
|
if err != nil {
|
|
|
|
logs.Error(err)
|
|
|
|
os.Exit(0)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
config := &tls.Config{Certificates: []tls.Certificate{cert}}
|
|
|
|
return tls.Server(conn, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTlsClientConn(conn net.Conn) net.Conn {
|
|
|
|
conf := &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
}
|
|
|
|
return tls.Client(conn, conf)
|
|
|
|
}
|