blob: 7ed22c4a9320468fbbf56fe68397cef44298926b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* ZeroTier One - Network Virtualization Everywhere
* Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#import "NetworkMonitor.h"
#import "Network.h"
#import "ServiceCom.h"
#import "NodeStatus.h"
@import AppKit;
NSString * const NetworkUpdateKey = @"com.zerotier.one.network-list";
NSString * const StatusUpdateKey = @"com.zerotier.one.status";
@interface NetworkMonitor (private)
- (NSString*)dataFile;
- (void)internal_updateNetworkInfo;
- (NSInteger)findNetworkWithID:(UInt64)networkId;
- (NSInteger)findSavedNetworkWithID:(UInt64)networkId;
- (void)saveNetworks;
@end
@implementation NetworkMonitor
- (id)init
{
self = [super init];
if(self)
{
_savedNetworks = [NSMutableArray<Network*> array];
_receivedNetworks = [NSArray<Network*> array];
_allNetworks = [NSMutableArray<Network*> array];
_timer = nil;
}
return self;
}
- (void)dealloc
{
[_timer invalidate];
}
- (void)start
{
NSLog(@"ZeroTier monitor started");
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:self
selector:@selector(updateNetworkInfo)
userInfo:nil
repeats:YES];
}
- (void)stop
{
NSLog(@"ZeroTier monitor stopped");
[_timer invalidate];
_timer = nil;
}
- (void)updateNetworkInfo
{
NSString *filePath = [self dataFile];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray<Network*> *networks = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
if(networks != nil) {
_savedNetworks = [networks mutableCopy];
}
}
NSError *error = nil;
[[ServiceCom sharedInstance] getNetworklist:^(NSArray<Network *> *networkList) {
_receivedNetworks = networkList;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[self internal_updateNetworkInfo];
} ];
} error:&error];
if(error) {
[self stop];
NSAlert *alert = [NSAlert alertWithError:error];
alert.alertStyle = NSCriticalAlertStyle;
[alert addButtonWithTitle:@"Quit"];
[alert addButtonWithTitle:@"Retry"];
NSModalResponse res = [alert runModal];
if(res == NSAlertFirstButtonReturn) {
[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
}
else if(res == NSAlertSecondButtonReturn) {
[self start];
return;
}
}
[[ServiceCom sharedInstance] getNodeStatus:^(NodeStatus *status) {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:status forKey:@"status"];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:StatusUpdateKey
object:nil
userInfo:userInfo];
}];
} error:&error];
if (error) {
[self stop];
NSAlert *alert = [NSAlert alertWithError:error];
alert.alertStyle = NSCriticalAlertStyle;
[alert addButtonWithTitle:@"Quit"];
[alert addButtonWithTitle:@"Retry"];
NSModalResponse res = [alert runModal];
if(res == NSAlertFirstButtonReturn) {
[NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
}
else if(res == NSAlertSecondButtonReturn) {
[self start];
return;
}
}
}
- (void)deleteSavedNetwork:(NSString*)networkId
{
UInt64 nwid = 0;
NSScanner *scanner = [NSScanner scannerWithString:networkId];
[scanner scanHexLongLong:&nwid];
NSInteger index = [self findNetworkWithID:nwid];
if(index != NSNotFound) {
[_allNetworks removeObjectAtIndex:index];
}
index = [self findSavedNetworkWithID:nwid];
if(index != NSNotFound) {
[_savedNetworks removeObjectAtIndex:index];
}
[self saveNetworks];
}
@end
@implementation NetworkMonitor (private)
- (NSString*)dataFile
{
NSURL *appSupport = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory
inDomains:NSUserDomainMask] objectAtIndex:0];
appSupport = [[[appSupport URLByAppendingPathComponent:@"ZeroTier"] URLByAppendingPathComponent:@"One"] URLByAppendingPathComponent:@"networkinfo.dat"];
return appSupport.path;
}
- (void)internal_updateNetworkInfo
{
NSMutableArray<Network*> *networks = [_savedNetworks mutableCopy];
for(Network *nw in _receivedNetworks) {
NSInteger index = [self findSavedNetworkWithID:nw.nwid];
if(index != NSNotFound) {
[networks setObject:nw atIndexedSubscript:index];
}
else {
[networks addObject:nw];
}
}
[networks sortUsingComparator:^NSComparisonResult(Network *obj1, Network *obj2) {
if(obj1.nwid > obj2.nwid) {
return true;
}
return false;
}];
@synchronized(_allNetworks) {
_allNetworks = networks;
}
[self saveNetworks];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:networks forKey:@"networks"];
[[NSNotificationCenter defaultCenter] postNotificationName:NetworkUpdateKey
object:nil
userInfo:userInfo];
}
- (NSInteger)findNetworkWithID:(UInt64)networkId
{
for(int i = 0; i < [_allNetworks count]; ++i) {
Network *nw = [_allNetworks objectAtIndex:i];
if(nw.nwid == networkId) {
return i;
}
}
return NSNotFound;
}
- (NSInteger)findSavedNetworkWithID:(UInt64)networkId
{
for(int i = 0; i < [_savedNetworks count]; ++i) {
Network *nw = [_savedNetworks objectAtIndex:i];
if(nw.nwid == networkId) {
return i;
}
}
return NSNotFound;
}
- (void)saveNetworks
{
NSString *filePath = [self dataFile];
@synchronized(_allNetworks) {
[NSKeyedArchiver archiveRootObject:_allNetworks toFile:filePath];
}
}
@end
|