private void uiUpdater_Tick(object sender, EventArgs e) { if (uiUpdater.Interval == 100) { uiUpdater.Interval = 5000; if (settings.startHidden) { this.Hide(); } } wlanCombobox.Items.Clear(); ssidList.Items.Clear(); WlanInterfaceState state = WlanInterfaceState.Disconnected; String ssid = ""; /// Update interface & SSID list foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces) { wlanCombobox.Items.Add($"{wlanInterface.InterfaceName} ({wlanInterface.InterfaceDescription})"); /// Get the interface state if ($"{wlanInterface.InterfaceName} ({wlanInterface.InterfaceDescription})" == settings.networkInterface) { try { ssid = wlanInterface.CurrentConnection.profileName; state = wlanInterface.CurrentConnection.isState; } catch (Exception ex) { } } } /// Load any missing SSID's from settings file if (settings.hosts != null) { foreach (KeyValuePair <String, String> s in settings.hosts) { if (!ssidList.Items.Contains(s.Key)) { ssidList.Items.Add(s.Key); } } } /// Make sure we have a selected interface that exists if (!wlanCombobox.Items.Contains(wlanCombobox.Text)) { wlanCombobox.Text = ""; } /// Update misc labels and visibilities statusLabel.Text = !listening ? $"Status: Idle (WLAN: {ssid} {state.ToString()})" : $"Status: Running (WLAN: {ssid} {state.ToString()})"; enableBtn.Text = !listening ? "Start" : "Stop"; if (wlanCombobox.Text == "") { enableBtn.Enabled = false; } else { enableBtn.Enabled = true; } settingsBox.Enabled = !listening; if (once) { ssidList.Text = currentSsid; once = false; } }
private void watchdog_Tick(object sender, EventArgs e) { /// Get the interface state WlanInterfaceState state = WlanInterfaceState.Disconnected; String ssid = ""; foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces) { if ($"{wlanInterface.InterfaceName} ({wlanInterface.InterfaceDescription})" == settings.networkInterface) { try { ssid = wlanInterface.CurrentConnection.profileName; state = wlanInterface.CurrentConnection.isState; } catch (Exception ex) { } } } bool updateHost = false; if (currentSsid != ssid) { updateHost = true; } else if (wlanState != state) { updateHost = true; } if (updateHost && state == WlanInterfaceState.Connected) { watchdog.Stop(); bool update = true; if (!settings.hosts.ContainsKey(ssid)) { DialogResult res = MessageBox.Show($"The current SSID does not have a host config yet, should we create one now?", "WiFi Connection changed", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (res == DialogResult.Yes) { settings.hosts.Add(ssid, defaultHost); settings.Save(); uiUpdater_Tick(null, null); update = true; } else { update = false; } } else if (settings.askBeforeSwitch) { DialogResult res = MessageBox.Show($"The SSID or status has changed\nCurrent SSID is {ssid} and the connection status is {state}\nWas {currentSsid} | {wlanState}\n\nSwitch host file?", "WiFi Connection changed", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (res == DialogResult.No) { update = false; } } if (update) { /// Update host file >> try { File.WriteAllText(@"C:\Windows\System32\drivers\etc\hosts", settings.hosts[ssid]); notifyIcon1.Text = $"Using host file for SSID {ssid}"; notifyIcon1.BalloonTipIcon = ToolTipIcon.Info; notifyIcon1.BalloonTipText = $"Host file has succesfully been applied.\nNow using host file for SSID {ssid}"; notifyIcon1.BalloonTipTitle = "Host file applied"; notifyIcon1.ShowBalloonTip(1000); } catch (UnauthorizedAccessException uaex) { MessageBox.Show("Failed to update host file!\nMake sure this program is running with admin rights!"); } } currentSsid = ssid; wlanState = state; watchdog.Start(); GC.Collect(); } }