Skip to content

Commit 5e7cfbd

Browse files
committed
Add Global Settings into DB initilization step
1 parent 38c1f3a commit 5e7cfbd

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

handler/routes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ func MachineIPAddresses() echo.HandlerFunc {
269269
if err != nil {
270270
log.Warn("Cannot get machine public ip address: ", err)
271271
} else {
272-
interfaceList = append(interfaceList, publicInterface)
272+
// prepend public ip to the list
273+
interfaceList = append([]model.Interface{publicInterface}, interfaceList...)
273274
}
274275

275276
return c.JSON(http.StatusOK, interfaceList)

templates/global_settings.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ <h4 class="modal-title">Endpoint Address Suggestion</h4>
137137
$.each(data, function(index, item) {
138138
$("#ip_suggestion").append(
139139
$("<option></option>")
140-
.text(item.ip_address + ' on ' + item.name)
140+
.text(item.ip_address + ' - ' + item.name)
141141
.val(item.ip_address)
142142
);
143143
});

util/db.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
const dbPath = "./db"
1818
const defaultServerAddress = "10.252.1.0/24"
1919
const defaultServerPort = 51820
20+
const defaultDNS = "1.1.1.1"
21+
const defaultMTU = 1450
22+
const defaultPersistentKeepalive = 15
23+
const defaultConfigFilePath = "/etc/wireguard/wg0.conf"
2024

2125
// DBConn to initialize the database connection
2226
func DBConn() (*scribble.Driver, error) {
@@ -33,6 +37,7 @@ func InitDB() error {
3337
var serverPath string = path.Join(dbPath, "server")
3438
var serverInterfacePath string = path.Join(serverPath, "interfaces.json")
3539
var serverKeyPairPath string = path.Join(serverPath, "keypair.json")
40+
var globalSettingPath string = path.Join(serverPath, "global_settings.json")
3641

3742
// create directories if they do not exist
3843
if _, err := os.Stat(clientPath); os.IsNotExist(err) {
@@ -74,6 +79,28 @@ func InitDB() error {
7479
db.Write("server", "keypair", serverKeyPair)
7580
}
7681

82+
// global settings
83+
if _, err := os.Stat(globalSettingPath); os.IsNotExist(err) {
84+
db, err := DBConn()
85+
if err != nil {
86+
return err
87+
}
88+
89+
publicInterface, err := GetPublicIP()
90+
if err != nil {
91+
return err
92+
}
93+
94+
globalSetting := new(model.GlobalSetting)
95+
globalSetting.EndpointAddress = fmt.Sprintf("%s:%d", publicInterface.IPAddress, defaultServerPort)
96+
globalSetting.DNSServers = []string{defaultDNS}
97+
globalSetting.MTU = defaultMTU
98+
globalSetting.PersistentKeepalive = defaultPersistentKeepalive
99+
globalSetting.ConfigFilePath = defaultConfigFilePath
100+
globalSetting.UpdatedAt = time.Now().UTC()
101+
db.Write("server", "global_settings", globalSetting)
102+
}
103+
77104
return nil
78105
}
79106

util/util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func GetPublicIP() (model.Interface, error) {
149149
consensus.AddVoter(externalip.NewHTTPSource("http://ifconfig.top"), 1)
150150

151151
publicInterface := model.Interface{}
152-
publicInterface.Name = "Public"
152+
publicInterface.Name = "Public Address"
153153

154154
ip, err := consensus.ExternalIP()
155155
if err != nil {

0 commit comments

Comments
 (0)