tricrypt/cmd/client/main.go

64 lines
1.3 KiB
Go
Raw Normal View History

2024-02-23 14:28:59 +01:00
package main
import (
"crypto/tls"
2024-02-23 15:32:08 +01:00
"crypto/x509"
2024-02-23 14:28:59 +01:00
"fmt"
"log"
"os"
"git.qowevisa.me/Qowevisa/gotell/env"
)
func main() {
2024-02-23 18:23:31 +01:00
host, err := env.GetHost()
2024-02-23 14:28:59 +01:00
if err != nil {
2024-02-25 02:36:00 +01:00
log.Fatal(err)
2024-02-23 14:28:59 +01:00
}
2024-02-23 18:23:31 +01:00
port, err := env.GetPort()
if err != nil {
2024-02-25 02:36:00 +01:00
log.Fatal(err)
2024-02-23 15:32:08 +01:00
}
2024-02-25 02:36:00 +01:00
2024-02-25 18:15:05 +01:00
loadingFileName := env.ServerFullchainFileName
cert, err := os.ReadFile(loadingFileName)
2024-02-23 18:23:31 +01:00
if err != nil {
2024-02-25 02:36:00 +01:00
log.Fatalf("client: load root cert: %s", err)
2024-02-23 15:32:08 +01:00
}
2024-02-25 18:15:05 +01:00
log.Printf("Certificate %s loaded successfully!\n", loadingFileName)
//
2024-02-23 18:23:31 +01:00
roots := x509.NewCertPool()
2024-02-25 02:36:00 +01:00
if ok := roots.AppendCertsFromPEM(cert); !ok {
log.Fatalf("client: failed to parse root certificate")
2024-02-23 15:32:08 +01:00
}
2024-02-25 02:54:35 +01:00
config := &tls.Config{
RootCAs: roots,
}
2024-02-25 02:36:00 +01:00
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
2024-02-23 15:32:08 +01:00
if err != nil {
2024-02-25 02:36:00 +01:00
log.Fatalf("client: dial: %s", err)
}
defer conn.Close()
log.Println("client: connected to: ", conn.RemoteAddr())
message := "Hello secure Server\n"
n, err := conn.Write([]byte(message))
if err != nil {
log.Fatalf("client: write: %s", err)
2024-02-23 14:28:59 +01:00
}
2024-02-23 15:32:08 +01:00
2024-02-25 02:36:00 +01:00
log.Printf("client: wrote %q (%d bytes)", message, n)
reply := make([]byte, 256)
n, err = conn.Read(reply)
if err != nil {
log.Fatalf("client: read: %s", err)
}
log.Printf("client: read %q (%d bytes)", string(reply[:n]), n)
log.Print("client: exiting")
2024-02-23 14:28:59 +01:00
}