Get Endpoint Address for suggestion form

This commit is contained in:
Khanh Ngo 2020-04-20 16:50:50 +07:00
parent deecd9c267
commit 85e466698f
No known key found for this signature in database
GPG key ID: D5FAA6A16150E49E
7 changed files with 164 additions and 2 deletions

1
go.mod
View file

@ -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

2
go.sum
View file

@ -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=

View file

@ -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)
}
}

View file

@ -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"))
}

7
model/misc.go Normal file
View file

@ -0,0 +1,7 @@
package model
// Interface model
type Interface struct {
Name string `json:"name"`
IPAddress string `json:"ip_address"`
}

View file

@ -27,8 +27,15 @@ Global Settings
<div class="card-body">
<div class="form-group">
<label for="endpoint_address">Endpoint Address</label>
<input type="text" class="form-control" id="endpoint_address" name="endpoint_address" placeholder="Endpoint Address"
value="{{ .globalSettings.EndpointAddress }}">
<div class="input-group input-group">
<input type="text" class="form-control" id="endpoint_address" name="endpoint_address" placeholder="Endpoint Address"
value="{{ .globalSettings.EndpointAddress }}">
<span class="input-group-append">
<button type="button" class="btn btn-success btn-flat" data-toggle="modal"
data-target="#modal_endpoint_address_suggestion"><i
class="nav-icon fas fa-magic"></i> Suggest</button>
</span>
</div>
</div>
<div class="form-group">
<label for="dns_servers" class="control-label">DNS Servers</label>
@ -60,6 +67,31 @@ Global Settings
</div>
</section>
<div class="modal fade" id="modal_endpoint_address_suggestion">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Endpoint Address Suggestion</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>Following is the list of public and local IP addresses for your consideration.</p>
<select id="ip_suggestion" class="select2"
data-placeholder="Select an IP address" style="width: 100%;">
</select>
</div>
<div class="modal-footer justify-content-between">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" id="btn_use_ip" disabled>Use selected IP address</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<!-- /.modal -->
{{end}}
{{define "bottom_js"}}
@ -88,6 +120,20 @@ Global Settings
}
});
}
function updateEndpointSuggestionIP() {
$.getJSON("/api/machine-ips", null, function(data) {
$("#ip_suggestion option").remove();
$.each(data, function(index, item) {
$("#ip_suggestion").append(
$("<option></option>")
.text(item.ip_address + ' on ' + item.name)
.val(item.ip_address)
);
});
document.getElementById("btn_use_ip").disabled = false;
});
}
</script>
<script>
// Wireguard Interface DNS server tag input
@ -150,5 +196,21 @@ Global Settings
}
});
});
// Endpoint IP suggestion modal event
$(document).ready(function () {
$('#modal_endpoint_address_suggestion').on('shown.bs.modal', function (e) {
updateEndpointSuggestionIP();
});
});
// Use selected IP address from suggestion form
$(document).ready(function () {
$('#btn_use_ip').click(function () {
var ip = $('#ip_suggestion').select2('val');
$('#endpoint_address').val(ip);
$('#modal_endpoint_address_suggestion').modal('hide');
});
});
</script>
{{end}}

View file

@ -4,7 +4,9 @@ import (
"fmt"
"net"
"strings"
"time"
externalip "github.com/glendc/go-external-ip"
"github.com/ngoduykhanh/wireguard-ui/model"
)
@ -87,3 +89,68 @@ func ValidateIPAddressList(ips []string) bool {
}
return true
}
// GetInterfaceIPs to get local machine's interface ip addresses
func GetInterfaceIPs() ([]model.Interface, error) {
// get machine's interfaces
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
var interfaceList = []model.Interface{}
// get interface's ip addresses
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue
}
iface := model.Interface{}
iface.Name = i.Name
iface.IPAddress = ip.String()
interfaceList = append(interfaceList, iface)
}
}
return interfaceList, err
}
// GetPublicIP to get machine's public ip address
func GetPublicIP() (model.Interface, error) {
// set time out to 5 seconds
cfg := externalip.ConsensusConfig{}
cfg.Timeout = time.Second * 5
consensus := externalip.NewConsensus(&cfg, nil)
// add trusted voters
consensus.AddVoter(externalip.NewHTTPSource("http://checkip.amazonaws.com/"), 1)
consensus.AddVoter(externalip.NewHTTPSource("http://whatismyip.akamai.com"), 1)
consensus.AddVoter(externalip.NewHTTPSource("http://ifconfig.top"), 1)
publicInterface := model.Interface{}
publicInterface.Name = "Public"
ip, err := consensus.ExternalIP()
if err != nil {
publicInterface.IPAddress = "N/A"
}
publicInterface.IPAddress = ip.String()
return publicInterface, err
}