|
1 | 1 | using System.Text.Json; |
2 | 2 | using System.Net.NetworkInformation; |
3 | 3 | using System.Reflection; |
| 4 | +using LibHandler.Models; |
| 5 | +using System.Net; |
4 | 6 |
|
5 | 7 | namespace LibHandler.Util |
6 | 8 | { |
7 | | - public class Mirror |
8 | | - { |
9 | | - public string Url { get; set; } |
10 | | - public string FullUrl { get; set; } |
11 | | - public string DownloadUrl { get; set; } |
12 | | - public long LastResponseTime { get; set; } |
13 | | - |
14 | | - public Mirror() |
15 | | - { |
16 | | - Url = String.Empty; |
17 | | - FullUrl = String.Empty; |
18 | | - DownloadUrl = String.Empty; |
19 | | - LastResponseTime = 10000; |
20 | | - } |
21 | | - } |
22 | | - |
23 | 9 | internal static class MirrorHandler |
24 | 10 | { |
25 | | - private static List<Mirror> Mirrors { get; set; } |
26 | | - public static Mirror MainMirror { get; set; } |
| 11 | + private static List<Mirror> SearchMirrors { get; set; } |
| 12 | + private static List<Mirror> DownloadMirrors { get; set; } |
| 13 | + |
| 14 | + public static Mirror MainSearchMirror { get; set; } |
| 15 | + public static Mirror MainDownloadMirror { get; set; } |
27 | 16 |
|
28 | 17 | static MirrorHandler() |
29 | 18 | { |
30 | 19 | StreamReader reader = new StreamReader( |
31 | 20 | Assembly.GetExecutingAssembly() |
32 | 21 | .GetManifestResourceStream(DataHandler.MIRROR_CONFIG_PATH) ?? Stream.Null); |
33 | 22 |
|
34 | | - //string config = File.ReadAllText(DataHandler.MIRROR_CONFIG_PATH); |
35 | 23 | string config = reader.ReadToEnd(); |
36 | | - Mirrors = JsonSerializer.Deserialize<List<Mirror>>(config) ?? new List<Mirror>(); |
| 24 | + List<Mirror> mirror = JsonSerializer.Deserialize<List<Mirror>>(config) ?? new List<Mirror>(); |
| 25 | + |
| 26 | + SearchMirrors = mirror.Where(m => m.MirrorType.Equals(MirrorType.SearchMirror)).ToList(); |
| 27 | + DownloadMirrors = mirror.Where(m => m.MirrorType.Equals(MirrorType.DownloadMirror)).ToList(); |
37 | 28 |
|
38 | | - MainMirror = new Mirror(); |
39 | | - MainMirror = GetMainMirror(); |
| 29 | + SetMirrorsResponseTimes(SearchMirrors); |
| 30 | + SetMirrorsResponseTimes(DownloadMirrors); |
| 31 | + |
| 32 | + MainSearchMirror = GetOptimalMirror(SearchMirrors); |
| 33 | + MainDownloadMirror = GetOptimalMirror(DownloadMirrors); |
40 | 34 | } |
41 | 35 |
|
42 | 36 | public static void RemoveMirror(Mirror m) |
43 | | - => Mirrors.Remove(m); |
44 | | - public static void RemoveMirror(int m) |
45 | | - => Mirrors.RemoveAt(m); |
46 | | - public static void ReplaceMainMirror() |
47 | 37 | { |
48 | | - Mirrors.Remove(MainMirror); |
49 | | - MainMirror = GetMainMirror(true); |
| 38 | + if (m.MirrorType == MirrorType.SearchMirror) SearchMirrors.Remove(m); |
| 39 | + else if (m.MirrorType == MirrorType.DownloadMirror) DownloadMirrors.Remove(m); |
50 | 40 | } |
51 | 41 |
|
52 | | - public static Mirror GetMainMirror(bool refresh = false) |
| 42 | + public static void SetMainMirror(Mirror m) |
53 | 43 | { |
54 | | - if (MainMirror.Url != String.Empty && !refresh) return MainMirror; |
55 | | - |
56 | | - Ping ping = new Ping(); |
| 44 | + if (m.MirrorType.Equals(MirrorType.SearchMirror)) MainSearchMirror = m; |
| 45 | + else if (m.MirrorType.Equals(MirrorType.DownloadMirror)) MainDownloadMirror = m; |
57 | 46 |
|
58 | | - Mirror optimalMirror = new Mirror(); |
| 47 | + } |
| 48 | + |
| 49 | + public static void ReplaceMainMirror(MirrorType type) |
| 50 | + { |
| 51 | + switch (type) |
| 52 | + { |
| 53 | + case MirrorType.SearchMirror: |
| 54 | + SearchMirrors.Remove(MainSearchMirror); |
| 55 | + MainSearchMirror = GetOptimalMirror(SearchMirrors); |
| 56 | + break; |
| 57 | + case MirrorType.DownloadMirror: |
| 58 | + DownloadMirrors.Remove(MainDownloadMirror); |
| 59 | + MainDownloadMirror = GetOptimalMirror(DownloadMirrors); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
59 | 63 |
|
60 | | - for (int i = 0; i < Mirrors.Count; i++) |
| 64 | + public static Mirror GetMainMirror(MirrorType type) |
| 65 | + { |
| 66 | + Mirror m = type switch |
61 | 67 | { |
62 | | - PingReply reply = ping.Send(Mirrors[i].Url); |
63 | | - if (reply.Status != IPStatus.Success) continue; |
| 68 | + MirrorType.SearchMirror => MainSearchMirror, |
| 69 | + MirrorType.DownloadMirror => MainDownloadMirror, |
| 70 | + _ => throw new ArgumentOutOfRangeException(nameof(type), "Please provide a valid MirrorType") |
| 71 | + }; |
| 72 | + |
| 73 | + return m; |
| 74 | + } |
| 75 | + |
| 76 | + public static void SetMirrorsResponseTimes(List<Mirror> mirrors) |
| 77 | + { |
| 78 | + Ping ping = new Ping(); |
64 | 79 |
|
65 | | - if (reply.RoundtripTime < optimalMirror.LastResponseTime) |
| 80 | + for (int i = 0; i < mirrors.Count; i++) |
| 81 | + { |
| 82 | + PingReply reply = ping.Send(mirrors[i].Url); |
| 83 | + Console.WriteLine(reply.Status); |
| 84 | + if (reply.Status != IPStatus.Success) |
66 | 85 | { |
67 | | - Mirrors[i].LastResponseTime = reply.RoundtripTime; |
68 | | - optimalMirror = Mirrors[i]; |
| 86 | + mirrors[i].LastResponseTime = TryIPPing(mirrors[i].Url); |
| 87 | + continue; |
69 | 88 | } |
| 89 | + mirrors[i].LastResponseTime = reply.RoundtripTime; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static long TryIPPing(string Url) |
| 94 | + { |
| 95 | + IPHostEntry host = Dns.GetHostEntry(Url); |
| 96 | + Ping p = new Ping(); |
| 97 | + |
| 98 | + foreach(IPAddress ip in host.AddressList) |
| 99 | + { |
| 100 | + PingReply rep = p.Send(ip); |
| 101 | + if (rep.Status == IPStatus.Success) |
| 102 | + return rep.RoundtripTime; |
70 | 103 | } |
71 | 104 |
|
72 | | - return optimalMirror; |
| 105 | + return 10000; |
| 106 | + } |
| 107 | + |
| 108 | + public static Mirror GetOptimalMirror(List<Mirror> mirrors) |
| 109 | + { |
| 110 | + long min = mirrors.Min(m => m.LastResponseTime); |
| 111 | + return mirrors.First(m => m.LastResponseTime == min); |
73 | 112 | } |
74 | 113 | } |
75 | 114 | } |
0 commit comments