-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
60 lines (49 loc) · 1.31 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
package main
import (
"encoding/json"
"errors"
"os"
)
// Config holds the configuration for the application
type Config struct {
Address string `json:"Address"`
ControlPortPassword string `json:"ControlPortPassword"`
Services []Service `json:"Services"`
LogFilePath string `json:"LogFilePath"`
}
// Service represents a hidden service that will be balanced
type Service struct {
PrivateKeyPath string `json:"PrivateKeyPath"`
BackendAddresses []string `json:"BackendAddresses"`
}
// isValid verifies the values in the config
func (c *Config) isValid() error {
if c.Address == "" {
return errors.New("missing address")
}
for _, onion := range c.Services {
if onion.PrivateKeyPath == "" {
return errors.New("missing private key path")
}
if len(onion.BackendAddresses) > 60 {
return errors.New("only a maximum of 60 backend instances is allowed")
}
}
return nil
}
// loadConfig returns a config object given the config file
func loadConfig(filename *string) (*Config, error) {
configFile, err := os.Open(*filename)
defer configFile.Close()
if err != nil {
return nil, err
}
var config Config
var jsonParser = json.NewDecoder(configFile)
jsonParser.Decode(&config)
err = config.isValid()
if err != nil {
return nil, err
}
return &config, nil
}