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);
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        public void TestParseValidDnsStamp()
        {
            DnsStamp dnsStamp = DnsApi.Instance.ParseDnsStamp(VALID_DNS_STAMP_STR);

            Assert.IsNotNull(dnsStamp);
            Assert.AreEqual("127.0.0.1:443", dnsStamp.ServerAddress);
            Assert.AreEqual("example.com", dnsStamp.ProviderName);
            Assert.IsNull(dnsStamp.PublicKey);
            Assert.AreEqual(1, dnsStamp.Hashes.Count);
            Assert.AreEqual(
                AGDnsApi.ag_server_informal_properties.AGSIP_DNSSEC |
                AGDnsApi.ag_server_informal_properties.AGSIP_NO_LOG |
                AGDnsApi.ag_server_informal_properties.AGSIP_NO_FILTER, dnsStamp.Properties);
            Assert.AreEqual("/dns-query", dnsStamp.DoHPath);
            Assert.AreEqual("127.0.0.1:443", dnsStamp.ServerAddress);
            Assert.AreEqual(AGDnsApi.ag_stamp_proto_type.DOH, dnsStamp.ProtoType);
            Assert.AreEqual("127.0.0.1:443", dnsStamp.ServerAddress);
            Assert.AreEqual("https://example.com/dns-query", dnsStamp.PrettyUrl);
            Assert.AreEqual("https://example.com/dns-query", dnsStamp.PrettierUrl);

            dnsStamp = DnsApi.Instance.ParseDnsStamp(VALID_DNS_STAMP_STR_1);
            Assert.IsNotNull(dnsStamp);
            Assert.IsNotNull(dnsStamp.PublicKey);
            Assert.AreEqual(32, dnsStamp.PublicKey.Length);
        }
Beispiel #4
0
 private bool Equals(DnsStamp other)
 {
     return(ProtoType == other.ProtoType &&
            Equals(ServerAddress, other.ServerAddress) &&
            ProviderName == other.ProviderName &&
            DoHPath == other.DoHPath);
 }
        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);
            }
        }
Beispiel #7
0
        internal static DnsStamp FromNativeObject(AGDnsApi.ag_dns_stamp agDnsStampC)
        {
            DnsStamp dnsStamp = new DnsStamp();

            MarshalUtils.AllPtrsToStrings(agDnsStampC, dnsStamp);
            MarshalUtils.CopyFieldsToProperties(agDnsStampC, dnsStamp);
            return(dnsStamp);
        }
Beispiel #8
0
        public void TestParseValidDnsStamp()
        {
            DnsStamp dnsStamp = DnsApi.Instance.ParseDnsStamp(VALID_DNS_STAMP_STR);

            Assert.IsNotNull(dnsStamp);
            Assert.AreEqual("127.0.0.1:443", dnsStamp.ServerAddress);
            Assert.AreEqual("example.com", dnsStamp.ProviderName);
            Assert.AreEqual("/dns-query", dnsStamp.DoHPath);
            Assert.AreEqual("127.0.0.1:443", dnsStamp.ServerAddress);
            Assert.AreEqual(AGDnsApi.ag_proto_type.DOH, dnsStamp.ProtoType);
            Assert.AreEqual("127.0.0.1:443", dnsStamp.ServerAddress);
        }
Beispiel #9
0
        internal static DnsStamp FromNativeObject(AGDnsApi.ag_dns_stamp agDnsStampC)
        {
            byte[]        publicKey = MarshalUtils.AgBufferToBytes(agDnsStampC.server_public_key);
            List <byte[]> hashes    = MarshalUtils.AgListToList <MarshalUtils.ag_buffer, byte[]>(
                agDnsStampC.hashes,
                MarshalUtils.AgBufferToBytes);
            DnsStamp dnsStamp = new DnsStamp
            {
                PublicKey = publicKey,
                Hashes    = hashes
            };

            MarshalUtils.AllPtrsToStrings(agDnsStampC, dnsStamp);
            MarshalUtils.CopyFieldsToProperties(agDnsStampC, dnsStamp);
            return(dnsStamp);
        }
Beispiel #10
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>
 public DnsStamp ParseDnsStamp(string dnsStampStr)
 {
     lock (SYNC_ROOT)
     {
         try
         {
             LOG.InfoFormat("Parsing DNS stamp");
             DnsStamp dnsStamp = DnsUtils.ParseDnsStamp(dnsStampStr);
             LOG.InfoFormat("Parsing DNS stamp has been successfully completed");
             return(dnsStamp);
         }
         catch (Exception ex)
         {
             LOG.ErrorFormat("Parsing DNS stamp failed with an error", ex);
             return(null);
         }
     }
 }
Beispiel #11
0
 public static AGDnsApi.ag_dns_stamp ToNativeObject(
     DnsStamp dnsStamp,
     Queue <IntPtr> allocatedPointers)
 {
     MarshalUtils.ag_buffer publicKeyC = MarshalUtils.BytesToAgBuffer(dnsStamp.PublicKey);
     MarshalUtils.ag_list   hashesC    = MarshalUtils.ListToAgList(
         dnsStamp.Hashes,
         (x, y) => MarshalUtils.BytesToAgBuffer(x),
         allocatedPointers);
     AGDnsApi.ag_dns_stamp dnsStampС = new AGDnsApi.ag_dns_stamp
     {
         ProtoType         = dnsStamp.ProtoType,
         ServerAddress     = MarshalUtils.StringToPtr(dnsStamp.ServerAddress),
         ProviderName      = MarshalUtils.StringToPtr(dnsStamp.ProviderName),
         DoHPath           = MarshalUtils.StringToPtr(dnsStamp.DoHPath),
         server_public_key = publicKeyC,
         hashes            = hashesC,
         Properties        = dnsStamp.Properties
     };
     return(dnsStampС);
 }
Beispiel #12
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 #13
0
        public void TestParseInvalidDnsStamp()
        {
            DnsStamp dnsStamp = DnsApi.Instance.ParseDnsStamp(INVALID_DNS_STAMP_STR);

            Assert.IsNull(dnsStamp);
        }