-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
267 lines (244 loc) · 7.81 KB
/
console.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Copyright © 2018-2022 Sad Pencil
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"bufio"
"context"
"encoding/json"
"fmt"
"github.com/SadPencil/sdunetd/sdunet"
"github.com/SadPencil/sdunetd/setting"
"github.com/SadPencil/sdunetd/utils"
"golang.org/x/term"
"net"
"os"
"runtime"
"strconv"
"strings"
"syscall"
)
func cartman() {
settings := setting.NewSettings()
reader := bufio.NewReader(os.Stdin)
fmt.Println("To generate a config file, a few questions need to be answered. Leave it blank if you want the default answer in the bracket.")
var err error
for {
fmt.Println()
fmt.Println("Question 0. Are you ready? [Yes]")
ans, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
ans = strings.TrimSpace(ans)
if ans == "" {
fmt.Println("Cool. Let's get started.")
break
} else {
fmt.Println("Ah, god dammit. I told you that you can just LEAVE IT BLANK if you want the default answer. Now try again.")
}
}
for {
fmt.Println()
fmt.Println("Question 1. What's your username? []")
settings.Account.Username, err = reader.ReadString('\n')
if err != nil {
panic(err)
}
err = checkUsername(settings)
if err != nil {
fmt.Println(err)
} else {
break
}
}
for {
fmt.Println()
fmt.Println("Question 2. What's your password? []")
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
fmt.Println() // it's necessary to add a new line after user's input
if err != nil {
panic(err)
} else {
settings.Account.Password = string(bytePassword)
}
err = checkPassword(settings)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Great. Your password contains", fmt.Sprint(len(settings.Account.Password)), "characters.")
break
}
}
for {
fmt.Println()
fmt.Println("Question 3. What's the authentication server's ip address? [" + setting.DEFAULT_AUTH_SERVER + "]")
fmt.Println("Hint: You can also write down the server's FQDN if necessary. You may specify either an IPv4 or IPv6 server.")
fmt.Println("Hint: The authentication servers of SDU-Qingdao are [2001:250:5800:11::1] and 101.76.193.1.")
settings.Account.AuthServer, err = reader.ReadString('\n')
if err != nil {
panic(err)
}
err = checkAuthServer(settings)
if err != nil {
fmt.Println(err)
} else {
if strings.Count(settings.Account.AuthServer, ":") >= 2 {
fmt.Println("Hint: Add a pair of [] with the IPv6 address. Omit this hint if you have already done so.")
fmt.Println("Example 1 \t [2001:250:5800:11::1]")
fmt.Println("Example 2 \t [2001:250:5800:11::1]:8080")
}
break
}
}
for {
fmt.Println()
fmt.Println("Question 4. Does the authentication server use HTTP, or HTTPS? [" + setting.DEFAULT_AUTH_SCHEME + "]")
fmt.Println("Hint: The authentication servers of SDU-Qingdao use HTTP.")
settings.Account.Scheme, err = reader.ReadString('\n')
if err != nil {
panic(err)
}
err = checkScheme(settings)
if err != nil {
fmt.Println(err)
} else {
break
}
}
//for {
// fmt.Println()
// fmt.Println("Question 5. Do you want to log out the network when the program get terminated? [y/N]")
// yesOrNoStr, err := reader.ReadString('\n')
// if err != nil {
// panic(err)
// }
// yesOrNoStr = strings.ToLower(strings.TrimSpace(yesOrNoStr))
// if yesOrNoStr == "" || yesOrNoStr == "n" {
// settings.Control.LogoutWhenExit = false
// break
// } else if yesOrNoStr == "y" {
// settings.Control.LogoutWhenExit = true
// break
// } else {
// fmt.Println("All you need to do is to answer me yes or no. Don't be a pussy.")
// }
//}
if runtime.GOOS == "linux" {
for {
fmt.Println()
fmt.Println("Question 5. (Linux only)")
var ips []string
var interfaceStrings []string
{
var err error
var ip string
for {
var manager *sdunet.Manager
manager, err = getManager(context.Background(), settings)
if err != nil {
break
}
info, err := manager.GetUserInfo(context.Background())
if err != nil {
break
}
ip = info.ClientIP
break
}
if err != nil {
ip = ""
fmt.Println("Warning: Failed to connected to the authentication server.")
fmt.Println(err)
fmt.Println()
}
ips = append(ips, ip)
interfaceStrings = append(interfaceStrings, "")
fmt.Println("["+fmt.Sprint(len(ips)-1)+"]", "\t", ip, "\t", "[Auto detect]")
}
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, networkInterface := range interfaces {
ip, err := utils.GetIPv4FromInterface(networkInterface.Name)
if err == nil {
ips = append(ips, ip)
interfaceStrings = append(interfaceStrings, networkInterface.Name)
fmt.Println("["+fmt.Sprint(len(ips)-1)+"]", "\t", ip, "\t", networkInterface.Name)
}
}
// TODO: get an IPv4 and an IPv6 address from each interface
if len(ips) == 0 {
fmt.Println("There is not even a network interface with a valid IPv4 address.")
fmt.Println("Screw you guys, I'm going home.")
return
}
fmt.Println("Network interfaces are listed above. Which one is connected to the Portal network? [0]")
fmt.Println("Hint: It is recommended to choose auto detect. Only choose a specific network interface if you have multiple network interfaces.")
choice, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
choice = strings.TrimSpace(choice)
var choiceID int
if choice == "" {
choiceID = 0
} else {
choiceID, err = strconv.Atoi(choice)
if err != nil {
fmt.Println(err)
continue
}
}
if choiceID == 0 {
settings.Network.Interface = ""
settings.Network.StrictMode = false
} else if choiceID > 0 && choiceID < len(interfaceStrings) {
settings.Network.Interface = interfaceStrings[choiceID]
settings.Network.StrictMode = true
} else {
fmt.Println("Make a valid selection, please. If you are not sure, just hit the enter key to select [0]. You are a fucking asshole.")
continue
}
if err != nil {
fmt.Println(err)
} else {
break
}
}
}
{
fmt.Println()
fmt.Println("That's all the information needed. Please save it to a configuration file. Where to save the file? [" + setting.DEFAULT_CONFIG_FILENAME + "]")
fmt.Println("Hint: If the program doesn't have permission to write, it will crash.")
filename, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
filename = strings.TrimSpace(filename)
if filename == "" {
filename = setting.DEFAULT_CONFIG_FILENAME
}
f, err := os.Create(filename)
defer f.Close()
if err != nil {
fmt.Println(err)
} else {
jsonBytes, err := json.Marshal(settings)
if err != nil {
fmt.Println(err)
} else {
_, err := f.WriteString(string(jsonBytes))
if err != nil {
fmt.Println(err)
} else {
fmt.Println(`File saved. You may re-run the program with the "-c" flag. Example: sdunetd -c config.json`)
}
}
}
}
}