Service to ping web server - php

I have a dedicated webserver running PHP software which needs to automatically collect and update the IP's of a couple Windows and possibly Linux machines of mine which have dynamic IP's (roughly same idea as the no-ip.com client). The simplest thing to do I think is to run a service on each machine which simply pulls a unique URL from the webserver which can then lookup the client IP and match it with the URL etc.
$_SERVER["HTTP_CLIENT_IP"]
What's the best language/library/environment to build a client service which can make a URL request with easy access to the machine's external IP (to check for changes to the dynamic IP so as to not flood the webserver)? It need not be anything fancy, it doesn't even need to read anything from the server, it just has to make the URL request.
Besides web programming, I have some experience with Python and C and a few others. Any pointers or resources I can read up on the subject would be appreciated. Also, am I over-thinking this? Thanks

you can write a shell script with wget calls followed by a sleep in a loop.
wget performs http requests and it's available for windows and is already installed on all/some unix machines.

Mechanize is way overkill for making URL requests in Python. It's really really easy:
from urllib2 import urlopen
urlopen("my://url").read()
The first line may vary depending on your version of Python (for instance, it's urllib.request in Python 3.)
There are hundreds of "What's my IP" services out there; or you could even write your own! Plug one of those into the URL read to get the IP. You'll have to poll to work out when it changes, since you can't run code on the router.

If you already have some kind of method to do an URL request, do one each 10 minutes to this Page:
http://checkip.dyndns.org/Current%20IP%20Check.htm
wich will tell you "Current IP Address: XXX.XXX.XXX.XXX", and you only have to extract some string data.
(You only flood the whatsmyip webserver, and not your own anymore!)
Glad to Help
EGOrecords

Related

detecting calls to outside PHP (curl, file get contents ...)

Is there a simple way to detect all outside calls from PHP?
In an open sourced project I have a lot of 3rd party scripts. With use of new relic I was able to debug long execution times leading back to some of this scripts making calls back to their servers.
I dont mind this but I want to know what data this scripts are sending and most of all I dont want to have slow site when 3rd party script server is down or not accessible.
Is there an easy way to log all curl, file get contents etc requests?
Thanks!
You are searching for a packet sniffer. Usually you'll use tcpdump and/or wireshark.
http://wiki.ubuntuusers.de/tcpdump
https://www.wireshark.org/
There are many solutions, but my preferred is
You build your own proxy : example a dedicated Apache Server (it can running in the same IP but different port who will handle this type of operations). After that, you change all of your old URL to pass by your proxy
Imagine that you have this in your code : curl_init('www.google.com'); so you have to change it by: curl_init('http://localhost:8090/CALLS_OUTSIDE_PHP_CONTROLLER.php?url_to_redirect=www.google.com');
The PHP controller running under 8090 can do many operations as : blacklist/whitelist some urls, doing regular URL check in background... many cool stuff

PHP best way to show a user's ping to the webserver?

I have several VPS' and want to use PHP or shell scripting to display the ping in ms from the client to the server(s) so the user may compare which server to connect to. What is the best way to do this?
Assuming you want this to be asynchronous (so it doesn't stop the whole page from loading) then you could wrap something up in an Ajax call and then have a server-side script perform a ping on the $_SERVER['REMOTE_ADDR'] (IP of the person visiting the site). As for performing the ping itself, you should just be able to wrap that up in a "system('ping ..." call.
Hope that helps!
Depending on how sophisticated you want to get with your networking, you might want to give anycast a shot. The premise is that you give the same IP address to the two different VPS servers in the two different locations. When the client tries to connect to your server using a DNS lookup, it will find the single IP address, and depending on the closer server (ergo, better pinging server) should pickup that client. Cloudflare does this, and they explain it in their blog. Although, this might be seen a using a cannon to kill a mosquito, but it seems to work and it might even work for you. Here's some data that shows that Anycast is not evil.

PHP Script that'll transfer every request and parameters

I'm trying to make something kind of fuzzy today :)
I'd like to create some PHP script / .htaccess to execute every request made to the file (I will redirect every request with .htaccess on my PHP script).
So the script should contact another server with those requests and return the content. My main goal is make it act like a hosts file. I'd send the request with all the parameters, making the other think I'm coming from that very host.
I do this because I can't modify any hosts file on my development server nor on my local workstation. So I'd redirect every request to my development server to my local workstation (on which I installed apache2).
The main questions are: How would you do so? Do you think the php/htaccess combo is okay? How would I identify as the current host to the other server?
I guess it's clear enough :) Any ideas?
Thanks!
As #Marc B says, you are looking to build a Proxy. Due to its nature as an interpreted language, PHP is not the optimal solution for this - using a specialized proxy program / server would be less resource intensive.
That said, if PHP is your only option, there is phpMyProxy that might work for you. I haven't used it myself locally, but the feature list and the demo look pretty impressive.

Is it possible to have a peer to peer communication using nothing but PHP

Is it possible to implement a p2p using just PHP? Without Flash or Java and obviously without installing some sort of agent/client on one's computer.
so even though it might not be "true" p2p, but it'd use server to establish connection of some sort, but rest of communication must be done using p2p
i apologize for little miscommunication, by "php" i meant not a php binary, but a php script that hosted on web server remote from both peers, so each peer have nothing but a browser.
without installing some sort of
agent/client on one's computer
Each computer would have to have the PHP binaries installed.
EDIT
I see in a different post you mentioned browser based. Security restrictions in javascript would prohibit this type of interaction
No.
You could write a P2P client / server in PHP — but it would have to be installed on the participating computers.
You can't have PHP running on a webserver cause two other computers to communicate with each other without having P2P software installed.
You can't even use JavaScript to help — the same origin policy would prevent it.
JavaScript running a browser could use a PHP based server as a middleman so that two clients could communicate — but you aren't going to achieve P2P.
Since 2009 (when this answer was originally written), the WebRTC protocol was written and achieved widespread support among browsers.
This allows you to perform peer-to-peer between web browsers but you need to write the code in JavaScript (WebAssembly might also be an option and one that would let you write PHP.)
You also need a bunch of non-peer server code to support WebRTC (e.g. for allow peer discovery and proxy data around firewalls) which you could write in PHP.
It is non-theoretical because server side application(PHP) does not have peer's system access which is required to define ports, IP addresses, etc in order to establish a socket connection.
ADDITION:
But if you were to go with PHP in each peer's web servers, that may give you what you're looking for.
Doesn't peer-to-peer communication imply that communication is going directly from one client to another, without any servers in the middle? Since PHP is a server-based software, I don't think any program you write on it can be considered true p2p.
However, if you want to enable client to client communications with a php server as the middle man, that's definitely possible.
Depends on if you want the browser to be sending data to this PHP application.
I've made IRC bots entirely in PHP though, which showed their status and output in my web browser in a fashion much like mIRC. I just set the timeout limit to infinite and connected to the IRC server using sockets. You could connect to anything though. You can even make it listen for incoming connections and handle them.
What you can't do is to get a browser to keep a two-way connection without breaking off requests (not yet anyways...)
Yes, but its not what's generally called p2p, since there is a server in between. I have a feeling though that what you want to do is to have your peers communicate with each other, rather than have a direct connection between them with no 'middleman' server (which is what is normally meant by p2p)
Depending on the scalability requirements, implementing this kind of communication can be trivial (simple polling script on clients), or demanding (asynchronous comet server).
In case someone comes here seeing if you can write P2P software in PHP, the answer is yes, in this case, Quentin's answer to the original question is correct, PHP would have to be installed on the computer.
You can do whatever you want to do in PHP, including writing true p2p software. To create a true P2P program in PHP, you would use PHP as an interpreted language WITHOUT a web server, and you would use sockets - just like you would in c/c++. The original accepted answer is right and wrong, unless however the original poster was asking if PHP running on a webserver could be a p2p client - which would of course be no.
Basically to do this, you'd basically write a php script that:
Opens a server socket connection (stream_socket_server/socket_create)
Find a list of peer IP's
Open a client connection to each peer
...
Prove everyone wrong.
No, not really. PHP scripts are meant to run only for very small amount of time. Usually the default maximum runtime is two minutes which will be normally not enough for p2p communication. After this the script will be canceled though the server administrator can deactivate that. But even then the whole downloading time the http connection between the server and the client must be hold. The client's browser will show in this time its page loading indicator. If the connection breakes most web servers will kill the php script so the p2p download is canceled.
So it may be possible to implement the p2p protocol, but in a client/server scenario you run into problems with the execution model of php scripts.
both parties would need to be running a server such as apache although for demonstration purposes you could get away with just using the inbuilt php test server. Next you are going to have to research firewall hole punching in php I saw a script i think on github but was long time ago . Yes it can be done , if your client is not a savvy programmer type you would probably need to ensure that they have php installed and running. The path variable may not work unless you add it to the system registry in windows so make sure you provide a bat file that both would ensure the path is in the system registry so windows can find it .Sorry I am not a linux user.
Next you have to develop the code. There are instrucions for how hole punching works and it does require a server on the public domain which is required to allow 2 computers to find each others ip address. Maybe you could rig up something on a free website such as www.000.webhost.com alternatively you could use some kind of a built in mechanism such as using the persons email address. To report the current ip.
The biggest problem is routers and firewalls but packets even if they are directed at a public ip still need to know the destination on a lan so the information on how to write the packet should be straight forwards. With any luck you might find a script that has done most of the work for you.

Communication between PHP and application

I'm playing with an embedded Linux device and looking for a way to get my application code to communicate with a web interface. I need to show some status information from the application on the devices web interface and also would like to have a way to inform the application of any user actions like uploaded files etc. PHP-seems to be a good way to make the interface, but the communication part is harder. I have found the following options, but not sure which would be the easiest and most convenient to use.
Sockets. Have to enable sockets for the PHP first to try this. Don't know if enabling will take much more space.
Database. Seems like an overkill solution.
Shared file. Seems like a lot of work.
Named pipes. Tried this with some success, but not sure if there will be problems with for example on simultaneous page loads. Maybe sockets are easier?
What would be the best way to go? Is there something I'm totally missing? How is this done in those numerous commercial Linux based network switches?
I recently did something very similar using sockets, and it worked really well. I had a Java application that communicates with the device, which listened on a server socket, and the PHP application was the client.
So in your case, the PHP client would initialize the connection, and then the server can reply with the status of the device.
There's plenty of tutorials on how to do client/server socket communication with most languages, so it shouldn't take too long to figure out.
What kind of device is it?
If you work with something like a shared file, how will the device be updated?
How will named pipes run into concurrency problems that sockets will avoid?
In terms of communication from the device to PHP, a file seems perfect. PHP can use something basic like file_get_contents(), the device can just write to the file. If you're worried about the moment in time the file is updated to a quick length check.
In terms of PHP informing the device of what to do, I'm also leaning towards files. Have the device watch a directory, and have the script create a file there with something like file_put_contents($path . uniqid(), $command); That way should two scripts run at the exact sime time, you simply have two files for the device to work with.
Embedded linux boxes for routing with web interface don't use PHP. They use CGI and have shell scripts deliver the web page.
For getting information from the application to the web interface, the Shared file option seems most reasonable to me. The application can just write information into the file which is read by PHP.
The other way round it looks not so good at first. PHP supports locking of files, but it most probably doesn't work on a system level. Perhaps one solution is that in fact every PHP script which has information for the application creates it own file (with a unique id filename, e.g. based on timestamp + random value). The application could watch a designated directory for these files to pop-up. After processing them, it could just delete them. For that, the application only needs write permission on the directory (so file ownership is not an issue).
If possible, use shell scripts.
I did something similar, i wrote a video surveillance application. The video part is handled by motion (a great FOSS package). The application is a turn-key solution on standardized hardware, used to monitor slot-machine casinos. It serves as a kiosk system locally and is accessible via internet. I wrote all UI code in PHP, the local display is a tightly locked down KDE desktop with a full screen browser defaulting to localhost. I used shell scripts to interact with motion and the OS.
On a second thought:
If you can use self-compiled applications on the device: Write a simple program that returns the value you want and use PHP's exec() or passthru() or system().

Categories