Test
This commit is contained in:
parent
44fa89723a
commit
3261a436cb
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -21,3 +21,8 @@
|
|||
# Go workspace file
|
||||
go.work
|
||||
|
||||
|
||||
*.pem
|
||||
*.key
|
||||
*.crt
|
||||
bin
|
||||
|
|
19
Makefile
Normal file
19
Makefile
Normal file
|
@ -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
|
||||
|
13
README.md
13
README.md
|
@ -1,3 +1,14 @@
|
|||
# gotell
|
||||
|
||||
simple communication application in golang
|
||||
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
|
||||
|
||||
|
|
47
cmd/client/main.go
Normal file
47
cmd/client/main.go
Normal file
|
@ -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]))
|
||||
}
|
||||
}
|
71
cmd/server/main.go
Normal file
71
cmd/server/main.go
Normal file
|
@ -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())
|
||||
}
|
6
env/env.go
vendored
Normal file
6
env/env.go
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
package env
|
||||
|
||||
const (
|
||||
Port = 2993
|
||||
ConnectPort = 1993
|
||||
)
|
Loading…
Reference in New Issue
Block a user