-
-
Notifications
You must be signed in to change notification settings - Fork 522
/
config.go
387 lines (368 loc) · 9 KB
/
config.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package config
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/adrg/xdg"
"github.com/asaskevich/govalidator"
"github.com/claudiodangelis/qrcp/util"
"github.com/manifoldco/promptui"
)
// Config of qrcp
type Config struct {
FQDN string `json:"fqdn"`
Interface string `json:"interface"`
Port int `json:"port"`
KeepAlive bool `json:"keepAlive"`
Path string `json:"path"`
Secure bool `json:"secure"`
TLSKey string `json:"tls-key"`
TLSCert string `json:"tls-cert"`
Output string `json:"output"`
}
var configFile string
// Options of the qrcp configuration
type Options struct {
Interface string
Port int
Path string
FQDN string
KeepAlive bool
Interactive bool
ListAllInterfaces bool
Secure bool
TLSCert string
TLSKey string
Output string
}
func chooseInterface(opts Options) (string, error) {
interfaces, err := util.Interfaces(opts.ListAllInterfaces)
if err != nil {
return "", err
}
if len(interfaces) == 0 {
return "", errors.New("no interfaces found")
}
if len(interfaces) == 1 && opts.Interactive == false {
for name := range interfaces {
fmt.Printf("only one interface found: %s, using this one\n", name)
return name, nil
}
}
// Map for pretty printing
m := make(map[string]string)
items := []string{}
for name, ip := range interfaces {
label := fmt.Sprintf("%s (%s)", name, ip)
m[label] = name
items = append(items, label)
}
// Add the "any" interface
anyIP := "0.0.0.0"
anyName := "any"
anyLabel := fmt.Sprintf("%s (%s)", anyName, anyIP)
m[anyLabel] = anyName
items = append(items, anyLabel)
prompt := promptui.Select{
Items: items,
Label: "Choose interface",
}
_, result, err := prompt.Run()
if err != nil {
return "", err
}
return m[result], nil
}
// Load a new configuration
func Load(opts Options) (Config, error) {
var cfg Config
// Read the configuration file, if it exists
if file, err := ioutil.ReadFile(configFile); err == nil {
// Read the config
if err := json.Unmarshal(file, &cfg); err != nil {
return cfg, err
}
}
// Prompt if needed
if cfg.Interface == "" {
iface, err := chooseInterface(opts)
if err != nil {
return cfg, err
}
cfg.Interface = iface
// Write config
if err := write(cfg); err != nil {
return cfg, err
}
}
return cfg, nil
}
// Wizard starts an interactive configuration managements
func Wizard(path string, listAllInterfaces bool) error {
if err := setConfigFile(path); err != nil {
return err
}
var cfg Config
if file, err := ioutil.ReadFile(configFile); err == nil {
// Read the config
if err := json.Unmarshal(file, &cfg); err != nil {
return err
}
}
// Ask for interface
opts := Options{
Interactive: true,
ListAllInterfaces: listAllInterfaces,
}
iface, err := chooseInterface(opts)
if err != nil {
return err
}
cfg.Interface = iface
// Ask for fully qualified domain name
validateFqdn := func(input string) error {
if input != "" && govalidator.IsDNSName(input) == false {
return errors.New("invalid domain")
}
return nil
}
promptFqdn := promptui.Prompt{
Validate: validateFqdn,
Label: "Choose fully-qualified domain name",
Default: "",
}
if promptFqdnString, err := promptFqdn.Run(); err == nil {
cfg.FQDN = promptFqdnString
}
// Ask for port
validatePort := func(input string) error {
_, err := strconv.ParseUint(input, 10, 16)
if err != nil {
return errors.New("Invalid number")
}
return nil
}
promptPort := promptui.Prompt{
Validate: validatePort,
Label: "Choose port, 0 means random port",
Default: fmt.Sprintf("%d", cfg.Port),
}
if promptPortResultString, err := promptPort.Run(); err == nil {
if port, err := strconv.ParseUint(promptPortResultString, 10, 16); err == nil {
cfg.Port = int(port)
}
}
validateIsDir := func(input string) error {
if input == "" {
return nil
}
path, err := filepath.Abs(input)
if err != nil {
return err
}
f, err := os.Stat(path)
if err != nil {
return err
}
if !f.IsDir() {
return errors.New("path is not a directory")
}
return nil
}
promptOutput := promptui.Prompt{
Label: "Choose default output directory for received files, empty does not set a default",
Default: cfg.Output,
Validate: validateIsDir,
}
if promptOutputResultString, err := promptOutput.Run(); err == nil {
if promptOutputResultString != "" {
p, _ := filepath.Abs(promptOutputResultString)
cfg.Output = p
}
}
// Ask for path
promptPath := promptui.Prompt{
Label: "Choose path, empty means random",
Default: cfg.Path,
}
if promptPathResultString, err := promptPath.Run(); err == nil {
if promptPathResultString != "" {
cfg.Path = promptPathResultString
}
}
// Ask for keep alive
promptKeepAlive := promptui.Select{
Items: []string{"No", "Yes"},
Label: "Should the server keep alive after transferring?",
}
if _, promptKeepAliveResultString, err := promptKeepAlive.Run(); err == nil {
if promptKeepAliveResultString == "Yes" {
cfg.KeepAlive = true
} else {
cfg.KeepAlive = false
}
}
// TLS
promptSecure := promptui.Select{
Items: []string{"No", "Yes"},
Label: "Should files be securely transferred with HTTPS?",
}
if _, promptSecureResultString, err := promptSecure.Run(); err == nil {
if promptSecureResultString == "Yes" {
cfg.Secure = true
} else {
cfg.Secure = false
}
}
pathIsReadable := func(input string) error {
if input == "" {
return nil
}
path, err := filepath.Abs(util.Expand(input))
if err != nil {
return err
}
fmt.Println(path)
fileinfo, err := os.Stat(path)
if err != nil {
return err
}
if fileinfo.Mode().IsDir() {
return fmt.Errorf(fmt.Sprintf("%s is a directory", input))
}
return nil
}
// TLS Cert
promptTLSCert := promptui.Prompt{
Label: "Choose TLS certificate path. Empty if not using HTTPS.",
Default: cfg.TLSCert,
Validate: pathIsReadable,
}
if promptTLSCertString, err := promptTLSCert.Run(); err == nil {
cfg.TLSCert = util.Expand(promptTLSCertString)
}
// TLS key
promptTLSKey := promptui.Prompt{
Label: "Choose TLS certificate key. Empty if not using HTTPS.",
Default: cfg.TLSKey,
Validate: pathIsReadable,
}
if promptTLSKeyString, err := promptTLSKey.Run(); err == nil {
cfg.TLSKey = util.Expand(promptTLSKeyString)
}
// Write it down
if err := write(cfg); err != nil {
return err
}
b, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
fmt.Printf("Configuration updated:\n%s\n", string(b))
return nil
}
// write the configuration file to disk
func write(cfg Config) error {
j, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
if err := ioutil.WriteFile(configFile, j, 0644); err != nil {
return err
}
return nil
}
func pathExists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func setConfigFile(path string) error {
// If not explicitly set then use the default
if path == "" {
// First try legacy location
var legacyConfigFile = filepath.Join(xdg.Home, ".qrcp.json")
if pathExists(legacyConfigFile) {
configFile = legacyConfigFile
return nil
}
// Else use modern location, first ensuring that the directory
// exists
var configDir = filepath.Join(xdg.ConfigHome, "qrcp")
if !pathExists(configDir) {
os.Mkdir(configDir, 0744)
}
configFile = filepath.Join(configDir, "config.json")
return nil
}
absolutepath, err := filepath.Abs(path)
if err != nil {
return err
}
fileinfo, err := os.Stat(absolutepath)
if err != nil && !os.IsNotExist(err) {
return err
}
if fileinfo != nil && fileinfo.IsDir() {
return fmt.Errorf("%s is not a file", absolutepath)
}
configFile = absolutepath
return nil
}
// New returns a new configuration struct. It loads defaults, then overrides
// values if any.
func New(path string, opts Options) (Config, error) {
var cfg Config
// Set configFile
if err := setConfigFile(path); err != nil {
return cfg, err
}
// Load saved file / defaults
cfg, err := Load(opts)
if err != nil {
return cfg, err
}
if opts.Interface != "" {
cfg.Interface = opts.Interface
}
if opts.FQDN != "" {
if govalidator.IsDNSName(opts.FQDN) == false {
return cfg, errors.New("invalid value for fully-qualified domain name")
}
cfg.FQDN = opts.FQDN
}
if opts.Port != 0 {
cfg.Port = opts.Port
} else if portVal, ok := os.LookupEnv("QRCP_PORT"); ok {
port, err := strconv.Atoi(portVal)
if err != nil {
return cfg, errors.New("could not parse port from environment variable QRCP_PORT")
}
cfg.Port = port
}
if cfg.Port != 0 && !govalidator.IsPort(fmt.Sprintf("%d", cfg.Port)) {
return cfg, fmt.Errorf("%d is not a valid port", cfg.Port)
}
if opts.KeepAlive {
cfg.KeepAlive = true
}
if opts.Path != "" {
cfg.Path = opts.Path
}
if opts.Secure {
cfg.Secure = true
}
if opts.TLSCert != "" {
cfg.TLSCert = opts.TLSCert
}
if opts.TLSKey != "" {
cfg.TLSKey = opts.TLSKey
}
if opts.Output != "" {
cfg.Output = opts.Output
}
return cfg, nil
}