1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Ethernet over WebSocket tunneling server/client |
---|
5 | # |
---|
6 | # depends on: |
---|
7 | # - python-2.7.2 |
---|
8 | # - python-pytun-0.2 |
---|
9 | # - websocket-client-0.7.0 |
---|
10 | # - tornado-2.2.1 |
---|
11 | # |
---|
12 | # todo: |
---|
13 | # - SSL support |
---|
14 | # - servant mode (like typical p2p software) |
---|
15 | # |
---|
16 | # =========================================================================== |
---|
17 | # Copyright (c) 2012, Atzm WATANABE <atzm@atzm.org> |
---|
18 | # All rights reserved. |
---|
19 | # |
---|
20 | # Redistribution and use in source and binary forms, with or without |
---|
21 | # modification, are permitted provided that the following conditions are met: |
---|
22 | # |
---|
23 | # 1. Redistributions of source code must retain the above copyright notice, |
---|
24 | # this list of conditions and the following disclaimer. |
---|
25 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
26 | # notice, this list of conditions and the following disclaimer in the |
---|
27 | # documentation and/or other materials provided with the distribution. |
---|
28 | # |
---|
29 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
30 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
31 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
32 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
33 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
34 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
35 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
36 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
37 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
38 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
39 | # POSSIBILITY OF SUCH DAMAGE. |
---|
40 | # =========================================================================== |
---|
41 | # |
---|
42 | # $Id$ |
---|
43 | |
---|
44 | import os |
---|
45 | import sys |
---|
46 | import argparse |
---|
47 | import threading |
---|
48 | |
---|
49 | import pytun |
---|
50 | import websocket |
---|
51 | import tornado.httpserver |
---|
52 | import tornado.ioloop |
---|
53 | import tornado.web |
---|
54 | import tornado.websocket |
---|
55 | |
---|
56 | |
---|
57 | class TapHandler(object): |
---|
58 | def __init__(self, dev, debug=False): |
---|
59 | self._debug = debug |
---|
60 | self._clients = [] |
---|
61 | self._tap = pytun.TunTapDevice(dev, pytun.IFF_TAP | pytun.IFF_NO_PI) |
---|
62 | self._tap.up() |
---|
63 | self._write_lock = threading.Lock() |
---|
64 | |
---|
65 | def fileno(self): |
---|
66 | return self._tap.fileno() |
---|
67 | |
---|
68 | def register_client(self, client): |
---|
69 | self._clients.append(client) |
---|
70 | |
---|
71 | def unregister_client(self, client): |
---|
72 | self._clients.remove(client) |
---|
73 | |
---|
74 | def write(self, caller, message): |
---|
75 | if self._debug: |
---|
76 | sys.stderr.write('%s: %s\n' % (caller.__class__.__name__, |
---|
77 | message.encode('hex'))) |
---|
78 | try: |
---|
79 | self._write_lock.acquire() |
---|
80 | |
---|
81 | clients = self._clients[:] |
---|
82 | |
---|
83 | if caller is not self: |
---|
84 | clients.remove(caller) |
---|
85 | self._tap.write(message) |
---|
86 | |
---|
87 | for c in clients: |
---|
88 | c.write_message(message, True) |
---|
89 | |
---|
90 | finally: |
---|
91 | self._write_lock.release() |
---|
92 | |
---|
93 | def __call__(self, fd, events): |
---|
94 | self.write(self, self._tap.read(self._tap.mtu)) |
---|
95 | |
---|
96 | |
---|
97 | class EtherWebSocket(tornado.websocket.WebSocketHandler): |
---|
98 | def __init__(self, app, req, tap, debug=False): |
---|
99 | super(EtherWebSocket, self).__init__(app, req) |
---|
100 | self._tap = tap |
---|
101 | self._debug = debug |
---|
102 | |
---|
103 | def open(self): |
---|
104 | self._tap.register_client(self) |
---|
105 | |
---|
106 | def on_message(self, message): |
---|
107 | self._tap.write(self, message) |
---|
108 | |
---|
109 | def on_close(self): |
---|
110 | self._tap.unregister_client(self) |
---|
111 | |
---|
112 | |
---|
113 | def daemonize(nochdir=False, noclose=False): |
---|
114 | if os.fork() > 0: |
---|
115 | sys.exit(0) |
---|
116 | |
---|
117 | os.setsid() |
---|
118 | |
---|
119 | if os.fork() > 0: |
---|
120 | sys.exit(0) |
---|
121 | |
---|
122 | if not nochdir: |
---|
123 | os.chdir('/') |
---|
124 | |
---|
125 | if not noclose: |
---|
126 | os.umask(0) |
---|
127 | sys.stdin.close() |
---|
128 | sys.stdout.close() |
---|
129 | sys.stderr.close() |
---|
130 | os.close(0) |
---|
131 | os.close(1) |
---|
132 | os.close(2) |
---|
133 | sys.stdin = open(os.devnull) |
---|
134 | sys.stdout = open(os.devnull, 'a') |
---|
135 | sys.stderr = open(os.devnull, 'a') |
---|
136 | |
---|
137 | |
---|
138 | def server_main(args): |
---|
139 | tap = TapHandler(args.device, debug=args.debug) |
---|
140 | app = tornado.web.Application([ |
---|
141 | (args.path, EtherWebSocket, {'tap': tap, 'debug': args.debug}), |
---|
142 | ]) |
---|
143 | server = tornado.httpserver.HTTPServer(app) |
---|
144 | server.listen(args.port, address=args.address) |
---|
145 | |
---|
146 | ioloop = tornado.ioloop.IOLoop.instance() |
---|
147 | ioloop.add_handler(tap.fileno(), tap, ioloop.READ) |
---|
148 | ioloop.start() |
---|
149 | |
---|
150 | |
---|
151 | def client_main(args): |
---|
152 | if args.debug: |
---|
153 | websocket.enableTrace(True) |
---|
154 | |
---|
155 | tap = TapHandler(args.device, debug=args.debug) |
---|
156 | client = websocket.WebSocketApp(args.uri) |
---|
157 | client.on_message = lambda s, m: tap.write(client, m) |
---|
158 | client.write_message = \ |
---|
159 | lambda m, b: client.sock.send(m, websocket.ABNF.OPCODE_BINARY) |
---|
160 | tap.register_client(client) |
---|
161 | |
---|
162 | t = threading.Thread(target=client.run_forever) |
---|
163 | t.setDaemon(True) |
---|
164 | t.start() |
---|
165 | |
---|
166 | ioloop = tornado.ioloop.IOLoop.instance() |
---|
167 | ioloop.add_handler(tap.fileno(), tap, ioloop.READ) |
---|
168 | ioloop.start() |
---|
169 | |
---|
170 | |
---|
171 | def main(): |
---|
172 | parser = argparse.ArgumentParser() |
---|
173 | parser.add_argument('--device', action='store', default='ethws%d') |
---|
174 | parser.add_argument('--foreground', action='store_true', default=False) |
---|
175 | parser.add_argument('--debug', action='store_true', default=False) |
---|
176 | |
---|
177 | subparsers = parser.add_subparsers(dest='subcommand') |
---|
178 | |
---|
179 | parser_server = subparsers.add_parser('server') |
---|
180 | parser_server.add_argument('--address', action='store', default='') |
---|
181 | parser_server.add_argument('--port', action='store', type=int, default=80) |
---|
182 | parser_server.add_argument('--path', action='store', default='/') |
---|
183 | |
---|
184 | parser_client = subparsers.add_parser('client') |
---|
185 | parser_client.add_argument('--uri', action='store', required=True) |
---|
186 | |
---|
187 | args = parser.parse_args() |
---|
188 | |
---|
189 | if not args.foreground: |
---|
190 | daemonize() |
---|
191 | |
---|
192 | if args.subcommand == 'server': |
---|
193 | server_main(args) |
---|
194 | elif args.subcommand == 'client': |
---|
195 | client_main(args) |
---|
196 | |
---|
197 | |
---|
198 | if __name__ == '__main__': |
---|
199 | main() |
---|