Skip to content

Commit d6a8ddf

Browse files
committed
Reformat
1 parent d56fa1c commit d6a8ddf

2 files changed

Lines changed: 130 additions & 153 deletions

File tree

SimpleDnsCrypt/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
ResourceDictionaryLocation.None,
1515
ResourceDictionaryLocation.SourceAssembly
1616
)]
17-
[assembly: AssemblyVersion("0.7.0")]
18-
[assembly: AssemblyFileVersion("0.7.0")]
17+
[assembly: AssemblyVersion("0.7.1")]
18+
[assembly: AssemblyFileVersion("0.7.1")]

SimpleDnsCrypt/ViewModels/MainViewModel.cs

Lines changed: 128 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class MainViewModel : Screen, INotifyDataErrorInfo
6565
private BindableCollection<LocalNetworkInterface> _localNetworkInterfaces =
6666
new BindableCollection<LocalNetworkInterface>();
6767
private BindableCollection<AvailableResolver> _resolvers;
68-
private BindableCollection<StampFileEntry> _relays;
68+
private readonly BindableCollection<StampFileEntry> _relays;
6969
private Language _selectedLanguage;
7070
private int _selectedTabIndex;
7171

@@ -951,102 +951,92 @@ public void ResolverClicked(AvailableResolver resolver)
951951

