Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions net/rtsphelper/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
PLUGIN_NAME= rtsphelper
PLUGIN_VERSION= 1.2
#PLUGIN_DEPENDS=
PLUGIN_COMMENT= RTSP Port Forwarder Helper
PLUGIN_MAINTAINER= [email protected]

.include "../../Mk/plugins.mk"
1 change: 1 addition & 0 deletions net/rtsphelper/pkg-descr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A simple helper script that opens ports to allow mis-configured RTSP servers to work behind NAT environment
112 changes: 112 additions & 0 deletions net/rtsphelper/src/etc/inc/plugins.inc.d/rtsphelper.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

use OPNsense\RTSPHelper\General;

function rtsphelper_enabled()
{
$model = new General();
return (string)$model->general->enabled == '1';
}

function rtsphelper_firewall($fw)
{
if (!rtsphelper_enabled()) {
return;
}

$fw->registerAnchor('rtsphelper', 'rdr');
$fw->registerAnchor('rtsphelper', 'fw');
}

function rtsphelper_services()
{
$services = array();

if (!rtsphelper_enabled()) {
return $services;
}

$pconfig = array();
$pconfig['name'] = 'rtsphelper';
$pconfig['description'] = gettext('RTSP Helper');
$pconfig['php']['restart'] = array('rtsphelper_stop', 'rtsphelper_start');
$pconfig['php']['start'] = array('rtsphelper_start');
$pconfig['php']['stop'] = array('rtsphelper_stop');
$pconfig['pidfile'] = '/var/run/rtsphelper.pid';
$services[] = $pconfig;

return $services;
}

function rtsphelper_start()
{
if (!rtsphelper_enabled()) {
return;
}

if (isvalidpid('/var/run/rtsphelper.pid')) {
return;
}

mwexec_bg('/usr/local/bin/python3 /usr/local/opnsense/scripts/net/rtsphelper/rtsphelper.py');
}

function rtsphelper_stop()
{
killbypid('/var/run/rtsphelper.pid', 'TERM', true);
mwexec('/sbin/pfctl -artsphelper -Fr 2>&1 >/dev/null');
mwexec('/sbin/pfctl -artsphelper -Fn 2>&1 >/dev/null');
}

function rtsphelper_configure()
{
return array('bootup' => array('rtsphelper_configure_do'));
}

function rtsphelper_configure_do($verbose = false)
{
rtsphelper_stop();

if (!rtsphelper_enabled()) {
return;
}

if ($verbose) {
echo 'Starting RTSP Helper...';
flush();
}

$model = new General();
$ext_iface = (string)$model->general->ext_iface;
$ext_ifname = get_real_interface($ext_iface);

// Log a warning if interface couldn't be resolved
// This can happen if the selected interface was deleted from the system
if ($ext_ifname == $ext_iface) {
syslog(LOG_WARNING, "rtsphelper: Interface '{$ext_iface}' could not be resolved to a device name");
}

$config_text = "ext_ifname={$ext_ifname}\n";

/* RTSP Helper access restrictions */
foreach ($model->permissions->permission->iterateItems() as $perm) {
$network = (string)$perm->network;
$port = (string)$perm->port;
$config_text .= "allow={$network} {$port}\n";
}

foreach ($model->hosts->host->iterateItems() as $host) {
$ip = (string)$host->ip;
$port = (string)$host->port;
$config_text .= "forward={$ip}:{$port}\n";
}

/* write out the configuration */
file_put_contents('/var/etc/rtsphelper.conf', $config_text);
rtsphelper_start();

if ($verbose) {
echo "done.\n";
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace OPNsense\RTSPHelper\Api;

use OPNsense\Base\ApiControllerBase;
use OPNsense\Core\Backend;

class ServiceController extends ApiControllerBase
{
public function startAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun('rtsphelper start');
return array("response" => $response);
}
return array("response" => array());
}

public function stopAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun('rtsphelper stop');
return array("response" => $response);
}
return array("response" => array());
}

public function restartAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun('rtsphelper restart');
return array("response" => $response);
}
return array("response" => array());
}

public function statusAction()
{
$backend = new Backend();
$response = $backend->configdRun('rtsphelper status');
return array("status" => trim($response));
}

