diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index f55b37e..3759b94 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -6,10 +6,13 @@ package cmd import ( "bufio" + "crypto/tls" + _ "embed" "fmt" "log" "net" "os" + "os/exec" "strings" ) @@ -19,8 +22,12 @@ var ( isLogging bool = false logLocation string listenerList []chan string + servInsecure bool ) +//go:embed gen-cert.sh +var script string + type User struct { Username string IP string @@ -32,8 +39,46 @@ func (u User) CreateUser(usr string, ip string) User { 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) + 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 { log.Fatalf("Error happened trying to listen on port: %v\n", err) } @@ -44,7 +89,7 @@ func Server() { if err != nil { 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) go handleConn(conn, chatChan) }