-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (70 loc) · 2.35 KB
/
main.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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/icyflame/wireguard-configuration-generator/internal/confgen"
"github.com/icyflame/wireguard-configuration-generator/internal/configuration"
"github.com/icyflame/wireguard-configuration-generator/internal/keygen"
)
const (
ExitOK = iota
ExitErr
)
const (
DefaultDNSServer = "1.1.1.1"
)
func main() {
err, returnCode := _main()
if err != nil {
log.Fatal("exit with error: ", err)
}
os.Exit(returnCode)
}
func _main() (error, int) {
var configurationFile string
var keysBaseDirectory string
var confsBaseDirectory string
var dnsServer string
flag.StringVar(&configurationFile, "configuration-file", "", "configuration file for generating wireguard configurations")
flag.StringVar(&keysBaseDirectory, "keys-base-dir", "", "base directory for storing the private and public keys required for wireguard")
flag.StringVar(&confsBaseDirectory, "confs-base-dir", "", "base directory for storing the wireguard configurations")
flag.StringVar(&dnsServer, "dns-server", DefaultDNSServer, "DNS server to inject into all configuration files")
flag.Parse()
if configurationFile == "" || keysBaseDirectory == "" || confsBaseDirectory == "" {
flag.PrintDefaults()
return fmt.Errorf("insufficient arguments"), ExitErr
}
networkConfig, err := configuration.Read(configurationFile)
if err != nil {
return fmt.Errorf("could not read config file: %w", err), ExitErr
}
configValidator := &configuration.ConfigurationValidator{}
keyGenerator := &keygen.KeyGenerator{
Base: keysBaseDirectory,
}
wgConfigGenerator := &confgen.WireguardConfigurationGenerator{
PeerConfigFile: "./template-configurations/peer.conf",
KR: &keygen.KeyRetriever{
Base: keysBaseDirectory,
},
Base: confsBaseDirectory,
DNSServer: dnsServer,
}
for networkName, config := range networkConfig {
err := configValidator.Validate(config)
if err != nil {
return fmt.Errorf("configuration for network %s is invalid: %w", networkName, err), ExitErr
}
err = keyGenerator.GenerateKeys(networkName, config)
if err != nil {
return fmt.Errorf("could not generate all keys for %s: %w", networkName, err), ExitErr
}
err = wgConfigGenerator.Generate(networkName, config)
if err != nil {
return fmt.Errorf("could not generate all the configuration files for %s: %w", networkName, err), ExitErr
}
}
return nil, ExitOK
}