Hi. I have setup an Openvpn 2.4.7 server on Ubuntu 20.04 LTS and got everything working properly. What i can't figure out is how to limit a client to 1 connection and reject any subsequent connection from that common name.
With "duplicate-cn" commented out in the server config, my tests shows a second connection is still successful although the second connection is assigned the same IP as the first connection, leading to IP conflict.
Can someone help me apply the setting that will reject the second connection attempt from a client that is already connected to the OpenVPN server?
Thank you.
Re: Limit concurrent connection to 1
Posted: Wed Jun 03, 2020 11:13 pm
by TinCanTech
How about --max-clients n
Re: Limit concurrent connection to 1
Posted: Mon Jun 08, 2020 8:59 pm
by antoniofiliona
From what i read, --max-clients n limits the total number of clients simultaneously connected to the server, which would work if the server was built for only 1 client, but since i have multiple clients and only want to limit the number of connections per client, that option won't work.
After many days searching online for other info, i did finally find a post similar to my request here.
#!/bin/bash
PERSIST_DIR=/tmp/pDir
function handle_connect {
CLIENTFILE=$PERSIST_DIR/$common_name
echo Testing for the existance of the temp folder
if [ -d $PERSIST_DIR ] ; then
echo "Folder exists"
else
echo "Folder doens't exist, creating folder now"
mkdir $PERSIST_DIR
fi
if [ -e $CLIENTFILE ]; then
echo Client file exists
NUMCONN=$(cat $CLIENTFILE)
NEWCONN=$(expr $NUMCONN + 1)
if [ $NEWCONN -gt 3 ]; then exit 1; fi
echo $NEWCONN >$CLIENTFILE
else
echo 1 >"$CLIENTFILE"
fi
}
function handle_disconnect {
echo Starting disconnect for $common_name
CLIENTFILE=$PERSIST_DIR/$common_name
if [ -e "$CLIENTFILE" ]; then
echo File found, remove connection
NUMCONN=$(cat $CLIENTFILE)
NEWCONN=$(expr $NUMCONN - 1)
echo $NEWCONN >$CLIENTFILE
fi
echo Completed disconnect
}
case "$script_type" in
up)
rm -f $PERSIST_DIR/*
;;
client-connect)
"handle_connect"
;;
client-disconnect)
"handle_disconnect"
;;
esac
And now that works perfectly for what i needed! I did allow 3 connections per client because my tests shows a loss of internet connectivity by switching from wifi to cell data did not cause a disconnect notification to be sent to the server which then continued to use up 1 of the 2 connections allowed until the connection timeout kicked in.
Re: Limit concurrent connection to 1
Posted: Mon Jun 08, 2020 9:37 pm
by TinCanTech
Sorry, I misunderstood your initial question.
Looks like a decent enough solution
Re: Limit concurrent connection to 1
Posted: Wed May 17, 2023 1:19 pm
by ivanff
I have also experimented with a similar task and the above script does not work properly. Yes, the client whose limit is exceeded will not immediately connect, but when reconnecting (reconnecting), it connects successfully., maybe it depends on the openvpn client, I tested it on securepoint.
but I still found the solution that I left. Here is the script below that I use both to generally limit client sessions per config, and to limit the session with a fixed address (in this case, limit = 1)
my sprit will need to enable the telnet management interface
I will be happy to improve and expand my solution!