Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- #set -x # uncomment/comment to enable/disable debug mode
- # name: ddwrt-installer.sh
- # version: 3.2.0, 10-nov-2024, by eibgrad
- # purpose: install pastebin script into dd-wrt router
- # function usage()
- usage() {
- echo 'Usage: ddwrt-installer.sh [options] pastebin-id [ext=sh]'
- echo
- echo ' Install pastebin script into dd-wrt router.'
- echo
- echo ' Installation directory hierarchy:'
- echo ' </path> (from --dir option)'
- echo ' /jffs/etc/config'
- echo ' /tmp'
- echo
- echo ' Options:'
- echo ' --dir PATH installation directory (created as necessary)'
- echo " --pre NUM add prefix to filename (e.g., '01')"
- echo ' --nocom remove blank lines and non-functional comments'
- echo ' --comp same as --nocom, plus remove leading whitespace'
- echo ' --noprompt silently overwrite files and accept defaults'
- echo ' --debug install script w/ debugging mode enabled'
- echo ' -h,--help this usage information'
- echo
- echo ' # e.g., install script w/o comments and w/ bash (sh) extension'
- echo ' ddwrt-installer.sh --nocom MxA19W1M'
- echo
- }
- # function query( message [default-reply] )
- query() {
- local reply
- read -p "$1 " reply < /dev/tty
- [ "$reply" ] && echo "$reply" || echo "$2"
- }
- # function is_mounted( mounting-point )
- is_mounted() { df | grep -Eq "[[:space:]]+${1}$"; }
- # function customize()
- customize() {
- # function _ddwrt_ovpn_split()
- _ddwrt_ovpn_split() {
- local route_up
- local route_down
- local default_vpn
- # function __add_directives()
- __add_directives() {
- local CONFIG='openvpncl_config'
- local TEMPF="/tmp/ddwrt-installer.$$.tmp"
- # retrieve current openvpn client config
- nvram get $CONFIG | tr -d '\r' > $TEMPF
- # delete any matching/competing directives (active or inactive)
- sed -r \
- -e '/^([[:space:]]|#|;)*(|--)route-up/d' \
- -e '/^([[:space:]]|#|;)*(|--)route-pre-down/d' \
- -e '/^([[:space:]]|#|;)*(|--)pull-filter.*redirect-gateway/d' \
- -e '/^([[:space:]]|#|;)*(|--)redirect-gateway/d' \
- -e '/^([[:space:]]|#|;)*(|--)redirect-private/d' \
- -i $TEMPF
- # delete old banner header and footer
- sed -i '/ added by ddwrt-installer /d' $TEMPF
- # configure default gateway (wan or vpn)
- local com="$([ ${default_vpn+x} ] && echo '#')"
- # add our directives
- {
- echo '# --- begin directives added by ddwrt-installer --- #'
- echo "route-up ${route_up}"
- echo "route-pre-down ${route_down}"
- echo "${com}pull-filter ignore redirect-gateway"
- echo "${com}redirect-private def1"
- echo '# ---- end directives added by ddwrt-installer ---- #'
- } >> $TEMPF
- # delete blank lines
- sed -i '/^[[:space:]]*$/d' $TEMPF
- # update openvpn client config
- nvram set $CONFIG="$(cat $TEMPF | tr -d '\r')"
- nvram commit &>/dev/null
- rm -f $TEMPF
- }
- if [ ! ${noprompt+x} ]; then
- # obtain openvpn default routing preference (default=lan->wan)
- while :; do
- case "$(query 'Default routing ([1]=lan->wan 2=lan->vpn)?' '1')" in
- '1') break;;
- '2') default_vpn=; break;;
- esac
- done
- fi
- route_up="$file_dir/route-up"
- route_down="$file_dir/route-down"
- # add symbolic links
- ln -sf "$file" "$route_up"
- ln -sf "$file" "$route_down"
- __add_directives
- }
- # script-specific customization (optional)
- case "$pbid" in
- 'nC27ETsp') _ddwrt_ovpn_split;;
- 'W2P3TDZT') _ddwrt_ovpn_split;;
- *) ;;
- esac
- }
- # function exit_1( [message] )
- exit_1() { [ "$1" ] && echo "$1"; exit 1; }
- # function exit_0( [message] )
- exit_0() { [ "$1" ] && echo "$1"; exit 0; }
- # handle help/usage requests
- for opt; do case $opt in -h|--help) usage; exit 0;; esac; done
- # try curl, fallback to wget
- which curl &>/dev/null && GET_URL='curl -sLk' || GET_URL='wget -qO -'
- # process command line options/arguments
- while [ $# -gt 0 ]; do
- case $1 in
- '--dir') shift; file_dir="${1//[[:space:]]/}";;
- '--pre') shift; file_pre="${1//[[:space:]]/}";;
- '--nocom') nocom=; unset comp;;
- '--comp') comp=; unset nocom;;
- '--noprompt') noprompt=;;
- '--debug') debug=;;
- *) break 2;;
- esac
- shift
- done
- # set pastebin-id
- [ "$1" ] && pbid="$1" || exit_0 'info: nothing to do'
- # set and verify extension
- ext="$([ "$2" ] && echo "${2//[[:space:]]/}" || echo sh)"
- echo $ext | grep -q '\.' && exit_1 "error: extension cannot contain '.'"
- # construct pastebin url for retrieving raw file (obscure from hosting site)
- url="$(echo wastebin | sed s/w/p/).com/raw/$pbid"
- if [ ! "$file_dir" ]; then
- # locate storage
- if is_mounted '/jffs'; then
- file_dir='/jffs/etc/config'
- else
- file_dir='/tmp'
- echo 'warning: /jffs not mounted; using /tmp'
- fi
- fi
- # convert cryptic pastebin id to common name
- case "$pbid" in
- 'gnxtZuqg') file='ddwrt-bind-static-routes-to-wan';;
- 'aySi7RhY') file='ddwrt-blacklist-domains';;
- 'MxA19W1M') file='ddwrt-dhcp-lease-persist';;
- 'VDZ32r2D') file='ddwrt-mount-usb-drives';;
- 'TKdKUmY1') file='ddwrt-ovpn-client-killswitch';;
- 'iNC273ER') file='ddwrt-ovpn-client-watchdog';;
- '4QyiANWh') file='ddwrt-ovpn-server-watchdog';;
- 'nC27ETsp') file='ddwrt-ovpn-split-advanced';;
- 'W2P3TDZT') file='ddwrt-ovpn-split-basic';;
- '9DUMFJgN') file='ddwrt-pptp-policy-based-routing';;
- 'NkKUUjsn') file='ddwrt-ultimate-dns-leak-test';;
- 'NUb73JqK') file='ddwrt-wol-port-forward';;
- '2gg5ZdRY') file='importvpncl';;
- *) file="pastebin-$pbid";;
- esac
- # check for existing files
- efiles="$(echo $file_dir/*$file*)"
- if [ ! ${noprompt+x} ] && [ "$efiles" != "$file_dir/*$file*" ]; then
- efile_count=0
- for efile in $efiles; do
- echo "warning: existing file: $efile"
- let efile_count++
- done
- if [ $efile_count -gt 0 ]; then
- # obtain permission to overwrite existing file(s)
- while :; do
- case "$(query 'Overwrite existing file(s) (yes/[no])?' 'no')" in
- 'yes') break;;
- 'no') exit_0 'info: installation aborted';;
- esac
- done
- # delete existing files
- for efile in $efiles; do rm -f $efile; done
- fi
- fi
- # option pre: add prefix to filename
- [ "$file_pre" ] && file="${file_pre}-$file"
- # create directory
- if ! mkdir -p "$file_dir" 2>/dev/null; then
- exit_1 "error: cannot create directory: $file_dir"
- fi
- # construct full path + filename + extension
- file="$file_dir/$file.$ext"
- # confirm file can be created through initialization
- if ! (> "$file") 2>/dev/null; then
- exit_1 "error: cannot create file: $file"
- fi
- # retrieve raw file from pastebin
- $GET_URL $url | tr -d '\r' > "$file"; echo >> "$file"
- # verify file is bash script by locating shebang
- if ! head -n1 "$file" | grep -Eq '^#!/bin/sh($|[[:space:]]+)'; then
- rm -f "$file"
- exit_1 "error: file not found: $pbid"
- fi
- # option nocom: remove blank lines and non-functional comments
- [ ${nocom+x} ] && sed -ri '/^[[:space:]]*($|#([[:space:]]|#|$))/d' "$file"
- # option comp: same as --nocom, plus remove leading whitespace
- [ ${comp+x} ] && sed -ri 's/^[[:space:]]*//;/^($|#([[:space:]]|#|$))/d' "$file"
- # option debug: install script w/ debugging mode enabled
- [ ${debug+x} ] && sed -ri '2 s/^#(DEBUG=; )/\1/' "$file"
- # mark file executable
- chmod +x "$file"
- # check for any script-specific customization
- customize
- exit_0 "installed: $file"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement