-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.go
More file actions
50 lines (44 loc) · 928 Bytes
/
utils.go
File metadata and controls
50 lines (44 loc) · 928 Bytes
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
45
46
47
48
49
50
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
)
// IntToHex converts an int64 to a byte array
func IntToHex(num int64) []byte {
buff := new(bytes.Buffer)
err := binary.Write(buff, binary.BigEndian, num)
if err != nil {
log.Panic(err)
}
return buff.Bytes()
}
// ReverseBytes reverses a byte array
func ReverseBytes(data []byte) {
for i, j := 0, len(data)-1; i < j; i, j = i+1, j-1 {
data[i], data[j] = data[j], data[i]
}
}
func GetExternalIp() string {
resp, err := http.Get("http://myexternalip.com/raw")
if err != nil {
return ""
}
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
return string(content)
}
func GetInternalIp() string {
conn, err := net.Dial("udp", "google.com:80")
if err != nil {
fmt.Println(err.Error())
log.Panic(err)
}
defer conn.Close()
return strings.Split(conn.LocalAddr().String(), ":")[0]
}