예제 #1
0
        public ContraCoreClient(string hostname)
        {
            Task.Run(() => Read(hostname));

            OnConnected += Setup;

            OnConnected += () =>
            {
                Connected = true;
                Common.Logger.Info("Connected to ContraCore");
                ConnectionComplete.Set();
                OnStatusChange?.Invoke();
            };
            OnDisconnected += () =>
            {
                Connected          = false;
                GeneratingRules    = false;
                GeneratingOUI      = false;
                ReloadingBlacklist = false;
                ReloadingWhitelist = false;
                OnGenRulesChange?.Invoke();
                OnGenOUIChange?.Invoke();
                OnReloadBlacklistChange?.Invoke();
                OnReloadWhitelistChange?.Invoke();
                Common.Logger.Info("Disconnected from ContraCore");
                OnStatusChange?.Invoke();
            };
        }
예제 #2
0
 public async Task GenRules()
 {
     GeneratingRules = true;
     OnGenRulesChange?.Invoke();
     await Send("gen_rules");
 }
예제 #3
0
        private async void Read(string hostname)
        {
            while (true)
            {
                try
                {
                    _client = new TcpClient(hostname, 64417);
                    _stream = _client.GetStream();
                    _reader = new StreamReader(_stream);

                    if (!_connected)
                    {
                        OnConnected?.Invoke();
                    }
                    _connected = true;
                }
                catch (Exception e)
                {
                    Common.Logger.Warning("Failed to connect to server; retrying...", e: e,
                                          meta: new Fields {
                        { "Hostname", hostname }
                    });
                    Thread.Sleep(1000);

                    if (_connected)
                    {
                        OnDisconnected?.Invoke();
                    }
                    _connected = false;

                    continue;
                }

                try
                {
                    string line;
                    while ((line = (await _reader.ReadLineAsync()) !) != null)
                    {
                        string cmd = line.Contains(" ") ? line.Substring(0, line.IndexOf(" ", StringComparison.Ordinal)) : line;
                        string val = line.Substring(line.IndexOf(" ", StringComparison.Ordinal) + 1);

                        if (cmd != "query" && cmd != "gen_oui.gen_progress")
                        {
                            Common.Logger.Debug($"NetMgr <- {cmd}");
                        }

                        switch (cmd)
                        {
                        case "ping":
                            await Send("ping.pong");

                            break;

                        case "ping.pong":
                            _pongEvent.Release();
                            break;

                        case "get_cache.cache":
                            List <Log> logs = JsonConvert.DeserializeObject <List <Log> >(val);

                            await _cacheChannel.Writer.WriteAsync(logs);

                            break;

                        case "query":
                            Log log = JsonConvert.DeserializeObject <Log>(val);
                            Common.Logger.Debug($"Incoming: {log}");

                            _data.Enqueue(log);

                            OnNewLog?.Invoke(log);
                            break;

                        case "gen_rules.sources":
                        case "gen_rules.gen_progress":
                        case "gen_rules.save_progress":
                        case "gen_rules.saved_in":
                            OnGenRulesCallback?.Invoke(val);
                            break;

                        case "gen_rules.complete":
                            GeneratingRules = false;
                            OnGenRulesChange?.Invoke();
                            break;

                        case "gen_oui.gen_progress":
                            OnGenOUICallback?.Invoke(val);
                            break;

                        case "gen_oui.generated_in":
                            GeneratingOUI = false;
                            OnGenOUIChange?.Invoke();
                            break;

                        case "reload_config.complete":
                            _reloadConfigCompleteEvent.Release();
                            break;

                        case "reload_blacklist.reload_progress":
                        case "reload_blacklist.reloaded_in":
                            OnReloadBlacklistStatusCallback?.Invoke(val);
                            break;

                        case "reload_blacklist.contradb_offline":
                            ReloadingBlacklist = false;
                            OnReloadBlacklistErrorCallback?.Invoke(val);
                            OnReloadBlacklistChange?.Invoke();
                            break;

                        case "reload_blacklist.complete":
                            ReloadingBlacklist = false;
                            OnReloadBlacklistChange?.Invoke();
                            break;

                        case "reload_whitelist.complete":
                            ReloadingWhitelist = false;
                            OnReloadWhitelistChange?.Invoke();
                            break;

                        case "reload_whitelist.contradb_offline":
                        case "reload_whitelist.error":
                            ReloadingWhitelist = false;
                            OnReloadWhitelistErrorCallback?.Invoke(val);
                            OnReloadWhitelistChange?.Invoke();
                            break;

                        default:
                            Common.Logger.Error(
                                $"Unmatched command received from NetManagerClient: {cmd}",
                                new InvalidOperationException()
                                );
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    Common.Logger.Warning("Failed to read from server; reconnecting...", e, new Fields {
                        { "Hostname", hostname }
                    });
                    Thread.Sleep(1000);

                    OnDisconnected?.Invoke();
                }
            }
        }