package main import ( "crypto/tls" "crypto/x509" "fmt" "io" "net/http" "os" ) type ClientConn struct { tlsConn *tls.Conn credentials Credentials isAuthed bool } type Credentials struct { user string pass string } func New(host string, port int) *tls.Conn { cert, err := os.ReadFile("./public.pem") if err != nil { fmt.Printf("Error reading cert from ./public.pem: %v", err) os.Exit(1) } certPool := x509.NewCertPool() if ok := certPool.AppendCertsFromPEM(cert); !ok { fmt.Printf("Error loading certificate %v into cert pool", cert) os.Exit(1) } config := &tls.Config{RootCAs: certPool} conn, err := tls.Dial("tcp", host+":"+string(rune(port)), config) return conn } func getPublicKey() { out, err := os.Create("./public.pem") if err != nil { fmt.Printf("Error closing file writer: %v", err) os.Exit(1) } defer out.Close() resp, err := http.Get(Config.GetAsString("General.http-server") + "/public.pem") if err != nil { fmt.Printf("Error fetching public key: %v", err) os.Exit(1) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { fmt.Printf("Request was unseccessful with code %v", resp.StatusCode) } _, err = io.Copy(out, resp.Body) if err != nil { fmt.Printf("Error writing public key to file: %v", err) os.Exit(1) } }