Implement serverside TLS

This commit is contained in:
raul 2024-05-14 09:06:45 +02:00
parent e3b681e904
commit 0f33d13d6a
1 changed files with 47 additions and 2 deletions

View File

@ -6,10 +6,13 @@ package cmd
import ( import (
"bufio" "bufio"
"crypto/tls"
_ "embed"
"fmt" "fmt"
"log" "log"
"net" "net"
"os" "os"
"os/exec"
"strings" "strings"
) )
@ -19,8 +22,12 @@ var (
isLogging bool = false isLogging bool = false
logLocation string logLocation string
listenerList []chan string listenerList []chan string
servInsecure bool
) )
//go:embed gen-cert.sh
var script string
type User struct { type User struct {
Username string Username string
IP string IP string
@ -32,8 +39,46 @@ func (u User) CreateUser(usr string, ip string) User {
return u return u
} }
func Server() { func createCerts() {
fmt.Println("[-] Certificates don't exist! Creating them...")
c := exec.Command("bash")
c.Stdin = strings.NewReader(script)
b, err := c.Output()
if err != nil {
log.Fatalf("Error occurred creating certificates: %v\n", err)
}
fmt.Print(string(b))
}
func startInsecureServer() (net.Listener, error) {
ln, err := net.Listen("tcp", ":"+listenPort) ln, err := net.Listen("tcp", ":"+listenPort)
return ln, err
}
func startSecureServer() (net.Listener, error) {
}
func Server() {
var ln net.Listener
var err error
if servInsecure == true {
ln, err = startInsecureServer()
} else {
ln, err = startSecureServer()
}
cer, err := tls.LoadX509KeyPair("server.crt", "server.key")
if os.IsNotExist(err) {
createCerts()
cer, err = tls.LoadX509KeyPair("server.crt", "server.key")
}
if err != nil {
log.Fatalf("Error happened loading certificates: %v\n", err)
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
ln, err := tls.Listen("tcp", ":"+listenPort, config)
if err != nil { if err != nil {
log.Fatalf("Error happened trying to listen on port: %v\n", err) log.Fatalf("Error happened trying to listen on port: %v\n", err)
} }
@ -44,7 +89,7 @@ func Server() {
if err != nil { if err != nil {
log.Fatalf("Error happened trying to accept connection: %v\n", err) log.Fatalf("Error happened trying to accept connection: %v\n", err)
} }
chatChan := make(chan string, 10) chatChan := make(chan string, 30)
listenerList = append(listenerList, chatChan) listenerList = append(listenerList, chatChan)
go handleConn(conn, chatChan) go handleConn(conn, chatChan)
} }