952952
public void HandleManageRoutes(AvailableResolver availableResolver)
953953
{
954-
//TODO: optimize
955954
try
956955
{
957-
if (availableResolver != null)
956+
if (availableResolver == null) return;
957+
if (!availableResolver.Protocol.Equals("DNSCrypt")) return;
958+
dynamic settings = new ExpandoObject();
959+
settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
960+
RouteViewModel.Route = new ObservableCollection<StampFileEntry>();
961+
if (availableResolver.Route?.via != null)
958962
{
959-
if (availableResolver.Protocol.Equals("DNSCrypt"))
963+
for (var v = 0; v < availableResolver.Route.via.Count; v++)
960964
{
961-
dynamic settings = new ExpandoObject();
962-
settings.WindowStartupLocation = WindowStartupLocation.CenterOwner;
963-
RouteViewModel.Route = new ObservableCollection<StampFileEntry>();
964-
if (availableResolver.Route != null)
965+
var stampFileEntry = _relays.FirstOrDefault(r => r.Name.Equals(availableResolver.Route.via[v]));
966+
if (stampFileEntry != null)
965967
{
966-
if (availableResolver.Route.via != null)
967-
{
968-
for (var v = 0; v < availableResolver.Route.via.Count; v++)
969-
{
970-
var stampFileEntry = _relays.Where(r => r.Name.Equals(availableResolver.Route.via[v])).FirstOrDefault();
971-
if (stampFileEntry != null)
972-
{
973-
//add only entries that are present in relays
974-
RouteViewModel.Route.Add(stampFileEntry);
975-
}
976-
else
977-
{
978-
//remove non-existent relays from route
979-
availableResolver.Route.via.RemoveAt(v);
980-
}
981-
}
982-
}
968+
//add only entries that are present in relays
969+
RouteViewModel.Route.Add(stampFileEntry);
983970
}
984-
RouteViewModel.Relays = _relays;
985-
RouteViewModel.Resolver = availableResolver.DisplayName;
986-
var result = _windowManager.ShowDialog(RouteViewModel, null, settings);
987-
if (result) return;
988-
989-
if (RouteViewModel.Route.Count() == 0)
971+
else
990972
{
991-
//remove route
992-
if (availableResolver.Route != null)
993-
{
994-
var oldRoute = _dnscryptProxyConfiguration.anonymized_dns.routes.FindIndex(r => r.server_name.Equals(availableResolver.Route.server_name));
995-
if (oldRoute != -1)
996-
{
997-
_dnscryptProxyConfiguration.anonymized_dns.routes.RemoveAt(oldRoute);
998-
}
999-
SaveDnsCryptConfiguration();
1000-
LoadResolvers();
1001-
}
973+
//remove non-existent relays from route
974+
availableResolver.Route.via.RemoveAt(v);
1002975
}
1003-
else
976+
}
977+
}
978+
RouteViewModel.Relays = _relays;
979+
RouteViewModel.Resolver = availableResolver.DisplayName;
980+
var result = _windowManager.ShowDialog(RouteViewModel, null, settings);
981+
if (result) return;
982+
983+
if (!RouteViewModel.Route.Any())
984+
{
985+
//remove route
986+
if (availableResolver.Route == null) return;
987+
var oldRoute = _dnscryptProxyConfiguration.anonymized_dns.routes.FindIndex(r => r.server_name.Equals(availableResolver.Route.server_name));
988+
if (oldRoute != -1)
989+
{
990+
_dnscryptProxyConfiguration.anonymized_dns.routes.RemoveAt(oldRoute);
991+
}
992+
SaveDnsCryptConfiguration();
993+
LoadResolvers();
994+
}
995+
else
996+
{
997+
var oldRoute = -1;
998+
if (availableResolver.Route != null && !string.IsNullOrEmpty(availableResolver.Route.server_name))
999+
{
1000+
oldRoute = _dnscryptProxyConfiguration.anonymized_dns.routes.FindIndex(r => r.server_name.Equals(availableResolver.Route.server_name));
1001+
}
1002+
if (oldRoute != -1)
1003+
{
1004+
//update
1005+
_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via = new ObservableCollection<string>();
1006+
foreach (var stampFileEntry in RouteViewModel.Route)
10041007
{
1005-
var oldRoute = -1;
1006-
if (availableResolver.Route != null && !string.IsNullOrEmpty(availableResolver.Route.server_name))
1007-
{
1008-
oldRoute = _dnscryptProxyConfiguration.anonymized_dns.routes.FindIndex(r => r.server_name.Equals(availableResolver.Route.server_name));
1009-
}
1010-
if (oldRoute != -1)
1008+
if (_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via == null)
10111009
{
1012-
//update
10131010
_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via = new ObservableCollection<string>();
1014-
foreach (var stampFileEntry in RouteViewModel.Route)
1015-
{
1016-
if (_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via == null)
1017-
{
1018-
_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via = new ObservableCollection<string>();
1019-
}
1020-
_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via.Add(stampFileEntry.Name);
1021-
}
10221011
}
1023-
else
1024-
{
1025-
//create
1026-
var newRoute = new Route
1027-
{
1028-
server_name = availableResolver.Name,
1029-
via = new ObservableCollection<string>()
1030-
};
1031-
foreach (var stampFileEntry in RouteViewModel.Route)
1032-
{
1033-
newRoute.via.Add(stampFileEntry.Name);
1034-
}
1012+
_dnscryptProxyConfiguration.anonymized_dns.routes[oldRoute].via.Add(stampFileEntry.Name);
1013+
}
1014+
}
1015+
else
1016+
{
1017+
//create
1018+
var newRoute = new Route
1019+
{
1020+
server_name = availableResolver.Name,
1021+
via = new ObservableCollection<string>()
1022+
};
1023+
foreach (var stampFileEntry in RouteViewModel.Route)
1024+
{
1025+
newRoute.via.Add(stampFileEntry.Name);
1026+
}
10351027

1036-
if (_dnscryptProxyConfiguration.anonymized_dns == null)
1037-
{
1038-
_dnscryptProxyConfiguration.anonymized_dns = new AnonymizedDns();
1039-
}
1040-
if (_dnscryptProxyConfiguration.anonymized_dns.routes == null)
1041-
{
1042-
_dnscryptProxyConfiguration.anonymized_dns.routes = new List<Route>();
1043-
}
1044-
_dnscryptProxyConfiguration.anonymized_dns.routes.Add(newRoute);
1045-
}
1046-
SaveDnsCryptConfiguration();
1047-
LoadResolvers();
1028+
if (_dnscryptProxyConfiguration.anonymized_dns == null)
1029+
{
1030+
_dnscryptProxyConfiguration.anonymized_dns = new AnonymizedDns();
1031+
}
1032+
if (_dnscryptProxyConfiguration.anonymized_dns.routes == null)
1033+
{
1034+
_dnscryptProxyConfiguration.anonymized_dns.routes = new List<Route>();
10481035
}
1036+
_dnscryptProxyConfiguration.anonymized_dns.routes.Add(newRoute);
10491037
}
1038+
SaveDnsCryptConfiguration();
1039+
LoadResolvers();
10501040
}
10511041
}
10521042
catch (Exception exception)
@@ -1121,34 +1111,23 @@ private void LoadResolvers()
11211111
{
11221112
if (!r.Name.Equals(resolver.Name)) continue;
11231113
first = r;
1124-
//TODO: optimize
1125-
if (_dnscryptProxyConfiguration.anonymized_dns != null)
1114+
if (_dnscryptProxyConfiguration.anonymized_dns?.routes != null)
11261115
{
1127-
if (_dnscryptProxyConfiguration.anonymized_dns.routes != null)
1116+
if (_dnscryptProxyConfiguration.anonymized_dns.routes.Count > 0)
11281117
{
1129-
if (_dnscryptProxyConfiguration.anonymized_dns.routes.Count > 0)
1118+
var route = _dnscryptProxyConfiguration.anonymized_dns.routes.FirstOrDefault(re => re.server_name.Equals(resolver.Name));
1119+
if (route != null)
11301120
{
1131-
var route = _dnscryptProxyConfiguration.anonymized_dns.routes.Where(re => re.server_name.Equals(resolver.Name)).FirstOrDefault();
1132-
if (route != null)
1121+
first.Route = route;
1122+
if (_relays != null && _relays.Count > 0)
11331123
{
1134-
first.Route = route;
1135-
if (_relays != null && _relays.Count > 0)
1136-
{
1137-
var relays = _relays.Select(x => x.Name).ToList();
1138-
var valid = first.Route.via.Intersect(relays).Count() == first.Route.via.Count();
1139-
if (valid)
1140-
{
1141-
first.RouteState = RouteState.Valid;
1142-
}
1143-
else
1144-
{
1145-
first.RouteState = RouteState.Invalid;
1146-
}
1147-
}
1148-
else
1149-
{
1150-
first.RouteState = RouteState.Invalid;
1151-
}
1124+
var relays = _relays.Select(x => x.Name).ToList();
1125+
var valid = first.Route.via.Intersect(relays).Count() == first.Route.via.Count();
1126+
first.RouteState = valid ? RouteState.Valid : RouteState.Invalid;
1127+
}
1128+
else
1129+
{
1130+
first.RouteState = RouteState.Invalid;
11521131
}
11531132
}
11541133
}
@@ -1185,55 +1164,53 @@ public async void UninstallService()
11851164
LocalizationEx.GetUiString("dialog_uninstall_title", Thread.CurrentThread.CurrentCulture),
11861165
MessageBoxButton.YesNo, BoxType.Default);
11871166

1188-
if (result == MessageBoxResult.Yes)
1189-
{
1190-
IsUninstallingService = true;
1167+
if (result != MessageBoxResult.Yes) return;
1168+
IsUninstallingService = true;
11911169

1192-
if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
1193-
{
1194-
await Task.Run(() => { DnsCryptProxyManager.Stop(); }).ConfigureAwait(false);
1195-
await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
1196-
}
1170+
if (DnsCryptProxyManager.IsDnsCryptProxyRunning())
1171+
{
1172+
await Task.Run(() => { DnsCryptProxyManager.Stop(); }).ConfigureAwait(false);
1173+
await Task.Delay(Global.ServiceStopTime).ConfigureAwait(false);
1174+
}
11971175

1198-
await Task.Run(() => { DnsCryptProxyManager.Uninstall(); }).ConfigureAwait(false);
1199-
await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
1200-
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
1201-
NotifyOfPropertyChange(() => IsResolverRunning);
1176+
await Task.Run(() => { DnsCryptProxyManager.Uninstall(); }).ConfigureAwait(false);
1177+
await Task.Delay(Global.ServiceUninstallTime).ConfigureAwait(false);
1178+
_isResolverRunning = DnsCryptProxyManager.IsDnsCryptProxyRunning();
1179+
NotifyOfPropertyChange(() => IsResolverRunning);
12021180

1203-
// recover the network interfaces (also the hidden and down cards)
1204-
var localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(
1205-
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.ToList());
1206-
foreach (var localNetworkInterface in localNetworkInterfaces)
1207-
{
1208-
if (!localNetworkInterface.UseDnsCrypt) continue;
1209-
var status = LocalNetworkInterfaceManager.SetNameservers(localNetworkInterface, new List<DnsServer>());
1210-
var card = _localNetworkInterfaces.SingleOrDefault(n => n.Description.Equals(localNetworkInterface.Description));
1211-
if (card != null) card.UseDnsCrypt = !status;
1212-
}
1181+
// recover the network interfaces (also the hidden and down cards)
1182+
var localNetworkInterfaces = LocalNetworkInterfaceManager.GetLocalNetworkInterfaces(
1183+
DnscryptProxyConfigurationManager.DnscryptProxyConfiguration.listen_addresses.ToList());
1184+
foreach (var localNetworkInterface in localNetworkInterfaces)
1185+
{
1186+
if (!localNetworkInterface.UseDnsCrypt) continue;
1187+
var status = LocalNetworkInterfaceManager.SetNameservers(localNetworkInterface, new List<DnsServer>());
1188+
var card = _localNetworkInterfaces.SingleOrDefault(n => n.Description.Equals(localNetworkInterface.Description));
1189+
if (card != null) card.UseDnsCrypt = !status;
1190+
}
12131191

1214-
await Task.Delay(1000).ConfigureAwait(false);
1215-
ReloadLoadNetworkInterfaces();
1216-
IsUninstallingService = false;
1217-
if (!DnsCryptProxyManager.IsDnsCryptProxyInstalled())
1218-
{
1219-
IsServiceInstalled = false;
1220-
_windowManager.ShowMetroMessageBox(
1221-
LocalizationEx.GetUiString("message_content_uninstallation_successful",
1222-
Thread.CurrentThread.CurrentCulture),
1223-
LocalizationEx.GetUiString("message_title_uninstallation_successful",
1224-
Thread.CurrentThread.CurrentCulture),
1225-
MessageBoxButton.OK, BoxType.Default);
1226-
}
1227-
else
1228-
{
1229-
IsServiceInstalled = true;
1230-
_windowManager.ShowMetroMessageBox(
1231-
LocalizationEx.GetUiString("message_content_uninstallation_error",
1232-
Thread.CurrentThread.CurrentCulture),
1233-
LocalizationEx.GetUiString("message_title_uninstallation_error",
1234-
Thread.CurrentThread.CurrentCulture),
1235-
MessageBoxButton.OK, BoxType.Warning);
1236-
}
1192+
await Task.Delay(1000).ConfigureAwait(false);
1193+
ReloadLoadNetworkInterfaces();
1194+
IsUninstallingService = false;
1195+
if (!DnsCryptProxyManager.IsDnsCryptProxyInstalled())
1196+
{
1197+
IsServiceInstalled = false;
1198+
_windowManager.ShowMetroMessageBox(
1199+
LocalizationEx.GetUiString("message_content_uninstallation_successful",
1200+
Thread.CurrentThread.CurrentCulture),
1201+
LocalizationEx.GetUiString("message_title_uninstallation_successful",
1202+
Thread.CurrentThread.CurrentCulture),
1203+
MessageBoxButton.OK, BoxType.Default);
1204+
}
1205+
else
1206+
{
1207+
IsServiceInstalled = true;
1208+
_windowManager.ShowMetroMessageBox(
1209+
LocalizationEx.GetUiString("message_content_uninstallation_error",
1210+
Thread.CurrentThread.CurrentCulture),
1211+
LocalizationEx.GetUiString("message_title_uninstallation_error",
1212+
Thread.CurrentThread.CurrentCulture),
1213+
MessageBoxButton.OK, BoxType.Warning);
12371214
}
12381215
}
12391216

0 commit comments

Comments
 (0)