Skip to content

Commit 37a4b6e

Browse files
committed
First working version that can actually block things
1 parent fc10c22 commit 37a4b6e

11 files changed

Lines changed: 869 additions & 5 deletions

File tree

AnyBlock/AnyBlock.csproj

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<ItemGroup>
36+
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
3640
<Reference Include="System" />
3741
<Reference Include="System.Core" />
42+
<Reference Include="System.Drawing" />
43+
<Reference Include="System.Windows.Forms" />
3844
<Reference Include="System.Xml.Linq" />
3945
<Reference Include="System.Data.DataSetExtensions" />
4046
<Reference Include="Microsoft.CSharp" />
@@ -43,11 +49,38 @@
4349
<Reference Include="System.Xml" />
4450
</ItemGroup>
4551
<ItemGroup>
52+
<Compile Include="Cache.cs" />
53+
<Compile Include="Ext.cs" />
54+
<Compile Include="Firewall.cs" />
55+
<Compile Include="frmMain.cs">
56+
<SubType>Form</SubType>
57+
</Compile>
58+
<Compile Include="frmMain.Designer.cs">
59+
<DependentUpon>frmMain.cs</DependentUpon>
60+
</Compile>
61+
<Compile Include="NativeMethods.cs" />
4662
<Compile Include="Program.cs" />
4763
<Compile Include="Properties\AssemblyInfo.cs" />
4864
</ItemGroup>
4965
<ItemGroup>
5066
<None Include="App.config" />
67+
<None Include="packages.config" />
68+
</ItemGroup>
69+
<ItemGroup>
70+
<EmbeddedResource Include="frmMain.resx">
71+
<DependentUpon>frmMain.cs</DependentUpon>
72+
</EmbeddedResource>
73+
</ItemGroup>
74+
<ItemGroup>
75+
<COMReference Include="NetFwTypeLib">
76+
<Guid>{58FBCF7C-E7A9-467C-80B3-FC65E8FCCA08}</Guid>
77+
<VersionMajor>1</VersionMajor>
78+
<VersionMinor>0</VersionMinor>
79+
<Lcid>0</Lcid>
80+
<WrapperTool>tlbimp</WrapperTool>
81+
<Isolated>False</Isolated>
82+
<EmbedInteropTypes>True</EmbedInteropTypes>
83+
</COMReference>
5184
</ItemGroup>
5285
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5386
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

