Fake online website when offline
I hacked together this little proxy server to only download missing files. Just set up your /etc/hosts file to point sites you want to cache at 127.0.0.1 and those you want to block at 0.0.0.0
#!/bin/sh
nc -ll -p 80 -e sh -c '
while read A B DUMMY
do
case "$A" in
[Gg][Ee][Tt])
FULL=$B #full path of the file
F=${FULL##*/}
F=${F%%\?*} #file name only
#if we have it cat it back to browser
[ -f "$F" ] && cat "$F" && break
;;
[Hh][Oo][Ss][Tt]*)
[ -f "$F" ] && break #file already exists
HOST=${B:0:$((${#B}-1))} #the host name
#resolve by DNS first so we can cache it
sed -i "s/hosts:\t\tfiles /hosts:\t\t/g" /etc/nsswitch.conf
wget -t 0 -q --no-dns-cache $HOST$FULL
#got it now revert to checking host file 1st
sed -i "s/hosts:\t\t/hosts:\t\tfiles /g" /etc/nsswitch.conf
#cat the file because I didn't think to wget through tee
cat "$F"
break
;;
esac
done
'
Note that it puts all files in one directory, so it may cause version conflicts. (I did this intentionally so I wouldn't have 500 copies of jquery)
It sounds like you're running a local web server. Excellent.
In your Mac's filesystem, there is a file called /etc/hosts
. You can redirect all requests for fooCDN.com
to your local machine by adding this line in /etc/hosts
:
127.0.0.1 foocdn.com www.foocdn.com
You will need root (super user) permissions to edit /etc/hosts
.
The above line means that fooCDN.com
will load from your own computer, where a web server is listening.
You didn't specify what web server you're running locally, though. Following the web server's documentation, you should create a virtual host that points the document root of fooCDN.com
to /Users/name/Desktop/
.
This is a sample configuration (I haven't tested it myself) you can try to use with Apache:
<VirtualHost 127.0.0.1:80>
ServerName foocdn.com
ServerAlias www.foocdn.com
DocumentRoot /Users/name/Desktop
</VirtualHost>
Here's a sample configuration for Nginx (also untested):
server {
listen 80;
root /Users/name/Desktop;
server_name foocdn.com;
}
Don't forget to restart the you web server service or reload the new configuration file.
You could use proxy server software that supports URL rewriting to accomplish the task. Many proxy server applications support URL rewriting. E.g., the Charles Web Debugging Proxy Application for Windows, Mac OS, and Linux supports URL rewriting. You could install it on your Mac system and then configure the browsers on the system to use the proxy server.
Alternatively, Apache, which is free and open source, has mod_proxy and mod_rewrite modules.
Mitmproxy is free and will also run on a Mac OS X system.
If you need to pull items out of your browser's cache to make them available via the proxy server, you can use techniques provided at Viewing Chrome cache (the easy way). E.g., in Google Chrome you can put chrome:\\cache
in the browser's address bar and then locate the relevant items in the Chrome cache and copy them elsewhere.
Sounds like good old wwwoffle should suite your needs. It's a proxy server where you can select resources for offline usage.