2024-03-13 23:06:13 +01:00
|
|
|
package tui
|
|
|
|
|
|
|
|
import (
|
2024-03-14 01:18:51 +01:00
|
|
|
"context"
|
2024-03-13 23:06:13 +01:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2024-03-14 01:18:51 +01:00
|
|
|
"sync"
|
2024-03-13 23:06:13 +01:00
|
|
|
|
|
|
|
"git.qowevisa.me/Qowevisa/gotell/env"
|
|
|
|
"git.qowevisa.me/Qowevisa/gotell/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetIntPercentFromData(a, b int) int {
|
|
|
|
return int(float64((a * 100)) / float64(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendMessageToConnectionEasy(msg *[]rune) (dataProcessHandler, dataT) {
|
|
|
|
return SendMessageToConnection, dataT{rawP: msg}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SendMessageToConnection(t *TUI, data dataT) error {
|
|
|
|
if t.tlsConnection == nil {
|
|
|
|
return errors.WrapErr("t.tlsConnection", errors.NOT_SET)
|
|
|
|
}
|
|
|
|
if data.rawP == nil {
|
|
|
|
return errors.WrapErr("data.rawP", errors.NOT_SET)
|
|
|
|
}
|
|
|
|
message := string(*data.rawP)
|
|
|
|
n, err := t.tlsConnection.Write([]byte(message))
|
|
|
|
if err != nil {
|
|
|
|
return errors.WrapErr("t.tlsConnection.Write", err)
|
|
|
|
}
|
|
|
|
log.Printf("Successfully wrote %d bytes to connection; Message: %s", n, message)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// takes data from storage
|
|
|
|
func FE_ConnectTLS(t *TUI, data dataT) error {
|
|
|
|
log.Printf("Start of FE_ConnectTLS")
|
|
|
|
host, exist := t.storage[STORAGE_HOST_CONST]
|
|
|
|
if !exist {
|
|
|
|
errors.WrapErr("t.storage:host", errors.NOT_SET)
|
|
|
|
}
|
|
|
|
portStr, exist := t.storage[STORAGE_PORT_CONST]
|
|
|
|
if !exist {
|
|
|
|
errors.WrapErr("t.storage:host", errors.NOT_SET)
|
|
|
|
}
|
|
|
|
port, err := strconv.ParseInt(portStr, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
errors.WrapErr("port.strconv.ParseInt", err)
|
|
|
|
}
|
|
|
|
loadingFileName := env.ServerFullchainFileName
|
|
|
|
cert, err := os.ReadFile(loadingFileName)
|
|
|
|
if err != nil {
|
|
|
|
errors.WrapErr("os.ReadFile", err)
|
|
|
|
}
|
|
|
|
log.Printf("Certificate %s loaded successfully!\n", loadingFileName)
|
|
|
|
//
|
|
|
|
roots := x509.NewCertPool()
|
|
|
|
if ok := roots.AppendCertsFromPEM(cert); !ok {
|
|
|
|
errors.WrapErr("client: failed to parse root certificate", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := &tls.Config{
|
|
|
|
RootCAs: roots,
|
|
|
|
}
|
2024-03-15 16:56:58 +01:00
|
|
|
if t.stateChannel == nil {
|
|
|
|
return errors.WrapErr("t.stateChannel", errors.NOT_INIT)
|
|
|
|
}
|
|
|
|
t.stateChannel <- "TLS Connecting"
|
2024-03-13 23:06:13 +01:00
|
|
|
conn, err := tls.Dial(
|
|
|
|
"tcp",
|
|
|
|
fmt.Sprintf("%s:%d", host, int(port)),
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-03-15 16:56:58 +01:00
|
|
|
t.stateChannel <- "TLS Connection error"
|
2024-03-13 23:06:13 +01:00
|
|
|
return errors.WrapErr("tls.Dial", err)
|
|
|
|
}
|
2024-03-15 16:56:58 +01:00
|
|
|
t.stateChannel <- "TLS Established"
|
2024-03-13 23:06:13 +01:00
|
|
|
t.tlsConnection = conn
|
|
|
|
t.isConnected = true
|
2024-03-14 01:18:51 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
t.tlsConnCloseData = closeData{
|
|
|
|
wg: wg,
|
|
|
|
cancel: cancel,
|
|
|
|
}
|
|
|
|
go t.launchReadingMessagesFromConnection(ctx, wg)
|
2024-03-13 23:06:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-14 01:18:51 +01:00
|
|
|
func CloseConnection(wg *sync.WaitGroup, cancel context.CancelFunc) {
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2024-03-13 23:06:13 +01:00
|
|
|
func AddToStorageEasy(key string, val *[]rune) (dataProcessHandler, dataT) {
|
|
|
|
// that's why I create wrapper around it.
|
|
|
|
// try to understand that, dear viewer!
|
|
|
|
return H_AddToStorage, dataT{rawP: val, op1: key}
|
|
|
|
}
|
|
|
|
|
|
|
|
func H_AddToStorage(t *TUI, data dataT) error {
|
|
|
|
log.Printf("Debug: %#v", data)
|
|
|
|
log.Printf("Adding to storage: %s = %s", data.op1, string(*data.rawP))
|
|
|
|
if t.storage == nil {
|
|
|
|
return errors.WrapErr("t.storage", errors.NOT_INIT)
|
|
|
|
}
|
|
|
|
t.storage[data.op1] = string(*data.rawP)
|
|
|
|
return nil
|
|
|
|
}
|