1
0

tls.go 578 B

12345678910111213141516171819202122232425262728293031323334
  1. package crypt
  2. import (
  3. "crypto/tls"
  4. "net"
  5. "os"
  6. "github.com/astaxie/beego/logs"
  7. )
  8. var pemPath, keyPath string
  9. func InitTls(pem, key string) {
  10. pemPath = pem
  11. keyPath = key
  12. }
  13. func NewTlsServerConn(conn net.Conn) net.Conn {
  14. cert, err := tls.LoadX509KeyPair(pemPath, keyPath)
  15. if err != nil {
  16. logs.Error(err)
  17. os.Exit(0)
  18. return nil
  19. }
  20. config := &tls.Config{Certificates: []tls.Certificate{cert}}
  21. return tls.Server(conn, config)
  22. }
  23. func NewTlsClientConn(conn net.Conn) net.Conn {
  24. conf := &tls.Config{
  25. InsecureSkipVerify: true,
  26. }
  27. return tls.Client(conn, conf)
  28. }