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
    • 最新
    • 标签
主页 / user-355465

Server Programmer's questions

Martin Hope
Server Programmer
Asked: 2016-05-31 16:27:00 +0800 CST

添加基于 MAC 地址的过滤器

  • 0

谁能告诉我如何根据他们的 MAC 地址过滤(更改带宽、延迟等)连接到 OpenVPN 的一个或多个用户?

为什么我想使用 MAC 地址是因为我可以有 100 名用户连接到 OpenVPN,并且我想单独限制每个客户端的带宽

非常感谢

openvpn
  • 1 个回答
  • 2407 Views
Martin Hope
Server Programmer
Asked: 2016-05-27 06:38:25 +0800 CST

当新用户连接和带宽塑造连接时自动调用脚本

  • 6

我希望这很容易

up.sh当我从命令行以 root 身份运行以下脚本时,它完美运行。

但是,不是每次新用户连接到 OpenVPN 时手动调用此脚本,以通过 tc (qdisc) 单独限制每个新用户(User1、User2、User3 到无穷大)的带宽、延迟等,我希望脚本每次新用户连接到 OpenVPN 时被调用,并且当新用户连接时能够单独调整新用户的带宽、延迟等,而不会影响当前用户的带宽、延迟等(可能是 100 或1000)

我尝试将脚本移动到以下文件夹/etc/network/if-up.d,以便在新用户连接到 OpenVPN 时执行它,但是由于某种原因,该脚本没有被调用(它对 qdisc 没有任何更改),但它是完全相同的脚本并且可以完美运行当我从命令行执行它时。

我还尝试将脚本重命名为learn-address.sh并将其放在以下文件夹/etc/openvpn/netem/learn-address.sh中,以便在 OpenVPN 学习新地址时自动调用,但这也不起作用

我还更新了 server.conf 文件,内容如下

脚本安全 3

学习地址 /etc/openvpn/netem/learn-address.sh

和

脚本安全 3

向上 /etc/network/if-up.d/up.sh

但它也没有工作

最后,我还尝试更新/etc/sudoers.tmp文件以授予脚本权限,但这似乎也无济于事(见文章末尾)

我正在运行 Ubuntu 14.04

非常感谢您的帮助

当我从命令行调用它时,这是一个名为 up.sh 的脚本:

#!/bin/bash  
# Full path to tc binary 

TC=$(which tc)

#
# NETWORK CONFIGURATION
# interface - name of your interface device
# interface_speed - speed in mbit of your $interface
# ip - IP address of your server, change this if you don't want to use
#      the default catch all filters.
#
interface=eth0
interface_speed=100mbit
ip=4.1.2.3 # The IP address bound to the interface

# Define the upload and download speed limit, follow units can be 
# passed as a parameter:
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: kilobits per second
# mbit: megabits per second
# bps: Bytes per second
download_limit=512kbit
upload_limit=10mbit    


# Filter options for limiting the intended interface.
FILTER="$TC filter add dev $interface protocol ip parent 1: prio 1 u32"

#
# This function starts the TC rules and limits the upload and download speed
# per already configured earlier.
# 

function start_tc { 
    tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"  
    [ "$?" -gt "0" ] && tc qdisc del dev $interface root; sleep 1  

    # start the tc configuration
    $TC qdisc add dev $interface root handle 1: htb default 30
    $TC class add dev $interface parent 1: classid 1:1 htb rate $interface_speed burst 15k

    $TC class add dev $interface parent 1:1 classid 1:10 htb rate $download_limit burst 15k
    $TC class add dev $interface parent 1:1 classid 1:20 htb rate $upload_limit burst 15k

    $TC qdisc add dev $interface parent 1:10 handle 10: sfq perturb 10
    $TC qdisc add dev $interface parent 1:20 handle 20: sfq perturb 10

    # Apply the filter rules
    
    # Catch-all IP rules, which will set global limit on the server
    # for all IP addresses on the server. 
    $FILTER match ip dst 0.0.0.0/0 flowid 1:10
    $FILTER match ip src 0.0.0.0/0 flowid 1:20

    # If you want to limit the upload/download limit based on specific IP address
    # you can comment the above catch-all filter and uncomment these:
    #
    # $FILTER match ip dst $ip/32 flowid 1:10
    # $FILTER match ip src $ip/32 flowid 1:20
}

