commit 7bb1c25bd4455d0eb57bb646346cc632fa0c1460 Author: hepingcaizi Date: Wed Feb 14 20:14:50 2024 +0800 first commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..c4c1b55 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Cns 一键脚本 +``` +wget --no-check-certificate -O cns.sh "https://jihulab.com/hepingcaizi/Cns/-/raw/master/cns.sh" && sh cns.sh +``` + + + diff --git a/cns.init b/cns.init new file mode 100644 index 0000000..7223adf --- /dev/null +++ b/cns.init @@ -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 diff --git a/cns.service b/cns.service new file mode 100644 index 0000000..7f8f7e3 --- /dev/null +++ b/cns.service @@ -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 + diff --git a/cns.sh b/cns.sh new file mode 100644 index 0000000..ed22c00 --- /dev/null +++ b/cns.sh @@ -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 diff --git a/darwin_amd64 b/darwin_amd64 new file mode 100644 index 0000000..7d7aed6 Binary files /dev/null and b/darwin_amd64 differ diff --git a/freebsd_386 b/freebsd_386 new file mode 100644 index 0000000..f86dbcf Binary files /dev/null and b/freebsd_386 differ diff --git a/freebsd_amd64 b/freebsd_amd64 new file mode 100644 index 0000000..d90d16c Binary files /dev/null and b/freebsd_amd64 differ diff --git a/linux_386 b/linux_386 new file mode 100644 index 0000000..6a18511 Binary files /dev/null and b/linux_386 differ diff --git a/linux_amd64 b/linux_amd64 new file mode 100644 index 0000000..2715b0a Binary files /dev/null and b/linux_amd64 differ diff --git a/linux_arm b/linux_arm new file mode 100644 index 0000000..cd211e0 Binary files /dev/null and b/linux_arm differ diff --git a/linux_arm64 b/linux_arm64 new file mode 100644 index 0000000..3961fdc Binary files /dev/null and b/linux_arm64 differ diff --git a/linux_mips b/linux_mips new file mode 100644 index 0000000..057fc9d Binary files /dev/null and b/linux_mips differ diff --git a/linux_mips64 b/linux_mips64 new file mode 100644 index 0000000..3a89925 Binary files /dev/null and b/linux_mips64 differ diff --git a/linux_mips64_softfloat b/linux_mips64_softfloat new file mode 100644 index 0000000..3a89925 Binary files /dev/null and b/linux_mips64_softfloat differ diff --git a/linux_mips64le b/linux_mips64le new file mode 100644 index 0000000..b51a6f4 Binary files /dev/null and b/linux_mips64le differ diff --git a/linux_mips64le_softfloat b/linux_mips64le_softfloat new file mode 100644 index 0000000..b51a6f4 Binary files /dev/null and b/linux_mips64le_softfloat differ diff --git a/linux_mips_softfloat b/linux_mips_softfloat new file mode 100644 index 0000000..c2ab8c0 Binary files /dev/null and b/linux_mips_softfloat differ diff --git a/linux_mipsle b/linux_mipsle new file mode 100644 index 0000000..199e0f7 Binary files /dev/null and b/linux_mipsle differ diff --git a/linux_mipsle_softfloat b/linux_mipsle_softfloat new file mode 100644 index 0000000..5f9862d Binary files /dev/null and b/linux_mipsle_softfloat differ diff --git a/windows_386.exe b/windows_386.exe new file mode 100644 index 0000000..8ef0ab0 Binary files /dev/null and b/windows_386.exe differ diff --git a/windows_amd64.exe b/windows_amd64.exe new file mode 100644 index 0000000..c3560d3 Binary files /dev/null and b/windows_amd64.exe differ