SimpleFileSync/serverconnection.go
Steven Tracey 29fe0123d5 Bleh
2023-05-19 16:16:53 -04:00

85 lines
1.6 KiB
Go

package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
)
type SFSConnection struct {
tlsConn *tls.Conn
writing bool
credentials Credentials
isAuthed bool
}
type Credentials struct {
user string
pass string
}
func NewSFSConnection(host string, port int) (*SFSConnection, error) {
_, err := os.Stat("./public.cer")
if err != nil {
getPublicKey()
}
cert, err := os.ReadFile("./public.cer")
if err != nil {
return nil, fmt.Errorf("error reading cert from ./public.cer: %v", err)
}
certPool := x509.NewCertPool()
if ok := certPool.AppendCertsFromPEM(cert); !ok {
return nil, fmt.Errorf("error loading certificate %v into cert pool", cert)
}
config := &tls.Config{
RootCAs: certPool,
CipherSuites: []uint16{
tls.TLS_AES_128_GCM_SHA256,
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
},
}
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
if err != nil {
return nil, err
}
return &SFSConnection{
conn,
false,
Credentials{"", ""},
true,
}, nil
}
func getPublicKey() {
out, err := os.Create("./public.cer")
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.cer")
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)
}
}