-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from mysteriumnetwork/connection-status-2
Connection status stream for iOS/macOS fixed
- Loading branch information
Showing
27 changed files
with
287 additions
and
327 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# file options | ||
|
||
--symlinks ignore | ||
--swiftversion 5.7 | ||
|
||
# rules | ||
--enable isEmpty | ||
--disable andOperator | ||
--disable wrapMultilineStatementBraces | ||
|
||
# format options | ||
|
||
--commas inline | ||
--comments indent | ||
--decimalgrouping 3,5 | ||
--exponentcase lowercase | ||
--exponentgrouping disabled | ||
--extensionacl on-declarations | ||
--fractiongrouping disabled | ||
--ifdef no-indent | ||
--importgrouping testable-top | ||
--operatorfunc no-space | ||
--nospaceoperators ..<, ... | ||
--selfrequired validate | ||
--someAny false | ||
--stripunusedargs closure-only | ||
--wraparguments preserve | ||
--wrapcollections preserve | ||
--wrapparameters preserve | ||
|
||
|
||
# rules | ||
|
||
--enable isEmpty | ||
--disable wrapMultilineStatementBraces | ||
--disable opaqueGenericParameters |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
import NetworkExtension | ||
|
||
/// ConnectionStatus returned to Dart | ||
enum ConnectionStatus: String { | ||
case connected | ||
case disconnected | ||
case connecting | ||
case disconnecting | ||
case unknown | ||
|
||
static func fromNEVPNStatus(status: NEVPNStatus) -> ConnectionStatus { | ||
switch status { | ||
case .connected: return ConnectionStatus.connected | ||
case .disconnected: return ConnectionStatus.disconnected | ||
case .connecting: return ConnectionStatus.connecting | ||
case .disconnecting: return ConnectionStatus.disconnecting | ||
default: return ConnectionStatus.unknown | ||
} | ||
} | ||
|
||
func string() -> String { | ||
rawValue | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,52 @@ | ||
#if os(iOS) | ||
import Flutter | ||
import UIKit | ||
#elseif os(macOS) | ||
import Cocoa | ||
import FlutterMacOS | ||
#else | ||
#error("Unsupported platform") | ||
#endif | ||
|
||
import NetworkExtension | ||
import os | ||
import WireGuardKit | ||
|
||
public class ConnectionStatusObserver: NSObject, FlutterStreamHandler { | ||
|
||
private var _sink: FlutterEventSink? | ||
private var _vpnManager: NETunnelProviderManager | ||
|
||
private var pIsRunning: Bool = false | ||
var isRunning: Bool { | ||
pIsRunning | ||
} | ||
|
||
init(vpnManager: NETunnelProviderManager) { | ||
_vpnManager = vpnManager | ||
} | ||
|
||
public func _statusChanged(_: Notification?) { | ||
guard let sink = _sink else { | ||
return | ||
} | ||
let status = _vpnManager.connection.status | ||
let mappedStatus: Dictionary<String, String> = { | ||
switch status { | ||
case .connected: | ||
return ["status": "connected"] | ||
case .disconnected: | ||
return ["status": "connected"] | ||
case .connecting: | ||
return ["status": "connecting"] | ||
case .disconnecting: | ||
return ["status": "disconnecting"] | ||
default: | ||
return ["status": "unknown"] | ||
} | ||
}() | ||
sink(mappedStatus) | ||
} | ||
|
||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) | ||
-> FlutterError? | ||
{ | ||
_sink = events | ||
|
||
if !pIsRunning { | ||
NotificationCenter.default.addObserver( | ||
forName: NSNotification.Name.NEVPNStatusDidChange, | ||
object: nil, | ||
queue: OperationQueue.main, | ||
using: _statusChanged | ||
) | ||
class ConnectionStatusObserver: NSObject, FlutterStreamHandler { | ||
private var eventSink: FlutterEventSink? | ||
private var isRunning: Bool = false | ||
|
||
override init() { | ||
super.init() | ||
NotificationCenter.default.addObserver( | ||
forName: NSNotification.Name.NEVPNStatusDidChange, | ||
object: nil, | ||
queue: OperationQueue.main, | ||
using: handleStatusChanged | ||
) | ||
} | ||
|
||
pIsRunning = true | ||
|
||
// Send the initial data. | ||
|
||
// No errors. | ||
return nil | ||
} | ||
deinit { | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
public func onCancel(withArguments arguments: Any?) -> FlutterError? { | ||
pIsRunning = false | ||
public func handleStatusChanged(notification: Notification?) { | ||
guard let conn = notification?.object as? NEVPNConnection else { | ||
return | ||
} | ||
let newStatus = ConnectionStatus.fromNEVPNStatus(status: conn.status) | ||
|
||
NotificationCenter.default.removeObserver(self) | ||
Logger.main.debug("VPN status changed: \(newStatus.string())") | ||
eventSink?(newStatus.string()) | ||
} | ||
|
||
_sink = nil | ||
public func onListen(withArguments arguments: Any?, eventSink events: @escaping FlutterEventSink) -> FlutterError? { | ||
eventSink = events | ||
return nil | ||
} | ||
|
||
return nil | ||
} | ||
public func onCancel(withArguments arguments: Any?) -> FlutterError? { | ||
eventSink = nil | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#if os(iOS) | ||
import Flutter | ||
#elseif os(macOS) | ||
import FlutterMacOS | ||
#else | ||
#error("Unsupported platform") | ||
#endif | ||
|
||
func nativeFlutterError(message: String) -> FlutterError { | ||
FlutterError(code: "NATIVE_ERR", message: message, details: nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
import os.log | ||
|
||
extension Logger { | ||
private static var subsystem = Bundle.main.bundleIdentifier! | ||
public static let main = Logger(subsystem: subsystem, category: "main") | ||
} |
Oops, something went wrong.