Implement choosing TLS/plaintext for client

This commit is contained in:
raul 2024-05-14 09:26:45 +02:00
parent afafb12663
commit 7493af68fc
2 changed files with 29 additions and 5 deletions

View File

@ -36,6 +36,7 @@ func init() {
rootCmd.AddCommand(clientCmd) rootCmd.AddCommand(clientCmd)
clientCmd.PersistentFlags().StringP("ip", "i", "", "Server IP to connect to") clientCmd.PersistentFlags().StringP("ip", "i", "", "Server IP to connect to")
clientCmd.PersistentFlags().StringP("port", "p", "1302", "Server port 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 { func setClientParameters(cmd *cobra.Command) error {
@ -57,5 +58,10 @@ func setClientParameters(cmd *cobra.Command) error {
} }
serverIP = parameterIP serverIP = parameterIP
insecure, err := cmd.Flags().GetBool("insecure")
if insecure == true {
clientInsecure = true
}
return nil return nil
} }

View File

@ -37,17 +37,35 @@ func (m Message) toSend() {
} }
var ( var (
serverPort string = "1302" serverPort string = "1302"
serverIP string serverIP string
data Message data Message
clientInsecure bool
) )
func Client() { func startSecureConnection() (net.Conn, error) {
//conn, err := net.Dial("tcp", serverIP+":"+serverPort)
conf := &tls.Config{ conf := &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,
} }
conn, err := tls.Dial("tcp", serverIP+":"+serverPort, conf) 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 { if err != nil {
log.Fatalf("Error occurred trying to connect to server: %v\n", err) log.Fatalf("Error occurred trying to connect to server: %v\n", err)
} }