Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复地址模型如果设置的是域名则解析Endpoint失败的bug #322

Merged
merged 1 commit into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/Surging.Core/Surging.Core.CPlatform/Address/AddressHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Surging.Core.CPlatform.Address
{
public class AddressHelper
{
public static string GetIpFromAddress(string address)
{
if (IsValidIp(address))
{
return address;
}
var ips = Dns.GetHostAddresses(address);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

会不会存在 拿不到 2333 如果拿不到 是不是应该throw

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetHostAddresses拿不到就直接throw了

return ips[0].ToString();
}

public static bool IsValidIp(string address)
{
if (Regex.IsMatch(address, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
{
string[] ips = address.Split('.');
if (ips.Length == 4 || ips.Length == 6)
{
if (int.Parse(ips[0]) < 256 && int.Parse(ips[1]) < 256 && int.Parse(ips[2]) < 256 && int.Parse(ips[3]) < 256)
{
return true;
}
}
return false;
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public IpAddressModel(string ip, int port)
/// <returns></returns>
public override EndPoint CreateEndPoint()
{
return new IPEndPoint(IPAddress.Parse(Ip), Port);
return new IPEndPoint(IPAddress.Parse(AddressHelper.GetIpFromAddress(Ip)), Port);
}


public override string ToString()
{
return string.Concat(new string[] {Ip,":" , Port.ToString() });
return string.Concat(new string[] { AddressHelper.GetIpFromAddress(Ip), ":" , Port.ToString() });
}

#endregion Overrides of AddressModel
Expand Down