mirror of
https://github.com/ngoduykhanh/wireguard-ui.git
synced 2025-05-24 00:24:06 +03:00
Get Endpoint Address for suggestion form
This commit is contained in:
parent
deecd9c267
commit
85e466698f
7 changed files with 164 additions and 2 deletions
67
util/util.go
67
util/util.go
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue