mirror of
https://github.com/Ysurac/openmptcprouter-feeds.git
synced 2025-03-09 15:40:03 +00:00
Add basic IPv6 support and replace haproxy by nginx for VPS failover
This commit is contained in:
parent
2f4e19176c
commit
715d53300d
23 changed files with 1419 additions and 9 deletions
21
luci-app-nginx-ha/LICENSE
Normal file
21
luci-app-nginx-ha/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2017 chenhw2
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
17
luci-app-nginx-ha/Makefile
Normal file
17
luci-app-nginx-ha/Makefile
Normal file
|
@ -0,0 +1,17 @@
|
|||
#
|
||||
# Copyright (C) 2016 chenhw2 <chenhw2@github.com>
|
||||
# Copyright (C) 2018 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
|
||||
#
|
||||
# See /LICENSE for more information.
|
||||
#
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
LUCI_TITLE:=LuCI Support for nginx load balancing
|
||||
LUCI_DEPENDS:=+nginx
|
||||
|
||||
PKG_LICENSE:=MIT
|
||||
|
||||
include ../luci/luci.mk
|
||||
|
||||
# call BuildPackage - OpenWrt buildroot signature
|
7
luci-app-nginx-ha/luasrc/controller/nginx-ha.lua
Normal file
7
luci-app-nginx-ha/luasrc/controller/nginx-ha.lua
Normal file
|
@ -0,0 +1,7 @@
|
|||
module("luci.controller.nginx-ha", package.seeall)
|
||||
|
||||
function index()
|
||||
entry(
|
||||
{"admin", "services", "nginx-ha"},
|
||||
cbi("nginx-ha"), _("Nginx High Availability"), 55)
|
||||
end
|
46
luci-app-nginx-ha/luasrc/model/cbi/nginx-ha.lua
Normal file
46
luci-app-nginx-ha/luasrc/model/cbi/nginx-ha.lua
Normal file
|
@ -0,0 +1,46 @@
|
|||
local m, s, o
|
||||
|
||||
if luci.sys.call("pgrep nginx >/dev/null") == 0 then
|
||||
m = Map("nginx-ha", translate("Nginx High Availability"), "%s - %s" %{translate("Nginx High Availability"), translate("RUNNING")})
|
||||
else
|
||||
m = Map("nginx-ha", translate("Nginx High Availability"), "%s - %s" %{translate("Nginx High Availability"), translate("NOT RUNNING")})
|
||||
end
|
||||
|
||||
s = m:section(TypedSection, "general", translate("General Setting"))
|
||||
s.anonymous = true
|
||||
|
||||
o = s:option(Flag, "enable", translate("Enable"))
|
||||
o.rmempty = false
|
||||
|
||||
o = s:option(Value, "startup_delay", translate("Startup Delay"))
|
||||
o:value(0, translate("Not enabled"))
|
||||
for _, v in ipairs({5, 10, 15, 25, 40}) do
|
||||
o:value(v, translate("%u seconds") %{v})
|
||||
end
|
||||
o.datatype = "uinteger"
|
||||
o.default = 0
|
||||
o.rmempty = false
|
||||
|
||||
o = s:option(Value, "listen", translate("Listen Address:Port"))
|
||||
o.placeholder = "0.0.0.0:6666"
|
||||
o.default = "0.0.0.0:6666"
|
||||
o.rmempty = false
|
||||
|
||||
o = s:option(Value, "timeout", translate("Timeout Connect (ms)"))
|
||||
o.placeholder = "666"
|
||||
o.default = "666"
|
||||
o.datatype = "range(33, 10000)"
|
||||
o.rmempty = false
|
||||
|
||||
o = s:option(Value, "retries", translate("Retries"))
|
||||
o.placeholder = "1"
|
||||
o.default = "1"
|
||||
o.datatype = "range(1, 10)"
|
||||
o.rmempty = false
|
||||
|
||||
|
||||
o = s:option(DynamicList, "upstreams", translate("UpStream Server"), translate("e.g. [123.123.123.123:65101 weight=1 max_fails=3 fail_timeout=30s]"))
|
||||
o.placeholder = "123.123.123.123:65101 weight=1 max_fails=3 fail_timeout=30s"
|
||||
o.rmempty = false
|
||||
|
||||
return m
|
8
luci-app-nginx-ha/root/etc/config/nginx-ha
Normal file
8
luci-app-nginx-ha/root/etc/config/nginx-ha
Normal file
|
@ -0,0 +1,8 @@
|
|||
|
||||
config general 'general'
|
||||
option enable '0'
|
||||
option retries '1'
|
||||
option timeout '1000'
|
||||
option listen '0.0.0.0:65101'
|
||||
option startup_delay '5'
|
||||
list upstreams '1.2.3.4:65101 weight=1 max_fails=3 fail_timeout=30s'
|
115
luci-app-nginx-ha/root/etc/init.d/nginx-ha
Executable file
115
luci-app-nginx-ha/root/etc/init.d/nginx-ha
Executable file
|
@ -0,0 +1,115 @@
|
|||
#!/bin/sh /etc/rc.common
|
||||
# Copyright (C) 2016 chenhw2 <chenhw2@github.com>
|
||||
# Copyright (C) 2018 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
|
||||
|
||||
START=85
|
||||
|
||||
USE_PROCD=1
|
||||
PROG_NAME=nginx
|
||||
PROG=/usr/sbin/${PROG_NAME}
|
||||
NAME=nginx-ha
|
||||
|
||||
PIDCOUNT=0
|
||||
|
||||
_log() {
|
||||
logger -p daemon.info -t ${PROG_NAME} "$@"
|
||||
}
|
||||
|
||||
_err() {
|
||||
logger -p daemon.err -t ${PROG_NAME} "$@"
|
||||
}
|
||||
|
||||
validate_section() {
|
||||
uci_validate_section nginx-ha general "${1}" \
|
||||
'enable:bool:0' \
|
||||
'retries:uinteger:3' \
|
||||
'timeout:uinteger:4000' \
|
||||
'startup_delay:uinteger:5' \
|
||||
'listen:string' \
|
||||
'upstreams:list(string)'
|
||||
}
|
||||
|
||||
genline_srv(){
|
||||
echo " server $1;"
|
||||
}
|
||||
|
||||
boot() {
|
||||
local delay=$(uci -q get $NAME.general.startup_delay)
|
||||
(sleep ${delay:-0} && start >/dev/null 2>&1) &
|
||||
return 0
|
||||
}
|
||||
|
||||
start_instance() {
|
||||
local enable retries timeout startup_delay listen upstreams
|
||||
|
||||
validate_section "${1}" || {
|
||||
_err "validation failed"
|
||||
return 1
|
||||
}
|
||||
|
||||
[ "$enable" = 1 ] || return 1
|
||||
|
||||
mkdir -p /var/etc
|
||||
cat <<-EOF > /var/etc/$PROG_NAME.cfg
|
||||
user nobody nogroup;
|
||||
worker_processes $(grep -c '^processor' /proc/cpuinfo | tr -d "\n");
|
||||
worker_rlimit_nofile 300000;
|
||||
|
||||
events {
|
||||
worker_connections 15000;
|
||||
multi_accept on;
|
||||
use epoll;
|
||||
}
|
||||
|
||||
stream {
|
||||
upstream allservers {
|
||||
zone dynamic 64k;
|
||||
$(config_list_foreach "${1}" "upstreams" genline_srv)
|
||||
}
|
||||
|
||||
server {
|
||||
listen ${listen:-0.0.0.0:6666} udp;
|
||||
proxy_pass allservers;
|
||||
}
|
||||
server {
|
||||
listen ${listen:-0.0.0.0:6666};
|
||||
proxy_pass allservers;
|
||||
}
|
||||
}
|
||||
EOF
|
||||
|
||||
procd_open_instance "nginx-ha"
|
||||
procd_set_param command /usr/sbin/nginx -c /var/etc/$PROG_NAME.cfg -g 'daemon off;'
|
||||
procd_set_param file /var/etc/$PROG_NAME.cfg
|
||||
procd_set_param respawn
|
||||
procd_close_instance
|
||||
}
|
||||
|
||||
start_service() {
|
||||
config_load nginx-ha
|
||||
config_foreach start_instance general
|
||||
}
|
||||
|
||||
reload_service() {
|
||||
stop
|
||||
start
|
||||
}
|
||||
|
||||
stop_service() {
|
||||
local _PID=$(cat /var/run/nginx.pid 2>/dev/null)
|
||||
kill -15 $_PID 2>/dev/null
|
||||
sleep 1 # give time to shutdown
|
||||
local _tmp=$(pgrep nginx | tr "\n" " ")
|
||||
if [ -z "$_tmp" ]; then
|
||||
logger -p daemon.notice -t "nginx-ha[$_PID]" "Shutdown successfully"
|
||||
else
|
||||
kill -9 $_tmp # Normally never come here
|
||||
logger -p daemon.warn -t "nginx-ha[$_tmp]" "Shutdown forced by KILL"
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
service_triggers() {
|
||||
procd_add_reload_trigger nginx-ha
|
||||
}
|
13
luci-app-nginx-ha/root/etc/uci-defaults/42_luci-nginx-ha
Normal file
13
luci-app-nginx-ha/root/etc/uci-defaults/42_luci-nginx-ha
Normal file
|
@ -0,0 +1,13 @@
|
|||
#!/bin/sh
|
||||
|
||||
uci -q batch <<-EOF >/dev/null
|
||||
delete ucitrack.@nginx-ha[-1]
|
||||
add ucitrack nginx-ha
|
||||
set ucitrack.@nginx-ha[-1].init=nginx-ha
|
||||
commit ucitrack
|
||||
EOF
|
||||
|
||||
/etc/init.d/nginx-ha enable >/dev/null 2>&1
|
||||
|
||||
rm -f /tmp/luci-indexcache
|
||||
exit 0
|
Loading…
Add table
Add a link
Reference in a new issue