rule add

#!/bin/bash

# clear all the existing rules
function clear_existing_rules()
{
    systemctl stop firewalld.service
    firewall-cmd --delete-service=openvpn
    iptables -F
    iptables -X
    iptables -Z
}


# start firewalld.server
function start_firewalld_add_basic_rule()
{
    systemctl start firewalld.service
    firewall-cmd --add-service=openvpn
}

# convert IP_type netmask to number
function convert_netmaks_to_number()
{
        subnet=$1
        ip_netmask=$2
        number_netmask=`ipcalc -p 0.0.0.1 $ip_netmask |awk -F= '{print $2}'`
        subnet_with_mask="${subnet}/${number_netmask}"
}

# get VPN subnet
function get_vpn_subnet_with_mask()
{
    vpnserver_type=`egrep -v "^#|^ *$|^;" ${config_file} | grep "^dev" | awk -F' ' '{print $2}'`
    if [ "${vpnserver_type}" == "tun" ]; then
        subnet=`egrep -v "^#|^ *$|^;" ${config_file} | grep "^server" | awk -F' ' '{print $2}'`
        netmask=`egrep -v "^#|^ *$|^;" ${config_file} | grep "^server" | awk -F' ' '{print $3}'`
    elif [ "${vpnserver_type}" == "tap0" ]; then
        subnet=`egrep -v "^#|^ *$|^;" ${config_file} | grep server-bridge | awk -F' ' '{print $2}'`
        netmask=`egrep -v "^#|^ *$|^;" ${config_file} | grep server-bridge | awk -F' ' '{print $3}'`
    fi
    convert_netmaks_to_number "${subnet}" "${netmask}"
}

# add additional firewall rules
function add_additional_firewall_rules()
{
    subnet_with_mask=$1
    echo "adding iptables rule started" >> /var/log/openvpn.log
    echo "firewall-cmd --direct --passthrough ipv4 -t nat -A POSTROUTING -s ${subnet_with_mask} -o eth1 -j MASQUERADE" >> /var/log/openvpn.log
    iptables -F
    iptables -X
    iptables -Z
    firewall-cmd --direct --passthrough ipv4 -t nat -A POSTROUTING -s ${subnet_with_mask} -o eth1 -j MASQUERADE
    echo "adding iptables rule finished" >> /var/log/openvpn.log
}

config_file=$1

clear_existing_rules
start_firewalld_add_basic_rule
get_vpn_subnet_with_mask
add_additional_firewall_rules ${subnet_with_mask}

posted on   yaoweilei  阅读(175)  评论(0编辑  收藏  举报

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示