|
#!/bin/bash |
|
|
|
service=xray-server |
|
|
|
# path |
|
data_path=/data/${service} |
|
cache_path=.cache |
|
mkdir -p ${data_path} ${cache_path} |
|
|
|
# ===== init =================================================================== |
|
|
|
configure_domain() { |
|
domain_file=${cache_path}/domain |
|
if [ ! -f ${domain_file} ]; then |
|
read -p "Enter your domain [www.example.com]: " DOMAIN |
|
echo ${DOMAIN} > ${domain_file} |
|
fi |
|
domain=${domain:-$(<${domain_file})} |
|
email=${email:-admin@${domain}} |
|
} |
|
|
|
configure_domain |
|
|
|
# ===== function =============================================================== |
|
|
|
install_basic() { |
|
if [ ! -x "$(command -v uuidgen)" ]; then |
|
apt install -y uuid-runtime |
|
fi |
|
if [ ! -x "$(command -v docker)" ]; then |
|
apt install -y docker.io |
|
fi |
|
} |
|
|
|
configure() { |
|
port=${port:-2333} |
|
uuid=${uuid:-$(uuidgen)} |
|
log_level=${log_level:-info} |
|
cert_path=${cert_path:-\\/etc\\/xray\\/certs\\/${domain}\\/${domain}.crt} |
|
cert_key_path=${cert_key_path:-\\/etc\\/xray\\/certs\\/${domain}\\/${domain}.key} |
|
trojan_port=${trojan_port:-10800} |
|
trojan_password=${trojan_password:-$(uuidgen)} |
|
vless_ws_path=${vless_ws_path:-\\/xray\\/vless\\/ws} |
|
vless_ws_port=${vless_ws_port:-10801} |
|
vmess_tcp_path=${vmess_tcp_path:-\\/xray\\/vmess\\/tcp} |
|
vmess_tcp_port=${vmess_tcp_port:-10802} |
|
vmess_ws_path=${vmess_ws_path:-\\/xray\\/vmess\\/ws} |
|
vmess_ws_port=${vmess_ws_port:-10803} |
|
|
|
cp config.example.json config.json |
|
sed -i 's/{{ uuid }}/'${uuid}'/g' config.json |
|
sed -i 's/"{{ port }}"/'${port}'/g' config.json |
|
sed -i 's/{{ email }}/'${email}'/g' config.json |
|
sed -i 's/{{ log_level }}/'${log_level}'/g' config.json |
|
sed -i 's/{{ cert_path }}/'${cert_path}'/g' config.json |
|
sed -i 's/{{ cert_key_path }}/'${cert_key_path}'/g' config.json |
|
sed -i 's/"{{ trojan_port }}"/'${trojan_port}'/g' config.json |
|
sed -i 's/{{ trojan_password }}/'${trojan_password}'/g' config.json |
|
sed -i 's/"{{ vless_ws_port }}"/'${vless_ws_port}'/g' config.json |
|
sed -i 's/{{ vless_ws_path }}/'${vless_ws_path}'/g' config.json |
|
sed -i 's/"{{ vmess_tcp_port }}"/'${vmess_tcp_port}'/g' config.json |
|
sed -i 's/{{ vmess_tcp_path }}/'${vmess_tcp_path}'/g' config.json |
|
sed -i 's/"{{ vmess_ws_port }}"/'${vmess_ws_port}'/g' config.json |
|
sed -i 's/{{ vmess_ws_path }}/'${vmess_ws_path}'/g' config.json |
|
cp config.json ${data_path}/config.json |
|
} |
|
|
|
cert() { |
|
if [ ! -x "$(command -v certbot)" ]; then |
|
apt install -y certbot |
|
fi |
|
certbot certonly --standalone -d ${domain} --email ${email} |
|
} |
|
|
|
mattermost() { |
|
docker run -d --name mattermost-preview --publish 127.0.0.1:80:8065 mattermost/mattermost-preview |
|
} |
|
|
|
install_script() { |
|
cp ./* ${data_path} |
|
} |
|
|
|
docker_start() { |
|
docker rm -f ${service} |
|
docker run -d \ |
|
--name ${service} \ |
|
--restart=always \ |
|
--network host \ |
|
-v ${data_path}:/etc/xray \ |
|
-v /etc/letsencrypt/live/${domain}/fullchain.pem:/etc/xray/certs/${domain}/${domain}.crt \ |
|
-v /etc/letsencrypt/live/${domain}/privkey.pem:/etc/xray/certs/${domain}/${domain}.key \ |
|
teddysun/xray |
|
} |
|
|
|
# ===== help =================================================================== |
|
|
|
show_help() { |
|
# domain |
|
echo "DOMAIN: ${domain}" |
|
|
|
# usage |
|
echo "" |
|
echo "Usage: ${0} Option" |
|
|
|
# options |
|
echo "" |
|
echo "Option:" |
|
grep -E '\s+[a-zA-Z_-]+\).*##' $0 | \ |
|
sed -r 's/\s+([a-zA-Z_-]+)\).*## (.*)/\1|\2/g' | \ |
|
awk -F '|' '{printf "\t\033[36m%-20s\033[0m %s\n", $1, $2}' |
|
} |
|
|
|
# ===== option ================================================================= |
|
|
|
case $1 in |
|
configure_domain) ## configure domain |
|
configure_domain |
|
;; |
|
init) ## install basic |
|
install_basic |
|
;; |
|
install_script) ## install script |
|
install_script |
|
;; |
|
cert) ## cert sign |
|
cert |
|
;; |
|
configure) ## configure |
|
configure |
|
;; |
|
docker_start) ## deploy xray |
|
docker_start |
|
;; |
|
*) |
|
show_help |
|
;; |
|
esac |