forked from NXRSE/bank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
181 lines (157 loc) · 4.48 KB
/
server.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
"crypto/rand"
"crypto/tls"
"errors"
"fmt"
"net"
"strings"
"github.com/ksred/bank/accounts"
"github.com/ksred/bank/appauth"
"github.com/ksred/bank/configuration"
"github.com/ksred/bank/payments"
"github.com/ksred/bank/push"
)
var Config configuration.Configuration
func runServer(mode string) (message string, err error) {
// Load app config
Config, err := configuration.LoadConfig()
if err != nil {
return "", errors.New("server.runServer: " + err.Error())
}
// Set config in packages
accounts.SetConfig(&Config)
payments.SetConfig(&Config)
appauth.SetConfig(&Config)
push.SetConfig(&Config)
switch mode {
case "tls":
cert, err := tls.LoadX509KeyPair("certs/server.pem", "certs/server.key")
if err != nil {
return "", err
}
// Load config and generate seed
config := tls.Config{Certificates: []tls.Certificate{cert}, ClientAuth: tls.RequireAnyClientCert}
config.Rand = rand.Reader
// Listen for incoming connections.
l, err := tls.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT, &config)
if err != nil {
return "", err
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on secure " + CONN_HOST + ":" + CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
return "", err
}
// Handle connections in a new goroutine.
go handleTCPRequest(conn)
}
case "no-tls":
// Listen for incoming connections.
l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
if err != nil {
return "", err
}
// Close the listener when the application closes.
defer l.Close()
fmt.Println("Listening on unsecure " + CONN_HOST + ":" + CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
return "", err
}
// Handle connections in a new goroutine.
go handleTCPRequest(conn)
}
}
return
}
// Handles incoming requests.
func handleTCPRequest(conn net.Conn) (err error) {
// Make a buffer to hold incoming data.
buf := make([]byte, 1024)
// Read the incoming connection into the buffer.
_, err = conn.Read(buf)
if err != nil {
return err
}
s := string(buf[:])
// Process
result, err := processCommand(s)
// Convert response to text
textResponse := "1~" + result
if err != nil {
textResponse = "0~" + err.Error()
}
// Send a response back to person contacting us.
conn.Write([]byte(textResponse + "\n"))
// Close the connection when you're done with it.
conn.Close()
return
}
func processCommand(text string) (result string, err error) {
// Commands are received split by tilde (~)
// command~DATA
cleanText := strings.Replace(text, "\n", "", -1)
fmt.Printf("### %s ####\n", cleanText)
command := strings.Split(cleanText, "~")
// Check if we received a command
if len(command) == 0 {
fmt.Println("No command received")
return
}
// Remove null termination from data
command[len(command)-1] = string(bytes.Trim([]byte(command[len(command)-1]), "\x00"))
// Check application auth. This is always the first value, if no token a 0 is sent
isCreateAccount := (command[0] == "0" && command[1] == "acmt" && command[2] == "1")
isLogIn := (command[0] == "0" && command[1] == "appauth" && command[2] == "2")
isCreateUserPassword := (command[0] == "0" && command[1] == "appauth" && command[2] == "3")
if !isCreateAccount || !isLogIn || !isCreateUserPassword {
err := appauth.CheckToken(command[0])
if err != nil {
return "", errors.New("server.processCommand: " + err.Error())
}
}
switch command[1] {
case "appauth":
// Check "help"
if command[2] == "help" {
return "Format of appauth: appauth~userName~password", nil
}
result, err = appauth.ProcessAppAuth(command)
if err != nil {
return "", errors.New("server.processCommand: " + err.Error())
}
break
case "pain":
// Check "help"
if command[2] == "help" {
return "Format of PAIN transaction:\npain\npainType~senderAccountNumber@SenderBankNumber\nreceiverAccountNumber@ReceiverBankNumber\ntransactionAmount\n\nBank numbers may be left void if bank is local", nil
}
result, err = payments.ProcessPAIN(command)
if err != nil {
return "", errors.New("server.processCommand: " + err.Error())
}
case "camt":
case "acmt":
// Check "help"
if command[2] == "help" {
return "", nil // @TODO Help section
}
result, err = accounts.ProcessAccount(command)
case "remt":
case "reda":
case "pacs":
case "auth":
break
default:
return "No valid command received", nil
}
return
}