Example #1
0
        /// <summary>
        /// Finds a valid IP address by scanning the network devices
        /// </summary>
        /// <returns></returns>
        public static string GetIp( IpType type )
        {
            var localIp = NoIp;

            var host = Dns.GetHostEntry( Dns.GetHostName() );
            foreach ( var ip in host.AddressList )
            {
                if ( ip.AddressFamily == AddressFamily.InterNetwork )
                {
                    if ( type == IpType.Local )
                    {
                        if ( IsLocalIpAddress( ip.ToString() ) )
                        {
                            localIp = ip.ToString();
                            return localIp;
                        }
                    }
                    else
                        localIp = ip.ToString();
                }
            }

            if ( localIp == NoIp )
                throw new InvalidOperationException( "The client was unable to detect an IP address or there is no active connection." );

            return localIp;
        }
Example #2
0
 public SourceResult(IpType ipType, string subnet, int subnetSize)
 {
     IpType     = ipType;
     Source     = "";
     Subnet     = subnet;
     SubnetSize = subnetSize;
 }
Example #3
0
        public string EncodeImageToBase64(IpType ipType, string image, string title)
        {
            string filePath = null;

            if (ipType == IpType.Patent)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Patent");
            }
            else if (ipType == IpType.Copyright)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Copyright");
            }
            else if (ipType == IpType.Trademark)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Trademark");
            }
            else if (ipType == IpType.Design)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Design");
            }

            if (image != null)
            {
                File.WriteAllBytes(filePath + "" + title + ".png", Convert.FromBase64String(image));
            }
            return(image);
        }
Example #4
0
        /// <summary>
        /// Finds a valid IP address by scanning the network devices
        /// </summary>
        /// <returns></returns>
        public static string GetIp(IpType type)
        {
            var localIp = NoIp;

            var host = Dns.GetHostEntry(Dns.GetHostName());

            foreach (var ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    if (type == IpType.Local)
                    {
                        if (IsLocalIpAddress(ip.ToString()))
                        {
                            localIp = ip.ToString();
                            return(localIp);
                        }
                    }
                    else
                    {
                        localIp = ip.ToString();
                    }
                }
            }

            if (localIp == NoIp)
            {
                throw new InvalidOperationException("The client was unable to detect an IP address or there is no active connection.");
            }

            return(localIp);
        }
Example #5
0
        //Image upload
        public async Task <Images> UploadedImage(IpType ipType, IFormFile image)
        {
            string filePath = null;

            if (ipType == IpType.Patent)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Patent");
            }
            else if (ipType == IpType.Copyright)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Copyright");
            }
            else if (ipType == IpType.Trademark)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Trademark");
            }
            else if (ipType == IpType.Design)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Images", "Design");
            }
            string fileName;
            Images images = new Images();

            try
            {
                var extension = "." + image.FileName.Split('.')[image.FileName.Split('.').Length - 1];
                fileName = image.FileName + extension;

                string uniqueFileName = null;

                if (image != null)
                {
                    string uploadsFolder = Path.Combine(_env.WebRootPath, filePath);
                    uniqueFileName = Guid.NewGuid().ToString() + "_" + image.FileName;
                    filePath       = Path.Combine(uploadsFolder, uniqueFileName);
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        image.CopyTo(fileStream);
                    }
                }

                images = new Images
                {
                    ImagePath = filePath,
                    Image     = image
                };
            }
            catch (Exception e)
            {
                //log error
                e.Message.ToString();
            }

            return(images);
        }
Example #6
0
        public async Task <Document> UploadFile(IpType ipType, IFormFile file)
        {
            var filePath = "";

            if (ipType == IpType.Patent)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Document", "Patent");
            }
            else if (ipType == IpType.Copyright)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Document", "Copyright");
            }
            else if (ipType == IpType.Trademark)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Document", "Trademark");
            }
            else if (ipType == IpType.Design)
            {
                filePath = Path.Combine(_env.ContentRootPath, "Files", "Document", "Design");
            }

            string   fileName;
            Document document = new Document();

            try
            {
                var extension = "." + file.FileName.Split('.')[file.FileName.Split('.').Length - 1];
                //fileName = DateTime.Now.Ticks + extension; //Create a new Name for the file due to security reasons.
                fileName = file.FileName + extension;
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                var path = Path.Combine(filePath, fileName);

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                document = new Document
                {
                    DocumentPath = path,
                    FileDocument = file
                };
            }
            catch (Exception e)
            {
                //log error
                e.Message.ToString();
            }

            return(document);
        }
