-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtunproxy.py
More file actions
104 lines (87 loc) · 3.3 KB
/
tunproxy.py
File metadata and controls
104 lines (87 loc) · 3.3 KB
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
#! /usr/bin/env python
#http://www.secdev.org/projects/tuntap_udp/files/tunproxy.py
#############################################################################
## ##
## tunproxy.py --- small demo program for tunneling over UDP with tun/tap ##
## ##
## Copyright (C) 2003 Philippe Biondi <phil@secdev.org> ##
## ##
## This program is free software; you can redistribute it and/or modify it ##
## under the terms of the GNU General Public License as published by the ##
## Free Software Foundation; either version 2, or (at your option) any ##
## later version. ##
## ##
## This program is distributed in the hope that it will be useful, but ##
## WITHOUT ANY WARRANTY; without even the implied warranty of ##
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ##
## General Public License for more details. ##
## ##
#############################################################################
import os, sys
from socket import *
from fcntl import ioctl
from select import select
import getopt, struct
MAGIC_WORD = "Wazaaaaaaaaaaahhhh !"
TUNSETIFF = 0x400454ca
IFF_TUN = 0x0001
IFF_TAP = 0x0002
TUNMODE = IFF_TUN
MODE = 0
DEBUG = 0
def usage(status=0):
print "Usage: tunproxy [-s port|-c targetip:port] [-e]"
sys.exit(status)
opts = getopt.getopt(sys.argv[1:],"s:c:ehd")
for opt,optarg in opts[0]:
if opt == "-h":
usage()
elif opt == "-d":
DEBUG += 1
elif opt == "-s":
MODE = 1
PORT = int(optarg)
elif opt == "-c":
MODE = 2
IP,PORT = optarg.split(":")
PORT = int(PORT)
peer = (IP,PORT)
elif opt == "-e":
TUNMODE = IFF_TAP
if MODE == 0:
usage(1)
f = os.open("/dev/net/tun", os.O_RDWR)
ifs = ioctl(f, TUNSETIFF, struct.pack("16sH", "toto%d", TUNMODE))
ifname = ifs[:16].strip("\x00")
print "Allocated interface %s. Configure it and use it" % ifname
s = socket(AF_INET, SOCK_DGRAM)
try:
if MODE == 1:
s.bind(("", PORT))
while 1:
word,peer = s.recvfrom(1500)
if word == MAGIC_WORD:
break
print "Bad magic word for %s:%i" % peer
s.sendto(MAGIC_WORD, peer)
else:
s.sendto(MAGIC_WORD, peer)
word,peer = s.recvfrom(1500)
if word != MAGIC_WORD:
print "Bad magic word for %s:%i" % peer
sys.exit(2)
print "Connection with %s:%i established" % peer
while 1:
r = select([f,s],[],[])[0][0]
if r == f:
if DEBUG: os.write(1,">")
s.sendto(os.read(f,1500),peer)
else:
buf,p = s.recvfrom(1500)
if p != peer:
print "Got packet from %s:%i instead of %s:%i" % (p+peer)
continue
if DEBUG: os.write(1,"<")
os.write(f, buf)
except KeyboardInterrupt:
print "Stopped by user."