Projekt

Allgemein

Profil

Setup n2n-supernode » Historie » Version 2

Jeremias Keihsler, 12.01.2017 09:59

1 1 Jeremias Keihsler
h1. Install n2n-Supernode
2
3
h2. Requirements
4
5
To install n2n you will need the following:
6
* a installed and supported operating system (e.g. CentOS 6.x)
7
* root-access
8
* EPEL repository
9
* a fast internet connection
10
11
h2. Preliminary note
12
13
most of this is taken from 
14
* [[http://www.ntop.org/products/n2n/]]
15
* [[https://github.com/lukablurr/n2n_v2_fork/blob/master/doc/multiple-supernodes.pdf]]
16
* [[http://listgateway.unipi.it/mailman/listinfo/n2n]]
17
* [[http://listgateway.unipi.it/pipermail/n2n/2009-October/000300.html]]
18
19
h2. Install
20
21
<pre><code class="bash">
22
yum install n2n
23
</code></pre>
24
25
h2. manual testing
26
27
h3. starting Supernode
28
29
<pre><code class="bash">
30
supernode -l 5422
31
</code></pre>
32
33
h3. setting up firewall
34
35
in order to access the supernode from one of the edge-clients the given UDP-port must be accessible from outside. Therefore we need to open the firewall.
36
<pre><code class="bash">
37
system-config-firewall-tui
38
</code></pre>
39
40
h2. running n2n-Supernode as a service
41
42
if everything is working as expected we maybe want n2n-Supernode to survive a system-reboot.
43
44
The following should work without modification for RHEL/CentOS linux and similar:
45
46
Create a folder /etc/n2n and create configuration files (as described below) using following file name format:
47
48
<pre><code class="bash">
49
mkdir /etc/n2n
50
</code></pre>
51
52
<pre><code class="bash">
53
supernode-gw1.example.com
54
supernode-gw2.example.com
55
supernode-*.example.com
56
</code></pre>
57
58
The interesting contents of supernode-gw1.example.com would look similar to this:
59
60
<pre><code class="bash">
61
vim /etc/n2n/supernode-gw1.example.com
62
</code></pre>
63
<pre>
64
# == supernode config values parsed by /etc/rc.d/init.d/edged ==
65
# Read 'man supernode' for details about these values...
66
#
67
# Example port number to listen on...
68
N2N_PORT="8765"
69
70
# Verbose or not (-v logs to std out)...
71
#   N2N_OPTS="-v"
72
N2N_OPTIONS=""
73
# === end supernode-gw1.example.com config file
74
==========
75
</pre>
76
77
Read comments in the scripts below to make more sense out of how they work.
78
79
Here are copies of some scripts that work when used with configuration files as described above:
80
81
<pre><code class="bash">
82
vim /etc/init.d/supernoded
83
</code></pre>
84
<pre>
85
#!/bin/sh
86
# $Id: supernoded.init,v 1.9 2009/10/23 12:34:56
87
# n2n supernode startup script.
88
#
89
# chkconfig:    345 96 26
90
# processname:  supernode
91
# pidfile:      /var/run/n2n-supernode.pid
92
#
93
# short-description: Provides client-to-client VPN access via Internet
94
# description: This is startup script for n2n 'edge',
95
#   n2n edge + supernode provide internet connection for vpn peers
96
#
97
# SEE:
98
#   for more options n2n 'man edge' and 'man supernode'
99
#
100
# NOTE:
101
#   Option to enter 'debug' as second var: e.g 'supernode start debug'
102
#   Using grep -w switch searches for whole word reduces ambiguity
103
#   Comment in/out the 'echo' stuff depending if thigs work as expected
104
#   Config files located/named in: /etc/n2n/supernode-your.example.com
105
#   Assumes CentOS linux init scripts located in /etc/rc.d/init.d/
106
#
107
# Created: FOOFORCE RB 2009-10-24
108
# License GPL: free to copy, improve and use.
109
# http://fuji.unipi.it/pipermail/n2n/2009-October/000300.html
110 2 Jeremias Keihsler
# ===============================================================
111 1 Jeremias Keihsler
112 2 Jeremias Keihsler
# ==== main init script variables ===============================
113 1 Jeremias Keihsler
# Make sure you include path where 'supernode' + sh + tools are installed..
114
# PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin"
115
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
116
117
# The name + path to the program/daemon...
118
NAME="supernode"
119
DAEMON="/usr/sbin/${NAME}"
120
121
# Modify $NAME to filter on 'grep -w [s]supernode' - faster than 'grep -v grep'...
122
GREPNAME=$(echo "[s]${NAME#s}")
123
124
# Enable some simple debug messages when running this script...
125
if [[ "${2}" == "debug" ]]; then
126
    echo "DEBUG: ${GREPNAME}"
127
fi
128
129
PIDFILE="/var/run/supernode.pid"
130
LOGFILE="/var/log/n2n-${NAME}.log"
131
STAMP=$(/bin/date +"%d/%b/%Y %H:%M:%S")
132
133
# Get the host/domain name to identify local configfile..
134
HOST=$(/bin/hostname)
135
FQDN=$(/bin/hostname -f)
136
137
# Initialise configfile variable to empty value...
138
CONFIGDIR="/etc/n2n"
139
140
# Initialise configfile variable to empty value...
141
CONFIGFILE=""
142
143
# Source function library.
144
. /etc/rc.d/init.d/functions
145
146
# Load values from configfile using our local domain name:
147
# Example:
148
#   Our local FQDN = 'myhost.example.com'
149
#   Our configfile = 'supernode-myhost.example.com'
150
#
151
# if [[ "${HOST}" == "" ]]; then
152
if [[ "${FQDN}" == "" ]]; then
153
    echo " ERROR: Missing ${FQDN} hostname in config file /etc/rc.d/init.d/${NAME}"
154
    exit 1
155
else
156
    CONFIGFILE="/etc/n2n/supernode-${FQDN}"
157
fi
158
159
if [[ -f "${CONFIGFILE}" ]]; then
160
    # Get values from local configuration file...
161
    if [[ "${2}" ==  "debug" ]]; then
162
        echo " Found config file: ${CONFIGFILE}"
163
    fi
164
    . ${CONFIGFILE}
165
else
166
    echo "ERROR: Missing config file ${CONFIGFILE}"
167
    exit 1
168
fi
169
170
# See if pid file exists and get pid number...
171
if [[ -f "${PIDFILE}" ]]; then
172
    CURPID=$(cat ${PIDFILE})
173
fi
174
175
# Quit if we can't find application...
176
test -f ${DAEMON} || exit 0
177
178
set -e
179
180
# Set any value to enable this script...
181
# N2N_RUN=1
182
#
183
# test -n "${N2N_RUN}" || exit 0
184
185
# Optionaly change into n2n config directory...
186
if [[ -d "${CONFIGDIR}" ]]; then
187
    cd ${CONFIGDIR}
188
fi
189 2 Jeremias Keihsler
# ==============================================================
190 1 Jeremias Keihsler
# Should not need to change much below here...
191 2 Jeremias Keihsler
# ==== see how we were called  + act accordingly ===============
192 1 Jeremias Keihsler
193
if [ "${N2N_PORT}" == "" ]; then
194
    N2N_PORT="1304"
195
    echo "No port specified in ${CONFIGFILE} - using  default port: ${N2N_PORT}"
196
fi
197
198
case "${1}" in
199
    start)
200
        # echo " ${NAME} ${1} process is ${0} on pid: ${$})..."
201
        echo "${STAMP} start n2n /etc/rc.d/init.d/${NAME}" >> ${LOGFILE}
202
203
        if [[ -n "${CURPID}" ]]; then
204
            echo " A PID:${CURPID} already exists for ${0}:"
205
            echo " To re-start with current config settings, run '$0 stop' then '$0 start' ..."
206
        else
207
        if [ -d /var/lock/subsys ] ; then
208
            touch /var/lock/subsys/${NAME};
209
        fi
210
211 2 Jeremias Keihsler
            # ====================================================
212 1 Jeremias Keihsler
            # All variables read from ${CONFIGFILE}...
213
            # If variable value not empty then add command-line switch to variable...
214
            if [[ -n "${N2N_PORT}" ]]; then
215
                N2N_PORT="-l ${N2N_PORT}";
216
            fi
217
218
            # next line for debug:
219
            if [[ "${2}" == "debug" ]]; then
220
                echo " DEBUG: ${DAEMON} ${N2N_PORT} ${N2N_OPTIONS}"
221
            fi
222
223
            # Execute the command + switches + values sources from config file...
224
            echo -n $"Starting $NAME: "
225
            ${DAEMON} ${N2N_PORT} ${N2N_OPTIONS} >> ${LOGFILE} &
226
            RETVAL=$?
227
228
            [ $RETVAL -eq 0 ] && echo_success
229
            [ $RETVAL -ne 0 ] && echo_failure
230
            echo
231
232 2 Jeremias Keihsler
            # =================================================
233 1 Jeremias Keihsler
            if [[ "${2}" ==  "debug" ]]; then
234
                echo " NOTICE: Creating new ${NAME} pidfile - deleting old ${CURPID}..."
235
            fi
236
237
            rm -f ${PIDFILE}
238
            touch ${PIDFILE}
239
240
            for PID in $(ps ax | grep -w "${GREPNAME}" | grep -v "(restart|stop|start)" | awk '{ print $1 }'); do
241
                if [[ "${2}" ==  "debug" ]]; then
242
                    echo " Saving into pidfile - new pid: ${PID}"
243
                fi
244
                echo ${PID} >> ${PIDFILE}
245
            done
246
        fi
247
        ;;
248
    stop)
249
        # echo " DEBUG: Searching for at least one live process id..."
250
        PID=$(ps ax | grep -w "${GREPNAME}" | grep -v "(restart|stop|start)" | awk '{ print $1 }')
251
252
        if [[ "${2}" == "debug" ]]; then
253
            echo " This ${NAME} ${1} init query ${0} is running on pid: ${$}) for ${PID}..."
254
        fi
255
256
        # echo " ${NAME} ${1} process is ${0} on pid: ${$})..."
257
        echo "${STAMP} stop /etc/rc.d/init.d/${NAME}" >> ${LOGFILE}
258
259
        # echo "  NOTICE: Kill name:${NAME} pid:${pid} before stopping..."
260
        # kill -9 $(cat /var/run/${NAME}_pid) && rm /var/run/${NAME}_pid
261
262
        # If a pid value found in file or in process id, then kill it...
263
        if [[ -n "${PID}" ]]; then
264
            echo -n  $"Stopping ${NAME}: "
265
            # kill -9 $(cat /var/run/${NAME}.pid) && rm /var/run/${NAME}.pid
266
            kill ${PID}
267
            RETVAL=$?
268
            sleep 4s
269
            rm -f ${PIDFILE}
270
            if [ $RETVAL -eq 0 ] ; then
271
                echo_success
272
                echo
273
            else
274
                echo_failure
275
                echo
276
            fi
277
        else
278
            # Else assume stale pid...
279
            if [[ "${2}" == "debug" ]]; then
280
                echo " NOTICE: No current active process ${PID}..."
281
            fi
282
283
            if [[ -f "${PIDFILE}" ]]; then
284
                rm -f "${PIDFILE}"
285
            fi
286
        fi
287
288
        # Indiscriminate pid killer...
289
        # for PID in $(ps ax | grep -w ${NAME} | grep -v stop | awk '{ print $1 }'); do
290
        #    echo " Killing currently active ${NAME} pid(s): ${PID}"
291
        #    # kill -HUP $PID
292
        #    kill ${PID}
293
        # done
294
295
        if [ -f /var/lock/subsys/${NAME} ]; then
296
            if [[ "${2}" ==  "debug" ]]; then
297
                echo " Removing stale lock..."
298
            fi
299
            rm -f /var/lock/subsys/${NAME}
300
        fi
301
302
        if [ -f "${PIDFILE}" ]; then
303
            echo " Removing stale pidfile..."
304
            rm -f ${PIDFILE}
305
        fi
306
        ;;
307
    restart)
308
        echo "* Re-start ${NAME}..."
309
        ${0} stop ${2}
310
311
        # Not a race...
312
        sleep 4s
313
314
        ${0} start ${2}
315
        ;;
316
    status)
317
        # status ${NAME}
318
319
        if [[ "${2}" == "debug" ]]; then
320
            echo " This init script ${NAME} ${1} query ${0} is running on pid: ${$})..."
321
        fi
322
323
        PID=$(ps ax | grep -w "${GREPNAME}" | grep -v status | awk '{ print $1 }')
324
325
        # If at least one active session then find all...
326
        if [[ -n "${PID}" ]]; then
327
            for PID in $(ps ax | grep -w "${GREPNAME}" | grep -v status | awk '{ print $1 }'); do
328
                if [[ -n "${PID}" ]]; then
329
                    echo "${NAME} (pid ${PID}) is running..."
330
                else
331
                    echo "${NAME} is stopped"
332
                fi
333
            done
334
        else
335
            echo "${NAME} is stopped"
336
        fi
337
        ;;
338
    *)
339
        echo " Usage: ${0} {start|stop|status|restart|any debug}" >&2
340
        exit 1
341
        ;;
342
esac
343
exit 0
344 2 Jeremias Keihsler
# === end supernoded ======================================
345 1 Jeremias Keihsler
</pre>
346
347
make init-script executeable
348
<pre><code class="bash">
349
chmod +x /etc/init.d/supernoded
350
</code></pre>
351
352
Using CentOS tools to activate the systemV init scripts:
353
<pre><code class="bash">
354
chkconfig --add supernoded
355
chkconfig supernoded on
356
</code></pre>
357
358
Command line syntax:
359
<pre><code class="bash">
360
service supernoded [start|stop|restart|any debug]
361
</code></pre>
362
For example:
363
<pre><code class="bash">
364
service supernoded restart
365
</code></pre>
366
… OR…
367
<pre><code class="bash">
368
service supernoded restart debug
369
</code></pre>
370
provides some extra clues.