fea: config: add tests

This commit is contained in:
Daniel Ding
2022-10-28 20:52:02 +08:00
parent a69383c919
commit 72d8316aaa
15 changed files with 209 additions and 228 deletions

View File

@@ -41,42 +41,26 @@ type Point struct {
PidFile string `json:"pid,omitempty"`
}
func DefaultPoint() *Point {
obj := &Point{
Alias: "",
Connection: "xx.openlan.net",
Network: "default",
Protocol: "tcp", // udp, kcp, tcp, tls, ws and wss etc.
Timeout: 60,
Log: Log{
File: "./point.log",
Verbose: libol.INFO,
},
Interface: Interface{
IPMtu: 1500,
Provider: "kernel",
Name: "",
Cost: 1000,
},
SaveFile: "./point.json",
RequestAddr: true,
Crypt: &Crypt{},
Cert: &Cert{},
Terminal: "on",
func (i *Interface) Correct() {
if i.Provider == "" {
i.Provider = "kernel"
}
if i.Cost == 0 {
i.Cost = 666
}
if i.IPMtu == 0 {
i.IPMtu = 1500
}
}
func (l *Log) Correct() {
if l.Verbose == 0 {
l.Verbose = libol.INFO
}
obj.Correct(nil)
return obj
}
func NewPoint() *Point {
obj := DefaultPoint()
p := &Point{
RequestAddr: true,
Crypt: obj.Crypt,
Cert: obj.Cert,
Interface: obj.Interface,
}
p.Flags()
p := &Point{RequestAddr: true}
p.Parse()
if p.Terminal == "off" {
log.SetFlags(0)
@@ -85,20 +69,11 @@ func NewPoint() *Point {
return p
}
func (ap *Point) Flags() {
obj := DefaultPoint()
flag.StringVar(&ap.Alias, "alias", obj.Alias, "Alias for this point")
flag.StringVar(&ap.Terminal, "terminal", obj.Terminal, "Run interactive terminal")
flag.StringVar(&ap.Connection, "conn", obj.Connection, "Connection access to")
flag.StringVar(&ap.Log.File, "log:file", obj.Log.File, "File log saved to")
flag.IntVar(&ap.Log.Verbose, "log:level", obj.Log.Verbose, "Log level value")
flag.StringVar(&ap.SaveFile, "conf", obj.SaveFile, "The configuration file")
flag.StringVar(&ap.PProf, "pprof", obj.PProf, "Http listen for pprof debug")
flag.StringVar(&ap.Cert.CaFile, "cacert", obj.Cert.CaFile, "CA certificate file")
flag.StringVar(&ap.PidFile, "pid", obj.PidFile, "Write pid to file")
}
func (ap *Point) Parse() {
flag.StringVar(&ap.Alias, "alias", "", "Alias for this point")
flag.StringVar(&ap.Log.File, "log:file", "", "File log saved to")
flag.StringVar(&ap.Terminal, "terminal", "", "Run interactive terminal")
flag.StringVar(&ap.SaveFile, "conf", "", "The configuration file")
flag.Parse()
}
@@ -110,58 +85,50 @@ func (ap *Point) Initialize() {
if err := ap.Load(); err != nil {
libol.Warn("NewPoint.Initialize %s", err)
}
ap.Default()
ap.Correct()
libol.SetLogger(ap.Log.File, ap.Log.Verbose)
}
func (ap *Point) Correct(obj *Point) {
func (ap *Point) Correct() {
if ap.Alias == "" {
ap.Alias = GetAlias()
}
if ap.Network == "" {
if strings.Contains(ap.Username, "@") {
ap.Network = strings.SplitN(ap.Username, "@", 2)[1]
} else if obj != nil {
ap.Network = obj.Network
}
}
CorrectAddr(&ap.Connection, 10002)
if runtime.GOOS == "darwin" {
ap.Interface.Provider = "tun"
}
if ap.Terminal == "" {
ap.Terminal = "on"
}
if ap.Protocol == "tls" || ap.Protocol == "wss" {
if ap.Cert == nil && obj != nil {
ap.Cert = obj.Cert
if ap.Cert == nil {
ap.Cert = &Cert{}
}
}
if ap.Protocol == "" && obj != nil {
ap.Protocol = obj.Protocol
if ap.Protocol == "" {
ap.Protocol = "tcp"
}
if ap.Cert != nil {
ap.Cert.Correct()
}
if ap.Timeout == 0 && obj != nil {
ap.Timeout = obj.Timeout
if ap.Crypt == nil {
ap.Crypt = &Crypt{}
}
if ap.Interface.Cost == 0 && obj != nil {
ap.Interface.Cost = obj.Interface.Cost
ap.Crypt.Correct()
if ap.Timeout == 0 {
ap.Timeout = 60
}
if ap.Interface.IPMtu == 0 && obj != nil {
ap.Interface.IPMtu = obj.Interface.IPMtu
}
}
func (ap *Point) Default() {
obj := DefaultPoint()
ap.Correct(obj)
ap.Interface.Correct()
ap.Log.Correct()
if ap.Queue == nil {
ap.Queue = &Queue{}
}
ap.Queue.Default()
//reset zero value to default
if ap.Crypt != nil {
ap.Crypt.Default()
}
ap.Queue.Correct()
}
func (ap *Point) Load() error {