Created
March 25, 2012 08:50
-
-
Save cybermonde/2192453 to your computer and use it in GitHub Desktop.
Personnaliser la page d'accueil de la PirateBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################# | |
# PirateBox global config. /etc/init.d/piratebox setup to take effect | |
pb_ip="192.168.1.1" | |
pb_wireless_ssid="PirateBox - Share Freely" | |
pb_hostname="piratebox" | |
pb_usbdevice="/dev/sda1" | |
pb_usbmount="/mnt/usb" | |
pb_usbdir="$pb_usbmount/PirateBox" | |
pb_share="$pb_usbdir/Shared" | |
pb_extimg="$pb_usbdir/OpenWRT.img" | |
pb_extmount="/mnt/ext" | |
pb_swapimg="$pb_usbdir/OpenWRT.swap" | |
############################################################################# | |
# PirateBox helper functions | |
pb_nodns() { | |
# disable local DNS per dnsmasq | |
cp /tmp/resolv.conf.auto /tmp/resolv.conf | |
} | |
pb_usbwait() { | |
# wait for USB device to show up | |
if [ ! -e "$pb_usbdevice" ] | |
then | |
echo "Please connect USB disk. If it is already plugged-in," | |
echo "wait for a few seconds, and if nothing happens, please" | |
echo "reconnect the device. Keep in mind that your router's" | |
echo "ports may not be able to supply enough power to" | |
echo "spin-up a USB HDD." | |
while [ ! -e "$pb_usbdevice" ] | |
do | |
sleep 1 | |
echo -n "." | |
done | |
echo | |
echo "USB disk is now available. Continuing..." | |
fi | |
} | |
pb_ismounted() { | |
mount | grep "on $1 " >/dev/null | |
} | |
pb_mount() { | |
parts=$* | |
[ "$parts" == "-a" ] && parts="usb ext swap" | |
for part in $* | |
do | |
case $part in | |
usb) | |
pb_ismounted "$pb_usbmount" && continue | |
[ -d "$pb_usbmount" ] || mkdir -p "$pb_usbmount" | |
if ! mount "$pb_usbdevice" "$pb_usbmount" | |
then | |
echo "${initscript}: ERROR: unable to mount USB storage" >&2 | |
exit 5 | |
fi | |
;; | |
ext) | |
pb_ismounted "$pb_extmount" && continue | |
pb_mount usb | |
[ -d "$pb_extmount" ] || mkdir -p "$pb_extmount" | |
if ! mount -o loop,rw,sync "$pb_extimg" "$pb_extmount" | |
then | |
echo "${initscript}: ERROR: unable to mount ext image" >&2 | |
exit 5 | |
fi | |
;; | |
swap) | |
pb_mount usb | |
swapon "$pb_swapimg" &>/dev/null | |
;; | |
esac | |
done | |
} | |
pb_umount() { | |
parts=$* | |
[ "$parts" == "-a" ] && parts="swap ext usb" | |
for part in $* | |
do | |
case $part in | |
usb) | |
pb_ismounted "$pb_usbmount" || continue | |
pb_umount swap ext | |
if ! umount "$pb_usbmount" | |
then | |
echo "${initscript}: WARNING: unable to umount USB storage" >&2 | |
fi | |
;; | |
ext) | |
# losetup -d here just in case user umounted manually | |
losetup -d /dev/loop0 &>/dev/null | |
pb_ismounted "$pb_extmount" || continue | |
if ! umount "$pb_extmount" | |
then | |
# PirateBox might still be running, so... | |
# silently try to stop PirateBox service | |
pb_stop &>/dev/null | |
sleep 2 | |
if ! umount "$pb_extmount" | |
then | |
echo "${initscript}: WARNING: unable to umount ext image" >&2 | |
fi | |
fi | |
losetup -d /dev/loop0 | |
;; | |
swap) | |
#TODO: error handling | |
swapoff "$pb_swapimg" &>/dev/null | |
;; | |
esac | |
done | |
} | |
pb_uciadd() { | |
local config=$1 ; shift | |
local section=$1 ; shift | |
local type=$1 ; shift | |
[ -e "/etc/config/$config" ] || touch "/etc/config/$config" | |
uci show "$config.$section" &>/dev/null \ | |
|| uci add "$config $section" &>/dev/null | |
uci set "$config.$section=$type" | |
} | |
pb_uciset() { | |
local config=$1 ; shift | |
local section=$1 ; shift | |
local type=$1 ; shift | |
pb_uciadd $config $section $type | |
for opt in "$@" | |
do | |
uci set "$config.$section.$opt" | |
done | |
uci commit | |
} | |
pb_netconfig() { | |
# reconfigure network | |
uci set "system.@system[0].hostname=$pb_hostname" | |
echo "127.0.0.1 $pb_hostname localhost." >/etc/hosts | |
echo "$pb_ip $pb_hostname" >>/etc/hosts | |
uci set "network.lan.ipaddr=$pb_ip" | |
#CAVE: wireless config might be platform specific | |
uci set "wireless.radio0.disabled=0" | |
uci set "wireless.@wifi-iface[0].ssid=$pb_wireless_ssid" | |
# redirect all 80/tcp traffic to the PirateBox | |
pb_uciset firewall piratebox redirect \ | |
src=lan \ | |
proto=tcp \ | |
src_dport=80 \ | |
dest_ip=$pb_ip \ | |
dest_port=80 | |
# redirect all DNS requests to PirateBox IP | |
pb_uciset dhcp piratebox domain \ | |
name='#' \ | |
ip=$pb_ip | |
# CAVE: ugly hack, patching a preinstalled file | |
# patch dnsmasq start script to properly handle domain wildcards | |
sed -i 's#^.*\${fqdn\%\.\*}\" ==.*$## ; s#^.*fqdn=\"\$fqdn.*$##' \ | |
/etc/init.d/dnsmasq | |
} | |
pb_usbconfig() { | |
# prepare USB disk for mounting | |
[ -d "$pb_usbmount" ] || mkdir -p "$pb_usbmount" | |
pb_uciset fstab piratebox mount \ | |
target=$pb_usbmount \ | |
device=$pb_usbdevice \ | |
fstype=vfat \ | |
options=rw,sync \ | |
enabled_fsck=0 \ | |
enabled=1 | |
} | |
pb_undoconfig() { | |
# undo config changes | |
uci delete dhcp.piratebox | |
uci delete firewall.piratebox | |
uci delete fstab.piratebox | |
uci delete fstab.pirateswap | |
uci set "system.@system[0].hostname=OpenWRT" | |
uci set "network.lan.ipaddr=192.168.1.1" | |
uci commit | |
cp /rom/etc/opkg.conf /etc/ | |
cp /rom/etc/hosts /etc/ | |
cp /rom/etc/init.d/dnsmasq /etc/init.d/ | |
rm /usr/local | |
[ -d "$pb_usbmount" ] && rmdir "$pb_usbmount" | |
[ -d "$pb_extmount" ] && rmdir "$pb_extmount" | |
} | |
pb_needswap() { | |
[ ! -e "$pb_swapimg" ] | |
} | |
pb_mkswap() { | |
pb_umount swap | |
pb_mount usb | |
[ -d "$(dirname $pb_swapimg)" ] || mkdir -p "$(dirname $pb_swapimg)" | |
# create a swap file | |
dd if=/dev/zero of="$pb_swapimg" bs=1M count=32 | |
mkswap "$pb_swapimg" | |
pb_uciset fstab swap pirateswap \ | |
device=$pb_swapimg \ | |
enabled=1 | |
} | |
pb_needextimg() { | |
# [ ! -e "$pb_extimg" ] | |
# always re-create ext image because of opkg woes | |
true | |
} | |
pb_mkextimg() { | |
pb_umount ext | |
pb_mount usb | |
[ -d "$(dirname $pb_extimg)" ] || mkdir -p "$(dirname $pb_extimg)" | |
# use a preformatted image to get rid of e2fsprogs dependency | |
#dd if=/dev/zero of="$pb_extimg" bs=1M count=16 | |
#echo y | mkfs.ext3 "$pb_extimg" | |
gzip -dc /usr/share/piratebox/OpenWRT.img.gz >"$pb_extimg" | |
[ -d "$pb_extmount" ] || mkdir -p "$pb_extmount" | |
pb_uciset fstab piratebox mount \ | |
target=$pb_extmount \ | |
device=$pb_extimg \ | |
options=loop \ | |
enabled=1 | |
} | |
pb_init() { | |
echo "${initscript}: Mounting external USB storage..." | |
pb_mount usb | |
[ -d "$pb_usbdir" ] || mkdir -p "$pb_usbdir" | |
if pb_needswap | |
then | |
echo "${initscript}: Creating swapspace..." | |
pb_mkswap | |
fi | |
if pb_needextimg | |
then | |
echo "${initscript}: Creating ext loop image..." | |
pb_mkextimg | |
fi | |
pb_mount ext | |
# prepare opkg for /usr/local | |
grep "^dest piratebox" /etc/opkg.conf \ | |
|| echo "dest piratebox $pb_extmount" >>/etc/opkg.conf | |
# link ext usr to /usr/local | |
ln -sf "$pb_extmount/usr" /usr/local | |
# fix for ext packages not installing to usr | |
[ -d "$pb_extmount/usr/lib" ] || mkdir -p "$pb_extmount/usr/lib" | |
ln -sf "$pb_extmount/usr/lib" "$pb_extmount/lib" | |
} | |
pb_update() { | |
pb_mount ext | |
# disable local DNS redirection | |
pb_nodns | |
# CAVE: ugly hack | |
# in case we were called from the postinst, remove opkg lock | |
[ -e /var/lock/opkg.lock ] && \ | |
mv /var/lock/opkg.lock /var/lock/opkg.lock_ | |
# install python to piratebox storage | |
if ! opkg update | |
then | |
echo "${initscript}: ERROR: No Internet connection on WAN port" >&2 | |
exit 5 | |
fi | |
opkg -d piratebox install python | |
# CAVE: ugly hack | |
# in case we were called from the postinst, restore opkg lock | |
[ -e /var/lock/opkg.lock_ ] && \ | |
mv /var/lock/opkg.lock_ /var/lock/opkg.lock | |
# get PirateBox files | |
[ -d "$pb_share" ] || mkdir -p "$pb_share" | |
mkdir -p "$pb_share" | |
cd "$pb_share" | |
wget http://daviddarts.com/piratebox/piratebox-logo.png -O piratebox-logo.png | |
wget http://daviddarts.com/piratebox/.READ.ME.htm -O .READ.ME.htm | |
wget http://daviddarts.com/piratebox/.BACK.TO.MENU.htm -O .BACK.TO.MENU.htm | |
# adjust hostname | |
sed -i "s#http://piratebox#http://$pb_hostname#g" .READ.ME.htm | |
sed -i "s#http://piratebox#http://$pb_hostname#g" .BACK.TO.MENU.htm | |
# install PirateBox droopy port | |
[ -d /usr/local/bin ] || mkdir -p /usr/local/bin | |
cd /usr/local/bin | |
wget http://daviddarts.com/piratebox/droopy -O droopy | |
chmod +x droopy | |
} | |
pb_start() { | |
pb_mount usb ext swap | |
# PirateBox Welcome HTML | |
local html=$(cat <<EOF | |
<p><br> | |
<b>1.</b> Learn more about the project | |
<a href="http://$pb_hostname:8001/.READ.ME.htm"><b>here</b></a>. | |
</p> | |
<p> | |
<b>2.</b> Click above to begin sharing. | |
</p> | |
<p> | |
<b>3.</b> Browse and download files | |
<a href="http://$pb_hostname:8001"><b>here</b></a>. | |
</p> | |
<p>Use <b>#PirateBoxBru</b> to tell about me...<p> | |
EOF | |
) | |
export PATH=$PATH:/usr/local/bin | |
export LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib | |
cd "$pb_share" | |
start-stop-daemon -S -x python \ | |
-p /var/run/${NAME}.droopy.pid \ | |
-m -b -- \ | |
/usr/local/bin/droopy \ | |
-p "$pb_share/piratebox-logo.png" \ | |
-d "$pb_share" -m "$html" | |
start-stop-daemon -S -x python \ | |
-p /var/run/${NAME}.simple.pid \ | |
-c nobody:nogroup -m -b -- \ | |
-m SimpleHTTPServer 8001 | |
} | |
pb_stop() { | |
# killing all piratebox processes | |
local pidfile | |
for pidfile in $(ls /var/run/${NAME}.*.pid 2>/dev/null) | |
do | |
start-stop-daemon -K -s HUP -p "${pidfile}" | |
rm -f "${pidfile}" | |
done | |
[ -z "${pidfile}" ] && echo "${initscript}: not running" | |
sleep 3 | |
pb_umount usb | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Documentation : http://blog.cybermonde.org/post/2012/03/26/PirateBox