Implement choosing TLS/plaintext for server

This commit is contained in:
raul 2024-05-14 09:11:22 +02:00
parent 51b0dd5258
commit afafb12663
2 changed files with 17 additions and 12 deletions

View File

@ -32,6 +32,7 @@ func init() {
serverCmd.PersistentFlags().StringP("port", "p", "1302", "port to use for listening") 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().StringP("history", "r", "", "File to store and recover chat history from")
serverCmd.PersistentFlags().String("password", "", "Password for accessing the chat server") 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 { func setServerParameters(cmd *cobra.Command) error {
@ -57,5 +58,9 @@ func setServerParameters(cmd *cobra.Command) error {
if parPassword != "" { if parPassword != "" {
password = parPassword password = parPassword
} }
insecure, err := cmd.Flags().GetBool("insecure")
if insecure == true {
servInsecure = true
}
return nil return nil
} }

View File

@ -56,17 +56,6 @@ func startInsecureServer() (net.Listener, error) {
} }
func startSecureServer() (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") cer, err := tls.LoadX509KeyPair("server.crt", "server.key")
if os.IsNotExist(err) { if os.IsNotExist(err) {
createCerts() createCerts()
@ -76,8 +65,19 @@ func Server() {
log.Fatalf("Error happened loading certificates: %v\n", err) log.Fatalf("Error happened loading certificates: %v\n", err)
} }
config := &tls.Config{Certificates: []tls.Certificate{cer}} config := &tls.Config{Certificates: []tls.Certificate{cer}}
ln, err := tls.Listen("tcp", ":"+listenPort, config) 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 { 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)