How do I detect web surfing habits and redirect the browser?
I have a simple basic program in mind to keep my kids off certain websites for prolonged periods of time.
Basically, I would like to run a l开发者_运维知识库ittle background process that would have something like this :
if (user is on website for > 20 minutes every 24 hours )
{
browser redirect user to specified website
}
I realise I could easily get a third party program that would do this, but I'd enjoy doing it myself ! The problem is that I have absolutely no idea how to understake such a project - or even what languages I could use to make something like this. I have a fairly decent knowledge of PHP and Java and am always learning, so any suggestions for a way to go about this would be really appreciated !
Thanks a lot :)
There is no simple basic program that will do this.
Something like this requires that they are always using the same browser, and that it publishes an inter-process API that allows you to see what websites are being viewed and navigate to other websites. That's a lot of assumptions. Your kids could always use a different browser, or they could kill the process on the system. No matter what software you install on their machine, there is almost invariably a way for them to get around it. There's not going to be an easy way to do it.
The best solution is hard. Basically, it's firewalling. Set up a firewall in your house and put a filter on packets being forwarded. Inspect the packets to find HTTP requests, and intercept them. Log what's being requested and if it exceeds your limit, rewrite the packets how you want.
It might be a better idea to just look for a commercial software that does what you want.
If you are using FireFox, you can probably achieve this with a GreaseMonkey script.
I think you could achieve that kind of feature with a programmable proxy. In the ruby world, there was, during _why's existence, the fantastic MouseHole. Unfortunatly, those times are gone. Nowadays, using Java, I would take a look at things like this document. Finally, you can also take a look at proxomitron.
I see two simple way to approach this problem:
- You can write an extension for your preferred browser. Look, for example, at Firefox Building an Extension tutorial.
- You can write a http(s) proxy and set your browser to use it.
I do not recommend the 2nd one, IMHO it requires too much work for a simple in-home solution.
Short answer is that this is a fairly difficult problem to solve because of the reasons cited by Erick.
The only way to do it would be to set up something along the lines of WireShark that would analyze all network traffic on the machine and look for requests made to a certain url pattern (facebook) and start a timer whenever it happens. However, there are additional complexities when one wants to say, "I don't want to spend more than 20 minutes here a day" because it's nearly impossible in todays web to define what it means to spend 20 minutes at a site. See this thread on the OS X Hints forums for a helpful discussion on why this is hard. It basically boils down to the way websites do requests for you these days rather than you manually clicking. Of course, if you don't care about the user actually being there, but just giving them 20 minutes a day from the first time the log on to the site, then the problem becomes a little easier.
You could figure out how to write software for your router as one possible project. Using Tomato or DD-WRT gets you the ability to control what's running on your router and would give the centralized access you need to place a sniffer on the whole network (especially if you have multiple PCs you're trying to do this to). Of course, that adds the complexity in of trying to figure out what it means for one person to spend time on the website over against another. If all your PCs are single user then the problems not that hard but if you have multiple users per PC then you run into not even being able to key off of the IP address as a unique user.
If you don't want to program your router, then you'd have to write up a network sniffer and install it on every machine. Something like jNetStream might give you a nice head start as writing a network sniffer probably doesn't qualify as a "simple" program like you thought you were going to write.
Anyway, once you get that set up and running you'd just have to figure out how to hook it into your OS. I doubt any language is really "inappropriate" for that task so have at it!
You need to program a (smart) http proxy server for that. This is generaly considered a non trivial activity. But hey, there are hackers out their who say it can be done:
A HTTP Proxy Server in 20 Lines of node.js Code (It uses the node.js library)
Just add you're own logic and there you are. King of the proxy hill ;-)
精彩评论