using DNS.Protocol; using System; using System.Net; using System.Net.Sockets; namespace FastGithub.Dns { /// /// 远程请求 /// sealed class RemoteRequest : Request { /// /// 获取远程地址 /// public EndPoint RemoteEndPoint { get; } /// /// 远程请求 /// /// /// public RemoteRequest(Request request, EndPoint remoteEndPoint) : base(request) { this.RemoteEndPoint = remoteEndPoint; } /// /// 获取对应的本机地址 /// /// public IPAddress? GetLocalAddress() { try { using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.Connect(this.RemoteEndPoint); return socket.LocalEndPoint is IPEndPoint localEndPoint ? localEndPoint.Address : default; } catch (Exception) { return default; } } } }