AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1004991
Accepted
Kam-ALIEN
Kam-ALIEN
Asked: 2020-02-29 08:03:02 +0800 CST2020-02-29 08:03:02 +0800 CST 2020-02-29 08:03:02 +0800 CST

在 openvpn 连接上添加 iptables 路由

  • 772

我的 openvpn 服务器启动并运行,我通过 ccd 指令将某些路由推送到我的客户端,我想知道如何在客户端连接时根据 ccd 文件更新 iptables。

所以可以说我的客户 1 的 ccd 是:

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 脚本相当陌生

scripting openvpn bash iptables
  • 1 1 个回答
  • 1482 Views

1 个回答

  • Voted
  1. Best Answer
    Piotr P. Karwasz
    2020-02-29T12:34:22+08:002020-02-29T12:34:22+08:00

    您可以将许多脚本挂接到OpenVPN配置中,这些脚本从服务器接收许多参数作为环境变量:cf。参考手册。

    您最感兴趣的是在服务器启动和关闭时插入规则up的脚本和每个客户端规则的脚本。您需要修改服务器配置以包含:downDROPclient-connectclient-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_remoteandifconfig_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
    
    • 3

相关问题

  • 知道任何适用于 Windows 的快速可编写脚本的 ftp 客户端吗?[关闭]

  • 如果同一个任务已经在运行,如何防止计划任务运行?

  • 需要从 Batch For 循环中排除特定结果

  • Bash 脚本:要求脚本以 root 身份运行(或使用 sudo)

  • 从命令行删除旧的(非引导)Windows Vista 目录

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve