1 | export var connectionType;
|
2 | (function (connectionType) {
|
3 | connectionType[connectionType["none"] = 0] = "none";
|
4 | connectionType[connectionType["wifi"] = 1] = "wifi";
|
5 | connectionType[connectionType["mobile"] = 2] = "mobile";
|
6 | connectionType[connectionType["ethernet"] = 3] = "ethernet";
|
7 | connectionType[connectionType["bluetooth"] = 4] = "bluetooth";
|
8 | connectionType[connectionType["vpn"] = 5] = "vpn";
|
9 | })(connectionType || (connectionType = {}));
|
10 |
|
11 | function _createReachability(host) {
|
12 | if (host) {
|
13 | return SCNetworkReachabilityCreateWithName(null, host);
|
14 | }
|
15 | else {
|
16 | const zeroAddress = new interop.Reference(sockaddr, {
|
17 | sa_len: 16,
|
18 | sa_family: 2,
|
19 | });
|
20 | return SCNetworkReachabilityCreateWithAddress(null, zeroAddress);
|
21 | }
|
22 | }
|
23 | function _getReachabilityFlags(host) {
|
24 | const reachability = _createReachability(host);
|
25 | const flagsRef = new interop.Reference();
|
26 | const gotFlags = SCNetworkReachabilityGetFlags(reachability, flagsRef);
|
27 | if (!gotFlags) {
|
28 | return null;
|
29 | }
|
30 | return flagsRef.value;
|
31 | }
|
32 | function _getConnectionType(host) {
|
33 | const flags = _getReachabilityFlags(host);
|
34 | return _getConnectionTypeFromFlags(flags);
|
35 | }
|
36 | function _getConnectionTypeFromFlags(flags) {
|
37 | if (!flags) {
|
38 | return connectionType.none;
|
39 | }
|
40 | const isReachable = flags & 2 ;
|
41 | const connectionRequired = flags & 4 ;
|
42 | if (!isReachable || connectionRequired) {
|
43 | return connectionType.none;
|
44 | }
|
45 | const isWWAN = flags & 262144 ;
|
46 | if (isWWAN) {
|
47 | return connectionType.mobile;
|
48 | }
|
49 | let keys;
|
50 | if (typeof CFNetworkCopySystemProxySettings !== 'undefined') {
|
51 | const cfDict = CFNetworkCopySystemProxySettings();
|
52 |
|
53 | if (cfDict && cfDict.takeUnretainedValue) {
|
54 | const nsDict = cfDict.takeUnretainedValue();
|
55 | keys = nsDict.objectForKey('__SCOPED__');
|
56 | }
|
57 | }
|
58 | if (isVPNConnected(keys)) {
|
59 | return connectionType.vpn;
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 | if (isBluetoothConnected(keys)) {
|
67 | return connectionType.bluetooth;
|
68 | }
|
69 | return connectionType.wifi;
|
70 | }
|
71 | function isBluetoothConnected(keys) {
|
72 | if (!keys) {
|
73 | return false;
|
74 | }
|
75 | const allKeys = keys.allKeys;
|
76 | const size = allKeys.count;
|
77 | let isBlueTooth = false;
|
78 | for (let i = 0; i < size; i++) {
|
79 | const key = allKeys.objectAtIndex(i);
|
80 | if (key === 'en4') {
|
81 | isBlueTooth = true;
|
82 | break;
|
83 | }
|
84 | }
|
85 | return isBlueTooth;
|
86 | }
|
87 | function isVPNConnected(keys) {
|
88 | if (!keys) {
|
89 | return false;
|
90 | }
|
91 | const allKeys = keys.allKeys;
|
92 | const size = allKeys.count;
|
93 | let isVPN = false;
|
94 | for (let i = 0; i < size; i++) {
|
95 | const key = allKeys.objectAtIndex(i);
|
96 | if (key === 'tap' || key === 'tun' || key === 'ppp' || key === 'ipsec' || key === 'ipsec0' || key === 'utun1') {
|
97 | isVPN = true;
|
98 | break;
|
99 | }
|
100 | }
|
101 | return isVPN;
|
102 | }
|
103 | export function getConnectionType() {
|
104 | return _getConnectionType();
|
105 | }
|
106 |
|
107 | function _reachabilityCallback(target, flags, info) {
|
108 | if (_connectionTypeChangedCallback) {
|
109 | const newConnectionType = _getConnectionTypeFromFlags(flags);
|
110 | _connectionTypeChangedCallback(newConnectionType);
|
111 | }
|
112 | }
|
113 | const _reachabilityCallbackFunctionRef = new interop.FunctionReference(_reachabilityCallback);
|
114 | let _monitorReachabilityRef;
|
115 | let _connectionTypeChangedCallback;
|
116 | export function startMonitoring(connectionTypeChangedCallback) {
|
117 | if (!_monitorReachabilityRef) {
|
118 | _monitorReachabilityRef = _createReachability();
|
119 | _connectionTypeChangedCallback = zonedCallback(connectionTypeChangedCallback);
|
120 | SCNetworkReachabilitySetCallback(_monitorReachabilityRef, _reachabilityCallbackFunctionRef, null);
|
121 | SCNetworkReachabilityScheduleWithRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
122 | _connectionTypeChangedCallback(_getConnectionType());
|
123 | }
|
124 | }
|
125 | export function stopMonitoring() {
|
126 | if (_monitorReachabilityRef) {
|
127 | SCNetworkReachabilityUnscheduleFromRunLoop(_monitorReachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
|
128 | _monitorReachabilityRef = undefined;
|
129 | _connectionTypeChangedCallback = undefined;
|
130 | }
|
131 | }
|
132 |
|
\ | No newline at end of file |