/
util.go
137 lines (120 loc) · 2.46 KB
/
util.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package util
import (
"errors"
"math/rand"
"net"
"os"
"time"
"github.com/afjoseph/commongo/print"
"github.com/anacrolix/torrent/metainfo"
)
var ERR_DECODE_FILE = errors.New("ERR_DECODE_FILE")
var ERR_RAND_BYTESLICE = errors.New("ERR_RAND_BYTESLICE")
func Min(a, b int) int {
if a < b {
return a
}
return b
}
func DecodeTorrentFile(filePath string) (*metainfo.MetaInfo, error) {
print.DebugFunc()
// TODO: This is a cop-out but it works for now
mi, err := metainfo.LoadFromFile(filePath)
if err != nil {
return nil, print.ErrorWrapf(ERR_DECODE_FILE, err.Error())
}
return mi, nil
}
func RandRange(min, max int) int {
return rand.Intn(max-min) + max
}
func RandByteSlice(max int) ([]byte, error) {
b := make([]byte, max)
_, err := rand.Read(b)
if err != nil {
return nil, print.ErrorWrapf(ERR_RAND_BYTESLICE, err.Error())
}
return b, nil
}
func GetIpAndPortFromNetAddr(addr net.Addr) (net.IP, int) {
var ip net.IP
var port int
switch typedAddr := addr.(type) {
case *net.UDPAddr:
ip = typedAddr.IP
port = typedAddr.Port
case *net.TCPAddr:
ip = typedAddr.IP
port = typedAddr.Port
}
return ip, port
}
func IsFile(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return !fileInfo.IsDir()
}
func IsDirectory(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
return false
}
return fileInfo.IsDir()
}
func MkdirTempOrPanic(dir, pattern string) string {
tmpdir, err := os.MkdirTemp(dir, pattern)
if err != nil {
panic(err)
}
return tmpdir
}
func MkdirAllOrPanic(dir string) {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
panic(err)
}
}
func IntSliceHas(haystack []int, needle int) bool {
for _, k := range haystack {
if needle == k {
return true
}
}
return false
}
func IntSliceAppendIfUnique(slice []int, elem int) []int {
didFind := false
for _, v := range slice {
if v == elem {
didFind = true
break
}
}
if !didFind {
slice = append(slice, elem)
}
return slice
}
func IntSliceUniqueMergeSlices(sliceA, sliceB []int) []int {
mergedSlice := []int{}
// 2,3,4,5,6,10,20
// 10,20,30,40
for _, v := range sliceA {
mergedSlice = IntSliceAppendIfUnique(mergedSlice, v)
}
for _, v := range sliceB {
mergedSlice = IntSliceAppendIfUnique(mergedSlice, v)
}
return mergedSlice
}
func IsPortTaken(port string) bool {
conn, _ := net.DialTimeout("tcp",
net.JoinHostPort("", port), 1*time.Second)
if conn != nil {
conn.Close()
return true
}
return false
}