Example #7
0
        /// <summary>
        /// 添加解析记录。
        /// </summary>
        /// <param name="type">记录类型</param>
        /// <param name="domain">域名或子域名</param>
        /// <param name="ip">公网ip</param>
        /// <returns>添加成功返回true,否则返回false。</returns>
        private bool AddRecord(IpType type, string domain, string ip)
        {
            try
            {
                ParseSubDomainAndLine(domain, out string subDomain, out string line);
                string pattern = @"^(\S*)\.(\S+)\.(\S+)$";
                Regex  regex   = new Regex(pattern);
                var    match   = regex.Match(subDomain);
                string domainName;
                string rr;
                if (match.Success)
                {
                    rr         = match.Groups[1].Value;
                    domainName = match.Groups[2].Value + "." + match.Groups[3].Value;
                }
                else
                {
                    rr         = "@";
                    domainName = subDomain;
                }

                var client = GetNewClient();
                AddDomainRecordRequest request = new AddDomainRecordRequest
                {
                    DomainName = domainName,
                    RR         = rr,
                    Type       = type.ToString(),
                    _Value     = ip,
                    TTL        = Options.Instance.TTL,
                    Line       = line
                };
                var response = client.GetAcsResponse(request);
                if (response.HttpResponse.isSuccess())
                {
                    Log.Print($"成功增加{ type.ToString() }记录{ domain }为{ ip }。");
                    return(true);
                }
                else
                {
                    Log.Print($"增加{ type.ToString() }记录{ domain }失败。");
                    return(false);
                }
            }
            catch (Exception e)
            {
                Log.Print($"增加{ type.ToString() }记录{ domain }时发生异常: { e }");
                return(false);
            }
        }
        public static string GetLocalIpAddress(IpType ipType)
        {
            string        ip      = string.Empty;
            StringBuilder sb      = new StringBuilder();
            IPHostEntry   ipEntry = Dns.GetHostEntry(Dns.GetHostName());

            IPAddress[] addr = addr = ipEntry.AddressList;
            switch (ipType)
            {
            case IpType.LocalAllIp:
                for (int i = 0; i < addr.Length; i++)
                {
                    sb.Append(addr[i].ToString());
                    sb.Append(",");
                }
                ip = sb.ToString().TrimEnd(',');
                break;

            case IpType.LocalFirstIp:
                for (int i = 0; i < addr.Length; i++)
                {
                    if (
                        addr[i].ToString().Length > 4 &&
                        addr[i].ToString() != "127.0.0.1" &&
                        addr[i].ToString().Substring(0, Math.Min(6, addr[i].ToString().Length)) != "0.0.0." &&
                        addr[i].AddressFamily.ToString() != "InterNetworkV6"
                        )
                    {
                        ip = addr[i].ToString();
                        break;
                    }
                }
                break;

            case IpType.SocketConnet:
                using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
                {
                    socket.Connect("8.8.8.8", 65530);
                    IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                    ip = endPoint.Address.ToString();
                }
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            return(ip);
        }
Example #9
0
        /// <summary>
        ///     判断字符串格式是否为指定类型的IP地址
        /// </summary>
        /// <param name="ip">需要判断的IP字符串</param>
        /// <param name="ipType">指定的IP类型</param>
        /// <returns>方法返回布尔值</returns>
        public static bool IsIp(this string ip, IpType ipType)
        {
            switch (ipType)
            {
            case IpType.A: return(Regex.IsMatch(ip, RegexPatterns.IpAPattern));

            case IpType.B: return(Regex.IsMatch(ip, RegexPatterns.IpBPattern));

            case IpType.C: return(Regex.IsMatch(ip, RegexPatterns.IpCPattern));

            case IpType.D: return(Regex.IsMatch(ip, RegexPatterns.IpDPattern));

            case IpType.E: return(Regex.IsMatch(ip, RegexPatterns.IpEPattern));

            default: return(false);
            }
        }
Example #10
0
        /// <summary>
        /// 获取当前记录。
        /// </summary>
        /// <param name="type">记录类型</param>
        /// <param name="rds">记录合集</param>
        /// <returns>有且仅有一条同类型记录时,返回该记录,否则返回null。</returns>
        private Record GetOldRecord(IpType type, IEnumerable <Record> rds)
        {
            Record res = null;
            string t   = type.ToString();

            foreach (var i in rds)
            {
                if (i.Type == t)
                {
                    if (res != null)
                    {
                        return(null);
                    }
                    else
                    {
                        res = i;
                    }
                }
            }

            return(res);
        }
Example #11
0
        /// <summary>
        /// Creates a new firewall source rule.
        /// </summary>
        /// <param name="ipType">The type of IP the firewall rule applies to.</param>
        /// <param name="protocol">The type of protocol the firewall rule applies
        /// to.</param>
        /// <param name="ports">The ports affected by the firewall rule.</param>
        /// <param name="source">The source that's allowed through the firewall
        /// rule.</param>
        /// <exception cref="ArgumentNullException">If any arguments are null or
        /// empty.</exception>
        public FirewallRule(
            IpType ipType, Protocol protocol, string ports, string source)
        {
            if (string.IsNullOrWhiteSpace(ports))
            {
                throw new ArgumentNullException(
                          nameof(ports), "Firewall rule ports must not be empty");
            }

            if (string.IsNullOrWhiteSpace(source))
            {
                throw new ArgumentNullException(
                          nameof(source), "Firewall rule source must not be empty");
            }

            IpType     = ipType;
            Ports      = ports;
            Protocol   = protocol;
            Source     = source;
            Subnet     = "0.0.0.0";
            SubnetSize = 0;
        }
Example #12
0
        /// <summary>
        /// 获取IP地址,本机IP地址hostname=dns.gethostname(),返回一个IP list
        /// </summary>
        /// <param name="hostname">hostname</param>
        /// <returns>返回一个字符串类型的ip list</returns>
        public static List <string> getIP(string hostname, IpType iptype)
        {
            List <string> iplist = new List <string>();

            System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostname);//会返回所有地址,包括IPv4和IPv6
            foreach (IPAddress ip in addressList)
            {
                if (iptype == IpType.IPV4)
                {
                    if (ip.AddressFamily.ToString() == "InterNetwork")
                    {
                        iplist.Add(ip.ToString());
                    }
                }
                if (iptype == IpType.IPV6)
                {
                    if (ip.AddressFamily.ToString() == "InterNetworkV6")
                    {
                        iplist.Add(ip.ToString());
                    }
                }
            }
            return(iplist);
        }
