csharp C#DNS转发器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#DNS转发器相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ARSoft.Tools;
using ARSoft;
using ARSoft.Tools.Net.Dns;
using DNSForwarderARSoft;
using ARSoft.Tools.Net;
using System.Net;
using System.Net.Sockets;
namespace DNSForwarderARSoft
{
class Program
{
static void Main(string[] args)
{
using (DnsServer server = new DnsServer(IPAddress.Any, 10, 10))
{
//server.
server.QueryReceived += OnQueryReceived;
server.Start();
Console.WriteLine("Press any key to stop server");
Console.ReadLine();
}
}
static async Task OnQueryReceived(object sender, QueryReceivedEventArgs e)
{
DnsMessage message = e.Query as DnsMessage;
if (message == null)
return;
DnsMessage response = message.CreateResponseInstance();
if ((message.Questions.Count == 1))
{
// send query to upstream server
DnsQuestion question = message.Questions[0];
//DnsMessage upstreamResponse = await DnsClient.Default.ResolveAsync(question.Name, question.RecordType, question.RecordClass);
IPAddress address = IPAddress.Parse("DNS Primary Forwarder");
DnsClient dnsClient = new DnsClient(address, 10);
DnsMessage upstreamResponse = await dnsClient.ResolveAsync(question.Name, question.RecordType, question.RecordClass);
// if got an answer, copy it to the message sent to the client
if (question != null)
{
foreach (DnsRecordBase record in (upstreamResponse.AnswerRecords))
{
//response.AnswerRecords.Add(record);
Console.WriteLine("AnswerRecords: " + record.ToString());
string dnsRecord = record.ToString();
string[] splitDNSRecord = dnsRecord.Split(' ');
if ((dnsRecord.ToUpper().Contains("RCH")) || dnsRecord.ToUpper().Contains("NLR") || dnsRecord.ToUpper().Contains("S-"))
{
Console.WriteLine("------------------ Client Request ------------------");
foreach (string splitRecord in splitDNSRecord)
{
if (CheckIPValid(splitRecord))
{
Console.WriteLine("valid IP: " + splitRecord);
if (CheckTCPConnection(splitRecord) == true)
{
Console.WriteLine("Connection true: " + splitRecord);
response.AnswerRecords.Add(record);
}
}
else
{
}
}
}
else
{
//Console.WriteLine("------------------ Server Request------------------");
response.AnswerRecords.Add(record);
}
}
response.ReturnCode = ReturnCode.NoError;
// set the response
e.Response = response;
}
}
}
public static bool CheckIPValid(string strIP)
{
// Split string by ".", check that array length is 3
char chrFullStop = '.';
string[] arrOctets = strIP.Split(chrFullStop);
if (arrOctets.Length != 4)
{
return false;
}
// Check each substring checking that the int value is less than 255 and that is char[] length is !> 2
Int16 MAXVALUE = 255;
Int32 temp; // Parse returns Int32
foreach (String strOctet in arrOctets)
{
if (strOctet.Length > 3)
{
return false;
}
temp = int.Parse(strOctet);
if (temp > MAXVALUE)
{
return false;
}
}
return true;
}
public static bool CheckTCPConnection(string IP)
{
bool open;
using (TcpClient tcpclient = new TcpClient())
{
var result = tcpclient.BeginConnect(IP, 135, null, null);
result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
if(tcpclient.Connected)
{
Console.WriteLine("Port open on ip: " + IP);
open = true;
}
else
{
Console.WriteLine("Port closed on ip: " + IP);
open = false;
}
}
return open;
}
}
}
以上是关于csharp C#DNS转发器的主要内容,如果未能解决你的问题,请参考以下文章
csharp C#でDozensのDNSレコードを操作するref:http://qiita.com/nakaji/items/008090c0fc8173afce53