-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
103 lines (87 loc) · 2.19 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
package talon_access_proxy
import (
"encoding/hex"
"errors"
"fmt"
"net/url"
"github.com/asaskevich/govalidator"
"go.uber.org/zap"
)
// Config contains settings for the proxy
type Config struct {
// TalonAPI is the URL to use
TalonAPI string
talonAPIUrl url.URL
// DNSServer to use for dns lookups (Default is 8.8.8.8:53)
DNSServer string
// MaxConnections to use
MaxConnections int
// Application ID
Application map[string]*ApplicationConfig
// Logger to write data to
Logger *zap.Logger
}
type ApplicationConfig struct {
// Calculate HMAC
CalculateHMAC bool
// Application Key
ApplicationKey string
applicationKeyBytes []byte
// ApplicationToken to use
ApplicationToken string
}
// SetDefaults validates and sets defaults for Config
func (config *Config) SetDefaults() error {
u, err := url.Parse(config.TalonAPI)
if err != nil {
return fmt.Errorf("Unable to parse TalonAPI: %s", err.Error())
}
config.talonAPIUrl = *u
if len(config.talonAPIUrl.Scheme) <= 0 {
config.talonAPIUrl.Scheme = "https"
}
if len(config.DNSServer) <= 0 {
config.DNSServer = "8.8.8.8:53"
}
if config.MaxConnections < 0 {
config.MaxConnections = 0
}
for id, key := range config.Application {
var err error
config.Application[id].applicationKeyBytes, err = hex.DecodeString(key.ApplicationKey)
if err != nil {
return fmt.Errorf("ApplicationKey is invalid, (ApplicationID=%s)", id)
}
}
if err := config.createLogger(); err != nil {
return err
}
return config.testConfig()
}
func (config *Config) testConfig() error {
if len(config.TalonAPI) <= 0 {
return errors.New("TalonAPI is not set")
}
if !govalidator.IsDialString(config.DNSServer) {
return errors.New("DNSServer is invalid, must be in the form of host:port")
}
for _, key := range config.Application {
if key.CalculateHMAC {
if len(key.applicationKeyBytes) <= 0 {
return errors.New("ApplicationKey must be set if you want to use the CalculateHMAC function")
}
}
}
return nil
}
func (config *Config) createLogger() error {
// create a logger if there is none set
if config.Logger == nil {
var err error
config.Logger, err = zap.NewProduction()
if err != nil {
return err
}
}
return nil
}