Default onion domains

This commit is contained in:
Bob Mottram 2016-09-28 21:13:27 +01:00
parent 02c888574c
commit e7539c36c9
1 changed files with 250 additions and 262 deletions

View File

@ -29,338 +29,326 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
function wait_for_onion_service { function wait_for_onion_service {
onion_service_name="$1" onion_service_name="$1"
sleep_ctr=0 sleep_ctr=0
while [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; do while [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; do
sleep 1 sleep 1
sleep_ctr=$((sleep_ctr + 1)) sleep_ctr=$((sleep_ctr + 1))
if [ $sleep_ctr -gt 10 ]; then if [ $sleep_ctr -gt 10 ]; then
break break
fi fi
done done
if [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then if [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then
# restart and try a second time # restart and try a second time
systemctl restart tor systemctl restart tor
sleep_ctr=0 sleep_ctr=0
while [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; do while [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; do
sleep 1 sleep 1
sleep_ctr=$((sleep_ctr + 1)) sleep_ctr=$((sleep_ctr + 1))
if [ $sleep_ctr -gt 10 ]; then if [ $sleep_ctr -gt 10 ]; then
break break
fi fi
done done
fi fi
} }
function remove_onion_service { function remove_onion_service {
onion_service_name="$1" onion_service_name="$1"
onion_service_port_to=$2 onion_service_port_to=$2
sed -i "/hidden_service_${onion_service_name}/d" /etc/tor/torrc sed -i "/hidden_service_${onion_service_name}/d" /etc/tor/torrc
sed -i "/127.0.0.1:${onion_service_port_to}/d" /etc/tor/torrc sed -i "/127.0.0.1:${onion_service_port_to}/d" /etc/tor/torrc
if [ $3 ]; then if [ $3 ]; then
sed -i "/127.0.0.1:${3}/d" /etc/tor/torrc sed -i "/127.0.0.1:${3}/d" /etc/tor/torrc
if [ $4 ]; then if [ $4 ]; then
sed -i "/127.0.0.1:${4}/d" /etc/tor/torrc sed -i "/127.0.0.1:${4}/d" /etc/tor/torrc
if [ $5 ]; then if [ $5 ]; then
sed -i "/127.0.0.1:${5}/d" /etc/tor/torrc sed -i "/127.0.0.1:${5}/d" /etc/tor/torrc
fi fi
fi fi
fi fi
systemctl restart tor systemctl restart tor
} }
function add_onion_service { function add_onion_service {
onion_service_name="$1" onion_service_name="$1"
onion_service_port_from=$2 onion_service_port_from=$2
onion_service_port_to=$3 onion_service_port_to=$3
if [ -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then if [ -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then
echo $(cat /var/lib/tor/hidden_service_${onion_service_name}/hostname) echo $(cat /var/lib/tor/hidden_service_${onion_service_name}/hostname)
return return
fi fi
if [ ! -d /var/lib/tor ]; then if [ ! -d /var/lib/tor ]; then
echo $"No Tor installation found. ${onion_service_name} onion site cannot be configured." echo $"No Tor installation found. ${onion_service_name} onion site cannot be configured."
exit 877367 exit 877367
fi fi
if ! grep -q "hidden_service_${onion_service_name}" /etc/tor/torrc; then if ! grep -q "hidden_service_${onion_service_name}" /etc/tor/torrc; then
echo "HiddenServiceDir /var/lib/tor/hidden_service_${onion_service_name}/" >> /etc/tor/torrc echo "HiddenServiceDir /var/lib/tor/hidden_service_${onion_service_name}/" >> /etc/tor/torrc
echo "HiddenServicePort ${onion_service_port_from} 127.0.0.1:${onion_service_port_to}" >> /etc/tor/torrc echo "HiddenServicePort ${onion_service_port_from} 127.0.0.1:${onion_service_port_to}" >> /etc/tor/torrc
fi fi
systemctl restart tor systemctl restart tor
function_check wait_for_onion_service function_check wait_for_onion_service
wait_for_onion_service ${onion_service_name} wait_for_onion_service ${onion_service_name}
if [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then if [ ! -f /var/lib/tor/hidden_service_${onion_service_name}/hostname ]; then
echo $"${onion_service_name} onion site hostname not found" echo $"${onion_service_name} onion site hostname not found"
exit 76362 exit 76362
fi fi
echo $(cat /var/lib/tor/hidden_service_${onion_service_name}/hostname) echo $(cat /var/lib/tor/hidden_service_${onion_service_name}/hostname)
} }
function set_default_onion_domains { function set_default_onion_domains {
# If sites are only visible via Tor then for installation # If sites are only visible via Tor then for installation
# purposes assign them some default domain names # purposes assign them some default domain names
if [[ $ONION_ONLY == "no" ]]; then if [[ $ONION_ONLY == "no" ]]; then
return return
fi fi
if [ ${#MICROBLOG_DOMAIN_NAME} -gt 1 ]; then MICROBLOG_DOMAIN_NAME='microblog.local'
MICROBLOG_DOMAIN_NAME='microblog.local' FULLBLOG_DOMAIN_NAME='blog.local'
fi WIKI_DOMAIN_NAME='wiki.local'
if [ ${#FULLBLOG_DOMAIN_NAME} -gt 1 ]; then DEFAULT_DOMAIN_NAME="${PROJECT_NAME}.local"
FULLBLOG_DOMAIN_NAME='blog.local' GIT_DOMAIN_NAME='git.local'
fi MEDIAGOBLIN_DOMAIN_NAME='media.local'
if [ ${#WIKI_DOMAIN_NAME} -gt 1 ]; then
WIKI_DOMAIN_NAME='wiki.local'
fi
if [ ${#DEFAULT_DOMAIN_NAME} -gt 1 ]; then
DEFAULT_DOMAIN_NAME="${PROJECT_NAME}.local"
fi
if [ ${#GIT_DOMAIN_NAME} -gt 1 ]; then
GIT_DOMAIN_NAME='git.local'
fi
if [ ${#MEDIAGOBLIN_DOMAIN_NAME} -gt 1 ]; then
MEDIAGOBLIN_DOMAIN_NAME='media.local'
fi
} }
function create_avahi_onion_domains { function create_avahi_onion_domains {
if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
return return
fi fi
if [ ! -d /etc/avahi/services ]; then if [ ! -d /etc/avahi/services ]; then
return return
fi fi
if [ $MICROBLOG_DOMAIN_NAME ]; then if [ $MICROBLOG_DOMAIN_NAME ]; then
function_check create_avahi_service function_check create_avahi_service
create_avahi_service microblog http tcp $MICROBLOG_ONION_PORT create_avahi_service microblog http tcp $MICROBLOG_ONION_PORT
fi fi
if [ $FULLBLOG_DOMAIN_NAME ]; then if [ $FULLBLOG_DOMAIN_NAME ]; then
function_check create_avahi_service function_check create_avahi_service
create_avahi_service blog http tcp $BLOG_ONION_PORT create_avahi_service blog http tcp $BLOG_ONION_PORT
fi fi
if [ $GIT_DOMAIN_NAME ]; then if [ $GIT_DOMAIN_NAME ]; then
function_check create_avahi_service function_check create_avahi_service
create_avahi_service git http tcp $GIT_ONION_PORT create_avahi_service git http tcp $GIT_ONION_PORT
fi fi
if [ $WIKI_DOMAIN_NAME ]; then if [ $WIKI_DOMAIN_NAME ]; then
function_check create_avahi_service function_check create_avahi_service
create_avahi_service wiki http tcp $WIKI_ONION_PORT create_avahi_service wiki http tcp $WIKI_ONION_PORT
fi fi
} }
function allow_ssh_to_onion_address { function allow_ssh_to_onion_address {
if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
return return
fi fi
if [ ! -d /home/$MY_USERNAME/.ssh ]; then if [ ! -d /home/$MY_USERNAME/.ssh ]; then
mkdir /home/$MY_USERNAME/.ssh mkdir /home/$MY_USERNAME/.ssh
fi fi
if [ ! -d /etc/tor ]; then if [ ! -d /etc/tor ]; then
echo $'Tor not found when updating ssh' echo $'Tor not found when updating ssh'
exit 528257 exit 528257
fi fi
if ! grep -q "onion" /home/$MY_USERNAME/.ssh/config; then if ! grep -q "onion" /home/$MY_USERNAME/.ssh/config; then
echo 'Host *.onion' >> /home/$MY_USERNAME/.ssh/config echo 'Host *.onion' >> /home/$MY_USERNAME/.ssh/config
echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/$MY_USERNAME/.ssh/config echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/$MY_USERNAME/.ssh/config
fi fi
} }
function enable_ssh_via_onion { function enable_ssh_via_onion {
if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
return return
fi fi
if grep -Fxq "enable_ssh_via_onion" $COMPLETION_FILE; then if grep -Fxq "enable_ssh_via_onion" $COMPLETION_FILE; then
return return
fi fi
apt-get -y install tor connect-proxy apt-get -y install tor connect-proxy
if ! grep -q 'Host *.onion' /home/$MY_USERNAME/.ssh/config; then if ! grep -q 'Host *.onion' /home/$MY_USERNAME/.ssh/config; then
if [ ! -d /home/$MY_USERNAME/.ssh ]; then if [ ! -d /home/$MY_USERNAME/.ssh ]; then
mkdir /home/$MY_USERNAME/.ssh mkdir /home/$MY_USERNAME/.ssh
fi fi
echo 'Host *.onion' >> /home/$MY_USERNAME/.ssh/config echo 'Host *.onion' >> /home/$MY_USERNAME/.ssh/config
echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/$MY_USERNAME/.ssh/config echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /home/$MY_USERNAME/.ssh/config
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh
chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh/config chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/.ssh/config
fi fi
if ! grep -q 'Host *.onion' /root/.ssh/config; then if ! grep -q 'Host *.onion' /root/.ssh/config; then
if [ ! -d /root/.ssh ]; then if [ ! -d /root/.ssh ]; then
mkdir /root/.ssh mkdir /root/.ssh
fi fi
echo 'Host *.onion' >> /root/.ssh/config echo 'Host *.onion' >> /root/.ssh/config
echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /root/.ssh/config echo 'ProxyCommand connect -R remote -5 -S 127.0.0.1:9050 %h %p' >> /root/.ssh/config
fi fi
echo 'enable_ssh_via_onion' >> $COMPLETION_FILE echo 'enable_ssh_via_onion' >> $COMPLETION_FILE
} }
function configure_ssh_onion { function configure_ssh_onion {
if grep -Fxq "configure_ssh_onion" $COMPLETION_FILE; then if grep -Fxq "configure_ssh_onion" $COMPLETION_FILE; then
return return
fi fi
if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
return return
fi fi
SSH_ONION_HOSTNAME=$(add_onion_service ssh ${SSH_PORT} ${SSH_PORT}) SSH_ONION_HOSTNAME=$(add_onion_service ssh ${SSH_PORT} ${SSH_PORT})
if ! grep -q "ssh onion domain" $COMPLETION_FILE; then if ! grep -q "ssh onion domain" $COMPLETION_FILE; then
echo "ssh onion domain:${SSH_ONION_HOSTNAME}" >> $COMPLETION_FILE echo "ssh onion domain:${SSH_ONION_HOSTNAME}" >> $COMPLETION_FILE
else else
sed -i "s|ssh onion domain.*|ssh onion domain:${SSH_ONION_HOSTNAME}|g" $COMPLETION_FILE sed -i "s|ssh onion domain.*|ssh onion domain:${SSH_ONION_HOSTNAME}|g" $COMPLETION_FILE
fi fi
echo 'configure_ssh_onion' >> $COMPLETION_FILE echo 'configure_ssh_onion' >> $COMPLETION_FILE
} }
function install_tor { function install_tor {
if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
return return
fi fi
if grep -Fxq "install_tor" $COMPLETION_FILE; then if grep -Fxq "install_tor" $COMPLETION_FILE; then
return return
fi fi
apt-get -y install tor apt-get -y install tor
if [ ! -f /etc/tor/torrc ]; then if [ ! -f /etc/tor/torrc ]; then
echo 'Tor failed to install' echo 'Tor failed to install'
exit 38259 exit 38259
fi fi
echo 'install_tor' >> $COMPLETION_FILE echo 'install_tor' >> $COMPLETION_FILE
} }
function resolve_dns_via_tor { function resolve_dns_via_tor {
if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
return return
fi fi
if grep -Fxq "resolve_dns_via_tor" $COMPLETION_FILE; then if grep -Fxq "resolve_dns_via_tor" $COMPLETION_FILE; then
return return
fi fi
if [ ! -f /etc/tor/torrc ]; then if [ ! -f /etc/tor/torrc ]; then
echo $'tor was not installed' echo $'tor was not installed'
exit 52952 exit 52952
fi fi
# resolve DNS via tor # resolve DNS via tor
if ! grep 'DNSPort 53' /etc/tor/torrc; then if ! grep 'DNSPort 53' /etc/tor/torrc; then
echo 'DNSPort 53' >> /etc/tor/torrc echo 'DNSPort 53' >> /etc/tor/torrc
echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
echo 'AutomapHostsSuffixes .exit,.onion' >> /etc/tor/torrc echo 'AutomapHostsSuffixes .exit,.onion' >> /etc/tor/torrc
systemctl restart tor systemctl restart tor
fi fi
# don't change resolv.conf # don't change resolv.conf
sed -i 's|, domain-name-servers||g' /etc/dhcp/dhclient.conf sed -i 's|, domain-name-servers||g' /etc/dhcp/dhclient.conf
# point resolv.conf to tor # point resolv.conf to tor
echo 'nameserver 127.0.0.1:53' > /etc/resolv.conf echo 'nameserver 127.0.0.1:53' > /etc/resolv.conf
# prevent resolv.conf from changing # prevent resolv.conf from changing
chattr +i /etc/resolv.conf chattr +i /etc/resolv.conf
echo 'resolve_dns_via_tor' >> $COMPLETION_FILE echo 'resolve_dns_via_tor' >> $COMPLETION_FILE
} }
# see https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy # see https://trac.torproject.org/projects/tor/wiki/doc/TransparentProxy
# Local Redirection and Anonymizing Middlebox # Local Redirection and Anonymizing Middlebox
function route_outgoing_traffic_through_tor { function route_outgoing_traffic_through_tor {
if grep -Fxq "route_outgoing_traffic_through_tor" $COMPLETION_FILE; then if grep -Fxq "route_outgoing_traffic_through_tor" $COMPLETION_FILE; then
return return
fi fi
if [[ $ROUTE_THROUGH_TOR != "yes" ]]; then if [[ $ROUTE_THROUGH_TOR != "yes" ]]; then
return return
fi fi
apt-get -y install tor tor-arm apt-get -y install tor tor-arm
### set variables ### set variables
# Destinations you don't want routed through Tor # Destinations you don't want routed through Tor
_non_tor="192.168.1.0/24 192.168.0.0/24" _non_tor="192.168.1.0/24 192.168.0.0/24"
# The user that Tor runs as # The user that Tor runs as
_tor_uid="debian-tor" _tor_uid="debian-tor"
# Tor's TransPort # Tor's TransPort
_trans_port="9040" _trans_port="9040"
# Your internal interface # Your internal interface
_int_if="eth0" _int_if="eth0"
### Set iptables *nat ### Set iptables *nat
iptables -t nat -A OUTPUT -o lo -j RETURN iptables -t nat -A OUTPUT -o lo -j RETURN
iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN iptables -t nat -A OUTPUT -m owner --uid-owner $_tor_uid -j RETURN
iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53 iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
# Allow clearnet access for hosts in $_non_tor # Allow clearnet access for hosts in $_non_tor
for _clearnet in $_non_tor; do for _clearnet in $_non_tor; do
iptables -t nat -A OUTPUT -d $_clearnet -j RETURN iptables -t nat -A OUTPUT -d $_clearnet -j RETURN
iptables -t nat -A PREROUTING -i $_int_if -d $_clearnet -j RETURN iptables -t nat -A PREROUTING -i $_int_if -d $_clearnet -j RETURN
done done
# Redirect all other pre-routing and output to Tor # Redirect all other pre-routing and output to Tor
iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $_trans_port
iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports 53 iptables -t nat -A PREROUTING -i $_int_if -p udp --dport 53 -j REDIRECT --to-ports 53
iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port iptables -t nat -A PREROUTING -i $_int_if -p tcp --syn -j REDIRECT --to-ports $_trans_port
### set iptables *filter ### set iptables *filter
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow clearnet access for hosts in $_non_tor # Allow clearnet access for hosts in $_non_tor
for _clearnet in $_non_tor 127.0.0.0/8; do for _clearnet in $_non_tor 127.0.0.0/8; do
iptables -A OUTPUT -d $_clearnet -j ACCEPT iptables -A OUTPUT -d $_clearnet -j ACCEPT
done done
# Allow only Tor output # Allow only Tor output
iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT iptables -A OUTPUT -m owner --uid-owner $_tor_uid -j ACCEPT
iptables -A OUTPUT -j REJECT iptables -A OUTPUT -j REJECT
function_check save_firewall_settings function_check save_firewall_settings
save_firewall_settings save_firewall_settings
if ! grep -q "fs.file-max" /etc/sysctl.conf; then if ! grep -q "fs.file-max" /etc/sysctl.conf; then
echo "fs.file-max=100000" >> /etc/sysctl.conf echo "fs.file-max=100000" >> /etc/sysctl.conf
/sbin/sysctl -p /sbin/sysctl -p
fi fi
echo 'domain localdomain' > /etc/resolv.conf echo 'domain localdomain' > /etc/resolv.conf
echo 'search localdomain' >> /etc/resolv.conf echo 'search localdomain' >> /etc/resolv.conf
echo 'nameserver 127.0.0.1' >> /etc/resolv.conf echo 'nameserver 127.0.0.1' >> /etc/resolv.conf
if ! grep -q "VirtualAddrNetworkIPv4" /etc/tor/torrc; then if ! grep -q "VirtualAddrNetworkIPv4" /etc/tor/torrc; then
echo 'VirtualAddrNetworkIPv4 10.192.0.0/10' >> /etc/tor/torrc echo 'VirtualAddrNetworkIPv4 10.192.0.0/10' >> /etc/tor/torrc
fi fi
if ! grep -q "AutomapHostsOnResolve" /etc/tor/torrc; then if ! grep -q "AutomapHostsOnResolve" /etc/tor/torrc; then
echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc echo 'AutomapHostsOnResolve 1' >> /etc/tor/torrc
fi fi
if ! grep -q "TransPort" /etc/tor/torrc; then if ! grep -q "TransPort" /etc/tor/torrc; then
echo 'TransPort 9040' >> /etc/tor/torrc echo 'TransPort 9040' >> /etc/tor/torrc
fi fi
if ! grep -q "TransListenAddress 127.0.0.1" /etc/tor/torrc; then if ! grep -q "TransListenAddress 127.0.0.1" /etc/tor/torrc; then
echo 'TransListenAddress 127.0.0.1' >> /etc/tor/torrc echo 'TransListenAddress 127.0.0.1' >> /etc/tor/torrc
fi fi
if ! grep -q "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then if ! grep -q "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
echo "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc echo "TransListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
fi fi
if ! grep -q "DNSPort" /etc/tor/torrc; then if ! grep -q "DNSPort" /etc/tor/torrc; then
echo 'DNSPort 53' >> /etc/tor/torrc echo 'DNSPort 53' >> /etc/tor/torrc
fi fi
if ! grep -q "DNSListenAddress 127.0.0.1" /etc/tor/torrc; then if ! grep -q "DNSListenAddress 127.0.0.1" /etc/tor/torrc; then
echo 'DNSListenAddress 127.0.0.1' >> /etc/tor/torrc echo 'DNSListenAddress 127.0.0.1' >> /etc/tor/torrc
fi fi
if ! grep -q "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then if ! grep -q "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" /etc/tor/torrc; then
echo "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc echo "DNSListenAddress $LOCAL_NETWORK_STATIC_IP_ADDRESS" >> /etc/tor/torrc
fi fi
echo 'route_outgoing_traffic_through_tor' >> $COMPLETION_FILE echo 'route_outgoing_traffic_through_tor' >> $COMPLETION_FILE
} }
# NOTE: deliberately no exit 0 # NOTE: deliberately no exit 0