forked from Elfocrash/L2dotNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkBlock.cs
More file actions
127 lines (108 loc) · 3.77 KB
/
Copy pathNetworkBlock.cs
File metadata and controls
127 lines (108 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using L2dotNET.Utility;
using NLog;
namespace L2dotNET.Network
{
public class NetworkBlock
{
private static readonly Logger Log = LogManager.GetCurrentClassLogger();
private static volatile NetworkBlock _instance;
private static readonly object SyncRoot = new object();
public static NetworkBlock Instance
{
get
{
if (_instance != null)
return _instance;
lock (SyncRoot)
{
if (_instance == null)
_instance = new NetworkBlock();
}
return _instance;
}
}
protected List<NetworkBlockModel> Blocks = new List<NetworkBlockModel>();
public void Initialize()
{
using (StreamReader reader = new StreamReader(new FileInfo(@"sq\blocks.txt").FullName))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine() ?? string.Empty;
if (line.Length == 0)
continue;
if (line.StartsWithIgnoreCase("//"))
continue;
if (line.StartsWithIgnoreCase("d"))
{
NetworkBlockModel nbModel = new NetworkBlockModel
{
DirectIp = line.Split(' ')[1],
Permanent = line.Split(' ')[2].EqualsIgnoreCase("0")
};
Blocks.Add(nbModel);
}
else
{
if (!line.StartsWithIgnoreCase("m"))
continue;
NetworkBlockModel nbModel = new NetworkBlockModel
{
Mask = line.Split(' ')[1],
Permanent = line.Split(' ')[2].EqualsIgnoreCase("0")
};
Blocks.Add(nbModel);
}
}
}
Log.Info($"{Blocks.Count} blocks.");
}
public bool Allowed(string ip)
{
if (Blocks.Count == 0)
return true;
foreach (NetworkBlockModel nbi in Blocks)
{
if (nbi.DirectIp?.Equals(ip) ?? false)
{
if (nbi.Permanent)
return false;
if (nbi.TimeEnd.CompareTo(DateTime.Now) == 1)
return false;
}
if (nbi.Mask == null)
continue;
string[] a = ip.Split('.'),
b = nbi.Mask.Split('.');
bool[] d = new bool[4];
for (int c = 0; c < 4; c++)
{
d[c] = false;
if (b[c] == "*")
d[c] = true;
else
{
if (b[c] == a[c])
d[c] = true;
else
{
if (!b[c].Contains("/"))
continue;
short n = short.Parse(b[c].Split('/')[0]),
x = short.Parse(b[c].Split('/')[1]);
short t = short.Parse(a[c]);
d[c] = (t >= n) && (t <= x);
}
}
}
if (d.Any(u => u))
return false;
}
return true;
}
}
}