Skip to content

Commit

Permalink
add file lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jhxu committed May 20, 2022
1 parent dd6b803 commit 51ae04a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 68 deletions.
48 changes: 24 additions & 24 deletions luasrc/controller/vssr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ function index()
return
end

local page = entry({'admin', 'services', 'vssr'}, alias('admin', 'services', 'vssr', 'client'), _('Hello World'), 0) -- 首页
local page = entry({ 'admin', 'services', 'vssr' }, alias('admin', 'services', 'vssr', 'client'), _('Hello World'), 0) -- 首页
page.dependent = true
page.acl_depends = { "luci-app-vssr" }

entry({'admin', 'services', 'vssr', 'client'}, cbi('vssr/client'), _('SSR Client'), 10).leaf = true -- 基本设置
entry({'admin', 'services', 'vssr', 'servers'}, cbi('vssr/servers'), _('Severs Nodes'), 11).leaf = true -- 服务器节点
entry({'admin', 'services', 'vssr', 'servers'}, arcombine(cbi('vssr/servers'), cbi('vssr/client-config')), _('Severs Nodes'), 11).leaf = true -- 编辑节点
entry({'admin', 'services', 'vssr', 'subscribe_config'}, cbi('vssr/subscribe-config', {hideapplybtn = true, hidesavebtn = true, hideresetbtn = true}), _('Subscribe'), 12).leaf = true -- 订阅设置
entry({'admin', 'services', 'vssr', 'control'}, cbi('vssr/control'), _('Access Control'), 13).leaf = true -- 访问控制
entry({'admin', 'services', 'vssr', 'router'}, cbi('vssr/router'), _('Router Config'), 14).leaf = true -- 访问控制
entry({'admin', 'services', 'vssr', 'socks5'}, cbi('vssr/socks5'), _('Local Proxy'), 15).leaf = true -- Socks5代理
entry({'admin', 'services', 'vssr', 'advanced'}, cbi('vssr/advanced'), _('Advanced Settings'), 16).leaf = true -- 高级设置
entry({'admin', 'services', 'vssr', 'server'}, arcombine(cbi('vssr/server'), cbi('vssr/server-config')), _('SSR Server'), 20).leaf = true -- 服务端
entry({'admin', 'services', 'vssr', 'log'}, cbi('vssr/log'), _('Log'), 30).leaf = true -- 日志
entry({ 'admin', 'services', 'vssr', 'client' }, cbi('vssr/client'), _('SSR Client'), 10).leaf = true -- 基本设置
entry({ 'admin', 'services', 'vssr', 'servers' }, cbi('vssr/servers'), _('Severs Nodes'), 11).leaf = true -- 服务器节点
entry({ 'admin', 'services', 'vssr', 'servers' }, arcombine(cbi('vssr/servers'), cbi('vssr/client-config')), _('Severs Nodes'), 11).leaf = true -- 编辑节点
entry({ 'admin', 'services', 'vssr', 'subscribe_config' }, cbi('vssr/subscribe-config', { hideapplybtn = true, hidesavebtn = true, hideresetbtn = true }), _('Subscribe'), 12).leaf = true -- 订阅设置
entry({ 'admin', 'services', 'vssr', 'control' }, cbi('vssr/control'), _('Access Control'), 13).leaf = true -- 访问控制
entry({ 'admin', 'services', 'vssr', 'router' }, cbi('vssr/router'), _('Router Config'), 14).leaf = true -- 访问控制
entry({ 'admin', 'services', 'vssr', 'socks5' }, cbi('vssr/socks5'), _('Local Proxy'), 15).leaf = true -- Socks5代理
entry({ 'admin', 'services', 'vssr', 'advanced' }, cbi('vssr/advanced'), _('Advanced Settings'), 16).leaf = true -- 高级设置
entry({ 'admin', 'services', 'vssr', 'server' }, arcombine(cbi('vssr/server'), cbi('vssr/server-config')), _('SSR Server'), 20).leaf = true -- 服务端
entry({ 'admin', 'services', 'vssr', 'log' }, cbi('vssr/log'), _('Log'), 30).leaf = true -- 日志
--entry({'admin', 'services', 'vssr', 'licence'}, template('vssr/licence'), _('Licence'), 40).leaf = true
entry({'admin', 'services', 'vssr', 'refresh'}, call('refresh_data')) -- 更新白名单和GFWLIST
entry({'admin', 'services', 'vssr', 'checkport'}, call('check_port')) -- 检测单个端口并返回Ping
entry({'admin', 'services', 'vssr', 'run'}, call('act_status')) -- 检测全局服务器状态
entry({'admin', 'services', 'vssr', 'change'}, call('change_node')) -- 切换节点
entry({'admin', 'services', 'vssr', 'allserver'}, call('get_servers')) -- 获取所有节点Json
entry({'admin', 'services', 'vssr', 'subscribe'}, call('get_subscribe')) -- 执行订阅
entry({'admin', 'services', 'vssr', 'flag'}, call('get_flag')) -- 获取节点国旗 iso code
entry({'admin', 'services', 'vssr', 'ip'}, call('check_ip')) -- 获取ip情况
entry({'admin', 'services', 'vssr', 'switch'}, call('switch')) -- 设置节点为自动切换
entry({'admin', 'services', 'vssr', 'delnode'}, call('del_node')) -- 删除某个节点
entry({ 'admin', 'services', 'vssr', 'refresh' }, call('refresh_data')) -- 更新白名单和GFWLIST
entry({ 'admin', 'services', 'vssr', 'checkport' }, call('check_port')) -- 检测单个端口并返回Ping
entry({ 'admin', 'services', 'vssr', 'run' }, call('act_status')) -- 检测全局服务器状态
entry({ 'admin', 'services', 'vssr', 'change' }, call('change_node')) -- 切换节点
entry({ 'admin', 'services', 'vssr', 'allserver' }, call('get_servers')) -- 获取所有节点Json
entry({ 'admin', 'services', 'vssr', 'subscribe' }, call('get_subscribe')) -- 执行订阅
entry({ 'admin', 'services', 'vssr', 'flag' }, call('get_flag')) -- 获取节点国旗 iso code
entry({ 'admin', 'services', 'vssr', 'ip' }, call('check_ip')) -- 获取ip情况
entry({ 'admin', 'services', 'vssr', 'switch' }, call('switch')) -- 设置节点为自动切换
entry({ 'admin', 'services', 'vssr', 'delnode' }, call('del_node')) -- 删除某个节点
end

