Add Global Settings into DB initilization step

This commit is contained in:
Khanh Ngo 2020-04-23 23:40:44 +07:00
parent 38c1f3a302
commit 5e7cfbd01f
No known key found for this signature in database
GPG key ID: D5FAA6A16150E49E
4 changed files with 31 additions and 3 deletions

View file

@ -269,7 +269,8 @@ func MachineIPAddresses() echo.HandlerFunc {
if err != nil {
log.Warn("Cannot get machine public ip address: ", err)
} else {
interfaceList = append(interfaceList, publicInterface)
// prepend public ip to the list
interfaceList = append([]model.Interface{publicInterface}, interfaceList...)
}
return c.JSON(http.StatusOK, interfaceList)

View file

@ -137,7 +137,7 @@ Global Settings
$.each(data, function(index, item) {
$("#ip_suggestion").append(
$("<option></option>")
.text(item.ip_address + ' on ' + item.name)
.text(item.ip_address + ' - ' + item.name)
.val(item.ip_address)
);
});

View file

@ -17,6 +17,10 @@ import (
const dbPath = "./db"
const defaultServerAddress = "10.252.1.0/24"
const defaultServerPort = 51820
const defaultDNS = "1.1.1.1"
const defaultMTU = 1450
const defaultPersistentKeepalive = 15
const defaultConfigFilePath = "/etc/wireguard/wg0.conf"
// DBConn to initialize the database connection
func DBConn() (*scribble.Driver, error) {
@ -33,6 +37,7 @@ func InitDB() error {
var serverPath string = path.Join(dbPath, "server")
var serverInterfacePath string = path.Join(serverPath, "interfaces.json")
var serverKeyPairPath string = path.Join(serverPath, "keypair.json")
var globalSettingPath string = path.Join(serverPath, "global_settings.json")
// create directories if they do not exist
if _, err := os.Stat(clientPath); os.IsNotExist(err) {
@ -74,6 +79,28 @@ func InitDB() error {
db.Write("server", "keypair", serverKeyPair)
}
// global settings
if _, err := os.Stat(globalSettingPath); os.IsNotExist(err) {
db, err := DBConn()
if err != nil {
return err
}
publicInterface, err := GetPublicIP()
if err != nil {
return err
}
globalSetting := new(model.GlobalSetting)
globalSetting.EndpointAddress = fmt.Sprintf("%s:%d", publicInterface.IPAddress, defaultServerPort)
globalSetting.DNSServers = []string{defaultDNS}
globalSetting.MTU = defaultMTU
globalSetting.PersistentKeepalive = defaultPersistentKeepalive
globalSetting.ConfigFilePath = defaultConfigFilePath
globalSetting.UpdatedAt = time.Now().UTC()
db.Write("server", "global_settings", globalSetting)
}
return nil
}

View file

@ -149,7 +149,7 @@ func GetPublicIP() (model.Interface, error) {
consensus.AddVoter(externalip.NewHTTPSource("http://ifconfig.top"), 1)
publicInterface := model.Interface{}
publicInterface.Name = "Public"
publicInterface.Name = "Public Address"
ip, err := consensus.ExternalIP()
if err != nil {