大网数据广告活动
服务器运维问答

Permit only N connection for a user/config in openvpn

· 52CE 我爱测

I have openvpn on a server and a few client configs. How can I permit only a single connection for a user? Or if not necessarily single connection, but no more than N connections for user. I've not found anything regarding this m…

问题原文

原帖链接 · Jodimoro · CC BY-SA 3.0

I have openvpn on a server and a few client configs. How can I permit only a single connection for a user? Or if not necessarily single connection, but no more than N connections for user. I've not found anything regarding this matter in the documentation.

已采纳解答

原帖链接 · Lacek · CC BY-SA 4.0

If you provide your own connect scripts, you can control how many connections are allowed per user. The only catch is that you need to have a persistent storage of some kind. Note that the default behavior is to allow a client only once.

Here is what you need to do.

On the server, include the following lines in the config:

script-security 2
duplicate-cn
up /etc/openvpn/connectScript.sh
client-connect /etc/openvpn/connectScript.sh
client-disconnect /etc/openvpn/connectScript.sh

The duplicate-cn is needed for having simultaneous client connections, the script-security 2 is needed for running external scripts. On the client, you need to add the explicit-exit-notify option, because without that, the server won't receive a disconnect message when the client exits or disconnects, thus, the disconnect script will be called only upon timeout.

The following environmental variables will be exported for the script (along with others -- see the "Environmental Variables" section of the manpage of openvpn):

  • script_type can be either up, client-connect or client-disconnect, depending on when the script is called.
  • common_name will contain the name of the user connecting, that is, the value of the CN field of the certificate or the username, depending on what kind of authentication you have on the server.

If script_type is up, you need to clear all client connection data, since at that time openvpn just started, and no client connections are present.

If script_type is client-connect, then your script needs to decide if the user specified in the common_name variable has already been connected, and if so, how many times. If the connection is permitted, the script should exit with a value of 0. Any other exit value is interpreted as an error, and the client won't connect. So you need to count the clients with the same name, and exit with an error if there are too many connections for that user. You should decrease the count when script_type is client-disconnect.

You need to make sure that the script is runnable by the user who runs the openvpn process, which is preferably not root (specified by the user and group configuration variables).

Here is a sample connect script. using this, every client can have two connections, the third will be denied. This is a very primitive sample only, you should create a more sophisticated one, but it's a good start.

#!/bin/bash

PERSIST_DIR=/tmp/pDir
mkdir -p $PERSIST_DIR

function handle_connect {
  CLIENTFILE=$PERSIST_DIR/$common_name
  if [ -e "$CLIENTFILE" ]; then
     NUMCONN=$(cat $CLIENTFILE)
     NEWCONN=$(expr $NUMCONN + 1)
     if [ $NEWCONN -gt 2 ]; then exit 1; fi
     echo $NEWCONN >$CLIENTFILE
  else
     echo 1 >$CLIENTFILE
  fi
}

function handle_disconnect {
  CLIENTFILE=$PERSIST_DIR/$common_name
  if [ -e "$CLIENTFILE" ]; then
     NUMCONN=$(cat $CLIENTFILE)
     NEWCONN=$(expr $NUMCONN - 1)
     echo $NEWCONN >$CLIENTFILE
  fi
}

case "$script_type" in
  up)
        rm -f $PERSIST_DIR/*
        ;;
  client-connect)
        "handle_connect"
        ;;
  client-disconnect)
        "handle_disconnect"
        ;;
esac

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-05-17)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。