summaryrefslogtreecommitdiffstats
path: root/retiolum/scripts/tinc_setup/new_install.sh
blob: ab42aedc7114279f635fdd7cd95ab2fbd1159320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/sh

usage()
{
cat << EOF
usage $0 options
This script gets you into the KREBS Darknet
all parameters are optional

Options:
 -h          Show this message(haha)
 -4 \$ipv4   specify an ip(version 4), this also disables random ip mode, default is random
 -t \$DIR    Choose another Temporary directory, default is /tmp/tinc-install-fu
 -o \$HOST   Choose another Hostname, default is your system hostname
 -n \$NET    Choose another tincd netname,this also specifies the path to your tinc config, default is retiolum
 -s \$SUBNET Choose another Subnet(version4), default is 10.243.
 -m \$MASK   Choose another Subnet Mask(version4), default is /16
 -u \$URL    specify another hostsfiles.tar.gz url, default is euer.krebsco.de/retiolum/hosts.tar.gz
EOF
}

#check if ip is valid ipv4 function
check_ip_valid4()
{
    if [ "$(echo $1 | awk -F"\." ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ' 2>/dev/null)" == "$1" ] && [ ${1:0:${#SUBNET4}} == $SUBNET4 ]
    then
        return 0
    else
        return 1
    fi
}

#check if ip is taken function
check_ip_taken()
{
    if grep -q -E "$1(#|/)" $TEMPDIR/hosts/* ;then
        echo $1 is taken
        return 1
    else
        echo $1 seems free
        return 0
    fi
}

#if hostname is taken, count upwards until it isn't taken function
check_hostname()
{
    TSTFILE=$TEMPDIR/hosts/$1
    LCOUNTER=0
    if test -e $TSTFILE; then
        while test -e $TSTFILE; do
            let LCOUNTER=LCOUNTER+1
            TSTFILE=$TEMPDIR/hosts/$1$LCOUNTER
        done
        HOSTN=$1$LCOUNTER
    else
        HOSTN=$1
    fi
}

TEMPDIR=/tmp/tinc-install-fu
HOSTN=$(hostname)
NETNAME=retiolum
SUBNET4=10.243.
MASK4=/16
RAND=1
URL=euer.krebsco.de/retiolum/hosts.tar.gz

#check if everything is installed
if $(! test -e "/usr/sbin/tincd"); then
    echo "Please install tinc"
    exit 1
fi

if $(! test -e /usr/bin/awk); then
    echo "Please install awk"
    exit 1
fi

if $(! test -e /usr/bin/curl); then
    echo "Please install curl"
    exit 1
fi

if $(! /bin/ping -c 1 euer.krebsco.de -W 5 &>/dev/null) ;then
    echo "Cant reach euer, check if your internet is working"
    exit 1
fi


#parse options
while getopts "h4:t:o:n:s:m:u:" OPTION
do
    case $OPTION in
        h)
            usage
            exit 1
            ;;
        4)
            IP4=$OPTARG
            RAND=0
            if ! check_ip_valid4 $IP4; then echo "ip is invalid" && exit 1; fi
            ;;
        t)
            TEMPDIR=$OPTARG
            ;;
        o)
            HOSTN=$OPTARG
            ;;
        n)
            NETNAME=$OPTARG
            ;;
        s)
            SUBNET4=$OPTARG
            ;;
        m)
            MASK4=$OPTARG
            ;;
        u)
            URL=$OPTARG
            if $(! curl -s --head $URL | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null); then
                echo "url not reachable"
                exit 1
            fi
            ;;

    esac
done

#test if tinc directory already exists
if test -e /etc/tinc/$NETNAME; then
    echo "tinc config directory /etc/tinc/$NETNAME does already exist. (backup and) delete config directory and restart"
    exit 1
fi

#get tinc-hostfiles
mkdir -p $TEMPDIR/hosts
curl euer.krebsco.de/retiolum/hosts.tar.gz | tar zx -C $TEMPDIR/hosts/

#check for free ip
until check_ip_taken $IP4; do
    if [ $RAND -eq 1 ]; then
        IP4="10.243.$((RANDOM%255)).$((RANDOM%255))"
    else
        printf 'choose new ip: '
        read IP4
        while !  check_ip_valid4 $IP4; do
            printf 'the ip is invalid, retard, choose a valid ip: '
            read IP4
        done
    fi
done

#check for free hostname
check_hostname $HOSTN

echo "your ip is $IP4"
echo "your hostname is $HOSTN"