-
Notifications
You must be signed in to change notification settings - Fork 20
/
Tweak.xm
150 lines (126 loc) · 5.52 KB
/
Tweak.xm
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
#include <notify.h>
#import "LSStatusBarItem.h"
#import "MBWiFiProxyInfo.h"
static BOOL enabled = NO;
static BOOL alwaysShow = YES;
static NSUInteger type = 0;
static MBWiFiProxyInfo *proxyInfo;
static LSStatusBarItem *statusBarItem;
@interface SBWiFiManager : NSObject
+ (instancetype)sharedInstance;
- (void)_powerStateDidChange;
- (NSString *)currentNetworkName;
- (BOOL)wiFiEnabled;
@end
@interface RadiosPreferences : NSObject
@property (nonatomic) BOOL airplaneMode;
@end
static BOOL canShowStatusBarIcon() {
return alwaysShow ? YES : [[%c(SBWiFiManager) sharedInstance] wiFiEnabled] && [[%c(SBWiFiManager) sharedInstance] currentNetworkName];
}
static void updateStatusBarImage() {
statusBarItem.imageName = type > 0 ? @"ProxySwitcher" : @"ProxySwitcherUnselected";
}
static void setStatusBarVisible(BOOL visible) {
statusBarItem.visible = enabled && canShowStatusBarIcon() ? visible : NO;
}
static void networkChanged() {
if (!enabled) { return; }
setStatusBarVisible(![[[%c(RadiosPreferences) alloc] init] airplaneMode]);
if ([[%c(SBWiFiManager) sharedInstance] wiFiEnabled] && [[%c(SBWiFiManager) sharedInstance] currentNetworkName]) {
if (type == 1) {
notify_post("com.mbo42.proxyswitcherd.enable");
} else {
notify_post("com.mbo42.proxyswitcherd.disable");
}
}
}
static void loadPreferences() {
CFArrayRef keyList = CFPreferencesCopyKeyList(CFSTR("com.mbo42.proxyswitcher"), kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
NSDictionary *preferences;
if (keyList) {
preferences = (__bridge NSDictionary *)CFPreferencesCopyMultiple(keyList,
CFSTR("com.mbo42.proxyswitcher"),
kCFPreferencesCurrentUser,
kCFPreferencesAnyHost);
if (!preferences) {
preferences = [NSDictionary dictionary];
}
CFRelease(keyList);
}
enabled = [preferences objectForKey:@"enabled"] ? [[preferences objectForKey:@"enabled"] boolValue] : YES;
alwaysShow = [preferences objectForKey:@"alwaysShow"] ? [[preferences objectForKey:@"alwaysShow"] boolValue] : YES;
proxyInfo = [MBWiFiProxyInfo infoFromDictionary:preferences];
type = [preferences objectForKey:@"type"] ? [[preferences objectForKey:@"type"] integerValue] : 0;
notify_post("com.mbo42.proxyswitcherd.refreshPreferences");
setStatusBarVisible(![[[%c(RadiosPreferences) alloc] init] airplaneMode]);
updateStatusBarImage();
networkChanged();
}
static void saveNewType() {
CFPreferencesSetAppValue(CFSTR("type"),
CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &type),
CFSTR("com.mbo42.proxyswitcher"));
networkChanged();
updateStatusBarImage();
}
static void toggleProxy() {
type = !type ? 1 : 0;
saveNewType();
}
%hook SpringBoard
- (void)applicationDidFinishLaunching:(id)arg1 {
%orig;
statusBarItem = [[%c(LSStatusBarItem) alloc] initWithIdentifier:@"com.mbo42.proxyswitcher" alignment:StatusBarAlignmentRight];
statusBarItem.imageName = @"ProxySwitcher";
loadPreferences();
}
%end
%hook RadiosPreferences
- (void)setAirplaneMode:(BOOL)airplaneMode {
%orig;
setStatusBarVisible(!airplaneMode);
}
%end
%hook _UIAlertControllerView
- (void)setAlertController:(id)controller {
%orig;
if ([controller isKindOfClass:[UIAlertController class]]) {
UIAlertController *alertVC = (UIAlertController *)controller;
NSString *message = alertVC.message;
if (!proxyInfo.server.length || !proxyInfo.port || !proxyInfo.username.length || !proxyInfo.password.length) {
return;
}
if (message &&
[message rangeOfString:proxyInfo.server].location != NSNotFound &&
[message rangeOfString:[proxyInfo.port stringValue]].location != NSNotFound) {
if (alertVC.textFields.count > 1) {
UITextField *usernameField = alertVC.textFields[0];
UITextField *passwordField = alertVC.textFields[1];
usernameField.text = proxyInfo.username;
passwordField.text = proxyInfo.password;
}
}
}
}
%end
%ctor {
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)networkChanged,
CFSTR("com.apple.system.config.network_change"),
NULL,
CFNotificationSuspensionBehaviorDeliverImmediately);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)loadPreferences,
CFSTR("com.mbo42.proxyswitcher/settingschanged"),
NULL,
CFNotificationSuspensionBehaviorCoalesce);
CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
NULL,
(CFNotificationCallback)toggleProxy,
CFSTR("com.mbo42.proxyswitcheruikit/didTapOnStatusBar"),
NULL,
CFNotificationSuspensionBehaviorCoalesce);
}