Skip to content

Commit ee9cd87

Browse files
committed
Fix GIL lock problems due to threading and add identifier finding
1 parent 77d3024 commit ee9cd87

File tree

9 files changed

+81
-15
lines changed

9 files changed

+81
-15
lines changed

config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99

1010
homenetwork = ipaddress.ip_network('192.168.0.0/24')
1111
homenetwork_router = ipaddress.ip_address('192.168.0.1')
12-
aggregate_google=True # That is a lot of domains
12+
aggregate_google=True # That is a lot of domains
13+
ignored_domains=["osoite.local"]

connections.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def getHomeIPFromConnection(conn):
3737

3838

3939
def getConnections():
40-
global conns_dirty
4140
"""
4241
Returns connections list:
4342
Dict of 'conntrackid': {'bytes': '2519',
@@ -54,13 +53,17 @@ def getConnections():
5453
'src': IPv4Address('192.168.0.142'),
5554
'state': 'ESTABLISHED'} # if tcp connection, shows conntrack state
5655
"""
56+
57+
global conns_dirty,conns_mainthread
58+
59+
# TODO: cleanup!!!
60+
#assert threading.current_thread() is threading.main_thread()
61+
5762
if conns_dirty:
5863
conns_dirty=False
5964
with conns_lock:
60-
for k,v in conns.items():
61-
if k not in conns_mainthread:
62-
conns_mainthread[k]=v
63-
65+
conns_mainthread=dict(conns) # GIL cannot yield in a dict copy
66+
6467
return conns_mainthread
6568

6669

connvis

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
sudo python3 main.py "$@"
1+
reset -I
2+
(
3+
sudo python3 main.py "$@"
4+
)
5+
reset -I
6+

ip2dns.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,30 @@ def load(*args):
5050
psl = PublicSuffixList()
5151

5252
def shorten(domain):
53-
return psl.privatesuffix(domain)
53+
if not domain:
54+
return domain
5455

55-
import dnsmasq_parser
56+
ret = psl.privatesuffix(domain)
57+
if ret:
58+
return ret
59+
if not psl.is_public(domain):
60+
print("FIXME: PSL could not digest:",domain)
61+
return domain # it IS a private suffix or not a domain
62+
63+
import dnsmasq_parser,config
5664
def onJournalMessage(entry):
5765
l=entry["MESSAGE"]
5866

5967
r=dnsmasq_parser.parse_reply(l)
6068
if r:
6169
ip=r["ip"]
6270
domain=r["domain"]
71+
if domain in config.ignored_domains:
72+
return
6373
if not "NODATA" in ip and not "NXDOMAIN" in ip:
6474
feed(ip,domain)
6575
return r
66-
76+
6777
def seedFromDnsmasq():
6878

6979
import select

monitor_domains.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def lastDomainResolveTime(domain,short=False):
4949
import dnsmasq_parser
5050
import ip2dns
5151
import ipaddress
52+
import config
5253
def onJournalMessage(entry):
5354
l=entry["MESSAGE"]
5455
ts=entry['__REALTIME_TIMESTAMP'] #we are parsing real time, no use
@@ -57,6 +58,9 @@ def onJournalMessage(entry):
5758
if q:
5859
source = ipaddress.ip_address(q["source"])
5960
domain=q["query"]
61+
if domain in config.ignored_domains:
62+
return
63+
6064
sdomain=ip2dns.shorten(domain)
6165
now=time.time()
6266

static/sankey/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async function updateLoop() {
5959
await sleep(333);
6060
};
6161

62+
var data = loadedData;
6263
try {
63-
var data = loadedData;
6464
if (!data) {
6565
let response = await fetch('/sankey.json');
6666
lastDataJson = await response.text();

templates/hello.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919

2020
</head>
2121
<body>
22-
22+
2323
<h1>CONNVIS Real Time Network Monitoring and Visualizer</h1>
2424
<br/>
25+
<span><b>Your identifier / tunnisteesi: </b><h2 class="yourname">{{ yourname }}</h2></span>
26+
<br/>
27+
<hr/>
28+
<br/>
2529
<a class="vislink" href="sankey/index.html">Local device to destinations</a> (<a href="sankey.json">json</a>)
2630
<br/>
2731
<a class="vislink" href="geo/index.html">Targets on a world map</a> (<a href="geo.json">json</a>)
@@ -41,8 +45,9 @@ <h1>CONNVIS Real Time Network Monitoring and Visualizer</h1>
4145
<br/>
4246
<br/>
4347
<br/>
44-
<br/>
4548
<hr>
49+
<br/>
50+
<br/>
4651
<small>This site or product includes IP2Location LITE data available from <a href="https://lite.ip2location.com">https://lite.ip2location.com</a>.</small>
4752
</body>
4853
</html>

utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,40 @@ def shortenHomeIPMemorableUniq(homeip,original_important=True,net=config.homenet
2222
homeipname=f"{homeip}\n({homeipname})"
2323
return homeipname
2424

25+
#TODO
26+
""" File "/home/display/connvis/utils.py", line 26, in aggregateBigCompanies
27+
if config.aggregate_google and "google" in identifier.lower(): # gvt2.com gstatic.om doubleclick
28+
AttributeError: 'NoneType' object has no attribute 'lower'
29+
130.232.129.162 - - [20/Aug/2021 13:49:50] "GET /dnsbar.json HTTP/1.1" 500 -
30+
Traceback (most recent call last):
31+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2088, in __call__
32+
return self.wsgi_app(environ, start_response)
33+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2073, in wsgi_app
34+
response = self.handle_exception(e)
35+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2070, in wsgi_app
36+
response = self.full_dispatch_request()
37+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1515, in full_dispatch_request
38+
rv = self.handle_user_exception(e)
39+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1513, in full_dispatch_request
40+
rv = self.dispatch_request()
41+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 1499, in dispatch_request
42+
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
43+
File "/home/display/connvis/vishttp.py", line 93, in dnsbar
44+
return jsonify(dnsBarProvider())
45+
File "/home/display/connvis/dnsvis.py", line 43, in provider
46+
shortdomain=utils.aggregateBigCompanies(shortdomain)
47+
File "/home/display/connvis/utils.py", line 26, in aggregateBigCompanies
48+
if config.aggregate_google and "google" in identifier.lower(): # gvt2.com gstatic.om doubleclick
49+
AttributeError: 'NoneType' object has no attribute 'lower'
50+
130.232.129.162 - - [20/Aug/2021 13:49:53] "GET /dnsbar.json HTTP/1.1" 500 -
51+
Traceback (most recent call last):
52+
File "/usr/local/lib/python3.9/dist-packages/flask/app.py", line 2088, in __call__
53+
"""
54+
2555
def aggregateBigCompanies(identifier,where=False):
56+
if not identifier: #TODO: ???
57+
return identifier
58+
2659
if config.aggregate_google and "google" in identifier.lower(): # gvt2.com gstatic.om doubleclick
2760
return "google.com"
2861
return identifier

vishttp.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,15 @@ def setSankeyProvider(p):
113113
def sankeyjson():
114114
return jsonify(SankeyProvider())
115115

116-
116+
import ipaddress,utils
117117
@app.route("/")
118118
def main():
119-
return render_template("hello.html")
119+
yourip=ipaddress.ip_address(request.remote_addr)
120+
yourname=request.remote_addr
121+
if yourip in config.homenetwork:
122+
yourname=utils.shortenHomeIPMemorableUniq(yourip,True)
123+
124+
return render_template("hello.html",yourip=yourip,yourname=yourname)
120125

121126

122127
def run(use_reloader=False):

0 commit comments

Comments
 (0)