-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgns3_proxy.py
executable file
·1406 lines (1199 loc) · 60.2 KB
/
gns3_proxy.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
gns3_proxy
GNS3 Proxy Server in Python.
based on proxy.py - HTTP Proxy Server in Python - copyright: (c) 2013-2018 by Abhinav Singh
:copyright: (c) 2020 by Sebastian Rieger.
:license: BSD, see LICENSE for more details.
"""
# TODO: modification of requests/responses on-the-fly, e.g., to change advertised GNS3 server version? (faking GNS3
# client or server version seems risky as API is not necessarily backward compatible, e.g., massive changes from
# 2.1 to 2.2)
# TODO: add logging/auditing, monitoring of load etc. (current open connections)
# TODO: integrate proxy.py updates, e.g., multi processing?
# TODO: code refactoring/removing unused proxy.py code
# TODO: evaluate reimplementing gns3_proxy as a plugin for proxy.py instead of stand-alone
# TODO: show proxy errors (e.g., console_host misconfiguration) explicitly in GNS3 GUI - if possible?
# TODO: web interface for manual replication and start/stop
# TODO: reservation system? Allowing certain users to access server backends or projects in specific timeframes
# TODO: exam environment? Lock down access to certain projects and export current state?
# TODO: LDAP integration for user login? Would require SSL connection to server which was dropped for GNS3 2.x, if
# we tackle this, also authentication for console access could be addressed (e.g., using websockify instead of
# telnet, using Auth or Token as in VIRL/CML-P)
# TODO: Remove duplicate code fragments, esp. in gns3_proxy tools
import argparse
import os
import sys
import base64
import socket
import select
import logging
import datetime
import threading
from collections import namedtuple
import configparser
import re
import time
from ipaddress import ip_address,ip_network
import json
if os.name != 'nt':
import resource
# Default arguments
DEFAULT_CONFIG_FILE = 'gns3_proxy_config.ini'
DEFAULT_LOG_LEVEL = 'INFO'
VERSION = (0, 9)
__version__ = '.'.join(map(str, VERSION[0:2]))
__description__ = 'GNS3 Proxy based on proxy.py by Abhinav Singh (https://github.com/abhinavsingh/proxy.py)'
__author__ = 'Sebastian Rieger'
__author_email__ = 'sebastian.rieger@informatik.hs-fulda.de'
__homepage__ = 'https://github.com/srieger1/gns3-proxy'
__download_url__ = '%s/archive/develop.zip' % __homepage__
__license__ = 'BSD'
# __version__ = '.'.join(map(str, VERSION[0:2]))
# __description__ = 'Lightweight HTTP, HTTPS, WebSockets Proxy Server in a single Python file'
# __author__ = 'Abhinav Singh'
# __author_email__ = 'mailsforabhinav@gmail.com'
# __homepage__ = 'https://github.com/abhinavsingh/proxy.py'
# __download_url__ = '%s/archive/master.zip' % __homepage__
# __license__ = 'BSD'
logger = logging.getLogger(__name__)
PY3 = sys.version_info[0] == 3
if PY3: # pragma: no cover
text_type = str
binary_type = bytes
from urllib import parse as urlparse
# else: # pragma: no cover
# text_type = unicode
# binary_type = str
# import urlparse
def text_(s, encoding='utf-8', errors='strict'): # pragma: no cover
"""Utility to ensure text-like usability.
If ``s`` is an instance of ``binary_type``, return
``s.decode(encoding, errors)``, otherwise return ``s``"""
if isinstance(s, binary_type):
return s.decode(encoding, errors)
return s
def bytes_(s, encoding='utf-8', errors='strict'): # pragma: no cover
"""Utility to ensure binary-like usability.
If ``s`` is an instance of ``text_type``, return
``s.encode(encoding, errors)``, otherwise return ``s``"""
if isinstance(s, text_type):
return s.encode(encoding, errors)
return s
version = bytes_(__version__)
CRLF, COLON, SP = b'\r\n', b':', b' '
# PROXY_AGENT_HEADER = b'Proxy-agent: proxy.py v' + version
PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT = CRLF.join([
b'HTTP/1.1 200 Connection established',
# PROXY_AGENT_HEADER,
CRLF
])
BAD_GATEWAY_RESPONSE_PKT = CRLF.join([
b'HTTP/1.1 502 Bad Gateway',
# PROXY_AGENT_HEADER,
b'Content-Length: 11',
b'Connection: close',
CRLF
]) + b'Bad Gateway'
# PROXY_AUTHENTICATION_REQUIRED_RESPONSE_PKT = CRLF.join([
# b'HTTP/1.1 407 Proxy Authentication Required',
# # PROXY_AGENT_HEADER,
# b'Content-Length: 29',
# b'Connection: close',
# CRLF
# ]) + b'Proxy Authentication Required'
# GNS3 Unauthorized Example HTTP Response:
# HTTP/1.1 401 Unauthorized
# X-Route: /v2/version
# Connection: close
# Server: Python/3.4 GNS3/2.1.11
# WWW-Authenticate: Basic realm="GNS3 server"
# Content-Length: 0
# Content-Type: application/octet-stream
# Date: Fri, 15 Mar 2019 20:58:59 GMT
# HSFD
PROXY_AUTHENTICATION_REQUIRED_RESPONSE_PKT = CRLF.join([
b'HTTP/1.1 401 Unauthorized',
b'X-Route: /v2/version',
b'Connection: close',
b'Server: Python/3.4 GNS3/2.1.11',
b'WWW-Authenticate: Basic realm="GNS3 server"',
b'Content-Length: 0',
b'Content-Type: application/octet-stream',
# TODO: dynamic date
b'Date: Fri, 15 Mar 2019 20:58:59 GMT',
CRLF
])
class ChunkParser(object):
"""HTTP chunked encoding response parser."""
states = namedtuple('ChunkParserStates', (
'WAITING_FOR_SIZE',
'WAITING_FOR_DATA',
'COMPLETE'
))(1, 2, 3)
def __init__(self):
self.state = ChunkParser.states.WAITING_FOR_SIZE
self.body = b'' # Parsed chunks
self.chunk = b'' # Partial chunk received
self.size = None # Expected size of next following chunk
def parse(self, data):
more = True if len(data) > 0 else False
while more:
more, data = self.process(data)
def process(self, data):
if self.state == ChunkParser.states.WAITING_FOR_SIZE:
# Consume prior chunk in buffer
# in case chunk size without CRLF was received
data = self.chunk + data
self.chunk = b''
# Extract following chunk data size
line, data = HttpParser.split(data)
if not line: # CRLF not received
self.chunk = data
data = b''
else:
self.size = int(line, 16)
self.state = ChunkParser.states.WAITING_FOR_DATA
elif self.state == ChunkParser.states.WAITING_FOR_DATA:
remaining = self.size - len(self.chunk)
self.chunk += data[:remaining]
data = data[remaining:]
if len(self.chunk) == self.size:
data = data[len(CRLF):]
self.body += self.chunk
if self.size == 0:
self.state = ChunkParser.states.COMPLETE
else:
self.state = ChunkParser.states.WAITING_FOR_SIZE
self.chunk = b''
self.size = None
return len(data) > 0, data
class HttpParser(object):
"""HTTP request/response parser."""
states = namedtuple('HttpParserStates', (
'INITIALIZED',
'LINE_RCVD',
'RCVING_HEADERS',
'HEADERS_COMPLETE',
'RCVING_BODY',
'COMPLETE'))(1, 2, 3, 4, 5, 6)
types = namedtuple('HttpParserTypes', (
'REQUEST_PARSER',
'RESPONSE_PARSER'
))(1, 2)
def __init__(self, parser_type):
assert parser_type in (HttpParser.types.REQUEST_PARSER, HttpParser.types.RESPONSE_PARSER)
self.type = parser_type
self.state = HttpParser.states.INITIALIZED
self.raw = b''
self.buffer = b''
self.headers = dict()
self.body = None
self.method = None
self.url = None
self.code = None
self.reason = None
self.version = None
self.chunk_parser = None
def is_chunked_encoded_response(self):
return self.type == HttpParser.types.RESPONSE_PARSER and \
b'transfer-encoding' in self.headers and \
self.headers[b'transfer-encoding'][1].lower() == b'chunked'
def parse(self, data):
self.raw += data
data = self.buffer + data
self.buffer = b''
more = True if len(data) > 0 else False
while more:
more, data = self.process(data)
self.buffer = data
def process(self, data):
# GNS3 Proxy
# include self.method == b'GET', as well as DELETE and PUT, otherwise requests with empty JSON "{}"
# will never get finished and will be killed after being inactive still being in state HEADERS_COMPLETE
#
# Reason is, that GNS3 uses REST calls, that allow GET to have a body, whereas proxy.py expects GET to
# have an empty body, as common for regular HTTP
if self.state in (HttpParser.states.HEADERS_COMPLETE,
HttpParser.states.RCVING_BODY,
HttpParser.states.COMPLETE) and \
(self.method == b'POST' or
self.method == b'GET' or
self.method == b'PUT' or
self.method == b'DELETE' or
self.type == HttpParser.types.RESPONSE_PARSER):
if not self.body:
self.body = b''
if b'content-length' in self.headers:
self.state = HttpParser.states.RCVING_BODY
self.body += data
if len(self.body) >= int(self.headers[b'content-length'][1]):
self.state = HttpParser.states.COMPLETE
elif self.is_chunked_encoded_response():
if not self.chunk_parser:
self.chunk_parser = ChunkParser()
self.chunk_parser.parse(data)
if self.chunk_parser.state == ChunkParser.states.COMPLETE:
self.body = self.chunk_parser.body
self.state = HttpParser.states.COMPLETE
return False, b''
line, data = HttpParser.split(data)
if line is False:
return line, data
if self.state == HttpParser.states.INITIALIZED:
self.process_line(line)
elif self.state in (HttpParser.states.LINE_RCVD, HttpParser.states.RCVING_HEADERS):
self.process_header(line)
# When connect request is received without a following host header
# See `TestHttpParser.test_connect_request_without_host_header_request_parse` for details
if self.state == HttpParser.states.LINE_RCVD and \
self.type == HttpParser.types.REQUEST_PARSER and \
self.method == b'CONNECT' and \
data == CRLF:
self.state = HttpParser.states.COMPLETE
# When raw request has ended with \r\n\r\n and no more http headers are expected
# See `TestHttpParser.test_request_parse_without_content_length` and
# `TestHttpParser.test_response_parse_without_content_length` for details
elif self.state == HttpParser.states.HEADERS_COMPLETE and \
self.type == HttpParser.types.REQUEST_PARSER and \
self.method != b'POST' and \
self.raw.endswith(CRLF * 2):
self.state = HttpParser.states.COMPLETE
elif self.state == HttpParser.states.HEADERS_COMPLETE and \
self.type == HttpParser.types.REQUEST_PARSER and \
self.method == b'POST' and \
(b'content-length' not in self.headers or
(b'content-length' in self.headers and
int(self.headers[b'content-length'][1]) == 0)) and \
self.raw.endswith(CRLF * 2):
self.state = HttpParser.states.COMPLETE
return len(data) > 0, data
def process_line(self, data):
line = data.split(SP)
if self.type == HttpParser.types.REQUEST_PARSER:
self.method = line[0].upper()
self.url = urlparse.urlsplit(line[1])
self.version = line[2]
else:
self.version = line[0]
self.code = line[1]
self.reason = b' '.join(line[2:])
self.state = HttpParser.states.LINE_RCVD
def process_header(self, data):
if len(data) == 0:
if self.state == HttpParser.states.RCVING_HEADERS:
self.state = HttpParser.states.HEADERS_COMPLETE
elif self.state == HttpParser.states.LINE_RCVD:
self.state = HttpParser.states.RCVING_HEADERS
else:
self.state = HttpParser.states.RCVING_HEADERS
parts = data.split(COLON)
key = parts[0].strip()
value = COLON.join(parts[1:]).strip()
self.headers[key.lower()] = (key, value)
def build_url(self):
if not self.url:
return b'/None'
url = self.url.path
if url == b'':
url = b'/'
if not self.url.query == b'':
url += b'?' + self.url.query
if not self.url.fragment == b'':
url += b'#' + self.url.fragment
return url
def build(self, del_headers=None, add_headers=None):
req = b' '.join([self.method, self.build_url(), self.version])
req += CRLF
if not del_headers:
del_headers = []
for k in self.headers:
if k not in del_headers:
req += self.build_header(self.headers[k][0], self.headers[k][1]) + CRLF
if not add_headers:
add_headers = []
for k in add_headers:
req += self.build_header(k[0], k[1]) + CRLF
req += CRLF
if self.body:
req += self.body
return req
@staticmethod
def build_header(k, v):
return k + b': ' + v
@staticmethod
def split(data):
pos = data.find(CRLF)
if pos == -1:
return False, data
line = data[:pos]
data = data[pos + len(CRLF):]
return line, data
class Connection(object):
"""TCP server/client connection abstraction."""
def __init__(self, what):
self.conn = None
self.buffer = b''
self.closed = False
self.what = what # server or client
def send(self, data):
# TODO: Gracefully handle BrokenPipeError exceptions
return self.conn.send(data)
def recv(self, bufsiz=8192):
try:
data = self.conn.recv(bufsiz)
if len(data) == 0:
logger.debug('rcvd 0 bytes from %s' % self.what)
return None
logger.debug('rcvd %d bytes from %s' % (len(data), self.what))
return data
except Exception as e:
# if errno == errno.ECONNRESET:
# logger.debug('%r' % e)
# else:
logger.exception(
'Exception while receiving from connection %s %r with reason %r' % (self.what, self.conn, e))
return None
def close(self):
# GNS3 proxy needs a clean closing of connections, otherwise, e.g., conn will be closed before
# HTTP response is delivered and for example ProxyAuthenticationException will not be displayed
# self.conn.close()
self.conn.shutdown(socket.SHUT_WR)
time.sleep(1)
self.conn.close()
self.closed = True
def buffer_size(self):
return len(self.buffer)
def has_buffer(self):
return self.buffer_size() > 0
def queue(self, data):
self.buffer += data
def flush(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
logger.debug('flushed %d bytes to %s' % (sent, self.what))
class Server(Connection):
"""Establish connection to destination server."""
def __init__(self, host, port):
super(Server, self).__init__(b'server')
self.addr = (host, int(port))
def __del__(self):
if self.conn:
self.close()
def connect(self):
self.conn = socket.create_connection((self.addr[0], self.addr[1]))
class Client(Connection):
"""Accepted client connection."""
def __init__(self, conn, addr):
super(Client, self).__init__(b'client')
self.conn = conn
self.addr = addr
class ProxyError(Exception):
pass
class ProxyConnectionFailed(ProxyError):
def __init__(self, host, port, reason):
self.host = host
self.port = port
self.reason = reason
def __str__(self):
return '<ProxyConnectionFailed - %s:%s - %s>' % (self.host, self.port, self.reason)
class ProxyAuthenticationFailed(ProxyError):
pass
class Proxy(threading.Thread):
"""HTTP proxy implementation.
Accepts `Client` connection object and act as a proxy between client and server.
"""
def __init__(self, client, backend_auth_code=None, backend_port=3080, server_recvbuf_size=65536,
client_recvbuf_size=65536, inactivity_timeout=300, auth_whitelist=None, auth_header='X-Auth-Username',
real_ip_header='X-Forwarded-For', allow_any_user=False, default_server=None, config_servers=None,
config_users=None, config_mapping=None, config_project_filter=None, config_deny=None):
super(Proxy, self).__init__()
self.start_time = self._now()
self.last_activity = self.start_time
self.inactivity_timeout = inactivity_timeout
self.auth_prefixes, self.auth_hosts = self._get_prefixes_and_hosts(auth_whitelist)
self.auth_header = auth_header
self.real_ip_header = real_ip_header
self.allow_any_user = allow_any_user
self.client = client
self.client_recvbuf_size = client_recvbuf_size
self.server = None
self.server_recvbuf_size = server_recvbuf_size
self.username = None
self.backend_auth_code = backend_auth_code
self.backend_port = backend_port
self.default_server = default_server
self.config_servers = config_servers
self.config_users = config_users
self.config_mapping = config_mapping
self.config_project_filter = config_project_filter
self.config_deny = config_deny
self.request = HttpParser(HttpParser.types.REQUEST_PARSER)
self.response = HttpParser(HttpParser.types.RESPONSE_PARSER)
@staticmethod
def _now():
return datetime.datetime.utcnow()
@staticmethod
def _get_prefixes_and_hosts(net_list):
if not net_list:
return None, None
# Convert comma-separated string to list
if type(net_list) is str:
net_list = net_list.split(',')
# Remove whitespace
net_list = [x.strip() for x in net_list]
# Sort prefixes and hostnames into separate lists
prefixes = list()
hosts = list()
for entry in net_list:
try:
prefixes.append(ip_network(entry))
except ValueError:
# If not an IP, try resolving it
try:
socket.getaddrinfo(entry, 80)
hosts.append(entry)
except socket.gaierror:
logger.fatal(
"auth whitelist entry %s is not a valid prefix or resolvable hostname " % entry +
"(e.g., 192.168.0.0/24, proxy.example.com)" )
raise ProxyError()
prefixes.sort(key=lambda x: x.num_addresses, reverse=True)
return prefixes, hosts
def _inactive_for(self):
return (self._now() - self.last_activity).seconds
def _is_inactive(self):
return self._inactive_for() > self.inactivity_timeout
def _lookup_user(self, username, password=None):
if self.config_users is not None and username in self.config_users:
# Check submitted password
if password == None or self.config_users[username] == password:
logger.debug("Successfully authenticated user %s" % username)
self.username = username
else:
logger.error("Wrong password for user %s" % username)
raise ProxyAuthenticationFailed()
else:
logger.error("User %s not found in config" % username)
raise ProxyAuthenticationFailed()
def _get_header_value(self, key):
# Simplifies fetching header values on requests
header = key.lower().encode()
value = self.request.headers[header][1]
return text_(value)
def _process_request(self, data):
# once we have connection to the server
# we don't parse the http request packets
# any further, instead just pipe incoming
# data from client to server
if self.server and not self.server.closed:
self.server.queue(data)
return
# parse http request
self.request.parse(data)
# once http request parser has reached the state complete
# we attempt to establish connection to destination server
if self.request.state == HttpParser.states.COMPLETE:
logger.debug('request parser is in state complete')
# GNS3 Proxy Authentication
source_ip = ip_address(self.client.addr[0])
logger.debug("Received request from IP %s" % source_ip)
logger.debug("Request headers: %s"
% {h[0].decode(): h[1].decode() for h in self.request.headers.values()})
# Checking if the request came from a proxy allowed for forward authentication
if (self.auth_prefixes or self.auth_hosts) and self.auth_header.lower().encode() in self.request.headers.keys():
for prefix in self.auth_prefixes:
if source_ip in prefix:
break
else:
for host in self.auth_hosts:
try:
# socket.getaddrinfo should be safe to use here, it uses the host's dns cache.
# It could potentially introduce delay if the DNS responses are slow or timeout.
resolved_ips = [ip_address(answer[-1][0]) for answer in socket.getaddrinfo(host, 80)]
except socket.gaierror:
logger.error(
"Problem while resolving hostname %s to IP address. " % host +
"This means the resolution to this host stopped working while proxy was running.")
continue
if source_ip in resolved_ips:
break
else:
logger.error(
"Request contains proxy auth header and source IP address %s " % source_ip +
"is not found in auth-whitelist.")
raise ProxyAuthenticationFailed()
# Determine real IP of the client
try:
real_ip = self._get_header_value(self.real_ip_header)
except KeyError:
logger.error(
"Request contains proxy auth header and %s header is missing from request."
% self.real_ip_header)
raise ProxyAuthenticationFailed()
try:
self.client.real_ip = ip_address(real_ip)
except ValueError:
logger.error("Value %s for %s header is not a valid IP address." % (real_ip, self.real_ip_header))
raise ProxyAuthenticationFailed()
# Get username from the auth header
username = self._get_header_value(self.auth_header)
logger.debug("Received authentication through downstream proxy %s from client %s with username %s" % (
source_ip, real_ip, username))
# Check if user should be allowed
if self.allow_any_user:
self.username = username
logger.debug("User %s allowed as allow-any-user is True" % self.username)
else:
self._lookup_user(username)
# Checking authentication and authorization of user supplied in request
elif b'authorization' in self.request.headers:
try:
auth_request = (base64.b64decode(self._get_header_value('authorization')[6:])).decode().split(":")
username, password = auth_request
except:
logger.error("Authorization header does not contain valid HTTP Basic credentials.")
raise ProxyAuthenticationFailed()
logger.debug("Received Authorization request %s %s %s" % (auth_request, username, password))
# Lookup user from request in config
self._lookup_user(username, password)
# Identify real client IP if the request came from a downstream proxy
try:
self.client.real_ip = ip_address(self._get_header_value(self.real_ip_header))
except:
self.client.real_ip = source_ip
else:
logger.error(
"Request did not contain an Authorization header. Please provide username and password in client.")
raise ProxyAuthenticationFailed()
# TODO: do not allow operations on filtered projects
# if /v2/projects/{project_id} ... resolve id? maybe store list of allowed ids on first project list
# retrieval, use as while list containing allowed project IDs
# a bit of a cosmetic issue, as using Web app and GNS3 client will not show filtered projects anyway
# only REST calls would be possible
# TODO: add configuration to match user groups found in headers to mappings, filters, and rules
# this would allow matching using group information passed from a downstream proxy and simplifies
# user management and securuty
# evaluate denied requests
if self.config_deny is not None and len(self.config_deny) > 0:
for item in self.config_deny:
if self.config_users is not None:
for key in self.config_users:
if re.fullmatch(item["user"], key):
logger.debug("Deny matched user %s = %s" % (item["user"], key))
access_user = key
logger.debug("Trying to match %s as %s" % (username, access_user))
if username == access_user:
logger.debug(
"User matched mapping %s = %s, evaluating deny rule %s" % (
item["user"], key, item))
logger.debug(
"Debug deny rule %s %s" % (
text_(self.request.method), text_(self.request.url.path)))
# logger.info("Method: %s %s %s" % ((re.fullmatch(item["method"],text_(self.request.method)),
# item["method"], text_(self.request.method))))
# logger.info("Path: %s %s %s" % ((re.fullmatch(item["url"],text_(self.request.url.path)),
# item["url"], text_(self.request.url.path))))
if (((item["method"] == "") or re.fullmatch(item["method"],
text_(self.request.method))) and
(item["url"] == "" or re.fullmatch(item["url"],
text_(self.request.url.path))) and
(item["header"] == "" or re.fullmatch(item["header"],
text_(self.request.headers))) and
(item["body"] == "" or re.fullmatch(item["body"],
text_(self.request.body)))):
logger.info("Request denied due to matching rule %s", item)
raise ProxyAuthenticationFailed()
else:
logger.info("Cannot evaluate deny rules. No users found in config.")
raise ProxyAuthenticationFailed()
# CONNECT not used by GNS3?
# if self.request.method == b'CONNECT':
# host, port = self.request.url.path.split(COLON)
# elif self.request.url:
# host, port = self.request.url.hostname, self.request.url.port if self.request.url.port else 80
# else:
# raise Exception('Invalid request\n%s' % self.request.raw)
# Redirect request based on client and user to appropriate backend server, according to mapping from config
# or default backend server
backend_server = None
# Try to find match for user in config
if self.config_mapping is not None and len(self.config_mapping) > 0:
for item in self.config_mapping:
if self.config_users is not None:
for key in self.config_users:
if re.fullmatch(item["match"], key):
logger.debug("User mapping matched %s = %s" % (item["match"], key))
access_user = key
logger.debug("Trying to match %s as %s" % (username, access_user))
if username == access_user:
logger.debug("User matched mapping %s = %s, choosing server %s" % (
item["match"], key, item["server"]))
if self.config_servers is not None and item["server"] in self.config_servers:
backend_server = self.config_servers[item["server"]]
else:
logger.fatal("Mapped server %s not found in config." % item["server"])
raise ProxyError()
break
else:
logger.info("Cannot evaluate mapping rules. No users found in config.")
raise ProxyAuthenticationFailed()
# if no server was chosen by mapping, try using default, otherwise raise exception
if backend_server is None:
if self.default_server is not None:
# if a default server is set in config, choose this one by default
if self.config_servers is not None and self.default_server in self.config_servers:
backend_server = self.config_servers[self.default_server]
logger.debug("Redirecting client %s to default backend server %s:%s" % (
self.client.real_ip, backend_server, self.backend_port))
else:
try:
backend_server = str(ip_address(self.default_server))
logger.debug("Trying to redirecting client %s to default backend server IP %s:%s" % (
self.client.real_ip, backend_server, self.backend_port))
except ValueError:
logger.fatal(
"Default server %s is neither an entry in server config nor a valid IP address"
% self.default_server)
raise ProxyError()
else:
logger.error("Cannot find appropriate server using mapping and no default server defined in "
"config.")
raise ProxyAuthenticationFailed()
self.server = Server(backend_server, self.backend_port)
try:
logger.debug('connecting to server %s:%s' % (backend_server, self.backend_port))
self.server.connect()
logger.debug('connected to server %s:%s' % (backend_server, self.backend_port))
except Exception as e: # TimeoutError, socket.gaierror
self.server.closed = True
raise ProxyConnectionFailed(backend_server, self.backend_port, repr(e))
# for http connect methods (https requests)
# queue appropriate response for client
# notifying about established connection
if self.request.method == b'CONNECT':
self.client.queue(PROXY_TUNNEL_ESTABLISHED_RESPONSE_PKT)
# for usual http requests, re-build request packet
# and queue for the server with appropriate headers
else:
self.server.queue(self.request.build(
# GNS3 Proxy REMOVED
#
# del_headers=[b'proxy-authorization', b'proxy-connection', b'connection', b'keep-alive'],
# add_headers=[(b'Via', b'1.1 proxy.py v%s' % version), (b'Connection', b'Close')]
del_headers=[b'authorization'],
add_headers=[(b'Authorization', self.backend_auth_code)]
))
def _process_response(self, data):
# parse incoming response packet
# only for non-https requests
if not self.request.method == b'CONNECT':
self.response.parse(data)
# filter project list
if self.config_project_filter is not None:
if b'x-route' in self.response.headers and self.response.headers[b'x-route'][1].lower() == \
b'/v2/projects':
logger.debug("Filtering project library in response")
user_matched = False
user_project_filters = list()
for project_filter in self.config_project_filter:
if re.fullmatch(project_filter["match"], self.username):
logger.debug("Project filter %s matched for user %s" % (project_filter["match"],
self.username))
user_matched = True
user_project_filters.append(project_filter)
if user_matched:
header_block = data[:data.find(b'\r\n\r\n')]
body = data[data.find(b'\r\n\r\n'):]
# make sure that body is complete, otherwise we cannot load and decode contained JSON
for header in header_block.split(CRLF):
if text_(header).startswith("Content-Length:"):
content_length = int(text_(header).split(":")[1])
while len(body) - 4 < content_length:
logger.debug("Body is not complete (len: %d of content-length: %d), cannot decode JSON, "
"trying to receive further content" % (len(body) - 4, content_length))
data += self.server.recv(self.server_recvbuf_size)
body = data[data.find(b'\r\n\r\n'):]
logger.debug("(len: %d of content-length: %d)" % (len(body) - 4, content_length))
try:
projects = json.loads(body)
projects_filtered = list()
for user_project_filter in user_project_filters:
for project in projects:
if re.fullmatch(user_project_filter["filter"], project["name"]):
logger.debug("Allowing project %s for user %s" % (project, self.username))
if project not in projects_filtered:
projects_filtered.append(project)
logger.info("Filtered project library for user %s from %d to %d entries.",
self.username, len(projects), len(projects_filtered))
body = json.dumps(projects_filtered)
header_block_out = b''
for header in header_block.split(CRLF):
if text_(header).startswith("Content-Length:"):
header_block_out += bytes_("Content-Length: " + str(len(body))) + CRLF
else:
header_block_out += header + CRLF
new_data = bytes_(header_block_out) + b'\r\n' + bytes_(body)
data = new_data
except json.decoder.JSONDecodeError as jde:
logger.error("JSONDecodeError during project filtering. %s %s", body, jde)
# if b'x-route' in self.response.headers:
# if self.response.headers[b'x-route'][1].lower() == b'/v2/projects/{project_id}/open':
# logger.debug("x-route: %s", self.response.headers[b'x-route'][1].lower())
# check console_host config in project nodes, if value is "0.0.0.0" backend is likely not setup correctly
# to work with the proxy (i.e., consoles not being accessible)
if b'x-route' in self.response.headers:
if self.response.headers[b'x-route'][1].lower() == b'/v2/projects/{project_id}/nodes':
logger.debug("Checking console_host in response for %s", self.response.headers[b'x-route'])
if data.find(b"\"console_host\": \"0.0.0.0\",") != -1:
logger.fatal("Backend %s is likely to be misconfigured! In gns3_server.conf host needs to be"
"changed to the primary IP address also used in the backend config. Seems to be"
"host = 0.0.0.0. Console connections to nodes on this backend will not work"
"when accessed through the proxy! See also gns3_proxy setup documentation."
% str(self.server.addr))
raise ProxyError()
# data = data.replace(b"\"console_host\": \"0.0.0.0\",", b"\"console_host\": \"192.168.76.205\",")
# data = data.replace(b"\"console_host\": \"0.0.0.0\",", b"\"console_host\": \"0.0.0.0\",")
# logger.info("%s", data)
# demo to intercept specific response
# if b'x-route' in self.response.headers:
# if self.response.headers[b'x-route'][1].lower() == b'/v2/settings':
# logger.info("%s", self.response.headers[b'x-route'])
# GNS3 Proxy Example to rewrite response content
#
# allow rewrite of console_host, does not work, not allowed to change console host in config, needs to be
# set in gns3_server.conf
#
# if b'x-route' in self.response.headers:
# if self.response.headers[b'x-route'][1].lower() == b'/v2/projects/{project_id}/nodes':
# logger.info("%s", self.response.headers[b'x-route'])
# #data = data.replace(b"\"console_host\": \"0.0.0.0\",", b"\"console_host\": \"192.168.76.205\",")
# #data = data.replace(b"\"console_host\": \"0.0.0.0\",", b"\"console_host\": \"0.0.0.0\",")
# #logger.info("%s", data)
# queue data for client
self.client.queue(data)
def _access_log(self):
# TODO: this section desparately needs to be rewritten with f-strings
host, port = self.server.addr if self.server else (None, None)
source_ip=ip_address(self.client.addr[0])
if self.request.method == b'CONNECT':
logger.info(
'%s:%s - %b %s:%s' % (self.client.addr[0], self.client.addr[1], self.request.method, host, port))
elif self.request.method:
real_ip = getattr(self.client, 'real_ip', source_ip)
if source_ip == real_ip:
logger.info('%s:%s (%s) - %s %s:%s%s - %s %s - %s bytes (%s threads)' % (
self.client.addr[0], self.client.addr[1], self.username, self.request.method, host, port,
self.request.build_url(), self.response.code, self.response.reason, len(self.response.raw),
threading.active_count()))
else:
logger.info('%s via %s:%s (%s) - %s %s:%s%s - %s %s - %s bytes (%s threads)' % (
self.client.real_ip, self.client.addr[0], self.client.addr[1], self.username, self.request.method,
host, port, self.request.build_url(), self.response.code, self.response.reason,
len(self.response.raw), threading.active_count()))
def _get_waitable_lists(self):
rlist, wlist, xlist = [self.client.conn], [], []
if self.client.has_buffer():
wlist.append(self.client.conn)
if self.server and not self.server.closed:
rlist.append(self.server.conn)
if self.server and not self.server.closed and self.server.has_buffer():
wlist.append(self.server.conn)
return rlist, wlist, xlist
def _process_wlist(self, w):
if self.client.conn in w:
logger.debug('client is ready for writes, flushing client buffer')
self.client.flush()
if self.server and not self.server.closed and self.server.conn in w:
logger.debug('server is ready for writes, flushing server buffer')
self.server.flush()
def _process_rlist(self, r):
"""Returns True if connection to client must be closed."""
if self.client.conn in r:
logger.debug('client is ready for reads, reading')
data = self.client.recv(self.client_recvbuf_size)
self.last_activity = self._now()
if not data:
logger.debug('client closed connection, breaking')
return True
try:
self._process_request(data)
except (ProxyAuthenticationFailed, ProxyConnectionFailed) as e:
logger.exception(e)
self.client.queue(Proxy._get_response_pkt_by_exception(e))
self.client.flush()
return True
if self.server and not self.server.closed and self.server.conn in r:
logger.debug('server is ready for reads, reading')
data = self.server.recv(self.server_recvbuf_size)
self.last_activity = self._now()
if not data:
logger.debug('server closed connection')
self.server.close()
else:
self._process_response(data)
return False
def _process(self):
while True:
rlist, wlist, xlist = self._get_waitable_lists()
r, w, x = select.select(rlist, wlist, xlist, 1)
self._process_wlist(w)
if self._process_rlist(r):
break
if self.client.buffer_size() == 0:
if self.response.state == HttpParser.states.COMPLETE:
logger.debug('client buffer is empty and response state is complete, breaking')