#!/bin/sh

killall dhclient

echo "Configuring Firefox to use Privoxy"
(
cd .mozilla/firefox/*.default
cat <<EOF >> prefs.js
user_pref("network.proxy.http", "127.0.0.1");
user_pref("network.proxy.http_port", 8118);
user_pref("network.proxy.ssl", "127.0.0.1");
user_pref("network.proxy.ssl_port", 8118);
user_pref("network.proxy.type", 1);
EOF
)

echo "Setting http_proxy to Privoxy"
export http_proxy="http://127.0.0.1:8118/"
export ftp_proxy=


echo "Preparing and starting Tor"
cp /usr/local/etc/tor/torrc.sample /usr/local/etc/tor/torrc
cat <<EOF >> /usr/local/etc/tor/torrc
ControlPort 9051
TransPort 9040
Log notice file /var/log/tor/notices.log
Log debug file /var/log/tor/debug.log
SafeLogging 0
EOF
mkdir -p /var/db/tor /var/log/tor/ 
chown _tor /var/db/tor /var/log/tor/ /dev/pf
/usr/local/etc/rc.d/tor forcestart


echo "Preparing and starting dns-proxy-tor"
cat <<EOF >> /etc/rc.conf
dns_proxy_tor_enable="YES"
dns_proxy_tor_bind_port="53"
dns_proxy_tor_start_privileged="YES"
EOF
echo "nameserver 127.0.0.1" > /etc/resolv.conf
sed -i bak 's@/usr/local/var/run/@/var/run/@' /usr/local/etc/rc.d/dns-proxy-tor
mkdir -p /var/run/dns-proxy-tor
chown _dns-proxy-tor /var/run/dns-proxy-tor
/usr/local/etc/rc.d/dns-proxy-tor forcestart


echo "Preparing and starting Privoxy"
mkdir /var/log/privoxy /var/run/privoxy/
chown privoxy /var/log/privoxy /var/run/privoxy/

# Since 3.0.6,1 the Privoxy port copies the config file as example.
# With the Privoxy 3.0.5 beta port the next line will cause an error message.
cp /usr/local/share/examples/privoxy/config /usr/local/etc/privoxy/config

cat <<EOF >> /usr/local/etc/privoxy/config
forward-socks4a / 127.0.0.1:9050 .
actionsfile freesbie
EOF

# These filters are available in Privoxy 3.0.6 and later
cat <<EOF >> /usr/local/etc/privoxy/freesbie.action
{+fast-redirects{check-decoded-url} \\
 +prevent-compression \\
}
/

{+filter{hide-tor-exit-notation} \\
 +filter-client-headers \\
}
.exit/

{+filter{google} \\
}
.google.

{+filter{yahoo} \\
}
.yahoo.

{+filter{msn} \\
}
.yahoo.

{+filter{blogspot} \\
}
.blogspot.

EOF

/usr/local/etc/rc.d/privoxy forcestart



echo -n "Waiting for Tor to get ready. This may take several minutes ..."
until ( grep "successfully" /var/log/tor/notices.log > /dev/null );
do
  echo -n .
  sleep 3;
done
echo;echo "Tor is ready."

echo "Configuring PF to redirect outgoing TCP connections into Tor"
# PF configuration mostly copied from:
# http://wiki.noreply.org/noreply/TheOnionRouter/TransparentProxy
cat <<EOF > /etc/pf-tor.conf
trans_port = "9040"
table <no_tor> persist

scrub in

rdr pass on lo1 inet proto tcp all -> 127.0.0.1 port \$trans_port

block return out

pass out quick on lo0 route-to lo1 inet proto tcp to 127.192.0.0/10 flags S/SA modulate state
pass quick on { lo0 lo1 } keep state

pass out quick inet to <no_tor> keep state
pass out quick inet proto tcp user _tor flags S/SA modulate state
pass out route-to lo1 inet proto tcp all flags S/SA modulate state
EOF

ifconfig lo1 create up 127.0.0.2
pfctl -ef /etc/pf-tor.conf


