Last active
March 2, 2022 09:27
-
-
Save selankon/49d61a7b167b6d24a02c45895598e46c to your computer and use it in GitHub Desktop.
Create all necessary files for a tinc host or server with an option to redirect all traffic to tinc interface
This file contains hidden or 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
#/bin/bash | |
# Script to manage easy the creation and configuration of a tinc client | |
# Net and host names | |
NETNAME=lanparty | |
HOSTNAME=mylaptop | |
# Server settings | |
SERVERNAME=server # Let this string empty if you are creating the server (with a public ip address) | |
PUBLICADDRESS=ageof.lol | |
PORT= # Empty for 655 default port | |
# Configs | |
INTERFACE=tinc | |
MODE=switch | |
IP=10.1.0.10 | |
SUBNET=$IP/32 | |
# Redirect-gateway mode | |
# Used to redirect all trafic of your computer to tinc interface when server is up | |
REDIRECTGATEWAY= # Empty for not do it. Write the rules for the server up-down to redirect all traffic to him. | |
VPN_GATEWAY=10.1.0.1 | |
if [ "$EUID" -ne 0 ] | |
then echo "Please run as root" | |
exit | |
fi | |
# Create all files | |
echo "Creating files.." | |
mkdir /etc/tinc/$NETNAME | |
mkdir /etc/tinc/$NETNAME/hosts | |
touch /etc/tinc/$NETNAME/tinc-up | |
touch /etc/tinc/$NETNAME/tinc-down | |
touch /etc/tinc/$NETNAME/tinc.conf | |
touch /etc/tinc/$NETNAME/hosts/$HOSTNAME | |
# fill tinc.conf | |
echo "Writting tinc.conf" | |
cd /etc/tinc/$NETNAME | |
echo "Name = $HOSTNAME" >> tinc.conf | |
echo "AddressFamily = ipv4" >> tinc.conf | |
echo "Interface = $INTERFACE" >> tinc.conf | |
if [ -n "$SERVERNAME" ]; then | |
echo "ConnectTo = $SERVERNAME" >> tinc.conf | |
fi | |
echo "Mode=$MODE" >> tinc.conf | |
# Writting tinc-up | |
echo "Writting tinc-up" | |
chmod 755 tinc-up | |
echo "ip link set \$INTERFACE up" >> tinc-up | |
echo "ip addr add $IP/24 dev \$INTERFACE" >> tinc-up | |
# Deprecated | |
#echo "ifconfig \$INTERFACE $IP netmask 255.255.255.0" >> tinc-up | |
# Writting tinc-down | |
echo "Writting tinc-down" | |
chmod 755 tinc-down | |
echo "ip addr $IP/24 dev \$INTERFACE " >> tinc-down | |
echo "ip link set \$INTERFACE down" >> tinc-down | |
# Deprecated | |
#echo "ifconfig \$INTERFACE down" >> tinc-down | |
# Writting host file | |
echo "Writting host file" | |
echo "Subnet = $SUBNET" >> hosts/$HOSTNAME | |
if [ ! -n "$SERVERNAME" ]; then # Add the redirect-gateway for the server | |
echo "Subnet = 0.0.0.0/0" >> hosts/$HOSTNAME | |
echo "Address = $PUBLICADDRESS" >> hosts/$HOSTNAME | |
else | |
touch hosts/$SERVERNAME | |
echo "<< IMPORTANT!! >> Manually copy or other peers server hosts file!" | |
#echo -e $SERVERKEY >> hosts/$SERVERNAME | |
fi | |
if [ -n "$PORT" ]; then | |
echo "Port = $PORT" >> hosts/$HOSTNAME | |
fi | |
# Creating keys | |
echo "Creating keys..." | |
sudo tincd -n $NETNAME -K4096 | |
# Creating redirect gateway files | |
if [ -n "$REDIRECTGATEWAY" ]; then | |
echo "Creating redirect gateway files" | |
SERVERUP=hosts/$SERVERNAME-up | |
echo "Creating $SERVERUP" | |
touch $SERVERUP | |
chmod 755 $SERVERUP | |
echo "#!/bin/sh" >> $SERVERUP | |
echo "VPN_GATEWAY=$VPN_GATEWAY" >> $SERVERUP | |
echo "ORIGINAL_GATEWAY=\`ip route show | grep ^default | cut -d ' ' -f 2-5\`" >> $SERVERUP | |
echo "ip route add \$REMOTEADDRESS \$ORIGINAL_GATEWAY" >> $SERVERUP | |
echo "ip route add \$VPN_GATEWAY dev \$INTERFACE" >> $SERVERUP | |
echo "ip route add 0.0.0.0/1 via \$VPN_GATEWAY dev \$INTERFACE" >> $SERVERUP | |
echo "ip route add 128.0.0.0/1 via \$VPN_GATEWAY dev \$INTERFACE" >> $SERVERUP | |
SERVERDOWN=hosts/$SERVERNAME-down | |
echo "Creating hosts/$SERVERDOWN" | |
touch $SERVERDOWN | |
chmod 755 $SERVERDOWN | |
echo "#!/bin/sh" >> $SERVERDOWN | |
echo "ORIGINAL_GATEWAY=\`ip route show | grep ^default | cut -d ' ' -f 2-5\`" >> $SERVERDOWN | |
echo "ip route del \$REMOTEADDRESS \$ORIGINAL_GATEWAY" >> $SERVERDOWN | |
echo "ip route del \$VPN_GATEWAY dev \$INTERFACE" >> $SERVERDOWN | |
echo "ip route del 0.0.0.0/1 dev \$INTERFACE" >> $SERVERDOWN | |
echo "ip route del 128.0.0.0/1 dev \$INTERFACE " >> $SERVERDOWN | |
fi | |
echo "done" | |
echo "Copy your server or other hosts files to connect to other peers on:" | |
echo "/etc/tinc/$NETNAME/hosts/" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment