Exemplo n.º 1
0
        public int ReadFromPayload(byte[] data, int offset = 0)
        {
            Array.Copy(data, offset, HashBytes, 0, HashBytes.Length);
            HashBytes = HashBytes.Reverse().ToArray();

            return(Size);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 根据域名获取IP地址
        /// </summary>
        /// <param name="domain">域名</param>
        /// <returns>IP地址,失败返回null</returns>
        internal unsafe static                                                 IPAddress[] Get(ref SubArray <byte> domain)
        {
            try
            {
                fixed(byte *domainFixed = domain.Array)
                {
                    byte *domainStart = domainFixed + domain.StartIndex;

                    AutoCSer.Memory.ToLowerNotNull(domainStart, domainStart + domain.Length);
                    HashBytes       key = domain;
                    DomainIPAddress value;

                    Monitor.Enter(domainIpLock);
                    try
                    {
                        value = domainIps.Get(ref key, default(DomainIPAddress));
                        if (value.Ips != null && value.Timeout < AutoCSer.Date.NowTime.Now)
                        {
                            domainIps.Remove(ref key, out value);
                            value.Ips = null;
                        }
                    }
                    finally { Monitor.Exit(domainIpLock); }
                    if (value.Ips == null)
                    {
                        if (value.Domain == null)
                        {
                            value.Domain = Memory_WebClient.BytesToStringNotEmpty(domainStart, domain.Length);
                        }
                        IPAddress ip;
                        if (IPAddress.TryParse(value.Domain, out ip))
                        {
                            value.Timeout = DateTime.MaxValue;
                            value.Domain  = null;
                            setDomainIp(key.Copy(), ref value);
                            return(value.Ips = new IPAddress[] { ip });
                        }
                        value.Ips = Dns.GetHostEntry(value.Domain).AddressList;
                        if (value.Ips.Length != 0)
                        {
                            value.Timeout = AutoCSer.Date.NowTime.Now.AddTicks(domainIpTimeoutTicks);
                            setDomainIp(key.Copy(), ref value);
                            return(value.Ips);
                        }
                    }
                    else
                    {
                        return(value.Ips);
                    }
                }
            }
            catch (Exception error)
            {
                AutoCSer.Log.Pub.Log.add(AutoCSer.Log.LogType.Error, error);
            }
            return(null);
        }
Exemplo n.º 3
0
 /// <summary>
 /// 设置域名转换IP地址
 /// </summary>
 /// <param name="key">域名</param>
 /// <param name="ipAddress">IP地址</param>
 private static void setDomainIp(HashBytes key, ref DomainIPAddress ipAddress)
 {
     Monitor.Enter(domainIpLock);
     try
     {
         domainIps.Set(ref key, ipAddress);
         if (domainIps.Count == DomainIPAddressConfig.Default.CacheCount)
         {
             domainIps.UnsafePopNode();
         }
     }
     finally { Monitor.Exit(domainIpLock); }
 }
Exemplo n.º 4
0
        internal ulong GetCacheIndex(HashBytes hashData)
        {
            Dictionary <HashBytes, DataCache> cache = DataCache[hashData.HashCode & 0xff];

            if (cache != null)
            {
                DataCache data;
                if (cache.TryGetValue(hashData, out data))
                {
                    return(index64 + (ulong)data.Index);
                }
            }
            return(0);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 设置证书信息
        /// </summary>
        /// <param name="domain">域名信息</param>
        /// <returns></returns>
        internal bool SetCertificate(Domain domain)
        {
            string         domainString   = domain.DomainData.toStringNotNull();
            SslCertificate sslCertificate = HttpRegister.Server.Config.GetCertificate(domain, domainString, RegisterServer.TcpServer.Log);

            if (sslCertificate == null)
            {
                RegisterServer.TcpServer.Log.Add(AutoCSer.Log.LogType.Error, "安全证书获取失败 " + domainString);
                return(false);
            }
            HashBytes domainKey;
            int       portIndex = domainString.IndexOf(':');

            if (portIndex != -1)
            {
                domainString = domainString.Substring(0, portIndex);
                domainKey    = domainString.getBytes();
            }
            else
            {
                domainKey = domain.DomainData;
            }
            Monitor.Enter(certificateLock);
            try
            {
                if (certificate == null)
                {
                    certificate       = sslCertificate;
                    certificateDomain = domainKey;
                }
                else
                {
                    certificates = DictionaryCreator.CreateHashBytes <SslCertificate>();
                    certificates.Add(certificateDomain, certificate);
                    certificates[domainKey] = sslCertificate;
                }
            }
            finally { Monitor.Exit(certificateLock); }
            return(true);
        }
Exemplo n.º 6
0
 public override string ToString() => HashBytes.ToHex();
Exemplo n.º 7
0
 /// <summary>
 /// 设置缓存数据
 /// </summary>
 /// <param name="index"></param>
 /// <param name="data"></param>
 internal void SetCache(long index, byte[] data)
 {
     if (DataCache == null)
     {
         IndexCache.UnsafeAdd(ref index, new HashBytes {
             SubArray = new SubArray <byte> {
                 Array = data
             }
         });
         if ((freeCacheSize -= data.Length) < 0)
         {
             HashBytes removeData;
             do
             {
                 removeData = IndexCache.UnsafePopValue();
             }while ((freeCacheSize += removeData.SubArray.Array.Length) < 0);
         }
     }
     else
     {
         HashBytes hashData = data;
         Dictionary <HashBytes, DataCache> cache = DataCache[hashData.HashCode & 0xff];
         if (cache == null)
         {
             IndexCache.UnsafeAdd(ref index, hashData);
             freeCacheSize -= data.Length;
             DataCache[hashData.HashCode & 0xff] = cache = DictionaryCreator.CreateHashBytes <DataCache>();
             cache.Add(hashData, new DataCache {
                 Data = hashData, Index = index, Count = 1
             });
         }
         else
         {
             DataCache count;
             if (cache.TryGetValue(hashData, out count))
             {
                 IndexCache.UnsafeAdd(ref index, count.Data);
                 ++count.Count;
                 freeCacheSize  -= data.Length;
                 cache[hashData] = count;
             }
             else
             {
                 IndexCache.UnsafeAdd(ref index, hashData);
                 freeCacheSize -= data.Length;
                 cache.Add(hashData, new DataCache {
                     Data = hashData, Index = index, Count = 1
                 });
             }
         }
         DataCache removeData;
         while (freeCacheSize < 0)
         {
             removeData.Data = IndexCache.UnsafePopValue();
             freeCacheSize  += removeData.Data.SubArray.Array.Length;
             cache           = DataCache[removeData.Data.HashCode & 0xff];
             if (cache != null && cache.TryGetValue(removeData.Data, out removeData))
             {
                 if (--removeData.Count == 0)
                 {
                     cache.Remove(removeData.Data);
                 }
                 else
                 {
                     cache[removeData.Data] = removeData;
                 }
             }
         }
     }
 }
Exemplo n.º 8
0
 public static HashBytes Copy(this HashBytes value)
 {
     return(new HashBytes {
         SubArray = new SubArray <byte>(value.SubArray.GetArray()), HashCode = value.HashCode
     });
 }