first commit

This commit is contained in:
hepingcaizi 2024-02-14 20:14:50 +08:00
commit 7bb1c25bd4
22 changed files with 297 additions and 0 deletions

1
LICENSE Normal file
View file

@ -0,0 +1 @@

7
README.md Normal file
View file

@ -0,0 +1,7 @@
# Cns 一键脚本
```
wget --no-check-certificate -O cns.sh "https://jihulab.com/hepingcaizi/Cns/-/raw/master/cns.sh" && sh cns.sh
```

109
cns.init Normal file
View file

@ -0,0 +1,109 @@
#!/bin/sh
#chkconfig: 2345 99 99
### BEGIN INIT INFO
# Provides: cns
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start cns daemon at boot time
# Description: Start cns daemon at boot time
### END INIT INFO
START=99
usage()
{
if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then
us="systemctl [start|stop|status|restart] cns.service"
else
if [ "$0" = '/etc/rc.common' ]; then
us="/etc/init.d/cns [start|stop|status|restart]"\
else
us="$0 [start|stop|status|restart]"
fi
fi &>/dev/null
echo "Usage:
$us
Config file is [cns_install_dir]/cns.json"
}
status()
{
{
grep -q cns /proc/`cat "[cns_install_dir]/run.pid" 2>/dev/null`/comm 2>/dev/null && \
echo "cns is running..." || \
echo "cns is stopped..."
} 2>/dev/null
}
start()
{
status | grep running && return 0
echo -n "Starting cns:"
for server_port in [cns_tcp_port_list]; do
iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
done 2>/dev/null
cd "[cns_install_dir]"
rm -f "[cns_install_dir]/run.pid"
./cns -json=cns.json -daemon >/dev/null
sleep 3
count=0
while [ ! -f "[cns_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do
sleep 1
count=$((count + 1))
done
grep -q cns /proc/`cat [cns_install_dir]/run.pid 2>/dev/null`/comm && \
echo -e "\033[60G[\033[32m OK \033[0m]" || \
echo -e "\033[60G[\033[31mFAILED\033[0m]"
}
stop()
{
for server_port in [cns_tcp_port_list]; do
while iptables -D INPUT -p 6 --dport $server_port -j ACCEPT; do :;done
while ip6tables -D INPUT -p 6 --dport $server_port -j ACCEPT; do :;done
done 2>/dev/null
status | grep stopped && return 0
echo -n "Stopping cns:"
kill `cat [cns_install_dir]/run.pid 2>/dev/null` 2>/dev/null
sleep 1
grep -q cns /proc/`cat [cns_install_dir]/run.pid`/comm 2>/dev/null && \
echo -e "\033[60G[\033[31mFAILED\033[0m]" || \
echo -e "\033[60G[\033[32m OK \033[0m]"
}
restart()
{
stop
start
}
systemdStart()
{
status | grep running && return 0
cd "[cns_install_dir]"
rm -f "[cns_install_dir]/run.pid"
for server_port in [cns_tcp_port_list]; do
iptables -I INPUT -p 6 --dport $server_port -j ACCEPT
ip6tables -I INPUT -p 6 --dport $server_port -j ACCEPT
done 2>/dev/null
./cns -json=cns.json >/dev/null
count=0
while [ ! -f "[cns_install_dir]/run.pid" ] && [ "$count" -le 10 ]; do
sleep 1
count=$((count + 1))
done
}
systemdRestart()
{
stop
systemdStart
}
if [ -z "$initscript" ]; then
${1:-usage}
fi

16
cns.service Normal file
View file

@ -0,0 +1,16 @@
[Unit]
Description=cns
Documentation=https://github.com/mmmdbybyd/CNS/
[Service]
Type=simple
PIDFile=[cns_install_dir]/run.pid
ExecStart=[cns_install_dir]/cns.init systemdStart
ExecReload=[cns_install_dir]/cns.init systemdRestart
ExecStop=[cns_install_dir]/cns.init stop
PrivateTmp=true
KillMode=control-group
[Install]
WantedBy=multi-user.target

164
cns.sh Normal file
View file

@ -0,0 +1,164 @@
#!/bin/bash
#==========================#
###### Author: CuteBi ######
#==========================#
#Stop cns & delete cns files.
Delete() {
systemctl disable cns.service
rm -f /etc/init.d/cns /lib/systemd/system/cns.service
if [ -f "${cns_install_dir:=/usr/local/cns}/cns.init" ]; then
"$cns_install_dir"/cns.init stop
rm -rf "$cns_install_dir"
fi
}
#Print error message and exit.
Error() {
echo $echo_e_arg "\033[41;37m$1\033[0m"
echo -n "remove cns?[y]: "
read remove
echo "$remove"|grep -qi 'n' || Delete
exit 1
}
#Make cns start cmd
Config() {
[ -n "$cns_install_dir" ] && return #Variables come from the environment
clear
echo $echo_e_arg "\033[0;34m以修改为本地国内源,请放心安装 \033[0m"
echo ""
echo -n '请输入cns服务端口(如果不用请留空): '
read cns_port
echo -n '请输入cns加密密码(默认不加密): '
read cns_encrypt_password
echo -n "请输入cns的udp标识(默认: 'httpUDP'): "
read cns_udp_flag
echo -n "请输入cns代理头域(默认: 'Meng'): "
read cns_proxy_key
echo -n '请输入tls服务端口(如果不用请留空): '
read cns_tls_port
echo -n '请输入cns安装目录(默认/usr/local/cns): '
read cns_install_dir
echo "${cns_install_dir:=/usr/local/cns}"|grep -q '^/' || cns_install_dir="$PWD/$cns_install_dir"
echo -n "是否安装UPX压缩版?[n]: "
read cns_UPX
echo "$cns_UPX"|grep -qi '^y' && cns_UPX="upx" || cns_UPX=""
}
GetAbi() {
machine=`uname -m`
#mips[...] use 'le' version
if echo "$machine"|grep -q 'mips64'; then
shContent=`cat "$SHELL"`
[ "${shContent:5:1}" = `echo $echo_e_arg "\x01"` ] && machine='mips64le' || machine='mips64'
elif echo "$machine"|grep -q 'mips'; then
shContent=`cat "$SHELL"`
[ "${shContent:5:1}" = `echo $echo_e_arg "\x01"` ] && machine='mipsle' || machine='mips'
elif echo "$machine"|grep -Eq 'i686|i386'; then
machine='386'
elif echo "$machine"|grep -Eq 'armv7|armv6'; then
machine='arm'
elif echo "$machine"|grep -Eq 'armv8|aarch64'; then
machine='arm64'
else
machine='amd64'
fi
}
#install cns files
InstallFiles() {
GetAbi
if echo "$machine" | grep -q '^mips'; then
cat /proc/cpuinfo | grep -qiE 'fpu|neon|vfp|softfp|asimd' || softfloat='_softfloat'
fi
mkdir -p "$cns_install_dir" || Error "Create cns install directory failed."
cd "$cns_install_dir" || exit 1
$download_tool_cmd cns https://jihulab.com/hepingcaizi/Cns/-/raw/master/linux_${machine}${softfloat} || Error "cns download failed."
$download_tool_cmd cns.init https://jihulab.com/hepingcaizi/Cns/-/raw/master/cns.init || Error "cns.init download failed."
[ -f '/etc/rc.common' ] && rcCommon='/etc/rc.common'
sed -i "s~#!/bin/sh~#!$SHELL $rcCommon~" cns.init
sed -i "s~\[cns_start_cmd\]~$cns_start_cmd~g" cns.init
sed -i "s~\[cns_install_dir\]~$cns_install_dir~g" cns.init
sed -i "s~\[cns_tcp_port_list\]~$cns_port $cns_tls_port~g" cns.init
ln -s "$cns_install_dir/cns.init" /etc/init.d/cns
cat >cns.json <<-EOF
{
`[ -n "$cns_port" ] && echo '"Listen_addr": [":'$cns_port'"],'`
"Proxy_key": "${cns_proxy_key:-Meng}",
"Encrypt_password": "${cns_encrypt_password}",
"Udp_flag": "${cns_udp_flag:-httpUDP}",
"Enable_dns_tcpOverUdp": true,
"Enable_httpDNS": true,
"Enable_TFO": false,
"Udp_timeout": 60,
"Tcp_timeout": 600,
"Pid_path": "${cns_install_dir}/run.pid"
`[ -n "$cns_tls_port" ] && echo ',
"Tls": {
"Listen_addr": [":'$cns_tls_port'"]
}'`
}
EOF
chmod -R +rwx "$cns_install_dir" /etc/init.d/cns
if type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ]; then
$download_tool_cmd /lib/systemd/system/cns.service https://jihulab.com/hepingcaizi/Cns/-/raw/master/cns.service || Error "cns.service download failed."
chmod +rwx /lib/systemd/system/cns.service
sed -i "s~\[cns_install_dir\]~$cns_install_dir~g" /lib/systemd/system/cns.service
systemctl daemon-reload
fi
}
#install initialization
InstallInit() {
echo -n "是否更新系统?[n]: "
read update
PM=`type apt-get || type yum`
PM=`echo "$PM" | grep -o '/.*'`
echo "$update"|grep -qi 'y' && $PM -y update
$PM -y install curl wget unzip
type curl && download_tool_cmd='curl -L -ko' || download_tool_cmd='wget --no-check-certificate -O'
}
Install() {
Config
Delete >/dev/null 2>&1
sleep 3
InstallInit
InstallFiles
"${cns_install_dir}/cns.init" start|grep -q FAILED && "${cns_install_dir}/cns.init" start|grep -q FAILED && Error "cns install failed."
type systemctl && [ -z "$(systemctl --failed|grep -q 'Host is down')" ] && systemctl restart cns
echo $echo_e_arg \
"\033[44;37mcns install success.\033[0;34m
\r cns server port:\033[35G${cns_port}
\r cns proxy key:\033[35G${cns_proxy_key:-Meng}
\r cns udp flag:\033[35G${cns_udp_flag:-httpUDP}
\r cns encrypt password:\033[35G${cns_encrypt_password}
\r cns tls server port:\033[35G${cns_tls_port}
\r`[ -f /etc/init.d/cns ] && /etc/init.d/cns usage || \"$cns_install_dir/cns.init\" usage`\033[0m"
}
Uninstall() {
if [ -z "$cns_install_dir" ]; then
echo -n "Please input cns install directory(default is /usr/local/cns): "
read cns_install_dir
fi
Delete >/dev/null 2>&1 && \
echo $echo_e_arg "\n\033[44;37mcns uninstall success.\033[0m" || \
echo $echo_e_arg "\n\033[41;37mcns uninstall failed.\033[0m"
}
#script initialization
ScriptInit() {
emulate bash 2>/dev/null #zsh emulation mode
if echo -e ''|grep -q 'e'; then
echo_e_arg=''
echo_E_arg=''
else
echo_e_arg='-e'
echo_E_arg='-E'
fi
}
ScriptInit
echo $*|grep -qi uninstall && Uninstall || Install

BIN
darwin_amd64 Normal file

Binary file not shown.

BIN
freebsd_386 Normal file

Binary file not shown.

BIN
freebsd_amd64 Normal file

Binary file not shown.

BIN
linux_386 Normal file

Binary file not shown.

BIN
linux_amd64 Normal file

Binary file not shown.

BIN
linux_arm Normal file

Binary file not shown.

BIN
linux_arm64 Normal file

Binary file not shown.

BIN
linux_mips Normal file

Binary file not shown.

BIN
linux_mips64 Normal file

Binary file not shown.

BIN
linux_mips64_softfloat Normal file

Binary file not shown.

BIN
linux_mips64le Normal file

Binary file not shown.

BIN
linux_mips64le_softfloat Normal file

Binary file not shown.

BIN
linux_mips_softfloat Normal file

Binary file not shown.

BIN
linux_mipsle Normal file

Binary file not shown.

BIN
linux_mipsle_softfloat Normal file

Binary file not shown.

BIN
windows_386.exe Normal file

Binary file not shown.

BIN
windows_amd64.exe Normal file

Binary file not shown.