Skip to content

Commit

Permalink
fixed changing wifi not dismissing dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
twocanoes committed Mar 17, 2023
1 parent d7126a0 commit 7a3d451
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 155 deletions.
4 changes: 3 additions & 1 deletion XCreds/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,11 @@ extension WebViewController: WKNavigationDelegate {
TCSLogWithMark("passwordElementID is \(passwordElementID)")

TCSLogWithMark("inserting javascript to get password")

let javaScript = "document.getElementById('\(passwordElementID.sanitized())').value"
webView.evaluateJavaScript(javaScript, completionHandler: { response, error in
if error != nil {
TCSLogWithMark(error?.localizedDescription ?? "unknown error")
}
if let rawPass = response as? String, rawPass != "" {
TCSLogWithMark("========= password set===========")
self.password=rawPass
Expand Down
110 changes: 82 additions & 28 deletions XCredsLoginPlugIn/WifiManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
import CoreWLAN
import Cocoa
import SystemConfiguration
import Network

enum SecurityType {
case none // show without additional fields
Expand All @@ -27,9 +28,10 @@ class WifiManager: CWEventDelegate {
private var currentInterface: CWInterface?

var timer: Timer?
var timerCount: Int = 0
var timerCount: Int = 0
let timerMaxRepeatCount = 14
weak var delegate: WifiManagerDelegate?
var monitor:NWPathMonitor?

init() {
let defaultInterface = CWWiFiClient.shared().interface()
Expand Down Expand Up @@ -139,25 +141,25 @@ class WifiManager: CWEventDelegate {
/** No authentication required */
.none: "None", // 0
/** WEP security */
.WEP: "WEP", // 1
.WEP: "WEP", // 1
/** WPA personal authentication */
.wpaPersonal: "WPAPersonal", // 2
.wpaPersonal: "WPAPersonal", // 2
/** WPA/WPA2 personal authentication */
.wpaPersonalMixed: "WPAPersonalMixed", // 3
.wpaPersonalMixed: "WPAPersonalMixed", // 3
/** WPA2 personal authentication */
.wpa2Personal: "WPA2Personal", // 4
.wpa2Personal: "WPA2Personal", // 4
.personal: "Personal", // 5
/** Dynamic WEP security */
.dynamicWEP: "DynamicWEP", // 6
.dynamicWEP: "DynamicWEP", // 6
/** WPA enterprise authentication */
.wpaEnterprise: "WPAEnterprise", // 7
.wpaEnterprise: "WPAEnterprise", // 7
/** WPA/WPA2 enterprise authentication */
.wpaEnterpriseMixed: "WPAEnterpriseMixed", // 8
.wpaEnterpriseMixed: "WPAEnterpriseMixed", // 8
/** WPA2 enterprise authentication */
.wpa2Enterprise: "WPA2Enterprise", // 9
.wpa2Enterprise: "WPA2Enterprise", // 9
.enterprise: "Enterprise", // 10
/** Unknown security type */
.unknown: "Unknown", // Int.max
.unknown: "Unknown", // Int.max
]

func networkSecurityType(_ network: CWNetwork) -> SecurityType {
Expand All @@ -166,7 +168,7 @@ class WifiManager: CWEventDelegate {
if(securityLabel.key == .none) {
return .none
} else if securityLabel.key == .enterprise || securityLabel.key == .wpaEnterprise
|| securityLabel.key == .wpa2Enterprise || securityLabel.key == .wpaEnterpriseMixed {
|| securityLabel.key == .wpa2Enterprise || securityLabel.key == .wpaEnterpriseMixed {
return .enterpriseUserPassword
} else {
return .password
Expand Down Expand Up @@ -200,25 +202,77 @@ class WifiManager: CWEventDelegate {
}

public func internetConnected() {
self.timer = Timer(timeInterval: 0.5, target: self, selector: #selector(self.timerCheckInternetConnection), userInfo: nil, repeats: true)
if let timer = self.timer {
timer.fire()
RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
}
TCSLogWithMark("turnin on network monitor")
configureNetworkMonitor()
self.timer = Timer.scheduledTimer(withTimeInterval: 30, repeats: false, block: { timer in
TCSLogWithMark("cancelMonitor")

self.monitor?.cancel()

})
// self.timer = Timer(timeInterval: 30, target: self, selector: #selector(self.cancelMonitor), userInfo: nil, repeats: false)
// if let timer = self.timer {
// TCSLogWithMark("firing timer")
// timer.fire()
// RunLoop.main.add(timer, forMode: RunLoop.Mode.common)
// }
}

@objc private func timerCheckInternetConnection() {
timerCount = timerCount + 1
if self.isConnectedToNetwork() || timerCount >= timerMaxRepeatCount {
self.timerCount = 0
self.timer?.invalidate()
self.timer = nil

delegate?.wifiManagerFullyFinishedInternetConnectionTimer()
}

if self.isConnectedToNetwork() {
delegate?.wifiManagerConnectedToNetwork?()
// @objc func cancelMonitor(){
// TCSLogWithMark("cancelMonitor")
// }
func configureNetworkMonitor(){

self.monitor = NWPathMonitor()

monitor?.pathUpdateHandler = { path in
TCSLogWithMark("network changed. Checking to see if it was WiFi...")
TCSLogWithMark()
if path.status != .satisfied {
TCSLogWithMark("not connected")
}
else if path.usesInterfaceType(.cellular) {
TCSLogWithMark("Cellular")
}
else if path.usesInterfaceType(.wifi) {
TCSLogWithMark("Wifi changed")
self.timer?.invalidate()

self.monitor?.cancel()
self.delegate?.wifiManagerConnectedToNetwork?()
}
else if path.usesInterfaceType(.wiredEthernet) {
TCSLogWithMark("Ethernet")
}
else if path.usesInterfaceType(.other){
TCSLogWithMark("Other")
}
else if path.usesInterfaceType(.loopback){
TCSLogWithMark("Loop Back")
}
else {
TCSLogWithMark("Unknown interface type")
}


}
let queue = DispatchQueue(label: "Monitor2")
monitor?.start(queue: queue)

}

// @objc private func timerCheckInternetConnection() {
// timerCount = timerCount + 1
// if self.isConnectedToNetwork() || timerCount >= timerMaxRepeatCount {
// self.timerCount = 0
// self.timer?.invalidate()
// self.timer = nil
//
// delegate?.wifiManagerFullyFinishedInternetConnectionTimer()
// }
//
// if self.isConnectedToNetwork() {
// delegate?.wifiManagerConnectedToNetwork?()
// }
// }
}
138 changes: 20 additions & 118 deletions XCredsLoginPlugIn/WifiWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,6 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
self.networkUsernameView?.isHidden=true
self.networkPasswordView?.isHidden=true

// networkSearch.performClick(nil)
// TCSLogWithMark()
//
// networkSearch.becomeFirstResponder()
// TCSLogWithMark()
//
//
// networkWifiPopup.action = #selector(networkWifiPopupChangedValue)
// TCSLogWithMark()
//
// networkWifiPopup.target = self
// TCSLogWithMark()
//
// perform(#selector(connectNetwork), with: nil, afterDelay: 0.05)
// TCSLogWithMark()
//
// self.networkConnectionSpinner.isHidden = true
// TCSLogWithMark()

}

Expand All @@ -84,7 +66,6 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
print("selected current");
}
else {
// configureUIForSelectedNetwork()
if let network = popupButton.selectedItem?.representedObject as? CWNetwork {
selectedNetwork = network
configureUIForSelectedNetwork(network: network)
Expand All @@ -97,13 +78,6 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
func menuNeedsUpdate(_ menu: NSMenu) {
updateNetworks()
}
// override func resizeSubviews(withOldSize oldSize: NSSize) {
// super.resizeSubviews(withOldSize: oldSize)
// if !(getManagedPreference(key: .LoginScreen) as? Bool ?? false) {
// fadeInBackgroundView()
// }
// }


@objc func updateAvailableNetworks() {
DispatchQueue.global().async {
Expand All @@ -127,47 +101,27 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
}
}

// guard networks != nil else {
// os_log("Unable to find any networks", log: wifiLog, type: .debug)
// self.networkWifiPopup?.addItem(withTitle: "No networks")
// return
// }


}


func updateNetworks() {
os_log("Remove allItems")
self.networkWifiPopup?.removeAllItems()
TCSLogWithMark()

TCSLogWithMark()

if networks.count == 0 {
os_log("Unable to find any networks", log: wifiLog, type: .debug)
self.networkWifiPopup?.addItem(withTitle: "No networks")
}
TCSLogWithMark()

for network in networks {
if let networkName = network.ssid {
self.networkWifiPopup?.addItem(withTitle: networkName)
self.networkWifiPopup?.lastItem?.representedObject=network
self.networks.insert(network)
}
}
TCSLogWithMark()

self.networkWifiPopup?.selectItem(withTitle: wifiManager.getCurrentSSID() ?? "")
TCSLogWithMark()

configCurrentNetwork()
TCSLogWithMark()

// configureUIForSelectedNetwork()
TCSLogWithMark()


}
func configCurrentNetwork() {
Expand All @@ -183,26 +137,7 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
private func configureAppearance() {
TCSLogWithMark()
self.networkWifiPopup?.removeAllItems()
TCSLogWithMark()
self.networkWifiPopup?.addItem(withTitle: "Choose Network...")
TCSLogWithMark()
// self.networkOpenStatusLabel.stringValue = "Open WiFi Networks are not supported. Find and join a secure Network to continue."
TCSLogWithMark()
// mainView.wantsLayer = true
// TCSLogWithMark()
// mainView.layer?.backgroundColor = NSColor.white.cgColor
// TCSLogWithMark()
// mainView.layer?.cornerRadius = 5
// TCSLogWithMark()
// mainView.alphaValue = 1
// TCSLogWithMark()
//
// backgroundView.wantsLayer = true
// TCSLogWithMark()
// backgroundView.layer?.backgroundColor = NSColor.lightGray.cgColor
// TCSLogWithMark()
// backgroundView.alphaValue = 0
// TCSLogWithMark()
}


Expand All @@ -212,42 +147,30 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
}

@IBAction func dismissButton(_ sender: Any) {
self.window?.close()
// NSAnimationContext.beginGrouping()
// NSAnimationContext.current.duration = defaultFadeDuration
//// animator().removeFromSuperview()
// NSAnimationContext.endGrouping()
// completionHandler?()
TCSLogWithMark("closing window")
DispatchQueue.main.async {
self.window?.close()
}
}

@IBAction func connect(_ sender: Any) {
// self.disableUI()
// for network in networks {
// if let networkName = network.ssid {
// if (networkName == self.networkWifiPopup?.selectedItem?.title) {
if let selectedNetwork = selectedNetwork {

let userPassword = self.networkPassword?.stringValue
let username = self.networkUsername?.stringValue

let userPassword = self.networkPassword?.stringValue
let username = self.networkUsername?.stringValue

let connected = wifiManager.connectWifi(with: selectedNetwork, password: userPassword, username: username)
if connected {
// self.networkstatusLabel?.stringValue = "Connected to: \(networkName)"
NSApp.stopModal()
credentialsWindow.orderOut(self)

wifiManager.delegate = self
wifiManager.internetConnected()
return
} else {
// self.networkstatusLabel?.stringValue = "No Internet Connection"
// self.enableUI()
credentialsWindow.shake(self)
}
}
// }
// }
let connected = wifiManager.connectWifi(with: selectedNetwork, password: userPassword, username: username)
if connected {
NSApp.stopModal()
credentialsWindow.orderOut(self)

wifiManager.delegate = self
wifiManager.internetConnected()
return
} else {
credentialsWindow.shake(self)
}
}
}


Expand Down Expand Up @@ -342,29 +265,7 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg

// In order to prevent a NSView from bleeding it's mouse events to the parent, one must implement the empty methods.

// func disableUI() {
// DispatchQueue.main.async {
// self.networkSearch?.isEnabled = false
// self.networkWifiPopup?.isEnabled = false
// self.networkUsername?.isEnabled = false
// self.networkPassword?.isEnabled = false
// self.networkConnectButton?.isEnabled = false
// self.networkConnectionSpinner?.isHidden = false
// self.networkConnectionSpinner?.startAnimation(self)
// }
// }
//
// func enableUI() {
// DispatchQueue.main.async {
// self.networkSearch?.isEnabled = true
// self.networkWifiPopup?.isEnabled = true
// self.networkUsername?.isEnabled = true
// self.networkPassword?.isEnabled = true
// self.networkConnectButton?.isEnabled = true
// self.networkConnectionSpinner?.isHidden = true
// self.networkConnectionSpinner?.stopAnimation(self)
// }
// }


// MARK: - WifiManager Delegates
func wifiManagerFullyFinishedInternetConnectionTimer() {
Expand All @@ -374,6 +275,7 @@ class WifiWindowController: NSWindowController, WifiManagerDelegate, NSMenuDeleg
}

func wifiManagerConnectedToNetwork() {
TCSLogWithMark("dismissing")
self.dismissButton(self)
}
}

0 comments on commit 7a3d451

Please sign in to comment.