From 7493af68fcb3aa189612dcdc1266cfa14a9944ed Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 14 May 2024 09:26:45 +0200 Subject: [PATCH] Implement choosing TLS/plaintext for client --- cmd/client.go | 6 ++++++ cmd/clientFunc.go | 28 +++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/cmd/client.go b/cmd/client.go index 6f658e1..78a63f3 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -36,6 +36,7 @@ func init() { rootCmd.AddCommand(clientCmd) clientCmd.PersistentFlags().StringP("ip", "i", "", "Server IP to connect to") clientCmd.PersistentFlags().StringP("port", "p", "1302", "Server port to connect to") + clientCmd.Flags().Bool("insecure", false, "[UNSAFE] Do not use TLS encryption") } func setClientParameters(cmd *cobra.Command) error { @@ -57,5 +58,10 @@ func setClientParameters(cmd *cobra.Command) error { } serverIP = parameterIP + insecure, err := cmd.Flags().GetBool("insecure") + if insecure == true { + clientInsecure = true + } + return nil } diff --git a/cmd/clientFunc.go b/cmd/clientFunc.go index 73d67f2..e74ecf3 100644 --- a/cmd/clientFunc.go +++ b/cmd/clientFunc.go @@ -37,17 +37,35 @@ func (m Message) toSend() { } var ( - serverPort string = "1302" - serverIP string - data Message + serverPort string = "1302" + serverIP string + data Message + clientInsecure bool ) -func Client() { - //conn, err := net.Dial("tcp", serverIP+":"+serverPort) +func startSecureConnection() (net.Conn, error) { conf := &tls.Config{ InsecureSkipVerify: true, } conn, err := tls.Dial("tcp", serverIP+":"+serverPort, conf) + return conn, err +} + +func startInsecureConnection() (net.Conn, error) { + conn, err := net.Dial("tcp", serverIP+":"+serverPort) + return conn, err +} + +func Client() { + var conn net.Conn + var err error + if clientInsecure == true { + fmt.Println("WARNING: Starting unencrypted connection!") + conn, err = startInsecureConnection() + } else { + conn, err = startSecureConnection() + } + if err != nil { log.Fatalf("Error occurred trying to connect to server: %v\n", err) }