public function reconfigureAction()
{
if ($this->request->isPost()) {
$backend = new Backend();
$response = $backend->configdRun('rtsphelper configure');
return array("response" => $response);
}
return array("response" => array());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OPNsense\RTSPHelper\Api;

use OPNsense\Base\ApiMutableModelControllerBase;

class SettingsController extends ApiMutableModelControllerBase
{
protected static $internalModelClass = '\OPNsense\RTSPHelper\General';
protected static $internalModelName = 'rtsphelper';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace OPNsense\RTSPHelper\Api;

use OPNsense\Base\ApiControllerBase;
use OPNsense\Core\Backend;

class StatusController extends ApiControllerBase
{
public function connectionsAction()
{
$backend = new Backend();
$response = $backend->configdRun('rtsphelper connections');
$rows = array();

foreach (explode("\n", $response) as $line) {
if (preg_match("/on (.*) inet proto (.*) from (.*) to (.*) port = (.*) -> (.*)/", $line, $matches)) {
$rows[] = array(
"interface" => $matches[1],
"proto" => $matches[2],
"source" => $matches[3],
"destination" => $matches[4],
"port" => $matches[5],
"redirect_to" => $matches[6]
);
}
}

return array("rows" => $rows);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace OPNsense\RTSPHelper;

use OPNsense\Base\IndexController;

class SettingsController extends IndexController
{
public function indexAction()
{
$this->view->pick('OPNsense/RTSPHelper/index');
$this->view->formGeneral = $this->getForm("general");
$this->view->formDialogHost = $this->getForm("dialog_host");
$this->view->formDialogPermission = $this->getForm("dialog_permission");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<form>
<field>
<id>host.ip</id>
<label>IP Address</label>
<type>text</type>
<help>Internal IP address.</help>
</field>
<field>
<id>host.port</id>
<label>Port</label>
<type>text</type>
<help>Port number.</help>
</field>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<form>
<field>
<id>permission.network</id>
<label>Network</label>
<type>text</type>
<help>Network (CIDR) or IP address.</help>
</field>
<field>
<id>permission.port</id>
<label>Port / Range</label>
<type>text</type>
<help>Port or port range (e.g. 1024-65535).</help>
</field>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<form>
<field>
<id>general.enabled</id>
<label>Enable</label>
<type>checkbox</type>
<help>Enable RTSP Helper</help>
</field>
<field>
<id>general.ext_iface</id>
<label>External Interface</label>
<type>dropdown</type>
<help>Select your primary WAN interface.</help>
</field>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<acl>
<page-service-rtsphelper>
<name>Service: RTSP Helper</name>
<patterns>
<pattern>ui/rtsphelper/*</pattern>
<pattern>api/rtsphelper/*</pattern>
</patterns>
</page-service-rtsphelper>
</acl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OPNsense\RTSPHelper;

use OPNsense\Base\BaseModel;

class General extends BaseModel
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<model>
<mount>//OPNsense/RTSPHelper</mount>
<version>1.0.0</version>
<items>
<general>
<enabled type="BooleanField">
<default>0</default>
<Required>Y</Required>
</enabled>
<ext_iface type="InterfaceField">
<Required>Y</Required>
<multiple>N</multiple>
</ext_iface>
</general>
<hosts>
<host type="ArrayField">
<ip type="NetworkField">
<Required>Y</Required>
<ValidationMessage>Please specify a valid IP address.</ValidationMessage>
</ip>
<port type="PortField">
<Required>Y</Required>
<ValidationMessage>Please specify a valid port number.</ValidationMessage>
</port>
</host>
</hosts>
<permissions>
<permission type="ArrayField">
<network type="NetworkField">
<Required>Y</Required>
<ValidationMessage>Please specify a valid network (CIDR) or IP address.</ValidationMessage>
</network>
<port type="TextField">
<Required>Y</Required>
<mask>/^(\d{1,5})(?:-(\d{1,5}))?$/</mask>
<ValidationMessage>Please specify a valid port or port range (e.g. 1024-65535).</ValidationMessage>
</port>
</permission>
</permissions>
</items>
</model>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<menu>
<Services>
<RTSPHelper VisibleName="RTSP Helper" cssClass="fa fa-plug fa-fw">
<Settings url="/ui/rtsphelper/settings"/>
</RTSPHelper>
</Services>
</menu>
Loading