-- 执行订阅
Expand Down Expand Up @@ -166,7 +166,7 @@ function check_port()
retstring = tostring(ret) == 'true' and '1' or '0'
local tt = t1 - t0
luci.http.prepare_content('application/json')
luci.http.write_json({ret = retstring, used = math.floor(tt * 1000 + 0.5)})
luci.http.write_json({ ret = retstring, used = math.floor(tt * 1000 + 0.5) })
end

-- 读取 GeoLite2 库 国旗图标
Expand Down Expand Up @@ -254,7 +254,7 @@ function refresh_data()
retstring = '-1'
end
elseif set == 'ip_data' then
local ip_data_url = uci:get("vssr","@socks5_proxy[0]","ip_data_url")
local ip_data_url = uci:get("vssr", "@socks5_proxy[0]", "ip_data_url")
refresh_cmd = "wget-ssl -O- '" .. ip_data_url .. "' > /tmp/china_ssr.txt 2>/dev/null"
sret = luci.sys.call(refresh_cmd)
icount = luci.sys.exec('cat /tmp/china_ssr.txt | wc -l')
Expand Down Expand Up @@ -304,5 +304,5 @@ function refresh_data()
end
end
luci.http.prepare_content('application/json')
luci.http.write_json({ret = retstring, retcount = icount})
luci.http.write_json({ ret = retstring, retcount = icount })
end
38 changes: 35 additions & 3 deletions root/etc/init.d/vssr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ CONFIG_UDP_FILE=/var/etc/${NAME}_u.json
CONFIG_SOCK5_FILE=/var/etc/${NAME}_s.json
CRON_FILE=/etc/crontabs/root

LOCK_FILE=/var/lock/vssr.lock

#定义可执行文件路径
VSSR_RULES_BIN=/usr/bin/vssr-rules

Expand All @@ -39,6 +41,30 @@ switch_server=$1
MAXFD=32768
threads=1

set_lock() {
exec 1000>"$LOCK_FILE"
flock -xn 1000
}

unset_lock() {
flock -u 1000
rm -rf "$LOCK_FILE"
}

unlock() {
failcount=1
while [ "$failcount" -le 10 ]; do
if [ -f "$LOCK_FILE" ]; then
echo "$(date "+%Y-%m-%d %H:%M:%S") wait for file unlock: $failcount count" >> /tmp/vssr.log
let "failcount++"
sleep 1s
[ "$failcount" -ge 10 ] && unset_lock
else
break
fi
done
}


#读取 uci 数据
uci_get_by_name() {
Expand Down Expand Up @@ -374,8 +400,6 @@ start_redir() {
local stype=$(uci_get_by_name $GLOBAL_SERVER type)
sscmd=$(find_bin $stype)



if [ "$(uci_get_by_type global threads 0)" = "0" ]; then
threads=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
else
Expand Down Expand Up @@ -576,6 +600,7 @@ rules() {
}

start() {
set_lock
if [ -z "$switch_server" ]; then
GLOBAL_SERVER=$(uci_get_by_type global global_server)
else
Expand Down Expand Up @@ -635,6 +660,8 @@ EOF
fi
fi

unset_lock

ENABLE_SERVER=$(uci_get_by_type global global_server)
[ "$ENABLE_SERVER" = "nil" ] && return 1
}
Expand All @@ -644,6 +671,9 @@ boot() {
}

stop() {
unlock
set_lock

echo "stop"
/usr/bin/vssr-rules -f
srulecount=$(iptables -L | grep SSR-SERVER-RULE | wc -l)
Expand All @@ -665,4 +695,6 @@ stop() {
/etc/init.d/dnsmasq restart >/dev/null 2>&1
fi
del_cron
}

unset_lock
}
Loading

0 comments on commit 51ae04a

Please sign in to comment.