-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
318 lines (261 loc) · 8.07 KB
/
install.sh
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
sudo apt update
apt list --upgradable
sudo apt install golang-go
sudo apt install net-tools
cat << 'EOF' > serverInfoV1.go
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
"net"
)
type SystemInfo struct {
IPs []string `json:"ips"`
TotalRAM uint64 `json:"total_ram_bytes"`
UsedRAM uint64 `json:"used_ram_bytes"`
CPUUsage float64 `json:"cpu_usage_percent"`
CPUCoreCount int `json:"cpu_core_count"`
ReceivedBytes uint64 `json:"received_bytes"`
TransmittedBytes uint64 `json:"transmitted_bytes"`
InstantReceivedBytes uint64 `json:"instant_received_bytes"`
InstantTransmittedBytes uint64 `json:"instant_transmitted_bytes"`
Xui string `json:"xui"`
Hysteria string `json:"hysteria"`
}
func getRAMInfo() (uint64, uint64, error) {
var stat syscall.Sysinfo_t
err := syscall.Sysinfo(&stat)
if err != nil {
return 0, 0, err
}
totalRamBytes := stat.Totalram * uint64(stat.Unit)
usedRamBytes := (stat.Totalram - stat.Freeram) * uint64(stat.Unit)
return totalRamBytes, usedRamBytes, nil
}
func getCPUCoreCount() (int, error) {
file, err := os.Open("/proc/cpuinfo")
if err != nil {
return 0, err
}
defer file.Close()
coreCount := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "processor") {
coreCount++
}
}
if err := scanner.Err(); err != nil {
return 0, err
}
return coreCount, nil
}
func getCPUUsage() (float64, error) {
idleTime1, totalTime1, err := readCPUStat()
if err != nil {
return 0, err
}
time.Sleep(500 * time.Millisecond)
idleTime2, totalTime2, err := readCPUStat()
if err != nil {
return 0, err
}
idleDelta := idleTime2 - idleTime1
totalDelta := totalTime2 - totalTime1
cpuUsage := 100.0 * (1.0 - float64(idleDelta)/float64(totalDelta))
return cpuUsage, nil
}
func readCPUStat() (idleTime, totalTime int64, err error) {
file, err := os.Open("/proc/stat")
if err != nil {
return 0, 0, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if !scanner.Scan() {
return 0, 0, fmt.Errorf("failed to read /proc/stat")
}
fields := strings.Fields(scanner.Text())
if len(fields) < 5 {
return 0, 0, fmt.Errorf("unexpected format in /proc/stat")
}
var total int64
for _, field := range fields[1:] {
val, err := strconv.ParseInt(field, 10, 64)
if err != nil {
return 0, 0, err
}
total += val
}
idle, err := strconv.ParseInt(fields[4], 10, 64)
if err != nil {
return 0, 0, err
}
return idle, total, nil
}
func getNetworkTraffic(interfaceName string) (uint64, uint64, error) {
file, err := os.Open("/proc/net/dev")
if err != nil {
return 0, 0, err
}
defer file.Close()
var receivedBytes, transmittedBytes uint64
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, interfaceName) {
fields := strings.Fields(line)
if len(fields) >= 10 {
received, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return 0, 0, err
}
transmitted, err := strconv.ParseUint(fields[9], 10, 64)
if err != nil {
return 0, 0, err
}
receivedBytes = received
transmittedBytes = transmitted
break
}
}
}
if err := scanner.Err(); err != nil {
return 0, 0, err
}
return receivedBytes, transmittedBytes, nil
}
func getInstantNetworkTraffic(interfaceName string, sampleDuration time.Duration) (uint64, uint64, error) {
receivedBytes1, transmittedBytes1, err := getNetworkTraffic(interfaceName)
if err != nil {
return 0, 0, err
}
time.Sleep(sampleDuration)
receivedBytes2, transmittedBytes2, err := getNetworkTraffic(interfaceName)
if err != nil {
return 0, 0, err
}
receivedDelta := receivedBytes2 - receivedBytes1
transmittedDelta := transmittedBytes2 - transmittedBytes1
return receivedDelta, transmittedDelta, nil
}
func handler(w http.ResponseWriter, r *http.Request) {
port := r.URL.Query().Get("port")
ipAddress := r.URL.Query().Get("ip")
ipEnterface := "eth0"
// Get a list of all interfaces.
interfaces, err := net.Interfaces()
if err != nil {
fmt.Println(err)
return
}
// Iterate over all interfaces and print their details.
for _, interf := range interfaces {
// Get all the addresses assigned to this interface.
addresses, err := interf.Addrs()
if err != nil {
fmt.Println(err)
continue
}
for _, addr := range addresses {
parts := strings.Split(addr.String(), "/")
ipAddress_ := parts[0]
if(ipAddress_ == ipAddress){
ipEnterface = interf.Name
}
}
fmt.Println()
}
// endddd
if _, err := strconv.Atoi(port); err != nil {
http.Error(w, "Invalid port", http.StatusBadRequest)
return
}
cmdString := fmt.Sprintf(`sudo netstat -anp | grep ':%s' | grep ESTABLISHED | awk '{print $5}' | cut -d':' -f1 | sort | uniq`, port)
out, err := exec.Command("bash", "-c", cmdString).Output()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
outputLines := strings.Split(string(out), "\n")
systemInfo := SystemInfo{}
for _, line := range outputLines {
if line != "" {
systemInfo.IPs = append(systemInfo.IPs, line)
}
}
systemInfo.TotalRAM, systemInfo.UsedRAM, err = getRAMInfo()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
systemInfo.CPUCoreCount, err = getCPUCoreCount()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
systemInfo.CPUUsage, err = getCPUUsage()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
systemInfo.ReceivedBytes, systemInfo.TransmittedBytes, err = getNetworkTraffic(ipEnterface)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sampleDuration := 1500 * time.Millisecond
systemInfo.InstantReceivedBytes, systemInfo.InstantTransmittedBytes, err = getInstantNetworkTraffic(ipEnterface, sampleDuration)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
outx, err := exec.Command("pgrep", "x-ui").Output()
if err == nil {
systemInfo.Xui = "ok"
fmt.Printf("x-ui running",outx)
}
outh, err := exec.Command("pgrep", "hysteria-server").Output()
if err == nil {
systemInfo.Hysteria = "ok"
fmt.Printf("hysteria running",outh)
}
jsonOutput, err := json.Marshal(systemInfo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, "%s", jsonOutput)
}
func main() {
http.HandleFunc("/netstat", handler)
fmt.Println("Server is running on port 2082...")
log.Fatal(http.ListenAndServe(":2082", nil))
}
EOF
cat << 'EOF' > /etc/systemd/system/serverInfoV1.service
[Unit]
Description=My Go App
[Service]
ExecStart=/usr/bin/go run /root/serverInfoV1.go
Restart=always
User=root
Group=root
Environment=PATH=/usr/bin:/usr/local/bin
Environment=OTHER_ENV_VARS=any_value
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl enable serverInfoV1.service
sudo systemctl stop serverInfoV1.service
sudo systemctl start serverInfoV1.service