2015-04-01 09:42:53 -06:00
|
|
|
#################################
|
2015-04-08 18:04:43 -06:00
|
|
|
# get base network from config
|
2015-04-01 09:42:53 -06:00
|
|
|
#################################
|
|
|
|
sub get_server_network_address()
|
|
|
|
{
|
2015-04-08 18:04:43 -06:00
|
|
|
my @list;
|
|
|
|
my $uciresult;
|
|
|
|
my ($rc,$server_net)=&uci_get_indexed_option("vtun","network","0","start");
|
|
|
|
if($rc eq 0 and $server_net ne "")
|
2015-04-01 09:42:53 -06:00
|
|
|
{
|
|
|
|
# to facilitate overrides (ie. moving the server to a new node)
|
|
|
|
# read the file into $mac
|
|
|
|
@list = split('\.', $server_net);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# or, calc based on eth0 mac address, then store it.
|
|
|
|
$mac=get_mac("eth0");
|
|
|
|
@MACS=split(/:/, $mac);
|
|
|
|
push @list, "172";
|
|
|
|
push @list, "31";
|
|
|
|
push @list, hex @MACS[5];
|
|
|
|
# strip off the high bits
|
|
|
|
push @list, ((hex @MACS[4]) << 2) & 255;
|
|
|
|
$server_net=sprintf("%d.%d.%d.%d",@list[0],@list[1],@list[2],@list[3]);
|
2015-04-08 18:04:43 -06:00
|
|
|
|
2015-04-09 09:14:04 -06:00
|
|
|
#($rc,$uciresult)=&uci_add_sectiontype("vtun","network");
|
2015-04-08 18:04:43 -06:00
|
|
|
($rc,$uciresult)=&uci_set_indexed_option("vtun","network","0","start",$server_net);
|
|
|
|
$rc=&uci_commit("vtun");
|
2015-04-01 09:42:53 -06:00
|
|
|
}
|
|
|
|
return @list;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub get_active_tun()
|
|
|
|
{
|
|
|
|
my @active_tun;
|
|
|
|
foreach(`ps -w|grep vtun|grep ' tun '`)
|
|
|
|
{
|
|
|
|
@parts = $_ =~ /.*\:.*-(172-31-.*)\stun\stun.*/g;1;
|
|
|
|
@parts[0] =~ s/\-/\./g;
|
|
|
|
push(@active_tun,@parts[0]);
|
|
|
|
}
|
|
|
|
return @active_tun;
|
|
|
|
}
|
|
|
|
|
|
|
|
# INPUT arg: Array of active tunnel IP's, IP of the tunnel network you are checking
|
|
|
|
sub is_tunnel_active()
|
|
|
|
{
|
|
|
|
my ($n, @active_tun) = @_;
|
|
|
|
my $match=0;
|
|
|
|
foreach(@active_tun){
|
|
|
|
#print "a=$_, n=$n\n";
|
|
|
|
if($n eq $_) {
|
|
|
|
$match = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $match; # the return value of the do block
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Add OLSRD interfaces - called when adding a new client connection
|
|
|
|
##########################
|
|
|
|
sub add_olsrd_interface() {
|
|
|
|
my ($tunnum) = @_;
|
|
|
|
# uci add_list olsrd.interface=vpn${tunnumber}
|
|
|
|
# uci commit vtundsrv
|
|
|
|
|
|
|
|
#config Interface
|
|
|
|
# list interface 'vpn50 vpn51 vpn52 vpn53 vpn54 vpn55 vpn56 vpn57 vpn58 vpn59'
|
|
|
|
# option Ip4Broadcast 255.255.255.255
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Delete OLSRD interfaces - called when deleting a new client connection
|
|
|
|
##########################
|
|
|
|
sub del_olsrd_interface() {
|
|
|
|
my ($tunnum) = @_;
|
|
|
|
# uci delete_list olsrd.interface.vpn${tunnumber}
|
|
|
|
# uci commit vtundsrv
|
|
|
|
|
|
|
|
#config Interface
|
|
|
|
# list interface 'vpn50 vpn51 vpn52 vpn53 vpn54 vpn55 vpn56 vpn57 vpn58 vpn59'
|
|
|
|
# option Ip4Broadcast 255.255.255.255
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Add network interfaces tun50 thru tun69 - called on install
|
|
|
|
##########################
|
|
|
|
sub add_network_interfaces() {
|
|
|
|
for ($tunnum = 50; $tunnum <= 69; $tunnum++)
|
|
|
|
{
|
|
|
|
system "uci set network.vpn${tunnum}=interface";
|
|
|
|
system "uci set network.vpn${tunnum}.ifname='tun${tunnum}";
|
|
|
|
system "uci set network.vpn${tunnum}.proto='none'";
|
|
|
|
}
|
|
|
|
system "uci commit network";
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Delete OLSRD interfaces - called when deleting a new client connection
|
|
|
|
##########################
|
|
|
|
sub del_olsrd_interface() {
|
|
|
|
my ($tunnum) = @_;
|
|
|
|
# uci delete_list olsrd.interface.vpn${tunnumber}
|
|
|
|
# uci commit vtundsrv
|
|
|
|
#
|
|
|
|
}
|
|
|
|
|
|
|
|
#################################
|
|
|
|
# Check Freespace on / filesystem
|
|
|
|
#################################
|
|
|
|
sub check_freespace()
|
|
|
|
{
|
|
|
|
my $fs = `df / | grep -v '^Filesystem' | awk 'NF=6{print \$4}NF==5{print \$3}{}'`;
|
|
|
|
chomp $fs;
|
|
|
|
return $fs;
|
|
|
|
}
|
|
|
|
|
|
|
|
##########################
|
|
|
|
# Config firewall to allow port 5525 on WAN interface
|
|
|
|
##########################
|
|
|
|
sub open_5525_on_wan() {
|
2015-04-07 16:06:57 -06:00
|
|
|
system "uci add firewall rule >/dev/null 2>&1";
|
|
|
|
system "uci set firewall.\@rule[-1].src='wan' >/dev/null 2>&1";
|
|
|
|
system "uci set firewall.\@rule[-1].dest_port='5525' >/dev/null 2>&1";
|
|
|
|
system "uci set firewall.\@rule[-1].proto='tcp' >/dev/null 2>&1";
|
|
|
|
system "uci set firewall.\@rule[-1].target='ACCEPT' >/dev/null 2>&1";
|
|
|
|
system "uci commit firewall >/dev/null 2>&1";
|
2015-04-01 09:42:53 -06:00
|
|
|
}
|
|
|
|
|
2015-04-01 16:26:52 -06:00
|
|
|
sub vpn_setup_required()
|
|
|
|
{
|
|
|
|
http_header();
|
|
|
|
html_header("$node setup", 1);
|
|
|
|
print "<body><center><table width=790>";
|
|
|
|
print "<tr><td>\n";
|
|
|
|
navbar("vpn");
|
|
|
|
print "</td></tr>";
|
|
|
|
#################
|
|
|
|
# messages
|
|
|
|
#################
|
|
|
|
if(@cli_err)
|
|
|
|
{
|
|
|
|
print "<tr><td align=center><b>ERROR:<br>";
|
|
|
|
foreach(@cli_err) { print "$_<br>" }
|
|
|
|
print "</b></td></tr>\n";
|
|
|
|
}
|
|
|
|
print "<tr><td align=center><br><b>";
|
2015-04-01 16:32:55 -06:00
|
|
|
print "Tunnel software needs to be installed.<br/>";
|
2015-04-01 16:26:52 -06:00
|
|
|
print "<form method='post' action='/cgi-bin/vpn' enctype='multipart/form-data'>\n";
|
2015-04-01 16:32:55 -06:00
|
|
|
print "<input type=submit name=button_install value='Click to install' class='btn_tun_install' />";
|
2015-04-01 16:26:52 -06:00
|
|
|
print "</form>";
|
|
|
|
print "</b></td></tr>\n";
|
|
|
|
print "</table></center></body></html>\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#################################
|
|
|
|
# Install VTUN Components/config
|
|
|
|
#################################
|
2015-04-07 16:06:57 -06:00
|
|
|
sub install_vtun
|
2015-04-01 16:26:52 -06:00
|
|
|
{
|
|
|
|
# check free disk space - get real values
|
|
|
|
$freespace=&check_freespace();
|
|
|
|
if($freespace < 600)
|
|
|
|
{
|
|
|
|
push @cli_err, "Insuffient free disk space!";
|
2015-04-07 16:06:57 -06:00
|
|
|
# redirect back to admin page
|
|
|
|
} else {
|
|
|
|
|
2015-04-01 16:26:52 -06:00
|
|
|
# Update/Install VTUN
|
2015-04-07 16:06:57 -06:00
|
|
|
system "opkg update >/dev/null 2>&1";
|
|
|
|
if ($? eq 0)
|
|
|
|
{
|
|
|
|
system "opkg install kmod-tun zlib libopenssl liblzo vtun >/dev/null 2>&1";
|
|
|
|
if ($? eq 0)
|
|
|
|
{
|
|
|
|
# add network interfaces
|
|
|
|
add_network_interfaces();
|
|
|
|
|
2015-04-07 23:15:36 -06:00
|
|
|
# allow port 5525 for server connections
|
|
|
|
open_5525_on_wan();
|
|
|
|
|
|
|
|
# enable init.d scripts
|
|
|
|
system("chmod +x /etc/init.d/vtundsrv");
|
|
|
|
system("chmod +x /etc/init.d/vtund");
|
2015-04-07 16:06:57 -06:00
|
|
|
|
2015-04-08 18:04:43 -06:00
|
|
|
# create UCI config file
|
|
|
|
system("touch /etc/config/vtun");
|
|
|
|
|
2015-04-07 16:06:57 -06:00
|
|
|
http_header();
|
|
|
|
html_header("TUNNEL INSTALLATION IN PROGRESS", 0);
|
|
|
|
print "</head>\n";
|
|
|
|
print "<body><center>\n";
|
|
|
|
print "<h2>Installing tunnel software...</h2>\n";
|
|
|
|
print "<h1>DO NOT REMOVE POWER UNTIL THE INSTALLATION IS FINISHED</h1>\n";
|
|
|
|
print "</center><br>\n";
|
|
|
|
unless($debug)
|
|
|
|
{
|
|
|
|
print "
|
|
|
|
<center><h2>The node is rebooting</h2>
|
|
|
|
<h3>Wait for the Status 4 LED to start blinking, then stop blinking.<br>
|
|
|
|
When the Status 4 LED is solid on you can reconnect with<br>
|
|
|
|
<a href='http://$node.local.mesh:8080/'>http://$node.local.mesh:8080/</a><br>
|
|
|
|
</h3>
|
|
|
|
</center>
|
|
|
|
";
|
|
|
|
page_footer();
|
|
|
|
print "</body></html>";
|
|
|
|
system "/sbin/reboot" unless $debug;
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
push @cli_err,"Package installation failed!";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
push @cli_err,"Package update failed!";
|
|
|
|
}
|
2015-04-01 16:26:52 -06:00
|
|
|
}
|
2015-04-07 16:06:57 -06:00
|
|
|
}
|
2015-04-01 16:26:52 -06:00
|
|
|
|
2015-04-07 16:06:57 -06:00
|
|
|
sub DEBUGEXIT()
|
|
|
|
{
|
|
|
|
my ($text) = @_;
|
|
|
|
http_header();
|
|
|
|
html_header("$node setup", 1);
|
|
|
|
print "DEBUG-";
|
|
|
|
print $text;
|
|
|
|
print "</body>";
|
|
|
|
exit;
|
2015-04-01 16:26:52 -06:00
|
|
|
}
|
2015-04-01 09:42:53 -06:00
|
|
|
|
|
|
|
#weird uhttpd/busybox error requires a 1 at the end of this file
|
|
|
|
1
|