#!/bin/bash DIR=`dirname $0` DIR="$(cd $DIR; pwd)" XCONF=/tmp/proxy-xray.json usage() { echo "proxy-xray " echo " -i|--stdin [Optional] Read config from stdin instead of auto generation" echo " -d|--debug [Optional] Start in debug mode with verbose output" echo " --ignore-china [Optional] Add routing rules to avoid domain and ip located in China being proxied" echo " --ignore-domain [Optional] Add a non-proxy routing rule for domain, like sina.cn or geosite:geosite:geolocation-cn" echo " --ignore-ip [Optional] Add a non-proxy routing rule for ip, like 1.1.1.1/32 or geoip:cn" echo " --proxy-domain [Optional] Add a proxy routing rule for domain, like twitter.com or geosite:google-cn" echo " --proxy-ip [Optional] Add a proxy routing rule for ip, like geoip:netflix" echo " --block-domain [Optional] Add a block routing rule for domain, like geosite:category-ads-all" echo " --block-ip [Optional] Add a block routing rule for ip, like geoip:private" echo " --ltx id@host:port" echo " --ltt id@host:port" echo " --lttw id@host:port:/webpath" echo " --lttg id@host:port:/svcpath" echo " --mtt id@host:port" echo " --mttw id@host:port:/webpath" echo " --ttt password@host:port" echo " --tttw password@host:port:/webpath" # echo " --ssa password:method@host:port" # echo " --sst password:method@host:port" } Jrules='{"rules":[]}' TEMP=`getopt -o di --long ltx:,ltt:,lttw:,lttg:,mtt:,mttw:,ttt:,tttw:,ssa:,sst:,ignore-domain:,ignore-ip:,ignore-china,proxy-domain:,proxy-ip:,block-domain:,block-ip:,stdin,debug -n "$0" -- $@` if [ $? != 0 ] ; then usage; exit 1 ; fi eval set -- "$TEMP" while true ; do case "$1" in --ltx|--ltt|--lttw|--lttg|--mtt|--mttw|--ttt|--tttw|--ssa|--sst) subcmd=`echo "$1"|tr -d "\-\-"` $DIR/proxy-${subcmd}.sh $2 >$XCONF if [ $? != 0 ]; then echo "${subcmd} Config failed: $DIR/proxy-${subcmd}.sh $2" exit 2 else XRAY=1 fi shift 2 ;; --ignore-domain) Jrules=`echo "${Jrules}" | jq --arg igndomain "$2" \ '.rules += [{"type":"field", "outboundTag":"direct", "domain":[$igndomain]}]'` shift 2 ;; --ignore-ip) Jrules=`echo "${Jrules}" | jq --arg ignip "$2" \ '.rules += [{"type":"field", "outboundTag":"direct", "ip":[$ignip]}]'` shift 2 ;; --ignore-china) Jrules=`echo "${Jrules}" | jq --arg igndomain "geosite:apple-cn" \ '.rules += [{"type":"field", "outboundTag":"direct", "domain":[$igndomain]}]'` Jrules=`echo "${Jrules}" | jq --arg igndomain "geosite:geolocation-cn" \ '.rules += [{"type":"field", "outboundTag":"direct", "domain":[$igndomain]}]'` Jrules=`echo "${Jrules}" | jq --arg ignip "geoip:cn" \ '.rules += [{"type":"field", "outboundTag":"direct", "ip":[$ignip]}]'` IGCHINA=1 shift 1 ;; --proxy-domain) Jrules=`echo "${Jrules}" | jq --arg pxydomain "$2" \ '.rules += [{"type":"field", "outboundTag":"proxy", "domain":[$pxydomain]}]'` shift 2 ;; --proxy-ip) Jrules=`echo "${Jrules}" | jq --arg pxyip "$2" \ '.rules += [{"type":"field", "outboundTag":"proxy", "ip":[$pxyip]}]'` shift 2 ;; --block-domain) Jrules=`echo "${Jrules}" | jq --arg blkdomain "$2" \ '.rules += [{"type":"field", "outboundTag":"block", "domain":[$blkdomain]}]'` shift 2 ;; --block-ip) Jrules=`echo "${Jrules}" | jq --arg blkip "$2" \ '.rules += [{"type":"field", "outboundTag":"block", "ip":[$blkip]}]'` shift 2 ;; -i|--stdin) STDINCONF=1 XRAY=1 shift 1 ;; -d|--debug) DEBUG=1 shift 1 ;; --) shift break ;; *) usage; exit 1 ;; esac done if [ "${XRAY}" != "1" ]; then usage exit 1 fi if [ "${IGCHINA}" = "1" ]; then cp -a /etc/dnsmasq-china.d/*.conf /etc/dnsmasq.d/ else rm -rf /etc/dnsmasq.d/*.china.conf fi dnsmasq Jrouting='{"routing": {"domainStrategy":"AsIs"}}' Jrouting=`echo "${Jrouting}" |jq --argjson jrules "${Jrules}" '.routing += $jrules'` cat $XCONF| jq --argjson jrouting "${Jrouting}" '. += $jrouting' |sponge $XCONF if [ "${STDINCONF}" = "1" ]; then exec /usr/local/bin/xray fi if [ "${DEBUG}" = "1" ]; then cat $XCONF |jq '.log.loglevel |="debug"' |sponge $XCONF cat $XCONF fi exec /usr/local/bin/xray -c $XCONF