private async Task BackgroundProcessing(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    var repository = scope.ServiceProvider.GetRequiredService <ITrapUsersRepository>();

                    if (await repository.HasNewUsers(_activeTrapUsers, stoppingToken))
                    {
                        _activeTrapUsers = await repository.GetUsers();

                        var ports = _activeTrapUsers.Select(usr => usr.Port).Distinct();
                        var users = new UserRegistry();
                        users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);

                        foreach (var trapUser in _activeTrapUsers)
                        {
                            IPrivacyProvider provider;
                            if (trapUser.EncryptionType == EncryptionType.DES)
                            {
                                provider = new DESPrivacyProvider(
                                    new OctetString(trapUser.Encryption),
                                    new MD5AuthenticationProvider(new OctetString(trapUser.Password)))
                                {
                                    EngineIds = new List <OctetString> {
                                        new OctetString(ByteTool.Convert(trapUser.EngineId))
                                    }
                                };
                            }
                            else
                            {
                                provider = new AESPrivacyProvider(
                                    new OctetString(trapUser.Encryption),
                                    new MD5AuthenticationProvider(new OctetString(trapUser.Password)))
                                {
                                    EngineIds = new List <OctetString> {
                                        new OctetString(ByteTool.Convert(trapUser.EngineId))
                                    }
                                };
                            }

                            users.Add(new OctetString(trapUser.UserName), provider);
                        }

                        var trap = new TrapV2MessageHandler();
                        trap.MessageReceived += TrapMessageReceived;
                        var trapv2Mapping = new HandlerMapping("v2,v3", "TRAPV2", trap);

                        //sudo snmptrap -v3 -e 0x090807060504030201 -l authPriv -u snmpro -a MD5 -A STrP@SSWRD -x DES -X STr0ngP@SSWRD 192.168.0.110:162 ''  1.3.6.1.4.1.8072.2.3.0.1 1.3.6.1.4.1.8072.2.3.2.1 s "Example Message"
                        var inform = new InformRequestMessageHandler();
                        inform.MessageReceived += InformMessageReceived;
                        var informMapping = new HandlerMapping("v2,v3", "INFORM", inform);

                        var membership = new ComposedMembershipProvider(new IMembershipProvider[]
                        {
                            new Version1MembershipProvider(new OctetString("public"), new OctetString("public")),
                            new Version2MembershipProvider(new OctetString("public"), new OctetString("public")),
                            new Version3MembershipProvider()
                        });

                        var handlerFactory  = new MessageHandlerFactory(new[] { trapv2Mapping, informMapping });
                        var pipelineFactory = new SnmpApplicationFactory(new ObjectStore(), membership, handlerFactory);

                        if (_engine?.Active ?? false)
                        {
                            _engine.Stop();
                            _engine.Dispose();
                        }

                        _engine = new SnmpEngine(pipelineFactory, new Listener {
                            Users = users
                        }, new EngineGroup());

                        foreach (var port in ports)
                        {
                            _engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, port));
                        }

                        _engine.Start();
                    }
                }

                await Task.Delay(5000, stoppingToken);
            }
        }
Esempio n. 2
0
 public void Close()
 {
     _engine.Stop();
     _engine.Dispose();
 }