-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
65 lines (55 loc) · 1.65 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
package main
import (
"encoding/json"
"log"
"net"
"os"
"strings"
)
type Config struct {
APIToken string `json:"api_token"`
WireguardSubnet string `json:"wireguard_subnet"`
WireguardRelayServerPublicIP string `json:"wireguard_relay_server_public_ip"`
WireguardListenPort int `json:"wireguard_listen_port"`
WireguardPrivateKey string `json:"wireguard_private_key"`
WireguardPublicKey string `json:"wireguard_public_key"`
}
var config *Config
func loadConfig() {
// load from config.json
jsonFile, err := os.Open("config.json")
if err != nil {
log.Println("Failed to open config.json:", err)
}
defer jsonFile.Close()
json.NewDecoder(jsonFile).Decode(&config)
if config == nil {
config = &Config{
WireguardListenPort: 51820,
}
}
// write to config.json
jsonFile, err = os.Create("config.json")
if err != nil {
log.Println("Failed to create config.json:", err)
}
defer jsonFile.Close()
encoder := json.NewEncoder(jsonFile)
encoder.SetIndent("", " ")
encoder.Encode(config)
// check if config.json is empty
if config.APIToken == "" || config.WireguardSubnet == "" || config.WireguardRelayServerPublicIP == "" || config.WireguardListenPort == 0 || config.WireguardPrivateKey == "" || config.WireguardPublicKey == "" {
log.Println("Config.json is empty, please fill in the required fields")
os.Exit(1)
}
}
func (c *Config) GetRelayWireguardAddress() string {
return strings.Split(config.WireguardSubnet, "/")[0]
}
func (c *Config) GetWireguardClientSubnet() string {
_, ipnet, err := net.ParseCIDR(c.WireguardSubnet)
if err != nil {
return ""
}
return ipnet.String()
}