-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathalpine-client.sh
More file actions
181 lines (154 loc) · 5.08 KB
/
Copy pathalpine-client.sh
File metadata and controls
181 lines (154 loc) · 5.08 KB
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
#!/bin/bash
# Check if script is run as root
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# Install bc based on system package manager
if command -v apk > /dev/null; then
apk update && apk add bc
else
echo "It's not alpine."
exit 1
fi
# Stop existing service if running
rc-service ak_client stop
# Function to detect main network interface
get_main_interface() {
local interfaces=$(ip -o link show | \
awk -F': ' '$2 !~ /^((lo|docker|veth|br-|virbr|tun|vnet|wg|vmbr|dummy|gre|sit|vlan|lxc|lxd|warp|tap))/{print $2}' | \
grep -v '@')
local interface_count=$(echo "$interfaces" | wc -l)
# 格式化流量大小的函数
format_bytes() {
local bytes=$1
if [ $bytes -lt 1024 ]; then
echo "${bytes} B"
elif [ $bytes -lt 1048576 ]; then # 1024*1024
echo "$(echo "scale=2; $bytes/1024" | bc) KB"
elif [ $bytes -lt 1073741824 ]; then # 1024*1024*1024
echo "$(echo "scale=2; $bytes/1024/1024" | bc) MB"
elif [ $bytes -lt 1099511627776 ]; then # 1024*1024*1024*1024
echo "$(echo "scale=2; $bytes/1024/1024/1024" | bc) GB"
else
echo "$(echo "scale=2; $bytes/1024/1024/1024/1024" | bc) TB"
fi
}
# 显示网卡流量的函数
show_interface_traffic() {
local interface=$1
if [ -d "/sys/class/net/$interface" ]; then
local rx_bytes=$(cat /sys/class/net/$interface/statistics/rx_bytes)
local tx_bytes=$(cat /sys/class/net/$interface/statistics/tx_bytes)
echo " ↓ Received: $(format_bytes $rx_bytes)"
echo " ↑ Sent: $(format_bytes $tx_bytes)"
else
echo " 无法读取流量信息"
fi
}
# 如果没有找到合适的接口或有多个接口时显示所有可用接口
echo "所有可用的网卡接口:" >&2
echo "------------------------" >&2
local i=1
while read -r interface; do
echo "$i) $interface" >&2
show_interface_traffic "$interface" >&2
i=$((i+1))
done < <(ip -o link show | grep -v "lo:" | awk -F': ' '{print $2}')
echo "------------------------" >&2
while true; do
read -p "请选择网卡,如上方显示异常或没有需要的网卡,请直接填入网卡名: " selection
# 检查是否为数字
if [[ "$selection" =~ ^[0-9]+$ ]]; then
# 如果是数字,检查是否在有效范围内
selected_interface=$(ip -o link show | grep -v "lo:" | sed -n "${selection}p" | awk -F': ' '{print $2}')
if [ -n "$selected_interface" ]; then
echo "已选择网卡: $selected_interface" >&2
echo "$selected_interface"
break
else
echo "无效的选择,请重新输入" >&2
continue
fi
else
# 直接使用输入的网卡名
echo "已选择网卡: $selection" >&2
echo "$selection"
break
fi
done
}
# Check if all arguments are provided
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <auth_secret> <url> <name>"
echo "Example: $0 your_secret wss://api.123.321 HK-Akile"
exit 1
fi
# Get system architecture
ARCH=$(uname -m)
CLIENT_FILE="akile_client-linux-amd64"
# Set appropriate client file based on architecture
if [ "$ARCH" = "x86_64" ]; then
CLIENT_FILE="akile_client-linux-amd64"
elif [ "$ARCH" = "aarch64" ]; then
CLIENT_FILE="akile_client-linux-arm64"
elif [ "$ARCH" = "x86_64" ] && [ "$(uname -s)" = "Darwin" ]; then
CLIENT_FILE="akile_client-darwin-amd64"
else
echo "Unsupported architecture: $ARCH"
exit 1
fi
# Assign command line arguments to variables
auth_secret="$1"
url="$2"
monitor_name="$3"
# Get network interface
net_name=$(get_main_interface)
echo "Using network interface: $net_name"
# Create directory and change to it
mkdir -p /etc/ak_monitor/
cd /etc/ak_monitor/
# Download client
wget -O client https://github.com/akile-network/akile_monitor/releases/latest/download/$CLIENT_FILE
chmod 777 client
# Create openrc service file
cat > /etc/init.d/ak_client << 'EOF'
#!/sbin/openrc-run
# 定义服务的名称变量
name="AkileCloud Monitor Service"
# 定义服务的执行路径,将使用该路径启动服务程序
command="/etc/ak_monitor/client"
directory="/etc/ak_monitor"
command_background=true
pidfile="/run/ak_client.pid"
supervisor=supervise-daemon
respawn_delay=5
respawn_max=0
# 定义服务的启动参数
# command_args="--param1 value1 --param2 value2"
# 提供服务的描述信息
description="Custom service for ${name}"
# 定义服务的依赖关系
depend() {
# 指定该服务需要网络(net 服务)支持
need net
}
EOF
# Create client configuration
cat > /etc/ak_monitor/client.json << EOF
{
"auth_secret": "${auth_secret}",
"url": "${url}",
"net_name": "${net_name}",
"name": "${monitor_name}"
}
EOF
# Set proper permissions
chmod 644 /etc/ak_monitor/client.json
chmod 755 /etc/init.d/ak_client
# Add service to default runlevel
rc-update add ak_client default
# Start service
rc-service ak_client start
echo "Installation complete! Service status:"
rc-service ak_client status