AnyBlock/Cache.cs

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Net;
9+
using System.Threading.Tasks;
10+
11+
namespace AnyBlock
12+
{
13+
public struct RangeEntry
14+
{
15+
public string Name;
16+
public Direction Direction;
17+
18+
public override string ToString()
19+
{
20+
return $"{Direction}: {Name}";
21+
}
22+
}
23+
24+
[Flags]
25+
public enum Direction
26+
{
27+
DISABLED = 0,
28+
IN = 1,
29+
OUT = 2,
30+
BOTH = IN | OUT
31+
}
32+
public static class Cache
33+
{
34+
public static readonly string CacheFile;
35+
36+
public static readonly string SettingsFile;
37+
38+
public static bool HasCache
39+
{
40+
get
41+
{
42+
return File.Exists(CacheFile);
43+
}
44+
}
45+
46+
public static bool CacheRecent
47+
{
48+
get
49+
{
50+
return HasCache && File.GetLastWriteTimeUtc(CacheFile) > DateTime.UtcNow.AddDays(-1);
51+
}
52+
}
53+
54+
private static JObject CacheContent;
55+
56+
public static RangeEntry[] SelectedRanges
57+
{
58+
get
59+
{
60+
if (File.Exists(SettingsFile))
61+
{
62+
return JsonConvert.DeserializeObject<RangeEntry[]>(File.ReadAllText(SettingsFile));
63+
}
64+
return new RangeEntry[0];
65+
}
66+
set
67+
{
68+
File.WriteAllText(SettingsFile, JsonConvert.SerializeObject(value.Where(m => ValidEntry(m.Name)).ToArray()));
69+
}
70+
}
71+
72+
public static string[] ValidEntries
73+
{
74+
get
75+
{
76+
var All = new List<string>();
77+
if (HasCache)
78+
{
79+
if (CacheContent == null)
80+
{
81+
CacheContent = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(CacheFile));
82+
}
83+
Stack<JToken> Entries = new Stack<JToken>(CacheContent.Children());
84+
while (Entries.Count > 0)
85+
{
86+
var Current = Entries.Pop();
87+
if (Current is JProperty || Current is JObject)
88+
{
89+
All.Add(Current.Path);
90+
foreach (var E in Current.Children<JToken>())
91+
{
92+
Entries.Push(E);
93+
}
94+
}
95+
}
96+
}
97+
return All.Distinct().OrderBy(m => m).ToArray();
98+
}
99+
}
100+
101+
static Cache()
102+
{
103+
using (var P = Process.GetCurrentProcess())
104+
{
105+
CacheFile = Path.Combine(Path.GetDirectoryName(P.MainModule.FileName), "cache.json");
106+
SettingsFile = Path.Combine(Path.GetDirectoryName(P.MainModule.FileName), "settings.json");
107+
}
108+
}
109+
110+
public static bool ValidEntry(string Entry)
111+
{
112+
if (HasCache)
113+
{
114+
if (CacheContent == null)
115+
{
116+
try
117+
{
118+
CacheContent = (JObject)JsonConvert.DeserializeObject(File.ReadAllText(CacheFile));
119+
}
120+
catch
121+
{
122+
return false;
123+
}
124+
}
125+
var Temp = CacheContent;
126+
foreach (var key in Entry.Split('.'))
127+
{
128+
if (Temp != null && Temp[key] != null)
129+
{
130+
//The lowest level doesn't has JObject but JArray
131+
if (Temp[key].GetType() == typeof(JObject))
132+
{
133+
Temp = (JObject)Temp[key];
134+
}
135+
else
136+
{
137+
//If we enter the loop again after this, the entry has more than 3 levels
138+
Temp = null;
139+
}
140+
}
141+
else
142+
{
143+
return false;
144+
}
145+
}
146+
return true;
147+
}
148+
return false;
149+
}
150+
151+
public static string[] GetAddresses(params string[] EntryNames)
152+
{
153+
return GetAddresses(EntryNames.AsEnumerable());
154+
}
155+
156+
public static string[] GetAddresses(IEnumerable<string> EntryNames)
157+
{
158+
Console.Error.WriteLine("Validating Names...");
159+
var Names = EntryNames.Where(m => ValidEntry(m)).ToArray();
160+
var Addr = new List<string>();
161+
Console.Error.WriteLine("Getting all matching Nodes...");
162+
//var Nodes = CacheContent.Descendants().Where(m => Names.Contains(m.Path));
163+
var Nodes = Names
164+
.SelectMany(m => CacheContent.SelectToken(m, false))
165+
.Where(m => m != null)
166+
.ToArray();
167+
foreach (var Node in Nodes)
168+
{
169+
string[] Values = new string[0];
170+
if (Node is JProperty)
171+
{
172+
Console.Error.WriteLine("Processing {0}...", Node.Path);
173+
var Prop = (JProperty)Node;
174+
Values = Prop.Descendants()
175+
.OfType<JValue>()
176+
.Select(m => m.ToString())
177+
.ToArray();
178+
}
179+
else if(Node is JValue)
180+
{
181+
Values = new string[] { Node.ToString() };
182+
}
183+
else if (Node is JArray)
184+
{
185+
var A = (JArray)Node;
186+
Values = A.Values().Select(m => m.ToString()).ToArray();
187+
}
188+
Addr.AddRange(Values);
189+
}
190+
return Addr.Distinct().ToArray();
191+
}
192+
193+
public static void Invalidate()
194+
{
195+
if (HasCache)
196+
{
197+
File.SetLastWriteTimeUtc(CacheFile, DateTime.UtcNow.AddDays(-2));
198+
CacheContent = null;
199+
}
200+
}
201+
202+
public static async Task DownloadCacheAsync()
203+
{
204+
if (!CacheRecent)
205+
{
206+
using (var WC = new WebClient())
207+
{
208+
WC.Headers.Add("User-Agent: AnyBlock/1.0 +https://github.com/AyrA/AnyBlock");
209+
await WC.DownloadFileTaskAsync("https://cable.ayra.ch/ip/global.json", CacheFile);
210+
}
211+
}
212+
}
213+
214+
public static bool DownloadCache(int Timeout = System.Threading.Timeout.Infinite)
215+
{
216+
var T = DownloadCacheAsync();
217+
T.Wait(Timeout);
218+
return !T.IsFaulted && T.IsCompleted && !T.IsCanceled;
219+
}
220+
}
221+
}

AnyBlock/Ext.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
3+
namespace AnyBlock
4+
{
5+
public static class Ext
6+
{
7+
public static string ToJson(this object o, bool Pretty = false)
8+
{
9+
return JsonConvert.SerializeObject(o, Pretty ? Formatting.Indented : Formatting.None);
10+
}
11+
12+
public static T FromJson<T>(this string s, T Default = default(T))
13+
{
14+
try
15+
{
16+
return JsonConvert.DeserializeObject<T>(s);
17+
}
18+
catch
19+
{
20+
21+
}
22+
return Default;
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)