using FirewallWindowsServer.Model;
|
using Helpers;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Net.Sockets;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
|
namespace FirewallWindowsServer.Methods
|
{
|
public class ListenPort
|
{
|
|
FirewallManage fw = new FirewallManage();//防火墙实例
|
|
public Dictionary<string, Socket> sckdic = new Dictionary<string, Socket>(); //连接列队
|
string hostip = "";
|
int count = 0;
|
|
public ListenPort()
|
{
|
#region 获取ip地址
|
var config = Tool.GetSystemConfig<Config>("Config.json");
|
var localIpList = Dns.GetHostEntry(config.Host).AddressList;
|
if (localIpList != null && localIpList.Count() > 0)
|
{
|
foreach (var a in localIpList)
|
{
|
hostip = $"{a}";
|
}
|
|
if (!string.IsNullOrWhiteSpace(hostip))
|
{
|
hostip = hostip.Remove(hostip.Length - 1);
|
}
|
}
|
else
|
{
|
hostip = $"{config.Host}";
|
}
|
#endregion
|
}
|
|
public void InitListen(int port)
|
{
|
IPAddress ipaddr = IPAddress.Parse(hostip);
|
IPEndPoint IPEP = new IPEndPoint(ipaddr, port);
|
Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
sk.Bind(IPEP);
|
sk.Listen(100);
|
while (true)
|
{
|
Socket socket = sk.Accept();//fun;
|
var guid = socket.RemoteEndPoint.ToString();
|
sckdic.Add(guid, socket);
|
|
Task.Delay(500).Wait();
|
}
|
}
|
|
private Socket ListenFun(Socket sk)
|
{
|
Socket socket;
|
sk.Listen(0);
|
socket = sk.Accept();
|
//await Task.Delay(1);
|
return socket;
|
}
|
|
/// <summary>
|
/// 消息接收监听
|
/// </summary>
|
/// <param name="sck"></param>
|
/// <param name="guid"></param>
|
public async void ReceiveMsg(Socket sck, string guid)
|
{
|
await Task.Delay(1);
|
var res = await ReceiveMsgAwaitFun(sck, guid);
|
}
|
//收到消息
|
private async Task<int> ReceiveMsgAwaitFun(Socket sck, string guid)
|
{
|
while (true)
|
{
|
try
|
{
|
string recStr = "";
|
byte[] recByte = new byte[4096];
|
while (true)
|
{
|
int bytes = sck.Receive(recByte, recByte.Length, 0);
|
if (bytes > 0)
|
{
|
recStr = Encoding.UTF8.GetString(recByte, 0, bytes);
|
Command.PrintLog(recStr);
|
try
|
{
|
if (recStr.IndexOf("PROXY ") == 0)
|
{
|
var ss = recStr.Split(' ');
|
var ip = ss[2];
|
Command.PrintLog(ip);
|
fw.SetBlackList(ip); //将IP加入到防火墙名单中
|
sck.Close();
|
}
|
}
|
catch (Exception ex)
|
{
|
Command.PrintLog(ex);
|
}
|
}
|
await Task.Delay(500);
|
}
|
}
|
catch
|
{
|
sckdic.Remove(guid);
|
Console.WriteLine("【{0}】连接已断开", guid);
|
return 0;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 发送消息
|
/// </summary>
|
/// <param name="sck"></param>
|
/// <param name="msg"></param>
|
/// <returns></returns>
|
public bool SendMsg(Socket sck, /*Models.Message msg*/string msg)
|
{
|
try
|
{
|
//string sendStr = Newtonsoft.Json.JsonConvert.SerializeObject(msg);
|
//byte[] sendByte = Encoding.UTF8.GetBytes(sendStr);
|
byte[] sendByte = Encoding.UTF8.GetBytes(msg);
|
sck.Send(sendByte, sendByte.Length, 0);
|
return true;
|
}
|
catch
|
{
|
if (count % 10 == 0)
|
{
|
count = 1;
|
return false;
|
}
|
else
|
{
|
return SendMsg(sck, msg);
|
}
|
}
|
}
|
}
|
}
|