Advertisement
eibgrad

ddwrt-installer.sh

May 25th, 2019 (edited)
1,189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.92 KB | None | 0 0
  1. #!/bin/sh
  2. #set -x # uncomment/comment to enable/disable debug mode
  3.  
  4. #     name: ddwrt-installer.sh
  5. #  version: 3.0.1, 15-aug-2022, by eibgrad
  6. #  purpose: install pastebin script into dd-wrt router
  7.  
  8. # function usage()
  9. usage() {
  10.     echo 'Usage: ddwrt-installer.sh [options] pastebin-id [ext=sh]'
  11.     echo
  12.     echo '  Install pastebin script into dd-wrt router.'
  13.     echo
  14.     echo '  Installation directory hierarchy:'
  15.     echo '    </path> (from --dir option)'
  16.     echo '    /jffs/etc/config'
  17.     echo '    /tmp'
  18.     echo
  19.     echo '  Options:'
  20.     echo '    --dir PATH  installation directory (created as necessary)'
  21.     echo "    --pre NUM   add prefix to filename (e.g., '01')"
  22.     echo '    --nocom     remove blank lines and non-functional comments'
  23.     echo '    --comp      same as --nocom, plus remove leading whitespace'
  24.     echo '    --noprompt  silently overwrite files and accept defaults'
  25.     echo '    --debug     install script w/ debugging mode enabled'
  26.     echo '    -h,--help   this usage information'
  27.     echo
  28.     echo '  # e.g., install script w/o comments and w/ bash (sh) extension'
  29.     echo '  ddwrt-installer.sh --nocom MxA19W1M'
  30.     echo
  31. }
  32.  
  33. # function query( message [default-reply] )
  34. query() {
  35.     local reply
  36.  
  37.     read -p "$1 " reply < /dev/tty
  38.     [ "$reply" ] && echo "$reply" || echo "$2"
  39. }
  40.  
  41. # function is_mounted( mounting-point )
  42. is_mounted() { df | grep -Eq "[[:space:]]+${1}$"; }
  43.  
  44. # function customize()
  45. customize() {
  46.     # function _ddwrt_ovpn_split()
  47.     _ddwrt_ovpn_split() {
  48.         local route_up
  49.         local route_down
  50.         local default_vpn
  51.  
  52.         # function __add_directives()
  53.         __add_directives() {
  54.             local CONFIG='openvpncl_config'
  55.             local TEMPF="/tmp/ddwrt-installer.$$.tmp"
  56.  
  57.             # retrieve current openvpn client config
  58.             nvram get $CONFIG | tr -d '\r' > $TEMPF
  59.  
  60.             # delete any matching/competing directives (active or inactive)
  61.             sed -r \
  62.                 -e '/^([[:space:]]|#|;)*(|--)route-up/d' \
  63.                 -e '/^([[:space:]]|#|;)*(|--)route-pre-down/d' \
  64.                 -e '/^([[:space:]]|#|;)*(|--)pull-filter.*redirect-gateway/d' \
  65.                 -e '/^([[:space:]]|#|;)*(|--)redirect-gateway/d' \
  66.                 -e '/^([[:space:]]|#|;)*(|--)redirect-private/d' \
  67.                 -i $TEMPF
  68.  
  69.             # delete old banner header and footer
  70.             sed -i '/ added by ddwrt-installer /d' $TEMPF
  71.  
  72.             # configure default gateway (wan or vpn)
  73.             local com="$([ ${default_vpn+x} ] && echo '#')"
  74.  
  75.             # add our directives
  76.             {
  77.             echo '# --- begin directives added by ddwrt-installer --- #'
  78.             echo "route-up ${route_up}"
  79.             echo "route-pre-down ${route_down}"
  80.             echo "${com}pull-filter ignore redirect-gateway"
  81.             echo "${com}redirect-private def1"
  82.             echo '# ---- end directives added by ddwrt-installer ---- #'
  83.             } >> $TEMPF
  84.  
  85.             # delete blank lines
  86.             sed -i '/^[[:space:]]*$/d' $TEMPF
  87.  
  88.             # update openvpn client config
  89.             nvram set $CONFIG="$(cat $TEMPF | tr -d '\r')"
  90.             nvram commit &>/dev/null
  91.  
  92.             rm -f $TEMPF
  93.         }
  94.  
  95.         if [ ! ${noprompt+x} ]; then
  96.             # obtain openvpn default routing preference (default=lan->wan)
  97.             while :; do
  98.                 case "$(query 'Default routing ([1]=lan->wan 2=lan->vpn)?' '1')" in
  99.                     '1') break;;
  100.                     '2') default_vpn=; break;;
  101.                 esac
  102.             done
  103.         fi
  104.  
  105.         route_up="$file_dir/route-up"
  106.         route_down="$file_dir/route-down"
  107.  
  108.         # add symbolic links
  109.         ln -sf "$file" "$route_up"
  110.         ln -sf "$file" "$route_down"
  111.  
  112.         __add_directives
  113.     }
  114.  
  115.     # script-specific customization (optional)
  116.     case "$pbid" in
  117.         'nC27ETsp') _ddwrt_ovpn_split;;
  118.         'W2P3TDZT') _ddwrt_ovpn_split;;
  119.         *) ;;
  120.     esac
  121. }
  122.  
  123. # function exit_1( [message] )
  124. exit_1() { [ "$1" ] && echo "$1"; exit 1; }
  125.  
  126. # function exit_0( [message] )
  127. exit_0() { [ "$1" ] && echo "$1"; exit 0; }
  128.  
  129. # handle help/usage requests
  130. for opt; do case $opt in -h|--help) usage; exit 0;; esac; done
  131.  
  132. # try curl, fallback to wget
  133. which curl &>/dev/null && GET_URL='curl -sLk' || GET_URL='wget -qO -'
  134.  
  135. # process command line options/arguments
  136. while [ $# -gt 0 ]; do
  137.     case $1 in
  138.              '--dir') shift; file_dir="${1//[[:space:]]/}";;
  139.              '--pre') shift; file_pre="${1//[[:space:]]/}";;
  140.            '--nocom') nocom=; unset comp;;
  141.             '--comp') comp=; unset nocom;;
  142.         '--noprompt') noprompt=;;
  143.            '--debug') debug=;;
  144.                    *) break 2;;
  145.     esac
  146.     shift
  147. done
  148.  
  149. # set pastebin-id
  150. [ "$1" ] && pbid="$1" || exit_0 'info: nothing to do'
  151.  
  152. # set and verify extension
  153. ext="$([ "$2" ] && echo "${2//[[:space:]]/}" || echo sh)"
  154. echo $ext | grep -q '\.' && exit_1 "error: extension cannot contain '.'"
  155.  
  156. # construct pastebin url for retrieving raw file (obscure from hosting site)
  157. url="$(echo wastebin | sed s/w/p/).com/raw/$pbid"
  158.  
  159. if [ ! "$file_dir" ]; then
  160.     # locate storage
  161.     if is_mounted '/jffs'; then
  162.         file_dir='/jffs/etc/config'
  163.     else
  164.         file_dir='/tmp'
  165.         echo 'warning: /jffs not mounted; using /tmp'
  166.     fi
  167. fi
  168.  
  169. # convert cryptic pastebin id to common name
  170. case "$pbid" in
  171.     'aySi7RhY') file='ddwrt-blacklist-domains';;
  172.     'MxA19W1M') file='ddwrt-dhcp-lease-persist';;
  173.     'VDZ32r2D') file='ddwrt-mount-usb-drives';;
  174.     'TKdKUmY1') file='ddwrt-ovpn-client-killswitch';;
  175.     'iNC273ER') file='ddwrt-ovpn-client-watchdog';;
  176.     'gnxtZuqg') file='ddwrt-ovpn-remote-access';;
  177.     'nC27ETsp') file='ddwrt-ovpn-split-advanced';;
  178.     'W2P3TDZT') file='ddwrt-ovpn-split-basic';;
  179.     '9DUMFJgN') file='ddwrt-pptp-policy-based-routing';;
  180.     'NkKUUjsn') file='ddwrt-ultimate-dns-leak-test';;
  181.     'NUb73JqK') file='ddwrt-wol-port-forward';;
  182.     '2gg5ZdRY') file='importvpncl';;
  183.              *) file="pastebin-$pbid";;
  184. esac
  185.  
  186. # check for existing files
  187. efiles="$(echo $file_dir/*$file*)"
  188.  
  189. if [ ! ${noprompt+x} ] && [ "$efiles" != "$file_dir/*$file*" ]; then
  190.     efile_count=0
  191.  
  192.     for efile in $efiles; do
  193.         echo "warning: existing file: $efile"
  194.         let efile_count++
  195.     done
  196.  
  197.     if [ $efile_count -gt 0 ]; then
  198.         # obtain permission to overwrite existing file(s)
  199.         while :; do
  200.             case "$(query 'Overwrite existing file(s) (yes/[no])?' 'no')" in
  201.                 'yes') break;;
  202.                  'no') exit_0 'info: installation aborted';;
  203.             esac
  204.         done
  205.  
  206.         # delete existing files
  207.         for efile in $efiles; do rm -f $efile; done
  208.     fi
  209. fi
  210.  
  211. # option pre: add prefix to filename
  212. [ "$file_pre" ] && file="${file_pre}-$file"
  213.  
  214. # create directory
  215. if ! mkdir -p "$file_dir" 2>/dev/null; then
  216.     exit_1 "error: cannot create directory: $file_dir"
  217. fi
  218.  
  219. # construct full path + filename + extension
  220. file="$file_dir/$file.$ext"
  221.  
  222. # confirm file can be created through initialization
  223. if ! (> "$file") 2>/dev/null; then
  224.     exit_1 "error: cannot create file: $file"
  225. fi
  226.  
  227. # retrieve raw file from pastebin
  228. $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
  229.  
  230. # verify file is bash script by locating shebang
  231. if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|[[:space:]]+)'; then
  232.     rm -f "$file"
  233.     exit_1 "error: file not found: $pbid"
  234. fi
  235.  
  236. # option nocom: remove blank lines and non-functional comments
  237. [ ${nocom+x} ] && sed -ri '/^[[:space:]]*($|#([[:space:]]|#|$))/d' "$file"
  238.  
  239. # option comp: same as --nocom, plus remove leading whitespace
  240. [ ${comp+x}  ] && sed -ri 's/^[[:space:]]*//;/^($|#([[:space:]]|#|$))/d' "$file"
  241.  
  242. # option debug: install script w/ debugging mode enabled
  243. [ ${debug+x} ] && sed -ri '2 s/^#(DEBUG=; )/\1/' "$file"
  244.  
  245. # mark file executable
  246. chmod +x "$file"
  247.  
  248. # check for any script-specific customization
  249. customize
  250.  
  251. exit_0 "installed: $file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement