First commit

This commit is contained in:
Marco 2022-10-22 20:01:55 +02:00
commit ade25708ad
7 changed files with 138 additions and 0 deletions

15
.vscode/launch.json vendored Normal file
View File

@ -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"
}
]
}

20
client_script.sh Executable file
View File

@ -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

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module tls_server
go 1.19

9
main.go Normal file
View File

@ -0,0 +1,9 @@
package main
import (
"tls_server/server"
)
func main() {
server.RunServer()
}

90
server/server.go Normal file
View File

@ -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)
}
}
}
}
}

BIN
tls_server Executable file

Binary file not shown.

1
var_file Normal file
View File

@ -0,0 +1 @@
11