|
| 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 | +} |
0 commit comments