summaryrefslogtreecommitdiff
path: root/through-vpn.sh
blob: b35623b6fdbf82eca543f0eb032b9db681d5d75a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
# Add host to RouterOS address list.
#

set -euo pipefail

LIST_NAME="through-vpn"
ROUTER_HOST="riga"

usage() {
  cat <<EOF
Usage: ${0##*/} [-t|-n|-h] name...

Name could be a domain name or URL.

Options:
  -h    print this help
  -t    timeout value (ex: 1m, 8h)
  -n    dry run, just print commands
EOF
}

extract_domain() {
  cat <<EOF | python3
from urllib.parse import urlparse

url = urlparse(r'$1')
print(url.hostname or '$1')
EOF
}

create_router_commands() {
  local timeout="$1"
  shift
  for name in "$@"; do
    domain=$(extract_domain "$name")
    echo "/ip firewall address-list add address=$domain list=$LIST_NAME timeout=$timeout"
  done
}

main() {
  local timeout=
  local dryrun=0
  while getopts ":nt:" opt; do
    case "${opt}" in
    t)
      timeout="${OPTARG}"
      ;;
    n)
      dryrun=1
      ;;
    *)
      usage
      exit
      ;;
    esac
  done
  shift $((OPTIND - 1))

  if [ $# -eq 0 ]; then
    echo 'First argument must be domain or URL'
    exit 1
  elif [ $dryrun -eq 0 ]; then
    create_router_commands "$timeout" "$@" | ssh "$ROUTER_HOST"
  else
    create_router_commands "$timeout" "$@"
  fi
}

main "$@"