forked from mschrag/speedlimit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpeedLimitPref.m
346 lines (292 loc) · 10.3 KB
/
SpeedLimitPref.m
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//
// SpeedLimitPref.m
// SpeedLimit
//
// Created by Michael Schrag on 8/27/08.
// Copyright (c) 2008 m Dimension Technology. All rights reserved.
//
#import "SpeedLimitPref.h"
#import "SLPort.h"
#import <Security/Authorization.h>
#import <Security/AuthorizationTags.h>
NSString *const PORTS_KEY = @"ports";
NSString *const DELAY_KEY = @"delay";
NSString *const SPEED_KEY = @"speed";
NSString *const RULES_KEY = @"rules";
NSString *const HOSTS_KEY = @"hosts";
NSString *const AUTH_STATE_KEY = @"authstate";
@implementation SpeedLimitPref
@synthesize speedsController;
@synthesize portsController;
@synthesize delay;
@synthesize hosts;
@synthesize speed;
@synthesize rules;
@synthesize slow;
@synthesize packetLossRatio;
@synthesize packetLossErrorSuppress;
@synthesize speedLimitLabel;
@synthesize portsView;
@synthesize hostsTextField;
@synthesize delayTextField;
@synthesize speedsPopUpButton;
@synthesize addButton;
@synthesize removeButton;
@synthesize startStopButton;
@synthesize authorizationView;
- (NSString *)speedLimiterPath {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *speedLimiterPath = [[bundle resourcePath] stringByAppendingPathComponent:@"SpeedLimiter"];
return speedLimiterPath;
}
- (void) mainViewDidLoad {
[speedsController addObject:[Speed speedWithName:@"T1" speed:1572]];
[speedsController addObject:[Speed speedWithName:@"DSL" speed:768]];
[speedsController addObject:[Speed speedWithName:@"3G" speed:384]];
[speedsController addObject:[Speed speedWithName:@"Edge" speed:64]];
[speedsController addObject:[Speed speedWithName:@"Dialup" speed:48]];
}
- (NSString *)execute:(NSString *)command withArguments:(NSArray *)arguments {
NSMutableArray *finalArguments = [NSMutableArray array];
[finalArguments addObject:command];
[finalArguments addObjectsFromArray:arguments];
char *args[[finalArguments count] + 2];
args[0] = (char *)[command cStringUsingEncoding:NSUTF8StringEncoding];
NSInteger argNum = 0;
for (NSString *argument in finalArguments) {
args[argNum ++] = (char *)[argument cStringUsingEncoding:NSUTF8StringEncoding];
}
args[argNum] = NULL;
FILE *ipfwPipe;
NSString *ipfwPipeStr = nil;
OSStatus err = AuthorizationExecuteWithPrivileges(authorizationRef, [[self speedLimiterPath] fileSystemRepresentation], 0, args, &ipfwPipe);
if (err == errAuthorizationSuccess) {
NSFileHandle *ipfwPipeHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileno(ipfwPipe)];
ipfwPipeStr = [[NSString alloc] initWithData:[ipfwPipeHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];
// NSLog(@"SpeedLimiter output = %@", ipfwPipeStr);
}
return [ipfwPipeStr autorelease];
}
- (void)saveSettings {
NSMutableDictionary *prefs = [NSMutableDictionary dictionary];
NSMutableArray *ports = [NSMutableArray array];
for (SLPort *thePort in [portsController arrangedObjects]) {
[ports addObject:[[thePort port] stringValue]];
}
[prefs setObject:ports forKey:PORTS_KEY];
if (self.delay) {
[prefs setObject:self.delay forKey:DELAY_KEY];
}
if (speed) {
[prefs setObject:[NSNumber numberWithInteger:speed.speed] forKey:SPEED_KEY];
}
if (rules) {
[prefs setObject:rules forKey:RULES_KEY];
}
if (self.hosts) {
[prefs setObject:self.hosts forKey:HOSTS_KEY];
}
if (authorizationView) {
[prefs setObject:[NSNumber numberWithInteger:[authorizationView authorizationState]] forKey:AUTH_STATE_KEY];
}
[[NSUserDefaults standardUserDefaults] removePersistentDomainForName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
[[NSUserDefaults standardUserDefaults] setPersistentDomain:prefs forName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
}
- (NSArray *)rulesForCommand:(NSString *)command withArguments:(NSArray *)arguments {
NSMutableArray *returnRules = [NSMutableArray array];
NSArray *lines = [[self execute:command withArguments:arguments] componentsSeparatedByString:@"\n"];
for (NSString *line in lines) {
if ([line length] > 0) {
NSInteger ruleNumber = [line integerValue];
if (!ruleNumber) {
[returnRules removeAllObjects];
}
else {
[returnRules addObject:[NSString stringWithFormat:@"%ld", ruleNumber]];
}
}
}
return returnRules;
}
- (void)updateStatus {
self.slow = [self.rules count];
if (self.slow) {
[startStopButton setTitle:@"Speed Up"];
[speedLimitLabel setStringValue:[NSString stringWithFormat:@"%ld", speed.speed]];
}
else {
[speedLimitLabel setStringValue:@"-"];
[startStopButton setTitle:@"Slow Down"];
}
}
- (void)refreshRules {
NSArray *previousRules = self.rules;
if ([previousRules count]) {
NSArray *returnRules = [self rulesForCommand:@"list" withArguments:previousRules];
if ([returnRules count] != [previousRules count]) {
self.rules = nil;
[self saveSettings];
}
}
[self updateStatus];
}
- (void)releaseAuthorization {
if (authorizationRef != NULL) {
AuthorizationFree(authorizationRef, kAuthorizationFlagDestroyRights);
authorizationRef = NULL;
}
}
- (void)willSelect {
NSDictionary *prefs = [[NSUserDefaults standardUserDefaults] persistentDomainForName:[[NSBundle bundleForClass:[self class]] bundleIdentifier]];
NSArray *previousPorts = [prefs objectForKey:PORTS_KEY];
[portsController removeObjects:[portsController arrangedObjects]];
if ([previousPorts count]) {
for (NSString *object in [previousPorts objectEnumerator]) {
SLPort *newPort = [[SLPort alloc] initWithPort:[object intValue]];
[portsController addObject:newPort];
[newPort release];
}
}
else {
[portsController addObject:[[[SLPort alloc] initWithPort:80] autorelease]];
[portsController addObject:[[[SLPort alloc] initWithPort:443] autorelease]];
}
[portsController setSelectedObjects:nil];
NSString *previousDelay = [prefs objectForKey:DELAY_KEY];
if (previousDelay) {
self.delay = previousDelay;
}
else {
self.delay = @"250";
}
NSString *previousHosts = [prefs objectForKey:HOSTS_KEY];
if (previousHosts) {
self.hosts = previousHosts;
}
else {
self.hosts = @"";
}
NSInteger previousSpeed = [[prefs objectForKey:SPEED_KEY] integerValue];
if (previousSpeed) {
for (Speed *loopSpeed in [speedsController arrangedObjects]) {
if (loopSpeed.speed == previousSpeed) {
self.speed = loopSpeed;
}
}
}
authorizationState = [[prefs objectForKey:AUTH_STATE_KEY] integerValue];
self.rules = [prefs objectForKey:RULES_KEY];
[speedLimitLabel setStringValue:@"-"];
if (authorizationState == SFAuthorizationViewLockedState)
[startStopButton setTitle:@"-"];
else
[startStopButton setTitle:@"Slow Down"];
}
- (void)didSelect {
if (authorizationView) {
const char *path = [[self speedLimiterPath] fileSystemRepresentation];
AuthorizationItem right = { kAuthorizationRightExecute, strlen(path), (char *)path, 0 };
AuthorizationRights rights = { 1, &right };
[authorizationView setDelegate:self];
[authorizationView setAuthorizationRights:&rights];
[authorizationView updateStatus:self];
}
}
- (void)willUnselect {
[self saveSettings];
}
-(void)dealloc {
[speedsController release];
[portsController release];
[speedLimitLabel release];
[portsView release];
[hostsTextField release];
[delayTextField release];
[speedsPopUpButton release];
[addButton release];
[removeButton release];
[startStopButton release];
[hosts release];
[delay release];
[rules release];
[authorizationView release];
[super dealloc];
}
- (void)enableInterfaces:(BOOL)enable {
[portsView setEnabled:enable];
[hostsTextField setEnabled:enable];
[delayTextField setEnabled:enable];
[speedsPopUpButton setEnabled:enable];
[addButton setEnabled:enable];
[removeButton setEnabled:enable];
[startStopButton setEnabled:enable];
if (!enable)
[startStopButton setTitle:@"-"];
}
-(IBAction)addPort:(id)sender {
SLPort *newPort = [[SLPort alloc] initWithPort:1000];
[portsController addObject:newPort];
[newPort release];
}
-(IBAction)removePort:(id)sender {
[portsController removeObjects:[portsController selectedObjects]];
}
-(IBAction)toggle:(id)sender {
[self refreshRules];
if (!self.slow) {
NSArray *ports = [self.portsController arrangedObjects];
NSMutableArray *portStrings = [NSMutableArray array];
for (SLPort *thePort in ports) {
[portStrings addObject:[[thePort port] stringValue]];
}
if (self.speed && [portStrings count]) {
NSString *finalSpeed = [NSString stringWithFormat:@"%ld", speed.speed];
NSString *finalDelay = (self.delay == nil || [self.delay length] == 0) ? 0 : self.delay;
NSString *finalHosts = (self.hosts == nil) ? @"" : self.hosts;
NSString *finalPacketLossRatio = [[NSNumber numberWithDouble:self.packetLossRatio] stringValue];
NSString *finalPacketLossErrorSuppress = (self.packetLossErrorSuppress) ? @"yes" : @"no";
NSMutableArray *arguments = [NSMutableArray array];
[arguments addObject:finalSpeed];
[arguments addObject:finalDelay];
[arguments addObject:finalPacketLossRatio];
[arguments addObject:finalPacketLossErrorSuppress];
[arguments addObject:finalHosts];
[arguments addObjectsFromArray:portStrings];
self.rules = [self rulesForCommand:@"start" withArguments:arguments];
[self saveSettings];
[self updateStatus];
if (![self.rules count]) {
[[NSAlert alertWithMessageText:@"Failed to set speed limit." defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal];
}
}
else {
[[NSAlert alertWithMessageText:@"You must select a speed and at least one port." defaultButton:@"OK" alternateButton:nil otherButton:nil informativeTextWithFormat:@""] runModal];
}
}
else {
if (self.rules) {
[self execute:@"stop" withArguments:self.rules];
}
self.rules = nil;
[self saveSettings];
[self updateStatus];
}
}
#pragma mark SFAuthorizationView delegate methods
- (void)authorizationViewCreatedAuthorization:(SFAuthorizationView *)view {
authorizationRef = [[view authorization] authorizationRef];
}
- (void)authorizationViewDidAuthorize:(SFAuthorizationView *)view {
[self refreshRules];
[self enableInterfaces:YES];
}
- (void)authorizationViewDidDeauthorize:(SFAuthorizationView *)view {
[self enableInterfaces:NO];
}
- (void)authorizationViewReleasedAuthorization:(SFAuthorizationView *)view {
[self releaseAuthorization];
}
- (BOOL)authorizationViewShouldDeauthorize:(SFAuthorizationView *)view {
return YES;
}
@end