-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
80 lines (62 loc) · 1.58 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
package main
import (
"errors"
"flag"
"math"
"net"
)
type Config struct {
strLA string
strSA string
un []byte
pwd []byte
unL int
pwdL int
useAuth bool
la *net.TCPAddr
sa *net.TCPAddr
debug bool
info bool
}
func NewConfig() (*Config, error) {
var (
err error
conf *Config
u string
p string
)
conf = &Config{}
flag.StringVar(&conf.strLA, "la", "", "local address, like :5678")
flag.StringVar(&conf.strSA, "sa", "", "server address")
flag.StringVar(&u, "un", "", "username")
flag.StringVar(&p, "pwd", "", "password")
flag.BoolVar(&conf.debug, "d", false, "print debug informations")
flag.BoolVar(&conf.info, "i", false, "print info messages")
flag.Parse()
if conf.strLA == "" {
return nil, errors.New("local address was missing")
}
if conf.strSA == "" {
return nil, errors.New("server address was missing")
}
if u != "" || p != "" {
conf.useAuth = true
}
// the length of username and password must little or equal then MaxUint8
// see https://tools.ietf.org/html/rfc1929
if conf.unL = len(u); conf.unL > math.MaxUint8 {
return nil, errors.New("too large username")
}
conf.un = []byte(u)
if conf.pwdL = len(p); conf.pwdL > math.MaxUint8 {
return nil, errors.New("too large passowrd")
}
conf.pwd = []byte(p)
if conf.la, err = net.ResolveTCPAddr("tcp", conf.strLA); err != nil {
return nil, errors.New("invalid local address: " + err.Error())
}
if conf.sa, err = net.ResolveTCPAddr("tcp", conf.strSA); err != nil {
return nil, errors.New("invalid server address: " + err.Error())
}
return conf, nil
}