Skip to content

Commit 482ed94

Browse files
committed
Import the source
1 parent 88111d7 commit 482ed94

16 files changed

+1772
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.o
2+
Makefile.depend
3+
udptunnel

COPYING

Lines changed: 339 additions & 0 deletions
Large diffs are not rendered by default.

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
prefix = /usr/local
2+
3+
CFLAGS ?= -g -O2
4+
5+
INSTALL ?= install
6+
PKG_CONFIG ?= pkg-config
7+
8+
ifeq ($(shell $(PKG_CONFIG) --exists libsystemd || echo NO),)
9+
DEFS += -DHAVE_SYSTEMD_SD_DAEMON_H $(shell $(PKG_CONFIG) --cflags libsystemd)
10+
LDADD += $(shell $(PKG_CONFIG) --libs libsystemd)
11+
endif
12+
13+
CPPFLAGS += $(DEFS) $(INCLUDES)
14+
15+
OBJECTS := log.o network.o utils.o udptunnel.o
16+
17+
all: depend udptunnel
18+
19+
install:
20+
$(INSTALL) -d $(BASEDIR)$(prefix)/sbin/
21+
$(INSTALL) -m 0755 udptunnel $(BASEDIR)$(prefix)/sbin/
22+
23+
clean:
24+
rm -f Makefile.depend $(OBJECTS) udptunnel
25+
26+
%.o: %.c
27+
$(CC) $(CPPFLAGS) $(CFLAGS) -c $<
28+
29+
udptunnel: $(OBJECTS)
30+
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDADD) $(LIBS)
31+
32+
depend: Makefile.depend
33+
Makefile.depend:
34+
$(CC) $(CPPFLAGS) $(CFLAGS) -MM -MG *.c > $@
35+
36+
-include Makefile.depend

examples/test-inetd.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Start a test inetd daemon with a command like:
2+
# /usr/sbin/inetd -i examples/test-inetd.conf
3+
#
4+
# and then try some DNS queries:
5+
# dig @127.0.0.1 -p 5335 www.debian.org
6+
# dig @127.0.0.1 -p 5336 www.debian.org
7+
8+
# test server
9+
55443 stream tcp nowait nobody /usr/sbin/tcpd /usr/local/sbin/udptunnel --inetd --timeout 60 --server 8.8.8.8:53
10+
11+
# test clients
12+
5335 dgram udp wait.2 nobody /usr/sbin/tcpd /usr/local/sbin/udptunnel --inetd --timeout 60 127.0.0.1:55443
13+
5336 dgram udp wait.2 nobody /usr/sbin/tcpd /usr/local/sbin/udptunnel --inetd --timeout 60 127.0.0.1:55443
14+

examples/toggle-endpoint

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/sh -e
2+
#
3+
# This script switches a wireguard tunnel between two endpoints.
4+
5+
WG_IF='wg0'
6+
WG_PEER_KEY='Jy9syevJmxfBLKQMTazSFvgDeyS2Pv3+laYIDMlquRk='
7+
8+
ENDPOINT_A='198.51.100.1:443'
9+
ENDPOINT_B='127.0.0.1:25779'
10+
11+
##############################################################################
12+
wg_get_endpoint() {
13+
local if="$1"
14+
local key="$2"
15+
16+
local key_re
17+
if [ "$key" ]; then
18+
key_re="^$(echo $key | sed -re 's/\+/\\+/g')[[:space:]]"
19+
else
20+
key_re='.'
21+
fi
22+
23+
[ "$if" -a -e /sys/class/net/$if/ ] || return 0
24+
wg show "$if" endpoints | egrep "$key_re" | cut -f 2
25+
}
26+
27+
wg_set_endpoint() {
28+
local if="$1"
29+
local peer_key="$2"
30+
31+
[ "$if" -a -e /sys/class/net/$if/ ] || return 0
32+
wg set "$if" peer "$peer_key" endpoint "$3"
33+
}
34+
35+
##############################################################################
36+
WG_ENDPOINT="$(wg_get_endpoint $WG_IF $WG_PEER_KEY)"
37+
38+
case "$WG_ENDPOINT" in
39+
"")
40+
;;
41+
"$ENDPOINT_A")
42+
wg_set_endpoint $WG_IF $WG_PEER_KEY $ENDPOINT_B
43+
echo "Switched to endpoint $ENDPOINT_B."
44+
;;
45+
"$ENDPOINT_B")
46+
wg_set_endpoint $WG_IF $WG_PEER_KEY $ENDPOINT_A
47+
echo "Switched to endpoint $ENDPOINT_A."
48+
;;
49+
*)
50+
echo "Unknown endpoint $WG_ENDPOINT!" >&2
51+
exit 1
52+
;;
53+
esac
54+

examples/udptunnel-client.service

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[Unit]
2+
Description=udptunnel client
3+
Documentation=man:udptunnel(1)
4+
Requires=udptunnel-client.socket
5+
6+
[Service]
7+
Type=notify
8+
Restart=on-failure
9+
RestartSec=30
10+
ExecStart=/usr/local/sbin/udptunnel server.example.net:443
11+
StandardOutput=journal
12+
StandardError=journal
13+
DynamicUser=yes
14+
NoNewPrivileges=yes
15+
PrivateTmp=yes
16+
PrivateDevices=yes
17+
ProtectSystem=strict
18+
ProtectHome=yes
19+
ProtectKernelTunables=yes
20+
ProtectKernelModules=yes
21+
ProtectControlGroups=yes
22+
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
23+
RestrictNamespaces=yes
24+
LockPersonality=yes
25+
MemoryDenyWriteExecute=yes
26+
RestrictRealtime=yes
27+
RemoveIPC=yes
28+
SystemCallArchitectures=native
29+

