2024-02-23 14:28:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-07 08:48:13 +02:00
|
|
|
"context"
|
2024-02-23 14:28:59 +01:00
|
|
|
"crypto/tls"
|
2024-02-23 15:32:08 +01:00
|
|
|
"crypto/x509"
|
2024-02-23 14:28:59 +01:00
|
|
|
"fmt"
|
2024-06-07 08:48:13 +02:00
|
|
|
"io"
|
2024-02-23 14:28:59 +01:00
|
|
|
"log"
|
2024-06-07 08:48:13 +02:00
|
|
|
"net"
|
2024-02-23 14:28:59 +01:00
|
|
|
"os"
|
2024-06-07 08:48:13 +02:00
|
|
|
"sync"
|
|
|
|
"time"
|
2024-02-23 14:28:59 +01:00
|
|
|
|
2024-06-07 08:48:13 +02:00
|
|
|
"git.qowevisa.me/Qowevisa/gotell/communication"
|
2024-02-23 14:28:59 +01:00
|
|
|
"git.qowevisa.me/Qowevisa/gotell/env"
|
|
|
|
)
|
|
|
|
|
2024-06-07 08:48:13 +02:00
|
|
|
var messageChannel chan ([]byte)
|
|
|
|
|
2024-02-23 14:28:59 +01:00
|
|
|
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())
|
2024-06-07 08:48:13 +02:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(ctx context.Context, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done() // Mark this goroutine as done when it exits
|
|
|
|
buf := make([]byte, 1024)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done(): // Check if context cancellation has been requested
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
timeoutDuration := 5 * time.Second
|
|
|
|
err := conn.SetReadDeadline(time.Now().Add(timeoutDuration))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
n, err := conn.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.EOF {
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
messageChannel <- buf[:n]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(ctx, wg)
|
|
|
|
go func() {
|
|
|
|
for message := range messageChannel {
|
|
|
|
msg, err := communication.Decode(message)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("ERROR: %v\n", err)
|
|
|
|
}
|
2024-06-07 09:02:39 +02:00
|
|
|
log.Printf("I get %v\n", *msg)
|
2024-06-07 08:48:13 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
2024-02-23 14:28:59 +01:00
|
|
|
}
|