Files
op-packages/luci-app-ipsec-vpnd/root/usr/libexec/rpcd/ipsec-vpnd
T

405 lines
12 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
#
# rpcd backend for luci-app-ipsec-vpnd.
# Exposes one read-only ubus object method "sessions": parse `ipsec statusall`
# (stroke), merge the currently-online IKEv1 SA set with a persisted history of
# seen clients (router-side, survives browser/reboot), and return the combined
# list with an "online" flag per entry.
#
# rpcd plugin protocol: argv[1] = "list" | "call", argv[2] = method name when
# calling; the request JSON arrives on stdin. stdout carries one JSON document.
#
# Persisted state: one line per known client, TAB separated:
# key\tuser\tremote\tvip\tage\tlast_seen (key = "user|remote")
. /usr/share/libubox/jshn.sh
STATE_FILE="/etc/luci-app-ipsec-vpnd-sessions.state"
# Working copy for sessions(); kept in /tmp so an unchanged 5s poll doesn't keep
# rewriting the persistent state file on flash. Never share this path with the
# updown hook.
SESSIONS_TMP="/tmp/.luci-app-ipsec-vpnd-sessions.tmp"
LOCK_FILE="/tmp/.luci-app-ipsec-vpnd-sessions.lock"
# Best-effort advisory lock: serialises the read/merge/write against the updown
# hook. If flock is unavailable each writer still uses its own tmp, so the worst
# case is a lost update in a tiny race window, never a corrupted file.
flock_lock() {
command -v flock >/dev/null 2>&1 || return 0
exec 9>"$LOCK_FILE" 2>/dev/null || return 0
flock -w 5 9 2>/dev/null || return 0
}
flock_unlock() {
command -v flock >/dev/null 2>&1 || return 0
flock -u 9 2>/dev/null
exec 9>&- 2>/dev/null
}
list_methods() {
printf '%s\n' '{"sessions":{},"clear_history":{},"deps":{},"apply":{},"status":{}}'
}
json_err() {
json_init
json_add_int code 1
json_add_string error "$1"
json_dump
}
# Parse `ipsec statusall` from stdin into TAB-separated online rows:
# key\tuser\tremote\tvip\tage
parse_statusall() {
awk '
function getsa(l) {
if (match(l, /^[ \t]*[A-Za-z0-9_.-]+\[[0-9]+\]:/)) {
s = substr(l, RSTART, RLENGTH - 1)
sub(/^[ \t]*/, "", s)
return s
}
return ""
}
function getts(s) {
if (match(s, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\/[0-9]+/))
return substr(s, RSTART, RLENGTH)
return ""
}
BEGIN { n = 0; last = "" }
{
line = $0
if (match(line, /^[ \t]*[A-Za-z0-9_.-]+\[[0-9]+\]: ESTABLISHED /)) {
sa = getsa(line)
if (!(sa in est)) {
est[sa] = 1
order[++n] = sa
last = sa
s = index(line, "ESTABLISHED ") + 12
e = index(line, " ago,")
age[sa] = (e > s) ? substr(line, s, e - s) : ""
rest = ""
i = index(line, "...")
if (i > 0) rest = substr(line, i + 3)
ip = ""
if (match(rest, /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/))
ip = substr(rest, RSTART, RLENGTH)
if (ip == "") {
sub(/^[ \t]*/, "", rest)
j = index(rest, "[")
if (j > 0) rest = substr(rest, 1, j - 1)
ip = rest
}
remote[sa] = ip
# 远端 IKE ID:形如 192.168.1.100[testuser]。
# strongSwan 只有在 IKE ID 与 XAuth 用户名不同时才打印
# "Remote XAuth identity:",所以拿不到该行时可以用 IKE ID 兜底,
# 两者一致 —— 这样与 updown 钩子记录的 key 也能对上,
# 不会在客户端列表里出现同一客户端的两行。
if (match(rest, /\[[^][]*\]/)) {
id = substr(rest, RSTART + 1, RLENGTH - 2)
gsub(/^[ \t"'"'"']+|[ \t"'"'"']+$/, "", id)
gsub(/[[:cntrl:]]/, "", id)
if (id != "") remoteid[sa] = id
}
}
next
}
if (line ~ /[Xx][Aa]uth/ && index(line, "identity:") > 0) {
sa = getsa(line)
if (sa != "" && (sa in est) && !(sa in user)) {
v = substr(line, index(line, "identity:") + 9)
gsub(/^[ \t"'"'"']+|[ \t"'"'"']+$/, "", v)
# 用户名完全由客户端可控(任何能连到 UDP 500 的人)。控制字符
# 会把 TSV 行撕开、字段错位并被持久化到历史文件里,这里统一剔除。
gsub(/[[:cntrl:]]/, "", v)
if (v != "") user[sa] = v
}
next
}
# CHILD_SA traffic selector carrying the assigned /32 client address;
# strongSwan 6.x puts the pool /32 on the RIGHT (remote) side here.
if (index(line, "===") > 0 && line ~ /\/[0-9]+/) {
i = index(line, "===")
l = getts(substr(line, 1, i - 1))
r = getts(substr(line, i + 3))
ip = ""
if (r ~ /\/32$/) ip = substr(r, 1, length(r) - 3)
else if (l ~ /\/32$/) ip = substr(l, 1, length(l) - 3)
if (ip != "") {
sa = getsa(line)
if (!(sa in est)) sa = last
if ((sa in est) && !(sa in vip)) vip[sa] = ip
}
next
}
}
END {
for (k = 1; k <= n; k++) {
sa = order[k]
u = user[sa]; r = remote[sa]; v = vip[sa]; a = age[sa]
if (u == "") u = remoteid[sa]
if (u == "") u = "-"
if (r == "") r = "-"
printf "%s|%s\t%s\t%s\t%s\t%s\n", u, r, u, r, v, a
}
}'
}
sessions() {
local now="$(date +%s)"
local online_tmp="/tmp/.luci-app-ipsec-vpnd-online.tsv"
# ensure the history file exists so awk can always read it as first input
# 文件里有客户端用户名与公网 IP,一开始就建成 0600
if [ ! -f "$STATE_FILE" ]; then
( umask 077; : > "$STATE_FILE" )
fi
chmod 600 "$STATE_FILE" 2>/dev/null
# current online clients as TSV
/usr/sbin/ipsec statusall 2>/dev/null | parse_statusall > "$online_tmp"
# serialize read/merge/write with the updown hook
flock_lock
awk -F'\t' -v now="$now" -v MAXROWS=200 -v state_tmp="$SESSIONS_TMP" -v state="$STATE_FILE" '
function jesc(s) {
gsub(/\\/, "\\\\", s)
gsub(/"/, "\\\"", s)
return s
}
BEGIN { n = 0 }
FILENAME == state {
# history rows: key user remote vip age last_seen
# 字段数不对的行是旧版本被恶意用户名污染过的脏行,直接丢掉
if (NF != 6) next
key = $1
if (key == "") next
gsub(/[[:cntrl:]]/, "", key)
if (!(key in seen)) { seen[key] = 1; order[++n] = key }
hu[key] = $2; hr[key] = $3; hv[key] = $4; ha[key] = $5; hl[key] = $6
gsub(/[[:cntrl:]]/, "", hu[key]); gsub(/[[:cntrl:]]/, "", hr[key])
gsub(/[[:cntrl:]]/, "", hv[key]); gsub(/[[:cntrl:]]/, "", ha[key])
next
}
{
# online rows: key user remote vip age
if (NF != 5) next
key = $1
if (key == "") next
gsub(/[[:cntrl:]]/, "", key)
active[key] = 1
if (!(key in seen)) { seen[key] = 1; order[++n] = key }
hu[key] = $2; hr[key] = $3; hv[key] = $4; ha[key] = $5; hl[key] = now
gsub(/[[:cntrl:]]/, "", hu[key]); gsub(/[[:cntrl:]]/, "", hr[key])
gsub(/[[:cntrl:]]/, "", hv[key]); gsub(/[[:cntrl:]]/, "", ha[key])
}
END {
# build output order: online first (kept), then offline by last_seen desc
nc = 0
for (i = 1; i <= n; i++)
if (active[order[i]]) out[++nc] = order[i]
oc = 0
for (i = 1; i <= n; i++)
if (!active[order[i]]) off[++oc] = order[i]
for (i = 1; i <= oc; i++)
for (j = i + 1; j <= oc; j++)
if ((hl[off[j]] + 0) > (hl[off[i]] + 0)) {
t = off[i]; off[i] = off[j]; off[j] = t
}
for (i = 1; i <= oc; i++) out[++nc] = off[i]
# 历史上限:在线的永远保留,离线的按最近使用排序后只留前 MAX 条。
# 避免状态文件无限膨胀、每次轮询都把全部历史塞进 JSON。
if (nc > MAXROWS) nc = MAXROWS
# JSON response
printf "{\"code\":0,\"sessions\":["
first = 1
for (i = 1; i <= nc; i++) {
k = out[i]
if (!first) printf ","
first = 0
printf "{\"user\":\"%s\",\"remote\":\"%s\",\"vip\":\"%s\",\"age\":\"%s\",\"online\":%s",
jesc(hu[k]), jesc(hr[k]), jesc(hv[k]), jesc(ha[k]),
(active[k] ? "true" : "false")
# 在线但没拿到地址:CHILD_SA 还没装好,或解析不到 /32 选择器。
# 给前端一个明确标记,避免和"没有地址"混为一谈。
if (active[k] && hv[k] == "")
printf ",\"vip_pending\":true"
printf "}"
}
printf "]}\n"
# rewrite persisted history (drop online flag; recomputed each call)
# 只写保留下来的行,超出的旧记录就此淘汰。
for (i = 1; i <= nc; i++) {
k = out[i]
printf "%s\t%s\t%s\t%s\t%s\t%s\n", k, hu[k], hr[k], hv[k], ha[k], hl[k] > state_tmp
}
}' "$STATE_FILE" "$online_tmp"
rc=$?
rm -f "$online_tmp"
# rewrite the persisted history only when it actually changed, so an idle
# 5s poll doesn't keep re-writing the state file on flash.
if [ -s "$SESSIONS_TMP" ] && ! cmp -s "$SESSIONS_TMP" "$STATE_FILE"; then
mv "$SESSIONS_TMP" "$STATE_FILE"
fi
rm -f "$SESSIONS_TMP"
# 状态文件里有客户端用户名与公网 IP,权限收紧到 0600
chmod 600 "$STATE_FILE" 2>/dev/null
flock_unlock
return $rc
}
apply() {
# 把 UCI 配置渲染成 strongSwan 配置,然后按 enabled 启停服务。
# charon 由 strongSwan 原生的 `ipsec` 服务托管,本包不再自建服务实例。
local render_out render_rc enabled
render_out="$(/usr/lib/ipsec/luci-ipsec-vpnd-render 2>&1)"
render_rc=$?
if [ "$render_rc" -ne 0 ]; then
json_err "failed to render ipsec.conf: $render_out"
return 1
fi
if [ ! -x /etc/init.d/ipsec ]; then
json_err "strongSwan 的 ipsec 服务不存在(缺 strongswan-ipsec 包)"
return 1
fi
# enabled 是总开关:关掉时连服务一起停,否则页面上开关是"关"、
# 服务却还在跑,charon 也不会因为配置里没有 conn 就自己退出。
enabled="$(uci -q get ipsec-vpnd.ipsec.enabled)"
if [ "$enabled" = "1" ]; then
/etc/init.d/ipsec enable >/dev/null 2>&1
# 只有配置真的变了才重启服务:别的页面触发一次 apply 不应该
# 把正在跑的 VPN 连接踢掉。
if [ "$render_out" = "changed" ] || ! /etc/init.d/ipsec status >/dev/null 2>&1; then
/etc/init.d/ipsec restart >/dev/null 2>&1
fi
else
/etc/init.d/ipsec disable >/dev/null 2>&1
/etc/init.d/ipsec stop >/dev/null 2>&1
fi
json_init
json_add_int code 0
json_add_string message "applied"
json_dump
}
status() {
# 页面状态用:enabled 来自 UCI,running 来自系统 ipsec 服务。
# 两者分开报,前端才能区分"没启用"和"启用了但没跑起来"。
local enabled running=0
enabled="$(uci -q get ipsec-vpnd.ipsec.enabled)"
[ "$enabled" = "1" ] || enabled="0"
if [ -x /etc/init.d/ipsec ]; then
/etc/init.d/ipsec status >/dev/null 2>&1 && running=1
fi
json_init
json_add_int code 0
json_add_string enabled "$enabled"
json_add_int running "$running"
json_dump
}
deps() {
# 运行时依赖自检:文件类依赖 + 软件包安装状态。
# 结果给 LuCI 的依赖诊断面板用;缺失时 init 脚本也会拒绝启动。
local files_out files_rc pkgs_out pkgs_rc
files_out="$(/usr/lib/ipsec/luci-ipsec-vpnd-deps files)"
files_rc=$?
pkgs_out="$(/usr/lib/ipsec/luci-ipsec-vpnd-deps pkgs)"
pkgs_rc=$?
local ok=1
[ "$files_rc" -eq 0 ] || ok=0
[ "$pkgs_rc" -eq 0 ] || ok=0
json_init
json_add_int code 0
json_add_int ok "$ok"
# 包管理器名:安装命令要用对应的那个(apk / opkg)
local pkg_mgr="apk"
if ! command -v apk >/dev/null 2>&1 && command -v opkg >/dev/null 2>&1; then
pkg_mgr="opkg"
fi
json_add_string pkg_mgr "$pkg_mgr"
local oldifs="$IFS"
local line key rest st det
IFS='
'
json_add_array files
for line in $files_out; do
[ -z "$line" ] && continue
key="${line%%|*}"; rest="${line#*|}"; st="${rest%%|*}"; det="${rest#*|}"
json_add_object ""
json_add_string key "$key"
json_add_string status "$st"
json_add_string detail "$det"
json_close_object
done
json_close_array
json_add_array packages
for line in $pkgs_out; do
[ -z "$line" ] && continue
key="${line%%|*}"; rest="${line#*|}"; st="${rest%%|*}"; det="${rest#*|}"
json_add_object ""
json_add_string key "$key"
json_add_string status "$st"
json_add_string detail "$det"
json_close_object
done
json_close_array
IFS="$oldifs"
json_dump
}
clear_history() {
flock_lock
rm -f "$STATE_FILE" "$SESSIONS_TMP"
flock_unlock
json_init
json_add_int code 0
json_add_string message "cleared"
json_dump
}
main() {
case "$1" in
list)
list_methods
;;
call)
case "$2" in
sessions) sessions ;;
clear_history) clear_history ;;
deps) deps ;;
apply) apply ;;
status) status ;;
*) json_err "unknown method" ;;
esac
;;
*)
printf '%s\n' '{}'
;;
esac
}
# source-able for offline parser testing
[ -n "$IPSEC_VPND_SKIP_MAIN" ] || main "$@"