Назад | Перейти на главную страницу

Добавление маршрута iptables в openvpn connect

У меня есть сервер openvpn, и я отправляю определенные маршруты своим клиентам с помощью директивы ccd. Я хотел бы знать, как я могу обновить iptables на основе файлов ccd при подключении клиента.

Итак, допустим, мой ccd для client1:

ifconfig-push 10.8.0.45 255.255.255.0
push 'route 10.10.0.45'

и я хочу добавить это в iptables.

iptables -A FORWARD -s 10.8.0.45 -d 10.10.0.45 -j ACCEPT

а потом

iptables -A FORWARD -s 10.8.0.0/24 -d 10.10.0.0/16 -j DROP

Если кто-то может указать мне в правильном направлении, я буду очень признателен, я довольно новичок в сценариях bash

Вы можете подключиться к OpenVPN конфигурация множества скриптов, которые получают множество параметров от сервера как переменные среды: ср. Справочное руководство.

Вас больше всего интересуют up, down скрипты для вставки DROP правило при запуске и завершении работы сервера и client-connect и client-disconnect сценарий для правил для каждого клиента. Вам необходимо изменить конфигурацию вашего сервера, чтобы он содержал:

# Allow user scripts
script-security 2
# up/down script
up /etc/openvpn/updown.sh
down /etc/openvpn/updown.sh
# Client connect/disconnect
client-connect /etc/openvpn/client.sh
client-disconnect /etc/openvpn/client.sh
  1. Ваш /etc/openvpn/updown.sh сценарий создаст OPENVPN и свяжите его с FORWARD цепочка:
#!/bin/bash
IPT=/usr/sbin/iptables
# 'script_type' contains the type of the script
if [ "$script_type" = "up" ]; then
  $IPT -N OPENVPN
  $IPT -A FORWARD -j OPENVPN
  $IPT -A OPENVPN -s 10.8.0.0/24 -d 10.10.0.0/16 -j DROP
else
  $IPT -F OPENVPN
  $IPT -D FORWARD -j OPENVPN
  $IPT -X OPENVPN
fi
  1. Ваш клиентский скрипт /etc/openvpn/client.sh будет сложнее: в то время как публичный и частный IP-адреса удаленного клиента содержатся в ifconfig_remote и ifconfig_pool_remote_ip, вам нужно будет проанализировать файл ccd, чтобы узнать, какие маршруты вы отправили клиенту:
#!/bin/bash
IPT=/usr/sbin/iptables
# We need to split the line into words as bash would, by
# interpreting the double quotes, hence the 'eval'.
function parse_ccd_line() {
  eval "local line=($1)"
  # If the first word is 'push' return the second one.
  if [ "${line[0]}" = "push" ]; then
    echo "${line[1]}"
  fi
}

# Your ccd_dir so we don't need to parse the OpenVPN
# server config file too.
ccd_dir=/etc/openvpn/ccd

if [ -f "$ccd_dir/$common_name" ]; then
  # We read the "$ccd_dir/$common_name" file line by line:
  while read line; do
    # We split the argument of every 'push' directive into 'cmd' and 'arg1'
    # If you need more arguments, the array 'push_opt' contains them.
    push_opt=($(parse_ccd_line "$line"))
    cmd=${push_opt[0]}
    arg1=${push_opt[1]}
    # We use just the 'route' commands
    if [ "$cmd" = "route" ]; then
      if [ "$script_type" = "client-connect" ]; then
        $IPT -I OPENVPN -s "$ifconfig_pool_remote_ip" -d "$arg1" -j ACCEPT
      else
        $IPT -D OPENVPN -s "$ifconfig_pool_remote_ip" -d "$arg1" -j ACCEPT
      fi
    fi
  done < "$ccd_dir/$common_name"
fi