This commit is contained in:
qowevisa 2024-02-23 16:32:08 +02:00
parent 33cf8582e9
commit db92aee2be
4 changed files with 71 additions and 58 deletions

View File

@ -7,13 +7,42 @@ all: rm def
rm:
rm ./bin/* 2>/dev/null || true
server:
server: server.srt server.key
go build -o ./bin/$@ ./cmd/$@
client:
client: ca.crt
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
all: ca.crt server.crt client.crt
ca.key:
openssl genrsa -out ca.key 4096
ca.crt: ca.key
openssl req -new -x509 -days 365 -key ca.key -out ca.crt -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=YourCA"
server.key:
openssl genrsa -out server.key 4096
server.csr: server.key
openssl req -new -key server.key -out server.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=server.yourdomain.com"
server.crt: server.csr ca.crt ca.key
openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
client.key:
openssl genrsa -out client.key 4096
client.csr: client.key
openssl req -new -key client.key -out client.csr -subj "/C=US/ST=YourState/L=YourCity/O=YourOrganization/CN=client.yourdomain.com"
client.crt: client.csr ca.crt ca.key
openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt
clean:
rm -f ca.key ca.crt server.key server.csr server.crt client.key client.csr client.crt

View File

@ -1,32 +1,48 @@
package main
import (
"bufio"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
"net/http"
"os"
"git.qowevisa.me/Qowevisa/gotell/env"
)
func main() {
url := fmt.Sprintf("chat.qowevisa.me:%d", env.ConnectPort)
conn, err := tls.Dial("tcp", url, &tls.Config{
InsecureSkipVerify: false, // Set to true if using self-signed certificates
})
caCert, err := os.ReadFile("ca.crt")
if err != nil {
log.Fatalf("Failed to dial: %v", err)
log.Fatalf("Reading CA cert file: %s", err)
}
defer conn.Close()
reader := bufio.NewScanner(os.Stdin)
for reader.Scan() {
text := reader.Text()
_, err := conn.Write([]byte(text + "\n"))
if err != nil {
log.Printf("Write error: %v", err)
break
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
RootCAs: caCertPool,
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
},
}
url := fmt.Sprintf("https://chat.qowevisa.me:%d", env.ConnectPort)
response, err := client.Get(url)
if err != nil {
log.Fatalf("Failed to request: %s", err)
}
defer response.Body.Close()
body, err := io.ReadAll(response.Body)
if err != nil {
log.Fatalf("Failed to read response: %s", err)
}
log.Printf("Server response: %s", body)
}

View File

@ -1,52 +1,20 @@
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"git.qowevisa.me/Qowevisa/gotell/env"
"net/http"
)
func main() {
url := fmt.Sprintf("127.0.0.1:%d", env.Port)
listener, err := net.Listen("tcp", url)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, you've reached the server!")
})
log.Println("Starting server on :1993...")
err := http.ListenAndServeTLS(":1993", "server.crt", "server.key", nil)
if err != nil {
log.Fatal(err)
}
defer listener.Close()
fmt.Printf("Server is listening on %s\n", url)
for {
conn, err := listener.Accept()
if err != nil {
log.Print(err)
continue
}
go handleConnection(conn)
log.Fatalf("Failed to start server: %v", err)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
fmt.Printf("Client connected: %v\n", conn.RemoteAddr())
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
text := scanner.Text()
fmt.Printf("Received: %s\n", text)
_, 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())
}

2
env/env.go vendored
View File

@ -2,5 +2,5 @@ package env
const (
Port = 2993
ConnectPort = 443
ConnectPort = 1993
)