#
# Removes the network speed limiting and restores the default TC configuration
#
function stop_tc {
    tc qdisc show dev $interface | grep -q "qdisc pfifo_fast 0"
    [ "$?" -gt "0" ] && tc qdisc del dev $interface root
}

function show_status {
        $TC -s qdisc ls dev $interface
}
#
# Display help 
#
function display_help {
        echo "Usage: tc [OPTION]"
        echo -e "\tstart - Apply the tc limit"
        echo -e "\tstop - Remove the tc limit"
        echo -e "\tstatus - Show status"
}

# Start
if [ -z "$1" ]; then
        display_help
elif [ "$1" == "start" ]; then
        start_tc
elif [ "$1" == "stop" ]; then
        stop_tc
elif [ "$1" == "status" ]; then
        show_status
fi

这是我还更新的以下文件:

/etc/sudoers.tmp

#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification    

# User privilege specification
root    ALL=(ALL:ALL) ALL
#nobody ALL=(ALL) NOPASSWD: /usr/lib/tc
nobody ALL=(ALL) NOPASSWD: /usr/lib/tc
www-data ALL=NOPASSWD: /user/lib/tc
root ALL=NOPASSWD: /user/lib/tc
root    ALL=(ALL:ALL) ALL
nobody  ALL=(ALL) NOPASSWD
nobody  ALL=(ALL) NOPASSWD: /etc/openvpn/netem/learn-address.sh
root  ALL=(ALL) NOPASSWD: /etc/openvpn/netem/learn-address.sh
www-data  ALL=(ALL) NOPASSWD: /etc/openvpn/netem/learn-address.sh
nobody  ALL=(ALL) NOPASSWD: /etc/openvpn/netem/up.sh
www-data  ALL=(ALL) NOPASSWD: /etc/openvpn/netem/up.sh
root  ALL=(ALL) NOPASSWD: /etc/openvpn/netem/up.sh
nobody  ALL=(ALL) NOPASSWD: /etc/network/if-up.d/up.sh
www-data  ALL=(ALL) NOPASSWD: /etc/network/if-up.d/up.sh
root  ALL=(ALL) NOPASSWD: /etc/network/if-up.d/up.sh  
    
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

这是server.conf

port 1194
proto udp
dev tun
sndbuf 0
rcvbuf 0
ca ca.crt
cert server.crt
key server.key
dh dh.pem
tls-auth ta.key 0
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
cipher AES-128-CBC
comp-lzo
#user nobody
#user openvpn
#group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
crl-verify crl.pem
script-security 2
down-pre
up /etc/openvpn/tc.sh
down /etc/openvpn/tc.sh
client-connect /etc/openvpn/tc.sh
client-disconnect /etc/openvpn/tc.sh
log /var/log/openvpn.log
openvpn bandwidth
  • 1 个回答
  • 11651 Views
Martin Hope
Server Programmer
Asked: 2016-05-22 09:51:58 +0800 CST

Bash 脚本不工作

  • 0

我希望这是一个简单的答案

问题:

  1. 我将以下名为 learn-address.sh 的 bash 脚本放在以下文件夹中:

vi /etc/openvpn/netem/learn-address.sh

  1. 在 .conf 文件中添加了以下 (2) 行:
script-security 3

learn-address /etc/openvpn/netem/learn-address.sh
  1. 并对 learn-address 脚本应用以下权限:
chmod 755 /etc/openvpn/netem/learn-address.sh
  1. 但是,该脚本确实会更新 tmp 文件中的文件($ip.classid 和 $ip.dev)并正确传递变量

  2. 但是 bash 脚本不执行 tc 类和过滤命令(qdisc 没有变化)

  3. 当用户连接到 OpenVPN 时调用 learn-address 脚本时,我将在脚本上使用什么权限来执行 tc 类和过滤命令,或者我错过了什么?

