diff --git a/go.mod b/go.mod index c2c9d91..b858250 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/ngoduykhanh/wireguard-ui go 1.14 require ( + github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd github.com/go-playground/universal-translator v0.17.0 // indirect github.com/jcelliott/lumber v0.0.0-20160324203708-dd349441af25 // indirect github.com/labstack/echo/v4 v4.1.16 diff --git a/go.sum b/go.sum index 803f7a8..dddb22a 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd h1:1BzxHapafGJd/XlpMvocLeDBin2EKn90gXv2AQt5sfo= +github.com/glendc/go-external-ip v0.0.0-20170425150139-139229dcdddd/go.mod h1:o9OoDQyE1WHvYVUH1FdFapy1/rCZHHq3O5wS4VA83ig= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= diff --git a/handler/routes.go b/handler/routes.go index 817e24f..bca9c77 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -281,3 +281,25 @@ func GlobalSettingSubmit() echo.HandlerFunc { return c.JSON(http.StatusOK, jsonHTTPResponse{true, "Updated global settings successfully"}) } } + +// MachineIPAddresses handler to get local interface ip addresses +func MachineIPAddresses() echo.HandlerFunc { + return func(c echo.Context) error { + // get private ip addresses + interfaceList, err := util.GetInterfaceIPs() + if err != nil { + return c.JSON(http.StatusInternalServerError, jsonHTTPResponse{false, "Cannot get machine ip addresses"}) + } + + // get public ip address + // TODO: Remove the go-external-ip dependency + publicInterface, err := util.GetPublicIP() + if err != nil { + log.Warn("Cannot get machine public ip address: ", err) + } else { + interfaceList = append(interfaceList, publicInterface) + } + + return c.JSON(http.StatusOK, interfaceList) + } +} diff --git a/main.go b/main.go index aa85b5d..9553c7f 100644 --- a/main.go +++ b/main.go @@ -16,5 +16,6 @@ func main() { app.POST("wg-server/keypair", handler.WireGuardServerKeyPair()) app.GET("/global-settings", handler.GlobalSettings()) app.POST("/global-settings", handler.GlobalSettingSubmit()) + app.GET("/api/machine-ips", handler.MachineIPAddresses()) app.Logger.Fatal(app.Start("127.0.0.1:5000")) } diff --git a/model/misc.go b/model/misc.go new file mode 100644 index 0000000..249cb9f --- /dev/null +++ b/model/misc.go @@ -0,0 +1,7 @@ +package model + +// Interface model +type Interface struct { + Name string `json:"name"` + IPAddress string `json:"ip_address"` +} diff --git a/templates/global_settings.html b/templates/global_settings.html index ed52490..2c4448b 100644 --- a/templates/global_settings.html +++ b/templates/global_settings.html @@ -27,8 +27,15 @@ Global Settings