#!/usr/bin/perl

# port forwarding script
# manages iptables rules on user connect
# provides forwarding from external ip given to user to internal one dynamically allocated by OpenVPN
#
# receives following GET parameters
#   command - specified action to perform
#   port - space separated port list
#   internal - internal IP address dynamically allocated by OpenVPN
#   external - external IP address provided rells

use IO::Socket;
use feature qw(switch);

require "bootstrap.pl";
require "/etc/openvpn/NVPNIptd.pm";

print "Content-type:text/plain\n\n";

$command = $_POST{'command'};
$port = $_POST{'port'};
$internal = $_POST{'internal'};
$external = $_POST{'external'};
$real = $_POST{'real'};
$type = $_POST{'type'};

# input parameters validation section
print "Access Denied" and exit if ($_POST{'secret'} ne "s4rt84hsrt564isr64str7ur567q8e6rh7q6");
print "internal address is empty" and exit if (length($internal) == 0);
print "external address is empty" and exit if (length($external) == 0);

$type = "static" if (length($type) == 0);

# command handling section
given ($command) {
	when ("add") {
		print nvpn_iptd_register($internal, $external, $real, $type, $port);
	}
	default {
		print "Unrecognized command";
	}
}
