mirror of
https://github.com/tim-beatham/smegmesh.git
synced 2024-12-12 17:41:08 +01:00
7be11dbaa3
Removed a client's grpc endpoint value. Client's aren't publicly available so there is no need for a client's gRPC endpoint. Also changed a node ID's to their public key. A node id's public address is an issue for mobility of clients as their endpoint is subject to change
62 lines
1.0 KiB
Go
62 lines
1.0 KiB
Go
package lib
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net"
|
|
"net/http"
|
|
)
|
|
|
|
// GetOutboundIP: gets the oubound IP of this packet
|
|
func GetOutboundIP() (net.IP, error) {
|
|
conn, err := net.Dial("udp", "8.8.8.8:80")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
|
return localAddr.IP, nil
|
|
}
|
|
|
|
const IP_SERVICE = "https://api.ipify.org?format=json"
|
|
|
|
type IpResponse struct {
|
|
Ip string `json:"ip"`
|
|
}
|
|
|
|
func (i *IpResponse) GetIP() net.IP {
|
|
return net.ParseIP(i.Ip)
|
|
}
|
|
|
|
// GetPublicIP: get the nodes public IP address. For when a node is behind NAT
|
|
func GetPublicIP() (net.IP, error) {
|
|
req, err := http.NewRequest(http.MethodGet, IP_SERVICE, nil)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resBody, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var jsonResponse IpResponse
|
|
|
|
err = json.Unmarshal([]byte(resBody), &jsonResponse)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return jsonResponse.GetIP(), nil
|
|
}
|