misc: small fix or general refactoring i did not bother commenting

This commit is contained in:
evilsocket 2018-04-07 03:28:44 +02:00
parent b0a6c0d01f
commit 34ec05a5d2
Failed to generate hash of commit
4 changed files with 42 additions and 12 deletions

View file

@ -3,6 +3,7 @@ package conman
import (
"fmt"
"net"
"os"
"github.com/evilsocket/opensnitch/daemon/dns"
"github.com/evilsocket/opensnitch/daemon/log"
@ -112,6 +113,8 @@ func NewConnection(nfp *netfilter.NFPacket, ip *layers.IPv4) (c *Connection, err
// lookup pid by inode and process by pid
if pid, found := sockets[c.Entry.INode]; found == false {
return nil, fmt.Errorf("Could not find process id for: %s", c)
} else if pid == os.Getpid() {
return nil, nil
} else if c.Process = procmon.FindProcess(pid); c.Process == nil {
return nil, fmt.Errorf("Could not find process by its pid %d for: %s", pid, c)
}

View file

@ -28,6 +28,8 @@ class PromptDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
self._lock = threading.Lock()
self._con = None
self._rule = None
self._local = True
self._peer = None
self._trigger.connect(self.on_connection_triggered)
self._done = threading.Event()
@ -52,11 +54,13 @@ class PromptDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
self._what_combo = self.findChild(QtWidgets.QComboBox, "whatCombo")
self._duration_combo = self.findChild(QtWidgets.QComboBox, "durationCombo")
def promptUser(self, connection):
def promptUser(self, connection, is_local, peer):
# one at a time
with self._lock:
# reset state
self._rule = None
self._local = is_local
self._peer = peer
self._con = connection
self._done.clear()
# trigger on_connection_triggered
@ -72,7 +76,11 @@ class PromptDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
self.show()
def _render_connection(self, con):
app_name, app_icon, desk = self._apps_parser.get_info_by_path(con.process_path, "terminal")
if self._local:
app_name, app_icon, _ = self._apps_parser.get_info_by_path(con.process_path, "terminal")
else:
app_name, app_icon = "", "terminal"
if app_name == "":
self._app_name_label.setText(con.process_path)
else:
@ -82,18 +90,33 @@ class PromptDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
pixmap = icon.pixmap(icon.actualSize(QtCore.QSize(48, 48)))
self._app_icon_label.setPixmap(pixmap)
self._message_label.setText("<b>%s</b> is connecting to <b>%s</b> on %s port %d" % ( \
con.process_path,
con.dst_host or con.dst_ip,
con.protocol,
con.dst_port
))
if self._local:
message = "<b>%s</b> is connecting to <b>%s</b> on %s port %d" % ( \
con.process_path,
con.dst_host or con.dst_ip,
con.protocol,
con.dst_port )
else:
message = "The process <b>%s</b> running on the computer <b>%s</b> is connecting to <b>%s</b> on %s port %d" % ( \
con.process_path,
self._peer.split(':')[1],
con.dst_host or con.dst_ip,
con.protocol,
con.dst_port )
self._message_label.setText(message)
self._src_ip_label.setText(con.src_ip)
self._dst_ip_label.setText(con.dst_ip)
self._dst_port_label.setText("%s" % con.dst_port)
self._dst_host_label.setText(con.dst_host)
self._uid_label.setText("%d (%s)" % (con.user_id, pwd.getpwuid(con.user_id).pw_name))
if self._local:
uid = "%d (%s)" % (con.user_id, pwd.getpwuid(con.user_id).pw_name)
else:
uid = "%d" % con.user_id
self._uid_label.setText(uid)
self._pid_label.setText("%s" % con.process_id)
self._args_label.setText(' '.join(con.process_args))

View file

@ -23,6 +23,7 @@ class StatsDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
self.daemon_connected = False
self._address = address
self._stats = None
self._trigger.connect(self._on_update_triggered)
@ -107,8 +108,11 @@ class StatsDialog(QtWidgets.QDialog, uic.loadUiType(DIALOG_UI_PATH)[0]):
self._udp_label.setText("%s" % self._stats.by_proto['udp'] or 0)
by_users = {}
for uid, hits in self._stats.by_uid.iteritems():
by_users["%s (%s)" % (pwd.getpwuid(int(uid)).pw_name, uid)] = hits
if self._address is None:
for uid, hits in self._stats.by_uid.iteritems():
by_users["%s (%s)" % (pwd.getpwuid(int(uid)).pw_name, uid)] = hits
else:
by_users = self._stats.by_uid
self._render_table(self._addrs_table, self._stats.by_address)
self._render_table(self._hosts_table, self._stats.by_host)

View file

@ -189,7 +189,7 @@ class UIService(ui_pb2_grpc.UIServicer, QtWidgets.QGraphicsObject):
def AskRule(self, request, context):
self._asking = True
rule = self._prompt_dialog.promptUser(request)
rule = self._prompt_dialog.promptUser(request, self._is_local_request(context), context.peer())
self._last_ping = datetime.now()
self._asking = False
return rule