From 341f08329dcb49ae1b85d0baaba0212b5ee8e151 Mon Sep 17 00:00:00 2001 From: MonoLogueChi Date: Wed, 7 Aug 2019 11:45:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9IPV6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Danmaku/Model/DanmakuModel.cs | 5 +-- Danmaku/Utils/Ip.cs | 61 ++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Danmaku/Model/DanmakuModel.cs b/Danmaku/Model/DanmakuModel.cs index 77d421c..34fbe53 100644 --- a/Danmaku/Model/DanmakuModel.cs +++ b/Danmaku/Model/DanmakuModel.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using System.Numerics; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; @@ -9,7 +10,7 @@ public class DanmakuModel : DanmakuGet { [MaxLength(32)] public string Id { get; set; } - public long Ip { get; set; } + public BigInteger Ip { get; set; } public string Referer { get; set; } public DateTime Date { get; set; } = DateTime.Now; } @@ -48,7 +49,7 @@ public DanmakuInsert(DanmakuModel danmaku) public object[] Data { get; set; } - public long Ip { get; set; } + public BigInteger Ip { get; set; } public string Referer { get; set; } public long Date { get; set; } diff --git a/Danmaku/Utils/Ip.cs b/Danmaku/Utils/Ip.cs index c53ae7a..e11715e 100644 --- a/Danmaku/Utils/Ip.cs +++ b/Danmaku/Utils/Ip.cs @@ -1,38 +1,39 @@ -using System.Text; +// Copyright @ 2019 矿冶园 +// Product: 实验室安全手机版 +// Subsystem: Danmaku +// Author: 迟竞雷 +// Time: 2019-07-24 8:54 +// Description: + +using System.Net; +using System.Numerics; namespace Danmaku.Utils { public class Ip - { - /// - /// Ip转化为10进制 - /// - /// 点分隔的ip - /// 十进制ip - public static long Ip2Long(string ip) - { - if (string.IsNullOrWhiteSpace(ip)) return 0; - char[] separator = {'.'}; - var items = ip.Split(separator); - return (long.Parse(items[0]) << 24) - | (long.Parse(items[1]) << 16) - | (long.Parse(items[2]) << 8) - | long.Parse(items[3]); - } - - /// - /// 十进制ip转换为字符串 - /// - /// 十进制ip - /// 字符串ip - public static string Long2Ip(long ipInt) + { + /// + /// Ip转化为10进制 + /// + /// 点分隔的ip + /// 十进制ip + public static BigInteger Ip2Long(string ip) + { + if (string.IsNullOrWhiteSpace(ip)) return 0; + if (IPAddress.TryParse(ip, out var a)) + return new BigInteger(a.GetAddressBytes()); + return 0; + } + + /// + /// 十进制ip转换为字符串 + /// + /// 十进制ip + /// 字符串ip + public static string Long2Ip(BigInteger ipInt) { - var sb = new StringBuilder(); - sb.Append((ipInt >> 24) & 0xFF).Append("."); - sb.Append((ipInt >> 16) & 0xFF).Append("."); - sb.Append((ipInt >> 8) & 0xFF).Append("."); - sb.Append(ipInt & 0xFF); - return sb.ToString(); + var a = ipInt.ToByteArray(); + return new IPAddress(a).ToString(); } } } \ No newline at end of file