From afafb126637b4d2647a07ec0adb93fe12ca10a3b Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 14 May 2024 09:11:22 +0200 Subject: [PATCH] Implement choosing TLS/plaintext for server --- cmd/server.go | 5 +++++ cmd/serverFunc.go | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 10b4bd1..a83c38b 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -32,6 +32,7 @@ func init() { serverCmd.PersistentFlags().StringP("port", "p", "1302", "port to use for listening") serverCmd.PersistentFlags().StringP("history", "r", "", "File to store and recover chat history from") serverCmd.PersistentFlags().String("password", "", "Password for accessing the chat server") + serverCmd.Flags().Bool("insecure", false, "[UNSAFE] Do not use TLS encryption") } func setServerParameters(cmd *cobra.Command) error { @@ -57,5 +58,9 @@ func setServerParameters(cmd *cobra.Command) error { if parPassword != "" { password = parPassword } + insecure, err := cmd.Flags().GetBool("insecure") + if insecure == true { + servInsecure = true + } return nil } diff --git a/cmd/serverFunc.go b/cmd/serverFunc.go index 3759b94..447b60c 100644 --- a/cmd/serverFunc.go +++ b/cmd/serverFunc.go @@ -56,17 +56,6 @@ func startInsecureServer() (net.Listener, error) { } 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() @@ -76,8 +65,19 @@ func Server() { log.Fatalf("Error happened loading certificates: %v\n", err) } config := &tls.Config{Certificates: []tls.Certificate{cer}} - ln, err := tls.Listen("tcp", ":"+listenPort, config) + return ln, err +} + +func Server() { + var ln net.Listener + var err error + if servInsecure == true { + fmt.Println("[WARNING] Starting unencrypted server!") + ln, err = startInsecureServer() + } else { + ln, err = startSecureServer() + } if err != nil { log.Fatalf("Error happened trying to listen on port: %v\n", err)