feat(脚本): 添加安装时自定义端口、DNS申请通配符证书、兼容不支持80、443端口的机器

pull/534/merge
mack-a 2022-10-11 16:02:05 +08:00
parent a527e20aec
commit a3b5279f57
1 changed files with 124 additions and 36 deletions

View File

@ -211,6 +211,9 @@ initVar() {
# 该域名是否通过dns安装通配符证书 # 该域名是否通过dns安装通配符证书
installDNSACMEStatus= installDNSACMEStatus=
# 自定义端口
customPort=
} }
# 读取tls证书详情 # 读取tls证书详情
@ -219,6 +222,16 @@ readAcmeTLS() {
installDNSACMEStatus=true installDNSACMEStatus=true
fi fi
} }
# 读取默认自定义端口
readCustomPort() {
if [[ -n "${configPath}" ]]; then
local port=
port=$(jq -r .inbounds[0].port "${configPath}02_VLESS_TCP_inbounds.json")
if [[ "${port}" != "443" ]]; then
customPort=${port}
fi
fi
}
# 检测安装方式 # 检测安装方式
readInstallType() { readInstallType() {
coreInstallType= coreInstallType=
@ -230,7 +243,6 @@ readInstallType() {
if [[ -d "/etc/v2ray-agent/v2ray" && -f "/etc/v2ray-agent/v2ray/v2ray" && -f "/etc/v2ray-agent/v2ray/v2ctl" ]]; then if [[ -d "/etc/v2ray-agent/v2ray" && -f "/etc/v2ray-agent/v2ray/v2ray" && -f "/etc/v2ray-agent/v2ray/v2ctl" ]]; then
if [[ -d "/etc/v2ray-agent/v2ray/conf" && -f "/etc/v2ray-agent/v2ray/conf/02_VLESS_TCP_inbounds.json" ]]; then if [[ -d "/etc/v2ray-agent/v2ray/conf" && -f "/etc/v2ray-agent/v2ray/conf/02_VLESS_TCP_inbounds.json" ]]; then
configPath=/etc/v2ray-agent/v2ray/conf/ configPath=/etc/v2ray-agent/v2ray/conf/
if grep </etc/v2ray-agent/v2ray/conf/02_VLESS_TCP_inbounds.json -q '"security": "tls"'; then if grep </etc/v2ray-agent/v2ray/conf/02_VLESS_TCP_inbounds.json -q '"security": "tls"'; then
# 不带XTLS的v2ray-core # 不带XTLS的v2ray-core
coreInstallType=2 coreInstallType=2
@ -530,6 +542,7 @@ initVar "$1"
checkSystem checkSystem
checkCPUVendor checkCPUVendor
readInstallType readInstallType
readCustomPort
readInstallProtocolType readInstallProtocolType
readConfigHostPathUUID readConfigHostPathUUID
readInstallAlpn readInstallAlpn
@ -821,12 +834,18 @@ initTLSNginxConfig() {
initTLSNginxConfig 3 initTLSNginxConfig 3
else else
dnsTLSDomain=$(echo "${domain}" | awk -F "[.]" '{print $(NF-1)"."$NF}') dnsTLSDomain=$(echo "${domain}" | awk -F "[.]" '{print $(NF-1)"."$NF}')
customPortFunction
local port=80
if [[ -n "${customPort}" ]]; then
port=${customPort}
fi
# 修改配置 # 修改配置
touch ${nginxConfigPath}alone.conf touch ${nginxConfigPath}alone.conf
cat <<EOF >${nginxConfigPath}alone.conf cat <<EOF >${nginxConfigPath}alone.conf
server { server {
listen 80; listen ${port};
listen [::]:80; listen [::]:${port};
server_name ${domain}; server_name ${domain};
root /usr/share/nginx/html; root /usr/share/nginx/html;
location ~ /.well-known { location ~ /.well-known {
@ -845,9 +864,6 @@ server {
} }
} }
EOF EOF
# 启动nginx
handleNginx start
checkIP
fi fi
readAcmeTLS readAcmeTLS
@ -866,24 +882,13 @@ updateRedirectNginxConf() {
} }
EOF EOF
else elif [[ -n "${customPort}" ]]; then
cat <<EOF >${nginxConfigPath}alone.conf cat <<EOF >${nginxConfigPath}alone.conf
server { server {
listen 80; listen 127.0.0.1:31300;
server_name _; server_name _;
return 403; return 403;
} }
server {
listen 127.0.0.1:31300;
server_name _;
return 403;
}
server {
listen 80;
listen [::]:80;
server_name ${domain};
return 302 https://${domain}\${request_uri};
}
EOF EOF
fi fi
@ -1014,7 +1019,13 @@ EOF
# 检查ip # 检查ip
checkIP() { checkIP() {
echoContent skyBlue "\n ---> 检查域名ip中" echoContent skyBlue "\n ---> 检查域名ip中"
localIP=$(curl -s -m 2 "${domain}/ip") local checkDomain=${domain}
if [[ -n "${customPort}" ]]; then
checkDomain="http://${domain}:${customPort}"
fi
local localIP=
localIP=$(curl -s -m 2 "${checkDomain}/ip")
handleNginx stop handleNginx stop
if [[ -z ${localIP} ]] || ! echo "${localIP}" | sed '1{s/[^(]*(//;s/).*//;q}' | grep -q '\.' && ! echo "${localIP}" | sed '1{s/[^(]*(//;s/).*//;q}' | grep -q ':'; then if [[ -z ${localIP} ]] || ! echo "${localIP}" | sed '1{s/[^(]*(//;s/).*//;q}' | grep -q '\.' && ! echo "${localIP}" | sed '1{s/[^(]*(//;s/).*//;q}' | grep -q ':'; then
echoContent red "\n ---> 未检测到当前域名的ip" echoContent red "\n ---> 未检测到当前域名的ip"
@ -1023,15 +1034,27 @@ checkIP() {
echoContent yellow " ---> 2.检查域名dns解析是否正确" echoContent yellow " ---> 2.检查域名dns解析是否正确"
echoContent yellow " ---> 3.如解析正确请等待dns生效预计三分钟内生效" echoContent yellow " ---> 3.如解析正确请等待dns生效预计三分钟内生效"
echoContent yellow " ---> 4.如报Nginx启动问题请手动启动nginx查看错误如自己无法处理请提issues" echoContent yellow " ---> 4.如报Nginx启动问题请手动启动nginx查看错误如自己无法处理请提issues"
echo
echoContent skyBlue " ---> 如以上设置都正确,请重新安装纯净系统后再次尝试" echoContent skyBlue " ---> 如以上设置都正确,请重新安装纯净系统后再次尝试"
if [[ -n ${localIP} ]]; then if [[ -n ${localIP} ]]; then
echoContent yellow " ---> 检测返回值异常建议手动卸载nginx后重新执行脚本" echoContent yellow " ---> 检测返回值异常建议手动卸载nginx后重新执行脚本"
fi fi
echoContent red " ---> 请检查防火墙规则是否开放443、80\n" local portFirewallPortStatus="443、80"
read -r -p "是否通过脚本修改防火墙规则开放443、80端口[y/n]:" allPortFirewallStatus
if [[ -n "${customPort}" ]]; then
portFirewallPortStatus="${customPort}"
fi
echoContent red " ---> 请检查防火墙规则是否开放${portFirewallPortStatus}\n"
read -r -p "是否通过脚本修改防火墙规则开放${portFirewallPortStatus}端口?[y/n]:" allPortFirewallStatus
if [[ ${allPortFirewallStatus} == "y" ]]; then if [[ ${allPortFirewallStatus} == "y" ]]; then
allowPort 80 if [[ -n "${customPort}" ]]; then
allowPort 443 allowPort "${customPort}"
else
allowPort 80
allowPort 443
fi
handleNginx start handleNginx start
checkIP checkIP
else else
@ -1107,9 +1130,13 @@ acmeInstallSSL() {
installSSLIPv6="--listen-v6" installSSLIPv6="--listen-v6"
fi fi
echo echo
read -r -p "是否使用DNS申请证书[y/n]:" installSSLDNStatus if [[ -n "${customPort}" ]]; then
if [[ ${installSSLDNStatus} == 'y' ]]; then
dnsSSLStatus=true dnsSSLStatus=true
else
read -r -p "是否使用DNS申请证书[y/n]:" installSSLDNStatus
if [[ ${installSSLDNStatus} == 'y' ]]; then
dnsSSLStatus=true
fi
fi fi
if [[ "${dnsSSLStatus}" == "true" ]]; then if [[ "${dnsSSLStatus}" == "true" ]]; then
@ -1146,6 +1173,38 @@ acmeInstallSSL() {
fi fi
readAcmeTLS readAcmeTLS
} }
# 自定义端口
customPortFunction() {
local historyCustomPortStatus=
if [[ -n "${customPort}" ]]; then
echo
read -r -p "读取到上次安装时的端口,是否使用上次安装时的端口 [y/n]:" historyCustomPortStatus
if [[ "${historyCustomPortStatus}" == "y" ]]; then
echoContent yellow "\n ---> 端口: ${customPort}"
fi
fi
if [[ "${historyCustomPortStatus}" == "n" || -z "${customPort}" ]]; then
echo
echoContent yellow "请输入自定义端口[例: 2083]自定义端口后只允许使用DNS申请证书[回车]使用443"
read -r -p "端口:" customPort
if ((customPort >= 1 && customPort <= 65535)); then
checkCustomPort
else
echoContent green " ---> 端口输入错误"
exit
fi
fi
}
# 检测端口是否占用
checkCustomPort() {
if lsof -i "tcp:${customPort}" | grep -q LISTEN; then
echoContent red "\n ---> ${customPort}端口被占用,请手动关闭后安装\n"
lsof -i tcp:80 | grep LISTEN
exit 0
fi
}
# 安装TLS # 安装TLS
installTLS() { installTLS() {
@ -1373,10 +1432,10 @@ renewalTLS() {
fi fi
fi fi
if [[ -d "$HOME/.acme.sh/${domain}_ecc" && -f "$HOME/.acme.sh/${domain}_ecc/${domain}.key" && -f "$HOME/.acme.sh/${domain}_ecc/${domain}.cer" ]] || [[ "${dnsTLSDomainStatus}" == "true" ]]; then if [[ -d "$HOME/.acme.sh/${domain}_ecc" && -f "$HOME/.acme.sh/${domain}_ecc/${domain}.key" && -f "$HOME/.acme.sh/${domain}_ecc/${domain}.cer" ]] || [[ "${installDNSACMEStatus}" == "true" ]]; then
modifyTime= modifyTime=
if [[ "${dnsTLSDomainStatus}" == "true" ]]; then if [[ "${installDNSACMEStatus}" == "true" ]]; then
modifyTime=$(stat "$HOME/.acme.sh/*.${dnsTLSDomain}_ecc/*.${dnsTLSDomain}.cer" | sed -n '7,6p' | awk '{print $2" "$3" "$4" "$5}') modifyTime=$(stat "$HOME/.acme.sh/*.${dnsTLSDomain}_ecc/*.${dnsTLSDomain}.cer" | sed -n '7,6p' | awk '{print $2" "$3" "$4" "$5}')
else else
modifyTime=$(stat "$HOME/.acme.sh/${domain}_ecc/${domain}.cer" | sed -n '7,6p' | awk '{print $2" "$3" "$4" "$5}') modifyTime=$(stat "$HOME/.acme.sh/${domain}_ecc/${domain}.cer" | sed -n '7,6p' | awk '{print $2" "$3" "$4" "$5}')
@ -2204,11 +2263,16 @@ EOF
# VLESS_TCP # VLESS_TCP
getClients "${configPath}../tmp/02_VLESS_TCP_inbounds.json" "${addClientsStatus}" getClients "${configPath}../tmp/02_VLESS_TCP_inbounds.json" "${addClientsStatus}"
local defaultPort=443
if [[ -n "${customPort}" ]]; then
defaultPort=${customPort}
fi
cat <<EOF >/etc/v2ray-agent/v2ray/conf/02_VLESS_TCP_inbounds.json cat <<EOF >/etc/v2ray-agent/v2ray/conf/02_VLESS_TCP_inbounds.json
{ {
"inbounds":[ "inbounds":[
{ {
"port": 443, "port": ${defaultPort},
"protocol": "vless", "protocol": "vless",
"tag":"VLESSTCP", "tag":"VLESSTCP",
"settings": { "settings": {
@ -2610,11 +2674,16 @@ EOF
# VLESS_TCP # VLESS_TCP
getClients "${configPath}../tmp/02_VLESS_TCP_inbounds.json" "${addClientsStatus}" getClients "${configPath}../tmp/02_VLESS_TCP_inbounds.json" "${addClientsStatus}"
local defaultPort=443
if [[ -n "${customPort}" ]]; then
defaultPort=${customPort}
fi
cat <<EOF >/etc/v2ray-agent/xray/conf/02_VLESS_TCP_inbounds.json cat <<EOF >/etc/v2ray-agent/xray/conf/02_VLESS_TCP_inbounds.json
{ {
"inbounds":[ "inbounds":[
{ {
"port": 443, "port": ${defaultPort},
"protocol": "vless", "protocol": "vless",
"tag":"VLESSTCP", "tag":"VLESSTCP",
"settings": { "settings": {
@ -3152,6 +3221,11 @@ addCorePort() {
# 开放端口 # 开放端口
allowPort "${port}" allowPort "${port}"
local settingsPort=443
if [[ -n "${customPort}" ]]; then
settingsPort=${customPort}
fi
cat <<EOF >"${fileName}" cat <<EOF >"${fileName}"
{ {
"inbounds": [ "inbounds": [
@ -3161,7 +3235,7 @@ addCorePort() {
"protocol": "dokodemo-door", "protocol": "dokodemo-door",
"settings": { "settings": {
"address": "127.0.0.1", "address": "127.0.0.1",
"port": 443, "port": ${settingsPort},
"network": "tcp", "network": "tcp",
"followRedirect": false "followRedirect": false
}, },
@ -4639,6 +4713,10 @@ customXrayInstall() {
installTools 1 installTools 1
# 申请tls # 申请tls
initTLSNginxConfig 2 initTLSNginxConfig 2
handleXray stop
handleNginx start
checkIP
installTLS 3 installTLS 3
handleNginx stop handleNginx stop
# 随机path # 随机path
@ -4715,6 +4793,11 @@ v2rayCoreInstall() {
installTools 2 installTools 2
# 申请tls # 申请tls
initTLSNginxConfig 3 initTLSNginxConfig 3
handleV2Ray stop
handleNginx start
checkIP
installTLS 4 installTLS 4
handleNginx stop handleNginx stop
# initNginxConfig 5 # initNginxConfig 5
@ -4745,6 +4828,11 @@ xrayCoreInstall() {
installTools 2 installTools 2
# 申请tls # 申请tls
initTLSNginxConfig 3 initTLSNginxConfig 3
handleXray stop
handleNginx start
checkIP
installTLS 4 installTLS 4
handleNginx stop handleNginx stop
randomPathFunction 5 randomPathFunction 5
@ -4902,7 +4990,7 @@ menu() {
cd "$HOME" || exit cd "$HOME" || exit
echoContent red "\n==============================================================" echoContent red "\n=============================================================="
echoContent green "作者:mack-a" echoContent green "作者:mack-a"
echoContent green "当前版本:v2.5.78" echoContent green "当前版本:v2.6.1"
echoContent green "Github:https://github.com/mack-a/v2ray-agent" echoContent green "Github:https://github.com/mack-a/v2ray-agent"
echoContent green "描述:八合一共存脚本\c" echoContent green "描述:八合一共存脚本\c"
showInstallStatus showInstallStatus