Beispiel #1
0
        /// <summary>
        /// Gets the DNS stamp pretty url specified by <see cref="DnsStamp"/> object
        /// </summary>
        /// <param name="dnsStamp">DNS stamp object
        /// (<seealso cref="DnsStamp"/>)</param>
        /// <returns>DNS stamp as a string</returns>
        public static string GetDnsStampPrettyUrl(DnsStamp dnsStamp)
        {
            IntPtr         pPrettyUrl        = IntPtr.Zero;
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            try
            {
                AGDnsApi.ag_dns_stamp dnsStampC =
                    DnsApiConverter.ToNativeObject(dnsStamp, allocatedPointers);
                IntPtr pDnsStampC = MarshalUtils.StructureToPtr(dnsStampC, allocatedPointers);
                pPrettyUrl = AGDnsApi.ag_dns_stamp_pretty_url(pDnsStampC);
                string prettyUrl = MarshalUtils.PtrToString(pPrettyUrl);
                return(prettyUrl);
            }
            catch (Exception ex)
            {
                Logger.Verbose("Getting DNS stamp pretty url failed with an error {0}", ex);
                return(null);
            }
            finally
            {
                MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                AGDnsApi.ag_str_free(pPrettyUrl);
            }
        }
        public void TestCertificateVerificationEventConverter()
        {
            AGDnsApi.ag_certificate_verification_event coreArgsС   = new AGDnsApi.ag_certificate_verification_event();
            CertificateVerificationEventArgs           certificate = DnsApiConverter.FromNativeObject(coreArgsС);

            Assert.IsNotNull(certificate);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the DNS stamp string specified by <see cref="DnsStamp"/> object
        /// </summary>
        /// <param name="dnsStamp">DNS stamp object
        /// (<seealso cref="DnsStamp"/>)</param>
        /// <returns>DNS stamp as a string</returns>
        public static string GetDnsStampString(DnsStamp dnsStamp)
        {
            // Don't invoke "dnsStamp.ToString()" within this method to prevent infinite recursion
            Logger.Verbose("Start getting DNS stamp string from {0}",
                           dnsStamp.ServerAddress);
            IntPtr         pDnsStampString   = IntPtr.Zero;
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            try
            {
                AGDnsApi.ag_dns_stamp dnsStampC =
                    DnsApiConverter.ToNativeObject(dnsStamp, allocatedPointers);
                IntPtr pDnsStampC = MarshalUtils.StructureToPtr(dnsStampC, allocatedPointers);
                pDnsStampString = AGDnsApi.ag_dns_stamp_to_str(pDnsStampC);
                string dnsStampString = MarshalUtils.PtrToString(pDnsStampString);
                Logger.Verbose("Getting DNS stamp string has been successfully completed");
                return(dnsStampString);
            }
            catch (Exception ex)
            {
                Logger.Verbose("Getting DNS stamp string failed with an error {0}", ex);
                return(null);
            }
            finally
            {
                MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                AGDnsApi.ag_str_free(pDnsStampString);
            }
        }
        public void TestDnsRequestProcessedConverter()
        {
            AGDnsApi.ag_dns_request_processed_event dnsRequestNative = new AGDnsApi.ag_dns_request_processed_event();
            DnsRequestProcessedEventArgs            dnsRequest       = DnsApiConverter.FromNativeObject(dnsRequestNative);

            Assert.IsNotNull(dnsRequest);
        }
        public void TestDnsStampConverter()
        {
            DnsStamp dnsStamp = new DnsStamp
            {
                ProtoType     = new AGDnsApi.ag_stamp_proto_type(),
                ServerAddress = "addressTest",
                ProviderName  = "Nametest",
                DoHPath       = "DoHPathTest",
                Hashes        = new List <byte[]>
                {
                    new byte[] { 12, 34, 15 },
                    new byte[] { 10, 8, 16, 3 }
                },
                Properties = new AGDnsApi.ag_server_informal_properties()
            };
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            AGDnsApi.ag_dns_stamp stampNative = DnsApiConverter.ToNativeObject(dnsStamp, allocatedPointers);
            string address = MarshalUtils.PtrToString(stampNative.ServerAddress);

            Assert.AreEqual(address, dnsStamp.ServerAddress);

            DnsStamp dnsStampConverted = DnsApiConverter.FromNativeObject(stampNative);

            Assert.AreEqual(dnsStamp.ServerAddress, dnsStampConverted.ServerAddress);
            Assert.AreEqual(dnsStamp.Hashes.Count, dnsStampConverted.Hashes.Count);
            for (int i = 0; i < dnsStamp.Hashes.Count; i++)
            {
                Assert.IsTrue(CollectionUtils.CollectionsEquals(dnsStamp.Hashes[i], dnsStampConverted.Hashes[i]));
            }
            Assert.AreEqual(dnsStamp.Hashes.Capacity, dnsStampConverted.Hashes.Capacity);
        }
Beispiel #6
0
        /// <summary>
        /// Parses a specified DNS stamp string (<seealso cref="dnsStampStr"/>)
        /// </summary>
        /// <param name="dnsStampStr">DNS stamp string</param>
        /// <returns>DNS stamp as a <see cref="DnsStamp"/> instance</returns>
        internal static DnsStamp ParseDnsStamp(string dnsStampStr)
        {
            LOG.InfoFormat("Start parsing DNS stamp {0}", dnsStampStr);
            IntPtr pDnsStampResult = IntPtr.Zero;

            try
            {
                pDnsStampResult = AGDnsApi.ag_parse_dns_stamp(dnsStampStr);
                AGDnsApi.ag_parse_dns_stamp_result dnsStampResult =
                    MarshalUtils.PtrToStructure <AGDnsApi.ag_parse_dns_stamp_result>(pDnsStampResult);
                if (dnsStampResult.error != IntPtr.Zero)
                {
                    string error = MarshalUtils.PtrToString(dnsStampResult.error);
                    LOG.InfoFormat("Parsing DNS stamp {0} failed with an error {1}",
                                   dnsStampStr,
                                   error);
                    return(null);
                }

                LOG.Info("Parsing DNS stamp has been completed successfully");
                DnsStamp dnsStamp = DnsApiConverter.FromNativeObject(dnsStampResult.stamp);
                return(dnsStamp);
            }
            catch (Exception ex)
            {
                LOG.InfoException("Parsing DNS stamp failed with an error {0}", ex);
                return(null);
            }
            finally
            {
                AGDnsApi.ag_parse_dns_stamp_result_free(pDnsStampResult);
            }
        }
        public void TestCertificateVerificationCallbackConverter()
        {
            ICertificateVerificationCallback certificateVerificationCallback = new CertificateVerificationCallback();

            AGDnsApi.cbd_onCertificateVerification certificate =
                DnsApiConverter.ToNativeObject(certificateVerificationCallback);
            Assert.NotNull(certificate);
        }
        public void TestUpstreamOptionsConverter()
        {
            IDnsProxyServerCallbackConfiguration dnsCallback = new DnsProxyServerCallbackConfiguration();
            DnsProxySettings currentDnsProxySettings         = new DnsProxySettings();
            IDnsProxyServer  proxyServer = new DnsProxyServer.DnsProxyServer(currentDnsProxySettings, dnsCallback);

            AGDnsApi.AGDnsProxyServerCallbacks serverCallbackNative =
                DnsApiConverter.ToNativeObject(dnsCallback, proxyServer);
            Assert.NotNull(serverCallbackNative);
        }
Beispiel #9
0
        /// <summary>
        /// Gets the DNS proxy settings,
        /// according to the specified <see cref="pCurrentDnsProxySettings"/>
        /// </summary>
        /// <param name="pCurrentDnsProxySettings">DNS proxy settings
        /// (<seealso cref="Func{TResult}"/>)</param>
        /// <exception cref="InvalidOperationException">Thrown,
        /// if cannot get the DNS proxy settings via native method</exception>
        /// <returns>The <see cref="DnsProxySettings"/> object</returns>
        private static DnsProxySettings GetDnsProxySettings(IntPtr pCurrentDnsProxySettings)
        {
            Logger.Info("Get DNS proxy settings settings");
            if (pCurrentDnsProxySettings == IntPtr.Zero)
            {
                throw new InvalidOperationException("Cannot get the DNS proxy settings");
            }

            AGDnsApi.ag_dnsproxy_settings currentDnsProxySettingsC =
                MarshalUtils.PtrToStructure <AGDnsApi.ag_dnsproxy_settings>(pCurrentDnsProxySettings);
            DnsProxySettings currentDnsProxySettings = DnsApiConverter.FromNativeObject(currentDnsProxySettingsC);

            Logger.Info("Finished getting the DNS proxy settings");
            return(currentDnsProxySettings);
        }
Beispiel #10
0
        /// <summary>
        /// Starts the proxy server
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        public void Start()
        {
            lock (m_SyncRoot)
            {
                LOG.Info("Starting the DnsProxyServer");
                if (IsStarted)
                {
                    LOG.Info("DnsProxyServer is already started, doing nothing");
                    return;
                }

                Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();
                try
                {
                    AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC =
                        DnsApiConverter.ToNativeObject(m_DnsProxySettings, allocatedPointers);
                    m_callbackConfigurationC = DnsApiConverter.ToNativeObject(m_CallbackConfiguration, this);

                    IntPtr pDnsProxySettingsC = MarshalUtils.StructureToPtr(dnsProxySettingsC, allocatedPointers);
                    m_pCallbackConfigurationC = MarshalUtils.StructureToPtr(m_callbackConfigurationC);
                    m_pProxyServer            = AGDnsApi.ag_dnsproxy_init(
                        pDnsProxySettingsC,
                        m_pCallbackConfigurationC);
                    if (m_pProxyServer == IntPtr.Zero)
                    {
                        const string errorMessage = "Failed to start the DnsProxyServer due to an error";
                        throw new InvalidOperationException(errorMessage);
                    }

                    m_IsStarted = true;
                    LOG.Info("Finished starting the DnsProxyServer");
                }
                catch (Exception ex)
                {
                    Dispose();
                    throw new InvalidOperationException("error while starting the DnsProxyServer", ex);
                }
                finally
                {
                    MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                }
            }
        }
Beispiel #11
0
        /// <summary>
        /// Parses a specified DNS stamp string (<seealso cref="dnsStampStr"/>)
        /// into the <see cref="DnsStamp"/> object.
        /// </summary>
        /// <param name="dnsStampStr">DNS stamp string</param>
        /// <returns>DNS stamp as a <see cref="DnsStamp"/> instance or null if smth went wrong</returns>
        public static DnsStamp ParseDnsStamp(string dnsStampStr)
        {
            Logger.Info("Start parsing DNS stamp {0}", dnsStampStr);
            IntPtr ppError         = IntPtr.Zero;
            IntPtr pError          = IntPtr.Zero;
            IntPtr pDnsStampResult = IntPtr.Zero;

            try
            {
                ppError         = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
                pDnsStampResult = AGDnsApi.ag_dns_stamp_from_str(dnsStampStr, ppError);
                if (pDnsStampResult == IntPtr.Zero)
                {
                    pError = MarshalUtils.SafeReadIntPtr(ppError);
                    string error = MarshalUtils.PtrToString(pError);
                    Logger.Info("Parsing DNS stamp {0} failed with an error {1}",
                                dnsStampStr,
                                error);
                    return(null);
                }

                AGDnsApi.ag_dns_stamp dnsStampResult =
                    MarshalUtils.PtrToStructure <AGDnsApi.ag_dns_stamp>(pDnsStampResult);
                DnsStamp dnsStamp = DnsApiConverter.FromNativeObject(dnsStampResult);
                Logger.Info("Parsing DNS stamp has been completed successfully");
                return(dnsStamp);
            }
            catch (Exception ex)
            {
                Logger.Info("Parsing DNS stamp failed with an error {0}", ex);
                return(null);
            }
            finally
            {
                AGDnsApi.ag_dns_stamp_free(pDnsStampResult);
                AGDnsApi.ag_str_free(pError);
                MarshalUtils.SafeFreeHGlobal(ppError);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Checks if upstream is valid and available
        /// </summary>
        /// <param name="upstreamOptions">Upstream options
        /// (<seealso cref="UpstreamOptions"/>)</param>
        /// <param name="ipv6Available">Whether IPv6 is available (i.e., bootstrapper is allowed to make AAAA queries)</param>
        /// <param name="offline">Don't perform online upstream check</param>
        public static bool TestUpstream(UpstreamOptions upstreamOptions, bool ipv6Available, bool offline)
        {
            IntPtr         pUpstreamOptionsC = IntPtr.Zero;
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();
            IntPtr         pError            = IntPtr.Zero;

            try
            {
                Logger.Info("Start testing upstream {0}", upstreamOptions);
                CertificateVerificationCallback certificateVerificationCallback = new CertificateVerificationCallback();
                AGDnsApi.ag_upstream_options    upstreamOptionsC =
                    DnsApiConverter.ToNativeObject(upstreamOptions, allocatedPointers);
                AGDnsApi.cbd_onCertificateVerification testUpstreamCallbackC =
                    DnsApiConverter.ToNativeObject(certificateVerificationCallback);
                pUpstreamOptionsC = MarshalUtils.StructureToPtr(upstreamOptionsC);
                pError            = AGDnsApi.ag_test_upstream(pUpstreamOptionsC, ipv6Available, testUpstreamCallbackC, offline);
                string error = MarshalUtils.PtrToString(pError);
                if (string.IsNullOrEmpty(error))
                {
                    Logger.Info("Testing upstream has been completed successfully");
                    return(true);
                }

                Logger.Info("Testing upstream failed with an error {0}", error);
                return(false);
            }
            catch (Exception ex)
            {
                Logger.Info("Testing upstream failed with an error {0}", ex);
                return(false);
            }
            finally
            {
                AGDnsApi.ag_str_free(pError);
                MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                MarshalUtils.SafeFreeHGlobal(pUpstreamOptionsC);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Starts the proxy server
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown, if cannot starting the proxy server
        /// for any reason</exception>
        public void Start()
        {
            lock (m_SyncRoot)
            {
                Logger.Info("Starting the DnsProxyServer");
                if (IsStarted)
                {
                    Logger.Info("DnsProxyServer is already started, doing nothing");
                    return;
                }

                Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();
                IntPtr         ppOutMessage      = IntPtr.Zero;
                IntPtr         pOutMessage       = IntPtr.Zero;
                IntPtr         pOutResult        = IntPtr.Zero;
                try
                {
                    AGDnsApi.ag_dnsproxy_settings dnsProxySettingsC =
                        DnsApiConverter.ToNativeObject(m_DnsProxySettings, allocatedPointers);
                    m_callbackConfigurationC = DnsApiConverter.ToNativeObject(m_CallbackConfiguration, this);

                    IntPtr pDnsProxySettingsC = MarshalUtils.StructureToPtr(dnsProxySettingsC, allocatedPointers);
                    m_pCallbackConfigurationC = MarshalUtils.StructureToPtr(m_callbackConfigurationC);

                    pOutResult     = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
                    ppOutMessage   = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
                    m_pProxyServer = AGDnsApi.ag_dnsproxy_init(
                        pDnsProxySettingsC,
                        m_pCallbackConfigurationC,
                        pOutResult,
                        ppOutMessage);
                    AGDnsApi.ag_dnsproxy_init_result outResultEnum = AGDnsApi.ag_dnsproxy_init_result.AGDPIR_OK;
                    if (m_pProxyServer == IntPtr.Zero)
                    {
                        int?outResult = MarshalUtils.PtrToNullableInt(pOutResult);
                        if (outResult.HasValue)
                        {
                            outResultEnum = (AGDnsApi.ag_dnsproxy_init_result)outResult.Value;
                        }

                        pOutMessage = MarshalUtils.SafeReadIntPtr(ppOutMessage);
                        string outMessage   = MarshalUtils.PtrToString(pOutMessage);
                        string errorMessage = $"Failed to start the DnsProxyServer with the result {outResultEnum} and message {outMessage}";
                        throw new InvalidOperationException(errorMessage);
                    }

                    m_IsStarted = true;
                    Logger.Info("Finished starting the DnsProxyServer");
                }
                catch (Exception ex)
                {
                    Dispose();
                    throw new InvalidOperationException("error while starting the DnsProxyServer: ", ex);
                }
                finally
                {
                    MarshalUtils.SafeFreeHGlobal(allocatedPointers);
                    AGDnsApi.ag_str_free(pOutMessage);
                    MarshalUtils.SafeFreeHGlobal(ppOutMessage);
                    MarshalUtils.SafeFreeHGlobal(pOutResult);
                }
            }
        }
        public void TestDnsProxySettingsConverter()
        {
            DnsProxySettings dnsSettings = new DnsProxySettings
            {
                Upstreams = new List <UpstreamOptions>
                {
                    new UpstreamOptions
                    {
                        Id        = 1,
                        Bootstrap = new List <string>()
                    },
                    new UpstreamOptions
                    {
                        Id        = 2,
                        Bootstrap = new List <string>()
                    },
                    new UpstreamOptions
                    {
                        Id        = 3,
                        Bootstrap = new List <string>
                        {
                            "bootStrapBegin",
                            "bootStrapEnd"
                        }
                    }
                },
                Fallbacks = new List <UpstreamOptions>()
                {
                    new UpstreamOptions
                    {
                        Bootstrap = new List <string>()
                    }
                },
                BlockedResponseTtlSec = 64,
                Dns64 = new Dns64Settings
                {
                    Upstreams = new List <UpstreamOptions>()
                },
                EngineParams = new EngineParams
                {
                    FilterParams = new List <FilterParams>()
                },
                Listeners = new List <ListenerSettings>
                {
                    new ListenerSettings
                    {
                        EndPoint = new IPEndPoint(1234567, 9898)
                    }
                },
                FallbackDomains = new List <string>
                {
                    "Test.com"
                },
                Ipv6Available            = true,
                BlockIpv6                = true,
                AdblockRulesBlockingMode = AGDnsApi.ag_dnsproxy_blocking_mode.AGBM_ADDRESS,
                HostsRulesBlockingMode   = AGDnsApi.ag_dnsproxy_blocking_mode.AGBM_ADDRESS,
                CustomBlockingIpv4       = "1.2.3.4",
                CustomBlockingIpv6       = "::AA",
                DnsCacheSize             = 23,
                OptimisticCache          = true
            };
            Queue <IntPtr> allocatedPointers = new Queue <IntPtr>();

            AGDnsApi.ag_dnsproxy_settings nativeDnsSettings =
                DnsApiConverter.ToNativeObject(dnsSettings, allocatedPointers);
            Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.fallbacks.entries);
            Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.fallbackDomains.entries);
            Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.listeners.entries);
            Assert.AreNotEqual(IntPtr.Zero, nativeDnsSettings.upstreams.entries);
            Assert.AreEqual(nativeDnsSettings.BlockedResponseTtlSec, dnsSettings.BlockedResponseTtlSec);
            Assert.AreEqual(nativeDnsSettings.Ipv6Available, dnsSettings.Ipv6Available);
            Assert.AreEqual(nativeDnsSettings.BlockIpv6, dnsSettings.BlockIpv6);
            Assert.AreEqual(nativeDnsSettings.AdblockRulesBlockingMode, dnsSettings.AdblockRulesBlockingMode);
            Assert.AreEqual(nativeDnsSettings.HostsRulesBlockingMode, dnsSettings.HostsRulesBlockingMode);
            Assert.AreEqual(MarshalUtils.PtrToString(nativeDnsSettings.CustomBlockingIpv4), dnsSettings.CustomBlockingIpv4);
            Assert.AreEqual(MarshalUtils.PtrToString(nativeDnsSettings.CustomBlockingIpv6), dnsSettings.CustomBlockingIpv6);
            Assert.AreEqual(nativeDnsSettings.DnsCacheSize, dnsSettings.DnsCacheSize);
            Assert.AreEqual(nativeDnsSettings.OptimisticCache, dnsSettings.OptimisticCache);

            DnsProxySettings dnsSettingsConverted = DnsApiConverter.FromNativeObject(nativeDnsSettings);

            Assert.AreEqual(dnsSettings.FallbackDomains, dnsSettingsConverted.FallbackDomains);
            Assert.AreEqual(dnsSettings.BlockedResponseTtlSec, dnsSettingsConverted.BlockedResponseTtlSec);
            Assert.AreEqual(dnsSettings.CustomBlockingIpv4, dnsSettingsConverted.CustomBlockingIpv4);
            Assert.AreEqual(dnsSettings.CustomBlockingIpv6, dnsSettingsConverted.CustomBlockingIpv6);
            Assert.AreEqual(dnsSettings.AdblockRulesBlockingMode, dnsSettingsConverted.AdblockRulesBlockingMode);
            Assert.AreEqual(dnsSettings.HostsRulesBlockingMode, dnsSettingsConverted.HostsRulesBlockingMode);
            bool isUpstreamsEqual = CollectionUtils.CollectionsEquals(dnsSettingsConverted.Upstreams, dnsSettings.Upstreams);

            Assert.IsTrue(isUpstreamsEqual);
        }