commit ade25708ad00e76af94c8f3f83e706316d440022 Author: Marco Date: Sat Oct 22 20:01:55 2022 +0200 First commit diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..c9e8dd9 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,15 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceFolder}/main.go" + } + ] +} \ No newline at end of file diff --git a/client_script.sh b/client_script.sh new file mode 100755 index 0000000..e6e90b0 --- /dev/null +++ b/client_script.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +var_file="./var_file" + +if [ -f "$var_file" ] +then + echo "file exists" + var=$(cat $var_file) +else + "file does not exist" + var=10 +fi + +echo "Value of var: " $var + +let var=$var+1 +echo $var ": Hallo Freunde der Sonne" | openssl s_client -connect localhost:8080 + + +echo $var > $var_file \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7624b43 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module tls_server + +go 1.19 diff --git a/main.go b/main.go new file mode 100644 index 0000000..e2c2d35 --- /dev/null +++ b/main.go @@ -0,0 +1,9 @@ +package main + +import ( + "tls_server/server" +) + +func main() { + server.RunServer() +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..39e438a --- /dev/null +++ b/server/server.go @@ -0,0 +1,90 @@ +package server + +import ( + "bufio" + "crypto/tls" + "fmt" + "io" + "log" + "net" + "os" + "strconv" +) + +const ( + port uint = 8080 + certFile = "/home/m/.local_ca/localhost.crt" + keyFile = "/home/m/.local_ca/localhost.key" +) + +func RunServer() error { + tls_cfg, err := configTls() + if err != nil { + fmt.Println(err) + os.Exit(-1) + } + + listener, err := listen(tls_cfg) + if err != nil { + fmt.Println(err) + os.Exit(-1) + } + + for { + connection, err := listener.Accept() + if err != nil { + fmt.Println(err) + os.Exit(-1) + } + + go handleConnection(connection) + } +} + +func configTls() (*tls.Config, error) { + cert, err := tls.LoadX509KeyPair(certFile, keyFile) + if err != nil { + log.Println("Error while loading x509 key pair") + log.Println(err) + return nil, err + } + + return &tls.Config{Certificates: []tls.Certificate{cert}}, nil +} + +func listen(tls_config *tls.Config) (net.Listener, error) { + listeningOnAddr := "localhost:" + strconv.FormatUint(uint64(port), 10) + + listener, err := tls.Listen("tcp", listeningOnAddr, tls_config) + if err != nil { + return nil, err + } + fmt.Println("We started to listen on port", port) + + return listener, nil +} + +func handleConnection(connection net.Conn) { + + testReader := *bufio.NewReader(connection) + + for { + + readBuf := make([]byte, 10) + + if n, err := testReader.Read(readBuf); n > 0 { + fmt.Printf("Type of connection: %T\n", connection) + fmt.Println(n, " bytes read: ", readBuf) + + if n > 0 { + switch err { + case nil: + case io.EOF: + fmt.Println(err) + default: + fmt.Println(err) + } + } + } + } +} diff --git a/tls_server b/tls_server new file mode 100755 index 0000000..e460e2b Binary files /dev/null and b/tls_server differ diff --git a/var_file b/var_file new file mode 100644 index 0000000..9d60796 --- /dev/null +++ b/var_file @@ -0,0 +1 @@ +11 \ No newline at end of file