mirror of
https://github.com/evilsocket/opensnitch.git
synced 2025-03-05 00:51:05 +01:00

Now the user can personalize GUI's appearance (#424). There're 15 default themes, dark and light, that will help integrating on some environments (#303, #335). More themes can be added, by creating a new xml under ~/.config/opensnitch/themes/ or /usr/lib/python3/dist-packages/opensnitch/ The lib used is https://github.com/UN-GCPDS/qt-material. https://github.com/UN-GCPDS/qt-material#custom-colors
106 lines
3.6 KiB
Python
Executable file
106 lines
3.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
from PyQt5 import QtWidgets, QtGui, QtCore
|
|
|
|
import sys
|
|
import os
|
|
import time
|
|
import signal
|
|
import argparse
|
|
import logging
|
|
from threading import Timer
|
|
|
|
logging.getLogger().disabled = True
|
|
|
|
from concurrent import futures
|
|
|
|
import grpc
|
|
|
|
dist_path = '/usr/lib/python3/dist-packages/'
|
|
if dist_path not in sys.path:
|
|
sys.path.append(dist_path)
|
|
|
|
from opensnitch.service import UIService
|
|
from opensnitch.config import Config
|
|
from opensnitch.utils import Themes
|
|
import opensnitch.version
|
|
import opensnitch.ui_pb2
|
|
from opensnitch.ui_pb2_grpc import add_UIServicer_to_server
|
|
|
|
def on_exit():
|
|
server.stop(0)
|
|
# try to exit gracefully. Otherwise after 10s force exit.
|
|
Timer(5, __force_exit__).start()
|
|
app.quit()
|
|
sys.exit(0)
|
|
|
|
def __force_exit__():
|
|
os._exit(1)
|
|
|
|
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
|
|
|
|
def load_translations():
|
|
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()
|
|
translator.load(i18n_path + "/" + locale.name() + "/opensnitch-" + locale.name() + ".qm")
|
|
|
|
return translator
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='OpenSnitch UI service.')
|
|
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")
|
|
parser.add_argument("--max-clients", dest="serverWorkers", default=10, help="Max number of allowed clients (incoming connections).")
|
|
|
|
args = parser.parse_args()
|
|
|
|
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
|
|
if supported_qt_version(5,6,0):
|
|
try:
|
|
# NOTE: maybe we also need Qt::AA_UseHighDpiPixmaps
|
|
QtCore.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
|
|
except Exception:
|
|
pass
|
|
|
|
translator = load_translations()
|
|
app = QtWidgets.QApplication(sys.argv)
|
|
app.installTranslator(translator)
|
|
thm = Themes.instance()
|
|
thm.load_theme(app)
|
|
|
|
service = UIService(app, on_exit)
|
|
# @doc: https://grpc.github.io/grpc/python/grpc.html#server-object
|
|
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),
|
|
))
|
|
|
|
add_UIServicer_to_server(service, server)
|
|
|
|
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)
|
|
|
|
# https://stackoverflow.com/questions/5160577/ctrl-c-doesnt-work-with-pyqt
|
|
signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
try:
|
|
# print "OpenSnitch UI service running on %s ..." % socket
|
|
server.start()
|
|
app.exec_()
|
|
except KeyboardInterrupt:
|
|
on_exit()
|
|
|