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)
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
}

View File

@ -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)
}