go-irc

A basic SSL-supporting IRC client written in golang.
git clone http://git.hanabi.in/repos/go-irc.git
Log | Files | Refs | LICENSE

tls-fns.go (949B)


      1 package main
      2 
      3 import (
      4 	"crypto/tls"
      5 	"log"
      6 	"os"
      7 
      8 	"github.com/joho/godotenv"
      9 )
     10 
     11 func GetCert() tls.Certificate {
     12 	err := godotenv.Load()
     13 	if err != nil {
     14 		log.Fatal(err)
     15 	}
     16 
     17 	SSL_CERT := os.Getenv("SSL_CERT")
     18 	SSL_KEY := os.Getenv("SSL_KEY")
     19 
     20 	cert, err := tls.LoadX509KeyPair(SSL_CERT, SSL_KEY)
     21 	if err != nil {
     22 		log.Fatal(err)
     23 	}
     24 	return cert
     25 }
     26 
     27 func GetTLSConfig(cert tls.Certificate) tls.Config {
     28 	config := tls.Config{
     29 		Certificates:       []tls.Certificate{cert},
     30 		InsecureSkipVerify: false,
     31 	}
     32 	return config
     33 }
     34 
     35 func GetTLSConn() *tls.Conn {
     36 	err := godotenv.Load()
     37 	if err != nil {
     38 		log.Fatal(err)
     39 	}
     40 	IRC_SERVER := os.Getenv("IRC_SERVER")
     41 	cert := GetCert()                                 // Load certs
     42 	config := GetTLSConfig(cert)                      // Create TLS configuration
     43 	conn, err := tls.Dial("tcp", IRC_SERVER, &config) // Connect TLS connection
     44 	if err != nil {
     45 		log.Fatalf("Client: dial: %s", err)
     46 	}
     47 	return conn
     48 }