From 5e7cfbd01fb7ccd488cdceddd01eb3094aef3301 Mon Sep 17 00:00:00 2001 From: Khanh Ngo Date: Thu, 23 Apr 2020 23:40:44 +0700 Subject: [PATCH] Add Global Settings into DB initilization step --- handler/routes.go | 3 ++- templates/global_settings.html | 2 +- util/db.go | 27 +++++++++++++++++++++++++++ util/util.go | 2 +- 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/handler/routes.go b/handler/routes.go index 409dc48..78193e1 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -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) diff --git a/templates/global_settings.html b/templates/global_settings.html index e54bc22..946cea8 100644 --- a/templates/global_settings.html +++ b/templates/global_settings.html @@ -137,7 +137,7 @@ Global Settings $.each(data, function(index, item) { $("#ip_suggestion").append( $("") - .text(item.ip_address + ' on ' + item.name) + .text(item.ip_address + ' - ' + item.name) .val(item.ip_address) ); }); diff --git a/util/db.go b/util/db.go index 536e007..817d567 100644 --- a/util/db.go +++ b/util/db.go @@ -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 } diff --git a/util/util.go b/util/util.go index 507db29..a0dd0ac 100644 --- a/util/util.go +++ b/util/util.go @@ -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 {