# openvpn_web_1 **Repository Path**: larcn/openvpn_web_1 ## Basic Information - **Project Name**: openvpn_web_1 - **Description**: OpenVPN简单的用户管理与用户登陆日志系统v1.0 - **Primary Language**: Python - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 27 - **Created**: 2025-03-31 - **Last Updated**: 2025-08-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # openvpn_web #### 介绍 OpenVPN简单的用户管理与用户登陆日志系统 ![输入图片说明](https://images.gitee.com/uploads/images/2020/0917/171622_6d1d01d5_1097803.png "1.png") ![输入图片说明](https://images.gitee.com/uploads/images/2019/0512/162557_07c99033_1097803.png "20190512162421.png") #### 部署文档 参见:https://gitee.com/k2y1982/someinstallreadme/tree/master/vpn_web - 用[openVPN2.5.5](https://github.com/OpenVPN/openvpn/tags) + [openvpn_web v1.0(web管理账户)的说明](https://gitee.com/lang13002/openvpn_web/tree/v1.0/) - 运行环境centos8.5(7.6) + openvpn2.5.5 +OpenVpn_web_v1.0 + python3(python2无法生成配置文件不明所以) - 可以先不管证书的事,openvpn_web 里包含 easyrsa - 我是在虚拟机里用最小化安装的,软件和依赖差得有点多。有些步骤看情况省略。 - 在一台7.6的云服务器上也试过了。 - 本来作者已经发布了openvpn_web_v2.0 但怎么也弄不出来,老是提示用户名密码错误,算了还是先用1.0 # 前期准备工作 及 centos 配置 - 先下载openvpn https://github.com/OpenVPN/openvpn/tags 后面有地址这里不管它 ## 关闭SELinux模式 查看指令,重启生效 - enforcing 设置为 disabled ``` sestatus vi /etc/selinux/config reboot ``` ## 开放 1194 8000/tcp端口 ``` firewall-cmd --permanent --zone=public --add-port=1194/tcp firewall-cmd --permanent --zone=public --add-port=8000/tcp firewall-cmd --reload firewall-cmd --zone=public --list-ports ``` ## systemctl 防火墙 指令 备注:不需要流量走服务器可不做此操作 - 用了iptables会导致防火墙失效,防火墙重启又会导致iptables规则清空 - 我直接关闭防火墙了(前2条 后面为备注) ``` systemctl stop firewalld -停止 systemctl mask firewalld -禁用 systemctl unmask firewalld -启用 systemctl enable firewalld -开机启动 systemctl disable firewalld -删除开机启动 systemctl status firewalld -查看状态 systemctl start firewalld -运行 systemctl restart firewalld -重新运行 ``` ## 开启转发功能并生效 不存在该配置则添加 备注:不需要流量走服务器可不做此操作 ``` grep 'net.ipv4.ip_forward = 1' /etc/sysctl.conf || echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf sysctl -p ``` ## 设置转发规则 ``` iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j MASQUERADE iptables-save > /etc/sysconfig/iptables iptables -L -n -t nat ``` ## 删除上面的iptables配置信息命令 作用:对比正常的访问和异常的访问(没用过不知道效果) ``` iptables -t nat -D POSTROUTING 1 ``` ## 安装依赖包 我是最小安装 依赖包差得有点多 ``` yum -y install lz4-devel lzo-devel pam-devel openssl-devel systemd-devel sqlite-devel tar autoconf automake libtool make vim git net-tools unzip wget expect python3 ``` ## 如果安装了python3 将python3 设置为 python 默认,不改 执行 connect.py disconnect.py 会报错 ## 或者想让python = python3 那就先将/usr/bin/python删除 ``` rm -f /usr/bin/python ``` ``` ln -s /usr/bin/python3 /usr/bin/python ``` - 备注 centos7.6安装python3后yum install 可能会报错 ``` File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ^ SyntaxError: invalid syntax ``` ``` File "/usr/libexec/urlgrabber-ext-down", line 28 except OSError, e: ^ SyntaxError: invalid syntax ``` - vim /usr/bin/yum 和 vim /usr/libexec/urlgrabber-ext-down - 将第一排的 #!/usr/bin/python 改为 #!/usr/bin/python2 即可 ## 安装 openvpn_web 依赖 ``` pip3 install peewee tornado ``` # 开始配置openvpn ## 下载openvpn源代码包并解压后编译安装,最后建立软连接 ``` wget https://github.do/https://github.com/OpenVPN/openvpn/archive/refs/tags/v2.5.5.tar.gz tar -xvf v2.5.5.tar.gz && cd openvpn-2.5.5 && autoreconf -i -v -f ./configure --prefix=/usr/local/openvpn --enable-lzo --enable-lz4 --enable-plugins --enable-port-share --enable-iproute2 --enable-pf --enable-plugin-auth-pam --enable-pam-dlopen --enable-systemd make && make install ln -s /usr/local/openvpn/sbin/openvpn /usr/local/sbin/openvpn ``` - --enable-crypto --enable-server 原文里还有前面2个参数,但实际安装时最后会报错 ## 配置文件修改 ``` vim /usr/local/openvpn/lib/systemd/system/openvpn-server@.service ``` - 找到 ExecStart 这行,改为如下 ``` ExecStart=/usr/local/openvpn/sbin/openvpn --config server.conf ``` ## 配置系统服务,并开机自启动 ``` cp -a /usr/local/openvpn/lib/systemd/system/openvpn-server@.service /usr/lib/systemd/system/openvpn.service ``` ## 复制 openvpn-plugin-auth-pam.so 到 /etc/openvpn/ ``` mkdir /etc/openvpn/ cp -a /usr/local/openvpn/lib/openvpn/plugins/openvpn-plugin-auth-pam.so /etc/openvpn/ ``` ## 创建服务端配置文件 ``` push "redirect-gateway def1 bypass-dhcp" -客户端的流量通过服务器IP流出 push "dhcp-option DNS 114.114.114.114" -推送给客户端的DNS topology subnet -此模式下客户端才能互通 ``` ## 以下代码复制进 server.conf | 去掉 ;为流量走服务器 ``` mkdir -p /etc/openvpn/server vim /etc/openvpn/server/server.conf ``` ``` port 1194 proto tcp dev tun ca /etc/openvpn/server/ca.crt cert /etc/openvpn/server/server.crt key /etc/openvpn/server/server.key dh /etc/openvpn/server/dh.pem server 10.8.0.0 255.255.255.0 ;push "redirect-gateway def1 bypass-dhcp" ;push "dhcp-option DNS 114.114.114.114" topology subnet ifconfig-pool-persist ipp.txt client-to-client keepalive 10 120 tls-crypt tc.key cipher AES-256-CBC auth SHA512 compress lz4-v2 push "compress lz4-v2" max-clients 100 user nobody group nobody persist-key persist-tun status openvpn-status.log log /etc/openvpn/server/openvpn.log verb 3 plugin /etc/openvpn/openvpn-plugin-auth-pam.so openvpn script-security 3 client-connect /etc/openvpn/server/connect.py client-disconnect /etc/openvpn/server/disconnect.py ``` # 添加 SQLite 认证,下载 pam_sqlite3 并安装 ``` cd /etc/openvpn git clone https://gitee.com/lang13002/pam_sqlite3.git cd /etc/openvpn/pam_sqlite3 && make && make install ``` ## 添加 pam 认证文件 ``` vim /etc/pam.d/openvpn ``` ``` auth required pam_sqlite3.so db=/etc/openvpn/openvpn.db table=t_user user=username passwd=password expire=expire crypt=1 account required pam_sqlite3.so db=/etc/openvpn/openvpn.db table=t_user user=username passwd=password expire=expire crypt=1 ``` # 下载 openvpn-web v1.0 ``` cd /etc/openvpn/ && git clone https://gitee.com/k2y1982/openvpn_web.git ``` ## 创建证书 - 先将 openvpn_web/easyrsa3 里的默认证书都删了 再手动创建证书 ## 删除默认证书 ``` rm -rf /etc/openvpn/openvpn_web/easyrsa3/pki/ ``` ## 生成服务端证书 ``` chmod 777 /etc/openvpn/openvpn_web/easyrsa3/easyrsa cd /etc/openvpn/openvpn_web/easyrsa3/ ./easyrsa init-pki ./easyrsa build-ca nopass ./easyrsa build-server-full server nopass ``` - 创建Diffie-Hellman,确保key穿越不安全网络的命令,时间会有点长,耐心等待 ``` ./easyrsa gen-dh ``` ## 为了提高安全性,生成ta.key - 加强认证方式,防攻击。如果配置文件中启用此项(默认是启用的),就需要执行上述命令,并把ta.key放到/etc/openvpn/server目录。 - 配置文件中服务端第二个参数为0,同时客户端也要有此文件,且client.conf中此指令的第二个参数需要为1。【服务端有该配置,那么客户端也必须要有】 - openvpn_web 生成的 ovpn最后用的tc实际是用的ta.key生成的,所以我们要将ta.key 复制一份为 tc.key ``` openvpn --genkey secret ta.key cp ta.key tc.key ``` ## 拷贝证书到/etc/openvpn/server/ ``` cp -a pki/ca.crt pki/private/server.key pki/issued/server.crt pki/dh.pem ta.key tc.key /etc/openvpn/server/ ``` ## 创建相应的数据库表 ``` sqlite3 /etc/openvpn/openvpn.db .read /etc/openvpn/openvpn_web/model/openvpn.sql .quit ``` ## 上传 connect.py disconnect.py - [下载地址](https://gitee.com/k2y1982/someinstallreadme/tree/master/vpn_web/py%E6%96%87%E4%BB%B6) 下载后放到 /etc/openvpn/server/ 里 ## 将整个 /etc/openvpn/赋予权限 ``` chmod -R 777 /etc/openvpn/ ``` ## 启动openvpn服务 第三条为备注 ``` systemctl enable openvpn.service systemctl start openvpn.service systemctl restart openvpn.service ``` ## 后台运行 myapp.py ``` nohup python /etc/openvpn/openvpn_web/myapp.py -u > /etc/openvpn/openvpn_web/nohup.out & ``` ## 查看pid以便结束 备注而已 不需要用到 ``` ps -aux | grep -v grep | grep /etc/openvpn/openvpn_web/myapp.py | awk '{print "myapp.py pid= " $2}' ``` ## 结束进程 备注而已 不需要用到 ``` kill -9 xxxxx ``` ## 查看日志 ``` tail /etc/openvpn/openvpn_web/nohup.out cat /etc/openvpn/server/openvpn.log cat /etc/openvpn/server/openvpn-status.log cat /etc/openvpn/server/ipp.txt ``` ## 登陆 http://服务器IP:8000 端口号可在myapp.py中修改 - id:admin pw:123456 - 如果用户生效的时候,浏览器没有下载 ovpn文件,新建用户的ovpn文件在 ```/etc/openvpn/openvpn_web/static/client``` - 查看外网地址 ``` curl ifconfig.me ``` ## 创建myapp.py开机启动 新建startup.sh ``` mkdir -p /root/startup vim /root/startup/startup.sh ``` ## 将下面代码复制进startup.sh PS:#!/bin/bash为设置运行环境 不是注释 ``` #!/bin/bash #description: myapp启动服务 nohup python /etc/openvpn/openvpn_web/myapp.py -u >> /etc/openvpn/openvpn_web/nohup.out 2>&1 & ``` ``` chmod +x /root/startup/startup.sh ``` ## 创建开机启动服务 ``` vim /usr/lib/systemd/system/myapp-autorun.service ``` ## 以下代码复制进 myapp-autorun.service ``` [Unit] Description=myapp for auto start Wants=network-online.target [Service] User=root Type=forking ExecStart=/usr/bin/bash /root/startup/startup.sh [Install] WantedBy=multi-user.target ``` ## 重新加载systemd配置 ``` systemctl daemon-reload ``` ## 添加开机自启动 ``` systemctl enable myapp-autorun.service ``` ## kill myapp后运行服务进行测试 或是重启测试 ``` systemctl start myapp-autorun systemctl status myapp-autorun ``` ## 查看正在运行的服务 ``` systemctl --type=service --state=running ``` ## 查看端口占用 ``` netstat -tanp ``` ## 客户端设置很简单 - 打开ovpn配置文件 - remote myvpn.org 1194 - 将myvpn.org设置为服务器外网IP即可