Example #13
0
        public static async Task <string> GetIp(IpType type)
        {
            var isInternetUp = CheckIpUp(@"8.8.8.8");

            if (!isInternetUp)
            {
                isInternetUp = CheckIpUp(@"8.8.4.4");
            }
            if (isInternetUp)
            {
                switch (type)
                {
                case IpType.Internet:
                    return(CallRemoteApi("https://api.ipify.org"));

                case IpType.Ovh:
                    return(await CallOvhAsync());

                default:
                    return(null);
                }
            }
            return("No internet");
        }
Example #14
0
 /// <summary>
 /// 获取公网IP。
 /// </summary>
 /// <param name="type">IP类型</param>
 /// <returns>IP字符串</returns>
 private string GetIp(IpType type)
 {
     return(type == IpType.A ? PublicIpGetter.GetIpv4() : PublicIpGetter.GetIpv6());
 }
Example #15
0
 //设置IP
 /// <summary>
 /// 0x0018 DWORD 服务器TCP 端口
 /// 0x0013 STRING 主服务器地址,IP 或域名
 /// </summary>
 /// <param name="apn"></param>
 /// <param name="type"></param>
 /// <param name="ips"></param>
 /// <param name="port"></param>
 /// <param name="span"></param>
 /// <returns></returns>
 public override byte[] SetNetConfigure(APNType apn, IpType type, byte[] ips, int port, int span)
 {
     uint ipId = 0x0018;
     uint portId = 0x0042;
     if (IpType.Udp == type)
     {
         portId = 0x0019;
     }
     string ip = string.Format("{0}.{1}.{2}.{3}", ips[0], ips[1], ips[2], ips[3]);
     var ipBytes = Bit.StringToBytes(ip);
     uint nport = Convert.ToUInt32(port);
     var portBytes = BitConverter.GetBytes(Endian.SwapUInt32(nport));
     var p = new TerminalParam(ipId, (byte)ipBytes.Length, ipBytes);
     var p2 = new TerminalParam(portId, (byte)portBytes.Length, portBytes);
     var list = new List<TerminalParam>();
     list.Add(p);
     list.Add(p2);
     return this.SetParamMessageByList(list);
 }
Example #16
0
 /// <summary>
 /// 下发指令使终端升级
 /// </summary>
 /// <param name="apn"></param>
 /// <param name="iptype"></param>
 /// <param name="ip"></param>
 /// <param name="prot"></param>
 /// <param name="version"></param>
 /// <returns></returns>
 public override byte[] SetDownload(APNType apn, IpType iptype, byte[] ip, int prot, string version)
 {
 }