2020-11-28 11:02:03 +01:00
#!/usr/bin/env python3
2018-04-10 16:56:29 +02:00
2018-09-17 23:52:51 -04:00
from PyQt5 import QtWidgets, QtGui, QtCore
2018-04-06 01:44:15 +02:00
2018-04-05 12:01:33 +02:00
import sys
import os
import time
import signal
2018-04-05 16:27:48 +02:00
import argparse
2018-04-11 15:35:29 +02:00
import logging
logging.getLogger().disabled = True
2018-04-05 12:01:33 +02:00
2018-04-11 15:35:29 +02:00
from concurrent import futures
2018-04-05 12:01:33 +02:00
import grpc
2020-09-26 01:10:00 +02:00
dist_path = '/usr/lib/python3/dist-packages/'
if dist_path not in sys.path:
sys.path.append(dist_path)
2018-04-11 15:35:29 +02:00
from opensnitch.service import UIService
2018-04-16 12:54:25 +02:00
from opensnitch.config import Config
2018-04-11 15:35:29 +02:00
import opensnitch.version
import opensnitch.ui_pb2
from opensnitch.ui_pb2_grpc import add_UIServicer_to_server
2018-04-05 12:01:33 +02:00
2018-04-06 01:44:15 +02:00
def on_exit():
app.quit()
server.stop(0)
sys.exit(0)
2018-04-05 12:01:33 +02:00
2019-11-25 21:41:25 +01:00
def supported_qt_version(major, medium, minor):
q = QtCore.QT_VERSION_STR.split(".")
return int(q[0]) >= major and int(q[1]) >= medium and int(q[2]) >= minor
2018-04-05 12:01:33 +02:00
if __name__ == '__main__':
2018-04-05 16:27:48 +02:00
parser = argparse.ArgumentParser(description='OpenSnitch UI service.')
2018-04-07 01:52:43 +02:00
parser.add_argument("--socket", dest="socket", default="unix:///tmp/osui.sock", help="Path of the unix socket for the gRPC service (https://github.com/grpc/grpc/blob/master/doc/naming.md).", metavar="FILE")
2020-05-12 01:18:40 +02:00
parser.add_argument("--max-clients", dest="serverWorkers", default=10, help="Max number of allowed clients (incoming connections).")
2018-04-05 16:27:48 +02:00
args = parser.parse_args()
2020-11-28 11:02:03 +01:00
2018-09-17 23:52:51 -04:00
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
2019-11-25 21:41:25 +01:00
if supported_qt_version(5,6,0):
2020-02-19 01:04:58 +01:00
try:
2020-02-22 00:48:19 +01:00
# NOTE: maybe we also need Qt::AA_UseHighDpiPixmaps
QtCore.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
2020-02-19 01:04:58 +01:00
except Exception:
pass
2018-04-06 01:44:15 +02:00
2021-01-02 21:40:19 +01:00
locale = QtCore.QLocale.system()
i18n_path = os.path.dirname(os.path.realpath(opensnitch.__file__)) + "/i18n"
print("Loading translations:", i18n_path, "locale:", locale.name())
translator = QtCore.QTranslator()
2021-01-03 00:50:35 +01:00
translator.load(i18n_path + "/" + locale.name() + "/opensnitch-" + locale.name() + ".qm")
2020-02-22 00:48:19 +01:00
app = QtWidgets.QApplication(sys.argv)
2021-01-02 21:40:19 +01:00
app.installTranslator(translator)
2020-02-22 00:48:19 +01:00
2020-02-09 20:56:26 +01:00
service = UIService(app, on_exit)
2020-05-12 01:18:40 +02:00
# @doc: https://grpc.github.io/grpc/python/grpc.html#server-object
2021-04-11 20:55:14 +02:00
server = grpc.server(futures.ThreadPoolExecutor(),
options=(
# https://github.com/grpc/grpc/blob/master/doc/keepalive.md
# https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
# send keepalive ping every 5 second, default is 2 hours)
('grpc.keepalive_time_ms', 5000),
# after 5s of inactivity, wait 20s and close the connection if
# there's no response.
('grpc.keepalive_timeout_ms', 20000),
('grpc.keepalive_permit_without_calls', True),
))
2018-04-05 12:01:33 +02:00
2018-04-11 15:35:29 +02:00
add_UIServicer_to_server(service, server)
2020-11-28 11:02:03 +01:00
2018-04-07 01:52:43 +02:00
if args.socket.startswith("unix://"):
socket = args.socket[7:]
socket = os.path.abspath(socket)
server.add_insecure_port("unix:%s" % socket)
else:
server.add_insecure_port(args.socket)
2018-04-05 12:01:33 +02:00
# https://stackoverflow.com/questions/5160577/ctrl-c-doesnt-work-with-pyqt
signal.signal(signal.SIGINT, signal.SIG_DFL)
try:
2018-04-06 01:44:15 +02:00
# print "OpenSnitch UI service running on %s ..." % socket
2018-04-05 12:01:33 +02:00
server.start()
app.exec_()
except KeyboardInterrupt:
2018-04-06 01:44:15 +02:00
on_exit()
2018-04-05 12:01:33 +02:00