forked from NXRSE/bank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
81 lines (66 loc) · 1.56 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"bufio"
"crypto/tls"
"fmt"
"log"
"net"
"os"
"github.com/bvnk/bank/configuration"
)
func runClient(mode string) {
fmt.Println("Go Banking Client\nWelcome")
// We create a loop which waits for inut on std io
fmt.Println("Running on " + CONN_HOST + ":" + CONN_PORT)
for {
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
// Check if we exit
if text == "exit\n" {
os.Exit(0)
}
message, err := sendToServer(text, mode)
if err != nil {
log.Fatal(err)
}
fmt.Println(message)
}
}
func sendToServer(text string, mode string) (message string, err error) {
switch mode {
case "tls":
// Connect to this socket
cert, err := tls.LoadX509KeyPair(configuration.ImportPath+"certs/client.pem", configuration.ImportPath+"certs/client.key")
if err != nil {
return "", err
}
config := tls.Config{Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true}
conn, err := tls.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT, &config)
if err != nil {
return "", err
}
// Send to socket
fmt.Fprintf(conn, text+"\n")
// Listen for reply
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return "", err
}
return message, nil
case "no-tls":
// Connect to this socket
conn, err := net.Dial(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
return "", err
}
// Send to socket
fmt.Fprintf(conn, text+"\n")
// Listen for reply
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
return "", err
}
return message, nil
}
return
}