Many changes.
This commit is contained in:
parent
ade25708ad
commit
b6e0b58631
@ -1,4 +1,5 @@
|
||||
#!/bin/bash
|
||||
echo $PWD
|
||||
|
||||
var_file="./var_file"
|
||||
|
||||
@ -17,4 +18,4 @@ let var=$var+1
|
||||
echo $var ": Hallo Freunde der Sonne" | openssl s_client -connect localhost:8080
|
||||
|
||||
|
||||
echo $var > $var_file
|
||||
echo $var > $var_file
|
||||
|
4
go.mod
4
go.mod
@ -1,3 +1,5 @@
|
||||
module tls_server
|
||||
module ws_server
|
||||
|
||||
go 1.19
|
||||
|
||||
require github.com/gorilla/websocket v1.5.0 // indirect
|
||||
|
2
go.sum
Normal file
2
go.sum
Normal file
@ -0,0 +1,2 @@
|
||||
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
13
main.go
13
main.go
@ -1,9 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"tls_server/server"
|
||||
"log"
|
||||
"net/http"
|
||||
"ws_server/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
server.RunServer()
|
||||
http.HandleFunc("/", server.SocketHandler)
|
||||
|
||||
err := http.ListenAndServe("localhost:8080", nil)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
log.Println("We ended")
|
||||
}
|
||||
|
@ -1,90 +1,34 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
const (
|
||||
port uint = 8080
|
||||
certFile = "/home/m/.local_ca/localhost.crt"
|
||||
keyFile = "/home/m/.local_ca/localhost.key"
|
||||
)
|
||||
var upgrader = websocket.Upgrader{} // use default options
|
||||
|
||||
func RunServer() error {
|
||||
tls_cfg, err := configTls()
|
||||
func SocketHandler(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
listener, err := listen(tls_cfg)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(-1)
|
||||
log.Print("Error during connection upgrading:", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
// The event loop
|
||||
for {
|
||||
connection, err := listener.Accept()
|
||||
messageType, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(-1)
|
||||
log.Println("Error during message reading:", err)
|
||||
break
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
log.Printf("Received: %s", message)
|
||||
err = conn.WriteMessage(messageType, message)
|
||||
if err != nil {
|
||||
log.Println("Error during message writing:", err)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
BIN
tls_server
BIN
tls_server
Binary file not shown.
Loading…
Reference in New Issue
Block a user