Advertisement
eibgrad

ddwrt-installer.sh

May 25th, 2019 (edited)
1,291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.98 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.2.0, 10-nov-2024, 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.     'gnxtZuqg') file='ddwrt-bind-static-routes-to-wan';;
  172.     'aySi7RhY') file='ddwrt-blacklist-domains';;
  173.     'MxA19W1M') file='ddwrt-dhcp-lease-persist';;
  174.     'VDZ32r2D') file='ddwrt-mount-usb-drives';;
  175.     'TKdKUmY1') file='ddwrt-ovpn-client-killswitch';;
  176.     'iNC273ER') file='ddwrt-ovpn-client-watchdog';;
  177.     '4QyiANWh') file='ddwrt-ovpn-server-watchdog';;
  178.     'nC27ETsp') file='ddwrt-ovpn-split-advanced';;
  179.     'W2P3TDZT') file='ddwrt-ovpn-split-basic';;
  180.     '9DUMFJgN') file='ddwrt-pptp-policy-based-routing';;
  181.     'NkKUUjsn') file='ddwrt-ultimate-dns-leak-test';;
  182.     'NUb73JqK') file='ddwrt-wol-port-forward';;
  183.     '2gg5ZdRY') file='importvpncl';;
  184.              *) file="pastebin-$pbid";;
  185. esac
  186.  
  187. # check for existing files
  188. efiles="$(echo $file_dir/*$file*)"
  189.  
  190. if [ ! ${noprompt+x} ] && [ "$efiles" != "$file_dir/*$file*" ]; then
  191.     efile_count=0
  192.  
  193.     for efile in $efiles; do
  194.         echo "warning: existing file: $efile"
  195.         let efile_count++
  196.     done
  197.  
  198.     if [ $efile_count -gt 0 ]; then
  199.         # obtain permission to overwrite existing file(s)
  200.         while :; do
  201.             case "$(query 'Overwrite existing file(s) (yes/[no])?' 'no')" in
  202.                 'yes') break;;
  203.                  'no') exit_0 'info: installation aborted';;
  204.             esac
  205.         done
  206.  
  207.         # delete existing files
  208.         for efile in $efiles; do rm -f $efile; done
  209.     fi
  210. fi
  211.  
  212. # option pre: add prefix to filename
  213. [ "$file_pre" ] && file="${file_pre}-$file"
  214.  
  215. # create directory
  216. if ! mkdir -p "$file_dir" 2>/dev/null; then
  217.     exit_1 "error: cannot create directory: $file_dir"
  218. fi
  219.  
  220. # construct full path + filename + extension
  221. file="$file_dir/$file.$ext"
  222.  
  223. # confirm file can be created through initialization
  224. if ! (> "$file") 2>/dev/null; then
  225.     exit_1 "error: cannot create file: $file"
  226. fi
  227.  
  228. # retrieve raw file from pastebin
  229. $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
  230.  
  231. # verify file is bash script by locating shebang
  232. if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|[[:space:]]+)'; then
  233.     rm -f "$file"
  234.     exit_1 "error: file not found: $pbid"
  235. fi
  236.  
  237. # option nocom: remove blank lines and non-functional comments
  238. [ ${nocom+x} ] && sed -ri '/^[[:space:]]*($|#([[:space:]]|#|$))/d' "$file"
  239.  
  240. # option comp: same as --nocom, plus remove leading whitespace
  241. [ ${comp+x}  ] && sed -ri 's/^[[:space:]]*//;/^($|#([[:space:]]|#|$))/d' "$file"
  242.  
  243. # option debug: install script w/ debugging mode enabled
  244. [ ${debug+x} ] && sed -ri '2 s/^#(DEBUG=; )/\1/' "$file"
  245.  
  246. # mark file executable
  247. chmod +x "$file"
  248.  
  249. # check for any script-specific customization
  250. customize
  251.  
  252. exit_0 "installed: $file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement