How to build a multi-wan router with web interface

Intro

We asked me in my work to build a multiwan router with an web interface which easily allow a user to change the default route between an ADSL or an 4G modem.

Network diagram

Configuration

Installing

root@host:~# apt-get install incron lighttpd
root@host:~# lighttpd-enable-mod cgi

Settings

incron

Incron works like the regular cron but is driven by filesystem events instead of time events.

/etc/incron.d/route

/tmp/4G IN_CREATE,IN_ATTRIB,IN_MODIFY /usr/local/sbin/iptables_4G.sh
/tmp/ADSL IN_CREATE,IN_ATTRIB,IN_MODIFY /usr/local/sbin/iptables_ADSL.sh

/usr/local/sbin/iptables_ADSL.sh

#! /bin/bash
ip route del default
ip route add default via 192.168.2.254

/usr/local/sbin/iptables_4G.sh

#! /bin/bash
ip route del default
ip route add default via 192.168.2.253

lighttpd

I'll use lighttpd as web server.

/etc/lighttpd/lighttpd.conf

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_auth",
        "mod_redirect",
#       "mod_rewrite",
)

#ajout de l'auth
auth.debug = 4
auth.backend = "plain"
auth.backend.plain.userfile = "/var/www/.lighttpdpassword"

$SERVER["socket"] == "192.168.1.1:80" {
        auth.require = ( "/" =>
        (
        "method" => "basic",
        "realm" => "Administration",
        "require" => "user=admin"
        )
        )
}

server.document-root        = "/var/www/html"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80
server.bind                 = "192.168.1.1"

index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi", ".sh" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

/etc/lighttpd/conf-enabled/10-cgi.conf

server.modules += ( "mod_cgi" )

$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = (
                ".sh" => "/bin/bash",
	)
}

/var/www/.lighttpdpassword

admin:password

/var/www/html/index.html

<html>
<head>
<title>Administration</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
</head>
<body>
<h1>Routage</h1>
<FORM ACTION="cgi-bin/route_adsl.sh" METHOD="GET">
        <INPUT TYPE="SUBMIT" VALUE="DISABLE 4G">
</FORM>

<FORM ACTION="cgi-bin/route_4g.sh" METHOD="GET">
        <INPUT TYPE="SUBMIT" VALUE="ENABLE 4G">
</FORM>
</body>

/var/www/cgi-bin/route_adsl.sh

#! /bin/bash

echo "Content-type: text/html"
echo ""
echo "<html>
<head>
<meta content=\"text/html;charset=utf-8\" http-equiv=\"Content-Type\">
<meta content=\"utf-8\" http-equiv=\"encoding\">
<title>WAN ADSL</title>
<body>"

echo "ADSL" > /tmp/ADSL
sleep 2s
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://monip.org/\">"
echo "</body>"

/var/www/cgi-bin/route_4G.sh

#! /bin/bash

echo "Content-type: text/html"
echo ""
echo "<html>
<head>
<meta content=\"text/html;charset=utf-8\" http-equiv=\"Content-Type\">
<meta content=\"utf-8\" http-equiv=\"encoding\">
<title>WAN 4G</title>
<body>"

echo "4G" > /tmp/4G
sleep 2s
echo "<meta HTTP-EQUIV=\"REFRESH\" content=\"0; url=http://monip.org/\">"
echo "</body>"

Restart services

root@host:~# systemctl restart incron.service
root@host:~# systemctl restart lighttpd.service
Licence Creative Commons
This website http://shebangthedolphins.net is licensed to the public under a licence Creative Commons Attribution licence.
Contact :