175 lines
4.8 KiB
C#
175 lines
4.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using EonaCat.Helpers.Extensions;
|
|
|
|
namespace EonaCat.Network
|
|
{
|
|
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
|
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
|
|
|
public class Helpers
|
|
{
|
|
private Helpers()
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Port scan
|
|
/// </summary>
|
|
/// <param name="IPPrefix">the c prefix of IP such as "192.168.0." </param>
|
|
/// <param name="DStart">Start of segment D such as 1.</param>
|
|
/// <param name="DEnd">The end of section D such as 255.</param>
|
|
/// <param name="port">Detected port.</param>
|
|
/// <param name="ConnectEvent">return of the detection result Pass in the IPAndPort terminal class object and the boolean status of whether it is turned on.</param>
|
|
public static void IPv4ScanPort(string IPPrefix, int DStart, int DEnd, int port, Action<IPAndPort, bool> ConnectEvent)
|
|
{
|
|
// Configure CallBack Event
|
|
ConnectedEvent = ConnectEvent;
|
|
|
|
// Check
|
|
if (!(IPv4Verify(IPPrefix + DStart) && IPv4Verify(IPPrefix + DEnd)))
|
|
{
|
|
throw new Exception("Wrong Scan Parameters");
|
|
}
|
|
|
|
if (DStart > DEnd)
|
|
{
|
|
int temp = DEnd;
|
|
|
|
DEnd = DStart;
|
|
|
|
DStart = temp;
|
|
}
|
|
|
|
// Init
|
|
scanThreads = new List<Thread>();
|
|
|
|
// Scan
|
|
for (int i = DStart; i <= DEnd; i++)
|
|
{
|
|
string ip = IPPrefix + i;
|
|
|
|
scanThreads.Add(new Thread(new ParameterizedThreadStart(ScanOne)));
|
|
|
|
if (scanThreads.Any())
|
|
{
|
|
scanThreads[scanThreads.Count - 1].Start(new IPAndPort(ip, port));
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Stop all port scanning threads
|
|
/// </summary>
|
|
public static void StopPortScan()
|
|
{
|
|
if (scanThreads == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (scanThreads.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (Thread t in scanThreads)
|
|
{
|
|
if (t == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
t.Abort();
|
|
}
|
|
|
|
scanThreads.Clear();
|
|
}
|
|
|
|
private static List<Thread> scanThreads;
|
|
private static Action<IPAndPort, bool> ConnectedEvent;
|
|
|
|
private static void ScanOne(object _para)
|
|
{
|
|
IPAndPort para = (IPAndPort)_para;
|
|
|
|
if (para.port == 0 || para.ip == null || para.ip == string.Empty)
|
|
{
|
|
NetworkHelper.Logger.Error("Wrong IP or Port");
|
|
return;
|
|
}
|
|
|
|
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
|
|
try
|
|
{
|
|
NetworkHelper.Logger.Debug(string.Format("try to connect {0}: ar {1}", para.ip, para.port));
|
|
socket.Connect(para.ip, para.port);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
NetworkHelper.Logger.Error(string.Format(" {0}: at {1} is close ,error msg :{2}", para.ip, para.port, e.Message));
|
|
}
|
|
finally
|
|
{
|
|
if (ConnectedEvent != null)
|
|
{
|
|
if (socket.Connected)
|
|
{
|
|
ConnectedEvent(para, true);
|
|
}
|
|
else
|
|
{
|
|
ConnectedEvent(para, false);
|
|
}
|
|
}
|
|
|
|
socket.Close();
|
|
socket = null;
|
|
}
|
|
}
|
|
|
|
public static bool IPv4Verify(string IP)
|
|
{
|
|
return Regex.IsMatch(IP, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the internal network IP
|
|
/// </summary>
|
|
/// <returns>The local ip.</returns>
|
|
public static string[] GetLocalIP()
|
|
{
|
|
string name = Dns.GetHostName();
|
|
|
|
List<string> result = new List<string>();
|
|
|
|
IPAddress[] iPs = Dns.GetHostAddresses(name);
|
|
|
|
foreach (IPAddress ip in iPs)
|
|
{
|
|
result.Add(ip.ToString());
|
|
}
|
|
|
|
return result.ToArray();
|
|
}
|
|
}
|
|
|
|
public class IPAndPort
|
|
{
|
|
public string ip;
|
|
|
|
public int port;
|
|
|
|
public IPAndPort(string _ip, int _port)
|
|
{
|
|
this.ip = _ip;
|
|
this.port = _port;
|
|
}
|
|
}
|
|
} |