Subversion Repositories web_pages

Rev

Last modification | View Log | Download | RSS feed

Last modification

Path Last modification Log Download RSS

OPNsense TOTP and OpenVPN Configuration Export System

A web-based system that provides users with secure access to their TOTP (Time-based One-Time Password) QR codes and OpenVPN configuration files from OPNsense routers. The system connects directly to OPNsense routers via API to retrieve and manage user configurations.

Features

System Components

Main Scripts

opnsense-totp-ovpn-export

The primary script that connects to OPNsense routers via API to:

Usage:

./opnsense-totp-ovpn-export

Requirements:

configure

Interactive configuration tool for managing router definitions:

Usage:

./configure [configfile]

(defaults to routers.json if no file specified)

index.php

Web interface for end users:

Configuration Files

routers.json

Stores router connection details and settings:

{

"routername": {

"url": "https://router.example.com",
"apiKey": "your-api-key",
"apiSecret": "your-api-secret",
"ovpnIndex": "provider-id",
"template": "PlainOpenVPN",
"hostname": "vpn.example.com",
"localPort": "1194",
"downloadToken": "random-token",
"formats": {
  "tcp": {
    "filename": "ROUTER_USER_tcp.ovpn",
    "additionalStrings": "proto tcp\\nport 443"
  },
  "udp": {
    "filename": "ROUTER_USER_udp.ovpn",
    "additionalStrings": "proto udp\\nport 1194"
  }
}

}

}

Formats Configuration:

users.json

Generated automatically by opnsense-totp-ovpn-export:

{

"routername": {

"qrLocation": "./qrcodes",
"ovpnLocation": "./openvpn_configs",
"lastUpdate": 1234567890,
"users": {
  "username": {
    "otp_seed": "BASE32SECRET",
    "password": "$2y$10$hashedpassword",
    "certs": ["cert-id"],
    "qrFile": "./qrcodes/router_user.png",
    "ovpnFile": "./openvpn_configs/router_user.ovpn"
  }
}

}

}

Note: ovpnFile can be a string (single file) or array (multiple files)

Directory Structure

.

├── configure # Router configuration tool

├── opnsense-totp-ovpn-export # Main export script

├── opnsense.pm # OPNsense API library

├── index.php # User web interface

├── dl.php # Download handler (legacy)

├── routers.json # Router configurations

├── users.json # User credentials (auto-generated)

├── updaterouters.cron # Cron job script

├── logs/ # Access logs directory

│ └── log_YYYY-MM-DD.log # Daily log files

├── qrcodes/ # Generated QR code images

│ └── *.png

└── openvpn_configs/ # Generated OpenVPN configs

└── *.ovpn

Installation

Prerequisites

Setup Steps

  1. Clone/copy files to web-accessible directory:

cd /var/www/html/web_pages

svn co totp_opnsense

cd totp_opnsense

  1. Set permissions:

chmod 755 configure opnsense-totp-ovpn-export

chmod 600 routers.json # Will be created by configure

chmod 755 qrcodes openvpn_configs logs

  1. Configure routers:

sudo ./configure

  1. Initial data pull:

sudo ./opnsense-totp-ovpn-export routername

  1. Set up cron job for automatic updates:

sudo crontab -e

Add line:

/15 * /var/www/html/web_pages/totp_opnsense/updaterouters.cron

  1. Configure web server:

Usage

For Administrators

Add a new router:

sudo ./configure

Select "Add new router"

Enter configuration details

Update router data manually:

sudo ./opnsense-totp-ovpn-export routername

Configure VPN formats:

sudo ./configure

Select router

Select "formats"

Add format definitions with filename templates and additional strings

For End Users

  1. Navigate to the web interface (e.g., https://server/totp_opnsense/)
  1. Enter your username and password
  1. Select the router from the dropdown
  1. Click "Login"
  1. Scan the QR code with your authenticator app (first time only)
  1. Download your OpenVPN configuration file(s)
  1. Import the .ovpn file into your OpenVPN client

If multiple VPN configurations are available:

Security Considerations

Current Security Features

Security Recommendations

  1. Use HTTPS: Always serve the web interface over HTTPS
  1. Restrict access: Use firewall rules or .htaccess to limit access
  1. Regular updates: Keep router data current but not too frequent
  1. Monitor logs: Review access logs regularly
  1. API key security: Use dedicated API keys with minimal permissions
  1. File permissions: Ensure sensitive files are not world-readable

Known Limitations

API Integration

The system uses the OPNsense API module (opnsense.pm) to interact with routers:

Key API endpoints used:

API Setup in OPNsense:

  1. System → Access → Users
  1. Create API user with necessary permissions
  1. Generate API key and secret
  1. Use credentials in configure script

Troubleshooting

Script won't run:

Users can't log in:

QR codes not displaying:

VPN configs not downloading:

Refresh doesn't work:

File Format Details

Log Format (logs/log_YYYY-MM-DD.log)

YYYY-MM-DD HH:MM:SSIP_ADDRESSSuccessUSERNAME

QR Code Encoding

Version History

See individual script files for detailed version history.

Current Versions:

License

Copyright (c) 2025, Daily Data, Inc.

All rights reserved.

See individual files for complete BSD 3-Clause license terms.

Support

For issues, questions, or contributions, contact your system administrator or Daily Data, Inc.

Tested Environments