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:
rm ./bin/* 2>/dev/null || true rm ./bin/* 2>/dev/null || true
server: server: server.srt server.key
go build -o ./bin/$@ ./cmd/$@ go build -o ./bin/$@ ./cmd/$@
client: client: ca.crt
go build -o ./bin/$@ ./cmd/$@ go build -o ./bin/$@ ./cmd/$@
gen_certs: gen_certs:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -config san.cnf 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 #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 package main
import ( import (
"bufio"
"crypto/tls" "crypto/tls"
"crypto/x509"
"fmt" "fmt"
"io"
"log" "log"
"net/http"
"os" "os"
"git.qowevisa.me/Qowevisa/gotell/env" "git.qowevisa.me/Qowevisa/gotell/env"
) )
func main() { func main() {
url := fmt.Sprintf("chat.qowevisa.me:%d", env.ConnectPort) caCert, err := os.ReadFile("ca.crt")
conn, err := tls.Dial("tcp", url, &tls.Config{
InsecureSkipVerify: false, // Set to true if using self-signed certificates
})
if err != nil { 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) caCertPool := x509.NewCertPool()
for reader.Scan() { caCertPool.AppendCertsFromPEM(caCert)
text := reader.Text()
_, err := conn.Write([]byte(text + "\n")) tlsConfig := &tls.Config{
if err != nil { RootCAs: caCertPool,
log.Printf("Write error: %v", err)
break
}
} }
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 package main
import ( import (
"bufio"
"fmt" "fmt"
"log" "log"
"net" "net/http"
"os"
"git.qowevisa.me/Qowevisa/gotell/env"
) )
func main() { func main() {
url := fmt.Sprintf("127.0.0.1:%d", env.Port) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
listener, err := net.Listen("tcp", url) 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 { if err != nil {
log.Fatal(err) log.Fatalf("Failed to start server: %v", 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)
} }
} }
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 ( const (
Port = 2993 Port = 2993
ConnectPort = 443 ConnectPort = 1993
) )