Get around DynDNS and others noip.com
(Version française ici)

Recently I had to connect to a device (A) which was running a server (apache) and was behind a box
(LiveBox) which, of course, did not have a permanent IP (also works for permanent IP's).
DynsDNS becoming non-free and noip.com looking constraining,
I used a very simple solution I'm going to detail here.
For this I needed:
- An account from my ISP to create my site in the "CLOUD"
- Two php files on my site (that I am going to detail)
- A small script on device 'A' that access everyday to my site.
Principle
Regularely, device A access a php page on my site through a script (enregistre.php on my drawing). Thus this page knows my curent public IP address and writes it down in a small file (devicea.txt) on the server where my site is. This way, that public IP address is associated to device A.
When I want to access the device A (I am the client now), I type an url that goes on another page of my site.
(aller.php on my drawing) which read this address in the small file and redirects me there.
Of course, this does not prevent to configure NAT routes on the box to help reaching the final device. (more details on NAT here).
The site (my site)
Let's imagine it's http://site.free.fr.
Device A
The best way I found is to use wget. On Windows download it ;
On Linux it's already there on most devices.
The primary use of wget is to download a specific HTML page at a given address.
So I type:
wget 'http://site.free.fr/enregistre.php?name=devicea&pass=mypass'
(in french enregistre means record)
In fact I type:
wget -q --spider 'http://site.free.fr/enregistre.php?name=devicea&pass=mypass'
The -q (quiet) option suppress all output messages.
The --spider option tells wget not to download the requested page (but still access it which is enough).
The simple quotes prevent Linux to interpret & sign.This command can be put in a Windows task (in a .bat or .vbs) or in the Linux crontab.
"devicea" is a name I choosed for device A. Put anything you want (linux hostname for example).
The password prevent any bot to accidentally write something in the file.
The enregistre.php page that receive this connetion contains:
(show code)
<?php
// Parameters name [and port] are passed in the url this way:
// enregistre.php?name=smth&pass=smthelse
$pass = $_GET['pass'];
if ($pass!="mypass") {
echo "Unauthorized!\n";
die;
}
$name = $_GET['name'];
$nomfic="redir/$name.txt";
$IPWAN=$_SERVER['REMOTE_ADDR'];
$myfic = @fopen($nomfic, "w");
if ($myfic) {
fwrite($myfic,$IPWAN);
fclose($myfic);
} else {
echo "Err ; Unable to open $name.txt. Contact webmaster !\n";
die;
}
?>
|
You can of course change mypass by anything else
provided you use the same one with wget.
This creates devicea.txt file in a redir folder and contains the plublic IP as it is seen from outside.
The magic link
So when I want to access to my device from anywhere, this is what I have to type:
http://site.free.fr/aller.php?name=devicea (in french aller means go) or even:
http://site.free.fr/aller.php?name=devicea&port=7000 And my aller.php page finds the 'devicea' address to redirect me to:
http://90.1.30.45:7000/ (show code)
<?php
// Syntax : http://user.free.fr/aller.php?name=toto&port=12345&secure&page=tutu.html
// Only ?name=xxx is mandatory
// Warning : No "echo" before the "Header" instruction (last line).
$name = $_GET['name'];
$nomfic="redir/$name.txt";
// test of file being here and read:
$myfic = @fopen($nomfic, "r");
if ($myfic) {
$IPWAN=trim(file_get_contents($nomfic));
fclose($myfic);
} else {
echo "Err: No data for $name";
die;
}
$port="";
if (isset($_GET['port'])) {
$port=":" . $_GET['port'];
}
$page="";
if (isset($_GET['page'])) {
$page="/" . $_GET['page'];
}
// Protocol:
$proto="http://";
if (isset($_GET['secure'])) {
$proto="https://";
}
$newsite="$proto$IPWAN$port$page";
// During tests,uncomment the next line and comment the "header" line.
//echo "trying to redirect to $newsite";
header("Location: $newsite");
?>
|
Whith the given parameters you can see in the code, I can even have the luxury to specify (in option):
- If it's http or https (with the &secure option)
- A port number (to help NAT to know what to do)
- A specific page (other than index.html)
Like :
http://site.free.fr/aller.php?name=devicea&port=7000&page=appli.htm
Et voilà !
Try on my site ? (french)
Questions?
migrigaut at gmail dot com
(Please report typos and better way to say things).
Ehancements since the first release:
- A new version allows passing of parameters to the page called
(the final page for device A) :
http://site.free.fr/aller.php?name=devicea&page=app.php&values=v=ab,foo=1,t=4
Which becomes:
http://90.1.30.45/app.php?v=ab&foo=1&t=4 -
Another version now allow a different password for different devices
with a classic subscribe form (login/pass) .
|