From 78c0da83c0d7d5376a141a6e8b70d6887a857f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20I=C3=B1iguez=20Goia?= Date: Thu, 4 Jun 2020 00:38:11 +0200 Subject: [PATCH] increase default timeout to ask for a rule Explained here: https://github.com/gustavo-iniguez-goya/opensnitch/issues/28#issuecomment-637484501 --- daemon/main.go | 4 ++-- daemon/rule/rule.go | 3 +++ daemon/statistics/stats.go | 5 ++++- daemon/ui/client.go | 7 ++++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/daemon/main.go b/daemon/main.go index 25d146deef..c8b08d0afa 100644 --- a/daemon/main.go +++ b/daemon/main.go @@ -199,8 +199,8 @@ func acceptOrDeny(packet *netfilter.Packet, con *conman.Connection) *rule.Rule { // UI client if connected and running r, connected = uiClient.Ask(con) if r == nil { - log.Error("Invalid rule received, skipping") - packet.SetVerdict(netfilter.NF_DROP) + log.Error("Invalid rule received, applying default action") + applyDefaultAction(packet) return nil } if connected { diff --git a/daemon/rule/rule.go b/daemon/rule/rule.go index 5dc1d32a61..ccb6c68acc 100644 --- a/daemon/rule/rule.go +++ b/daemon/rule/rule.go @@ -83,6 +83,9 @@ func Deserialize(reply *protocol.Rule) *Rule { } func (r *Rule) Serialize() *protocol.Rule { + if r == nil { + return nil + } return &protocol.Rule{ Name: string(r.Name), Enabled: bool(r.Enabled), diff --git a/daemon/statistics/stats.go b/daemon/statistics/stats.go index 71fec2d03b..335f106443 100644 --- a/daemon/statistics/stats.go +++ b/daemon/statistics/stats.go @@ -134,7 +134,7 @@ func (s *Statistics) onConnection(con *conman.Connection, match *rule.Rule, wasM s.RuleHits++ } - if match.Action == rule.Allow { + if wasMissed == false && match.Action == rule.Allow { s.Accepted++ } else { s.Dropped++ @@ -155,6 +155,9 @@ func (s *Statistics) onConnection(con *conman.Connection, match *rule.Rule, wasM if nEvents == maxEvents { s.Events = s.Events[1:] } + if wasMissed { + return + } s.Events = append(s.Events, NewEvent(con, match)) } diff --git a/daemon/ui/client.go b/daemon/ui/client.go index 7391c60889..dd15ba541d 100644 --- a/daemon/ui/client.go +++ b/daemon/ui/client.go @@ -225,12 +225,13 @@ func (c *Client) Ask(con *conman.Connection) (*rule.Rule, bool) { c.Lock() defer c.Unlock() - ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) + // FIXME: if timeout is fired, the rule is not added to the list in the GUI + ctx, cancel := context.WithTimeout(context.Background(), time.Second*120) defer cancel() reply, err := c.client.AskRule(ctx, con.Serialize()) if err != nil { - log.Warning("Error while asking for rule: %s", err, con) - return clientErrorRule, false + log.Warning("Error while asking for rule: %s - %v", err, con) + return nil, false } return rule.Deserialize(reply), true