非常感谢

脚本名称:learn-address.sh

#!/bin/bash

statedir=/tmp/

function bwlimit-enable() {
ip=$1
user=$2
dev=eth0


# Disable if already enabled.
bwlimit-disable $ip

# Find unique classid.
if [ -f $statedir/$ip.classid ]; then
    # Reuse this IP's classid
    classid=`cat $statedir/$ip.classid`
else
    if [ -f $statedir/last_classid ]; then
        classid=`cat $statedir/last_classid`
        classid=$((classid+1))
    else
        classid=1
    fi
    echo $classid > $statedir/last_classid
fi

# Find this user's bandwidth limit
# downrate: from VPN server to the client
# uprate: from client to the VPN server
if [ "$user" == "myuser" ]; then
    downrate=10mbit
    uprate=10mbit
elif [ "$user" == "anotheruser"]; then
    downrate=2mbit
    uprate=2mbit
else
    downrate=5mbit
    uprate=5mbit
fi

# Limit traffic from VPN server to client
tc class add dev $dev parent 1: classid 1:$classid htb rate $downrate
tc filter add dev $dev protocol all parent 1:0 prio 1 u32 match ip dst $ip/32 flowid 1:$classid

# Limit traffic from client to VPN server
tc filter add dev $dev parent ffff: protocol all prio 1 u32 match ip src $ip/32 police rate $uprate burst 80k drop flowid :$classid

# Store classid and dev for further use.
echo $classid > $statedir/$ip.classid
echo $dev > $statedir/$ip.dev
}

function bwlimit-disable() {
ip=$1

if [ ! -f $statedir/$ip.classid ]; then
    return
fi
if [ ! -f $statedir/$ip.dev ]; then
    return
fi

classid=`cat $statedir/$ip.classid`
dev=`cat $statedir/$ip.dev`

tc filter del dev $dev protocol all parent 1:0 prio 1 u32 match ip dst $ip/32
tc class del dev $dev classid 1:$classid

tc filter del dev $dev parent ffff: protocol all prio 1 u32 match ip src $ip/32

# Remove .dev but keep .classid so it can be reused.
rm $statedir/$ip.dev
}

# Make sure queueing discipline is enabled.
tc qdisc add dev $dev root handle 1: htb 2>/dev/null || /bin/true
tc qdisc add dev $dev handle ffff: ingress 2>/dev/null || /bin/true

case "$1" in
    add|update)
        bwlimit-enable $2 $3
        ;;
    delete)
        bwlimit-disable $2
        ;;
    *)
        echo "$0: unknown operation [$1]" >&2
        exit 1
        ;;
esac

exit 0
openvpn bash bandwidth tc
  • 1 个回答
  • 354 Views
Martin Hope
Server Programmer
Asked: 2016-05-20 13:42:14 +0800 CST

如何对每个 OpenVPN 客户端使用 TC 进行流量整形(速率限制)

  • 3

这个问题与来自@Oliver的一个很好的答案和脚本的另一个问题有关。

目标:我想修改/扩展此答案中提供的脚本 以满足我的要求,如下所示:

  1. 我有大量客户(最多 1000 个)。每个客户端应根据其 CN(通用名称)分配一个订阅类别和相应的最大数据速率。这些速率限制应在客户端连接时应用,并在其断开连接时删除:

    • bronze: 1 兆位
    • silver: 10 兆比特
    • gold: 100 兆位
  2. 我想在客户端连接到 OpenVPN 服务器时即时调整每个客户端的订阅类别和相应的活动数据速率限制。客户端不必重新连接到 OpenVPN 服务器。这是可能的还是我们必须断开每个客户端并将其重新连接到 OpenVPN 以导致再次调用脚本来更改tc配置?

  3. tc我们将如何从另一台计算机或应用程序(即通过 PHP)即时更新客户端订阅类和相应的活动数据速率限制,而不是使用 shell 手动修改配置?

非常感谢

openvpn bandwidth rate-limiting tc
  • 1 个回答
  • 14554 Views

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