From 3261a436cbd87329ae35e4103ca7642ce54626f1 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Fri, 23 Feb 2024 15:28:59 +0200 Subject: [PATCH] Test --- .gitignore | 5 ++++ Makefile | 19 +++++++++++++ README.md | 13 ++++++++- cmd/client/main.go | 47 ++++++++++++++++++++++++++++++ cmd/server/main.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++ env/env.go | 6 ++++ go.mod | 3 ++ 7 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 Makefile create mode 100644 cmd/client/main.go create mode 100644 cmd/server/main.go create mode 100644 env/env.go create mode 100644 go.mod diff --git a/.gitignore b/.gitignore index adf8f72..18710ff 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,8 @@ # Go workspace file go.work + +*.pem +*.key +*.crt +bin diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d6d3c04 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +def: server client + @ + +all: rm def + @ + +rm: + rm ./bin/* 2>/dev/null || true + +server: + go build -o ./bin/$@ ./cmd/$@ + +client: + go build -o ./bin/$@ ./cmd/$@ + +gen_certs: + openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -config san.cnf + #openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 + diff --git a/README.md b/README.md index 2e8c34d..abde93b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,14 @@ # gotell -simple communication application in golang \ No newline at end of file +Simple communication application in golang + +## Goals + +I want to create as minimalistic communication application in Go as possible. + +So in my list contains: + +1. Go to stable without use of any third party dependency +2. Add security to the communication +3. Implement and test my idea about rotating public-private key pairs + diff --git a/cmd/client/main.go b/cmd/client/main.go new file mode 100644 index 0000000..2901632 --- /dev/null +++ b/cmd/client/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "bufio" + "crypto/tls" + "fmt" + + // "fmt" + "io" + "log" + "os" + + "git.qowevisa.me/Qowevisa/gotell/env" +) + +func main() { + cert, err := tls.LoadX509KeyPair("tls.crt", "tls.key") + if err != nil { + log.Fatalf("server: loadkeys: %s", err) + } + config := tls.Config{Certificates: []tls.Certificate{cert}} + // config.Rand = rand.Reader + + url := fmt.Sprintf("chat.qowevisa.md:%d", env.ConnectPort) + // Dial a TLS connection + conn, err := tls.Dial("tcp", url, &config) + if err != nil { + log.Fatalf("Failed to dial: %v", err) + } + defer conn.Close() + + // Buffer to hold data read from the connection + // buf := make([]byte, 1024) // Adjust size as needed + reader := bufio.NewScanner(os.Stdin) + for reader.Scan() { + text := reader.Text() + // Read from the connection + _, err := conn.Write([]byte(text + "\n")) + if err != nil { + if err != io.EOF { + log.Printf("Read error: %v", err) + } + break + } + // fmt.Printf("Received: %s\n", string(buf[:n])) + } +} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..e49b458 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,71 @@ +package main + +import ( + "bufio" + "crypto/rand" + "crypto/tls" + "fmt" + "log" + "net" + "os" + + "git.qowevisa.me/Qowevisa/gotell/env" +) + +func main() { + // Listen on TCP port 8080 on all available unicast and anycast IP addresses of the local system. + cert, err := tls.LoadX509KeyPair("tls.crt", "tls.key") + if err != nil { + log.Fatalf("server: loadkeys: %s", err) + } + config := tls.Config{Certificates: []tls.Certificate{cert}} + config.Rand = rand.Reader + url := fmt.Sprintf(":%d", env.Port) + listener, err := tls.Listen("tcp", url, &config) + if err != nil { + log.Fatal(err) + } + defer listener.Close() + + fmt.Println("Server is listening on port 8080") + + for { + // Wait for a connection. + conn, err := listener.Accept() + if err != nil { + log.Print(err) + continue + } + // Handle the connection in a new goroutine. + // The loop then returns to accepting, so that + // multiple connections may be served concurrently. + go handleConnection(conn) + } +} + +func handleConnection(conn net.Conn) { + defer conn.Close() + + fmt.Printf("Client connected: %v\n", conn.RemoteAddr()) + + // Create a new reader for each client. + scanner := bufio.NewScanner(conn) + for scanner.Scan() { + // Read the incoming connection into the buffer. + text := scanner.Text() + fmt.Printf("Received: %s\n", text) + + // Send a response back to client. + _, err := conn.Write([]byte("Message received: " + text + "\n")) + if err != nil { + log.Printf("Failed to write to connection: %v", err) + break + } + } + + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "Error reading from connection: %s\n", err) + } + + fmt.Printf("Client disconnected: %v\n", conn.RemoteAddr()) +} diff --git a/env/env.go b/env/env.go new file mode 100644 index 0000000..2bbfd59 --- /dev/null +++ b/env/env.go @@ -0,0 +1,6 @@ +package env + +const ( + Port = 2993 + ConnectPort = 1993 +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9cd8f2a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.qowevisa.me/Qowevisa/gotell + +go 1.20