-
Notifications
You must be signed in to change notification settings - Fork 2
/
utility.go
44 lines (37 loc) · 867 Bytes
/
utility.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package utility
import (
"crypto/sha256"
"encoding/binary"
"log"
"math"
"net"
)
/*
***************************************
UTILITY FUNCTIONS FOR MAIN
***************************************
*/
const M = 32
/*
Function to generate the hash of the the input IP address
*/
func GenerateHash(input string) uint64 {
data := []byte(input)
id := sha256.Sum256(data)
unmoddedID := float64(binary.BigEndian.Uint64(id[:8]))
modValue := float64(math.Pow(2, M))
moddedID := math.Mod(unmoddedID, modValue)
return uint64(moddedID)
}
/*
Function to automatically get the outbound IP without user input in .env file
*/
func GetOutboundIP() net.IP {
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
}