-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
179 lines (156 loc) · 4.5 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package p0d
import (
"fmt"
. "github.com/logrusorgru/aurora"
"github.com/shirou/gopsutil/mem"
"net"
"strconv"
"syscall"
"time"
)
func logv(args ...any) {
log("%v", args...)
}
func log(s string, args ...any) {
fmt.Printf(timefmt(s), args...)
}
func slog(s string, args ...any) {
time.Sleep(time.Millisecond * 125)
fmt.Printf(timefmt(s), args...)
}
func timefmt(s string) string {
now := time.Now().Format(time.Kitchen)
return fmt.Sprintf("%s %s\n", Gray(8, now), s)
}
func getUlimit() (string, int64) {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
return "not determined", 0
} else {
return fmt.Sprintf("%s",
Yellow(FGroup(int64(rLimit.Cur)))),
int64(rLimit.Cur)
}
}
func getRAMBytes() uint64 {
v, _ := mem.VirtualMemory()
return v.Total
}
func FGroup(n int64) string {
in := strconv.FormatInt(n, 10)
numOfDigits := len(in)
if n < 0 {
numOfDigits-- // First character is the - sign (not a digit)
}
numOfCommas := (numOfDigits - 1) / 3
out := make([]byte, len(in)+numOfCommas)
if n < 0 {
in, out[0] = in[1:], '-'
}
for i, j, k := len(in)-1, len(out)-1, 0; ; i, j = i-1, j-1 {
out[j] = in[i]
if i == 0 {
return string(out)
}
if k++; k == 3 {
j, k = j-1, 0
out[j] = ','
}
}
}
func ByteCountIEC(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%ciB",
float64(b)/float64(div), "KMGTPE"[exp])
}
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%dB", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f%cB",
float64(b)/float64(div), "kMGTPE"[exp])
}
func PrintLogo() (int, error) {
return fmt.Printf("%v",
Cyan("\n .╬╠╠╠╠╠╠╠╠╠╬`").String()+Red(" ╠╠ ,╬╠ ").String()+Cyan("╙╠╠╠╠╠╠╠╠╠φ").String()+
Cyan("\n ╠╠╠╙ ╠╠╠╙").String()+Red(" ╠╬ε └╠ε ╠╩ ╔#▒ ").String()+Cyan("`╠╠╠ ╙╠╠╠").String()+
Cyan("\n ╠╠╠ ]╠╠╩").String()+Red(" └╝▒ ╙Γ ╩ ╔╬╩` ").String()+Cyan("╠╠ε ╠╠╠").String()+
Cyan("\n ╠╠╠ε ╚╠╠").String()+Red(" ╝╠▒╗, \"╓δΘ╦\",╓φ▒╬╠▒ε ").String()+Cyan("╚╠▒ ╠╠╠").String()+
Cyan("\n ╚╠╠╠╠╠╠╠").String()+Red(" ,╠ ").String()+Yellow("⬤ ").String()+Red(" ╩≈╔╓╓╓, ").String()+Cyan("]╠╠ ╠╠╠").String()+
Cyan("\n ╠╠╠⌐").String()+Red(" ²╠╠╝╩\"²╓∩⌠╙╠`φ, `╙╝╠ ").String()+Cyan("╠╠╠ ╠╠╠").String()+
Cyan("\n [╠╠╠ ").String()+Red(" ╓@╬ ╬ ╠ε ╙╠φ ").String()+Cyan(",╠╠Γ ╠╠╠").String()+
Cyan("\n ╠╠╠▒").String()+Red(" '╝╩\" .╬╠ ]╠ε ╚╩ ").String()+Cyan("╔╠╠╠ ╓╠╠╠").String()+
Cyan("\n ╚╠╠╠╠φ,").String()+Red(" ╠╩ ╠╛ ").String()+Cyan("φ╠╠╠╠╠╠╠╠╠╩").String()+
"\n")
}
func PrintVersion() {
fmt.Printf(Cyan("p0d %s\n").String(), Version)
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}
return false
}
type SpinnerAnim struct {
chars []string
index int
}
func NewSpinnerAnim() *SpinnerAnim {
return &SpinnerAnim{
chars: []string{"\\", "|", "/", "-"},
index: 0,
}
}
func (b *SpinnerAnim) Next() string {
c := b.chars[b.index]
b.index = (b.index + 1) % len(b.chars)
return c
}
var privateIPBlocks []*net.IPNet
func init() {
for _, cidr := range []string{
"127.0.0.0/8", // IPv4 loopback
"10.0.0.0/8", // RFC1918
"172.16.0.0/12", // RFC1918
"192.168.0.0/16", // RFC1918
"169.254.0.0/16", // RFC3927 link-local
"::1/128", // IPv6 loopback
"fe80::/10", // IPv6 link-local
"fc00::/7", // IPv6 unique local addr
} {
_, block, err := net.ParseCIDR(cidr)
if err != nil {
panic(fmt.Errorf("parse error on %q: %v", cidr, err))
}
privateIPBlocks = append(privateIPBlocks, block)
}
}
func isPrivateIP(ip net.IP) bool {
if ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
return true
}
for _, block := range privateIPBlocks {
if block.Contains(ip) {
return true
}
}
return false
}