//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace TouchSocket.Sockets
{
///
/// IP解析映射
///
/// 支持端口,ip,域名等。具体格式如下:
///
/// - 端口:直接按入参,该操作一般在监听时使用。
/// - ip:按127.0.0.1:7789入参。
/// - 域名:按tcp://127.0.0.1:7789、或者http://baidu.com入参。
///
///
///
public class IPHost
{
///
/// IP解析映射
///
/// 支持端口,ip,域名等。具体格式如下:
///
/// - 端口:直接按入参,该操作一般在监听时使用。
/// - ip:按127.0.0.1:7789入参。
/// - 域名:按tcp://127.0.0.1:7789、或者http://baidu.com入参。
///
///
///
///
public IPHost(string host)
{
if (TouchSocketUtility.IsURL(host))
{
IsUri = true;
Uri = new Uri(host);
if (Uri.Port > 0)
{
Host = $"{Uri.Host}:{Uri.Port}";
}
else
{
Host = Uri.Host;
}
if (TouchSocketUtility.IsIPv4(Uri.Host) || TouchSocketUtility.IsIPV6(Uri.Host))
{
Analysis(Uri.Host, Uri.Port.ToString());
}
else
{
if (HostNameToIP(Uri.Host, out IPAddress[] addresses))
{
Analysis(addresses[0].ToString(), Uri.Port.ToString());
}
}
}
else
{
Host = host;
int r = host.LastIndexOf(":");
string ip = host.Substring(0, r);
Analysis(ip, host.Substring(r + 1, host.Length - (r + 1)));
}
}
///
/// 从IPAddress和端口号
///
///
///
public IPHost(IPAddress iPAddress, int port) : this($"{iPAddress}:{port}")
{
}
///
/// 从端口号创建。
///
///
public IPHost(int port) : this($"0.0.0.0:{port}")
{
}
///
/// 寻址方案
///
public AddressFamily AddressFamily { get; private set; }
///
/// 终结点
///
public IPEndPoint EndPoint { get; private set; }
///
/// 具有端口信息的host
///
public string Host { get; private set; }
///
/// IP
///
public string IP { get; private set; }
///
/// 是否为Uri
///
public bool IsUri { get; private set; }
///
/// 端口号
///
public int Port { get; private set; }
///
/// 统一资源标识
///
public Uri Uri { get; private set; }
///
/// 解析一个组的地址。
///
///
///
public static IPHost[] ParseIPHosts(string[] strs)
{
List iPs = new List();
foreach (var item in strs)
{
iPs.Add(new IPHost(item));
}
return iPs.ToArray();
}
///
/// 获取Url全路径
///
///
public string GetUrlPath()
{
if (IsUri)
{
return Uri.PathAndQuery;
}
return default;
}
///
/// 返回EndPoint字符串
///
///
public override string ToString()
{
return EndPoint == null ? null : EndPoint.ToString();
}
private static bool HostNameToIP(string hostname, out IPAddress[] address)
{
try
{
IPHostEntry hostInfo = Dns.GetHostEntry(hostname);
address = hostInfo.AddressList;
if (address.Length > 0)
{
return true;
}
return false;
}
catch
{
address = null;
return false;
}
}
private void Analysis(string ip, string port)
{
try
{
int portNum = int.Parse(port);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), portNum);
if (ip.Contains(":"))
{
AddressFamily = AddressFamily.InterNetworkV6;
}
else
{
AddressFamily = AddressFamily.InterNetwork;
}
EndPoint = endPoint;
IP = ip;
Port = portNum;
}
catch (Exception ex)
{
throw new Exception($"IPHost初始化失败,信息:{ex.Message}", ex);
}
}
}
}