examples/udptunnel-client.socket

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[Unit]
2+
Description=Sockets for udptunnel-client
3+
Documentation=man:udptunnel(1)
4+
5+
[Socket]
6+
ListenDatagram=127.0.0.1:25779
7+
8+
[Install]
9+
WantedBy=sockets.target
10+

examples/udptunnel-server.service

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[Unit]
2+
Description=udptunnel server
3+
Documentation=man:udptunnel(1)
4+
Requires=udptunnel-server.socket
5+
6+
[Service]
7+
Type=notify
8+
Restart=on-failure
9+
ExecStart=/usr/local/sbin/udptunnel --server -v 127.0.0.1:25779
10+
StandardOutput=journal
11+
StandardError=journal
12+
DynamicUser=yes
13+
NoNewPrivileges=yes
14+
PrivateTmp=yes
15+
PrivateDevices=yes
16+
ProtectSystem=strict
17+
ProtectHome=yes
18+
ProtectKernelTunables=yes
19+
ProtectKernelModules=yes
20+
ProtectControlGroups=yes
21+
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
22+
RestrictNamespaces=yes
23+
LockPersonality=yes
24+
MemoryDenyWriteExecute=yes
25+
RestrictRealtime=yes
26+
RemoveIPC=yes
27+
SystemCallArchitectures=native
28+

examples/udptunnel-server.socket

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Sockets for udptunnel-server
3+
Documentation=man:udptunnel(1)
4+
5+
[Socket]
6+
ListenStream=0.0.0.0:443
7+
ListenStream=[::]:443
8+
9+
[Install]
10+
WantedBy=sockets.target
11+

log.c

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (C) 2018 Marco d'Itri
3+
*
4+
* Inspired by log.c from the cowdancer package by James Clarke.
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19+
*/
20+
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <unistd.h>
24+
#include <string.h>
25+
#include <errno.h>
26+
#include <syslog.h>
27+
28+
#include "log.h"
29+
#include "utils.h"
30+
31+
static log_level filter_level = log_info;
32+
33+
/*
34+
* Return the appropriate file handle (stdout vs. stderr) for the log level.
35+
*/
36+
static FILE *file_for_level(log_level level)
37+
{
38+
if (level & log_stderr || filter_level & log_stderr)
39+
return stderr;
40+
41+
if ((level & LOG_LEVEL_MASK) > log_warning)
42+
return stdout;
43+
else
44+
return stderr;
45+
}
46+
47+
static void log_doit(log_level level, const char *format, va_list args)
48+
{
49+
static int syslog_initialized;
50+
51+
if ((level & LOG_LEVEL_MASK) > (filter_level & LOG_LEVEL_MASK))
52+
return;
53+
54+
if (level & log_syslog || filter_level & log_syslog) {
55+
if (!syslog_initialized) {
56+
openlog(NULL, LOG_PID, LOG_DAEMON);
57+
syslog_initialized = 1;
58+
}
59+
60+
if (level & log_strerror) {
61+
int len = strlen(format);
62+
char *format2;
63+
64+
format2 = NOFAIL(malloc(len + 4 + 1));
65+
strcpy(format2, format);
66+
strcpy(format2 + len, ": %m");
67+
vsyslog(level & LOG_LEVEL_MASK, format2, args);
68+
free(format2);
69+
} else {
70+
vsyslog(level & LOG_LEVEL_MASK, format, args);
71+
}
72+
return;
73+
}
74+
75+
vfprintf(file_for_level(level), format, args);
76+
if (level & log_strerror)
77+
fprintf(file_for_level(level), ": %s", strerror(errno));
78+
fprintf(file_for_level(level), "\n");
79+
}
80+
81+
log_level log_get_filter_level(void)
82+
{
83+
return filter_level;
84+
}
85+
86+
void log_set_options(log_level filter_level_new)
87+
{
88+
filter_level = filter_level_new;
89+
}
90+
91+
void log_printf(log_level level, const char *format, ...)
92+
{
93+
va_list args;
94+
95+
va_start(args, format);
96+
log_doit(level, format, args);
97+
va_end(args);
98+
}
99+
100+
void log_printf_exit(int status, log_level level, const char *format, ...)
101+
{
102+
va_list args;
103+
104+
va_start(args, format);
105+
log_doit(level, format, args);
106+
va_end(args);
107+
108+
exit(status);
109+
}
110+
111+
void log_printf_err(log_level level, const char *format, ...)
112+
{
113+
va_list args;
114+
115+
va_start(args, format);
116+
log_doit(level | log_strerror, format, args);
117+
va_end(args);
118+
}
119+
120+
void log_printf_err_exit(int status, log_level level, const char *format, ...)
121+
{
122+
va_list args;
123+
124+
va_start(args, format);
125+
log_doit(level | log_strerror, format, args);
126+
va_end(args);
127+
128+
exit(status);
129+
}
130+

0 commit comments

Comments
 (0)