Skip to content

Commit 5ab8a33

Browse files
author
Alexandre Colucci
committed
1 parent ab24239 commit 5ab8a33

40 files changed

Lines changed: 3234 additions & 21 deletions

Common/ACConnectionManager.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//
2+
// ACConnectionManager.h
3+
// VPN
4+
//
5+
// Created by Alexandre Colucci on 07.07.2018.
6+
// Copyright © 2018 Timac. All rights reserved.
7+
//
8+
// This class contains the logic to auto connect to a service
9+
//
10+
11+
#import <Cocoa/Cocoa.h>
12+
13+
@class ACNEService;
14+
15+
@interface ACConnectionManager : NSObject
16+
17+
18+
/**
19+
Get the singleton
20+
*/
21+
+ (ACConnectionManager *)sharedManager;
22+
23+
24+
/**
25+
Connect or disconnect the VPN service based on its current state
26+
*/
27+
-(void)toggleConnectionForService:(ACNEService *)inService;
28+
29+
/**
30+
Save the preferences for the ACNEService and start the timer for auto connecting
31+
*/
32+
-(void)setAlwaysAutoConnect:(BOOL)inAlwaysAutoConnect forACNEService:(ACNEService *)inNEService;
33+
34+
/**
35+
Pause the auto connect feature for inDuration seconds
36+
*/
37+
-(void)pauseAutoConnect:(NSInteger)inDuration;
38+
39+
/**
40+
Resume auto connect
41+
*/
42+
-(void)resumeAutoConnect;
43+
44+
/**
45+
Has the auto connect feature been paused by the user?
46+
*/
47+
-(BOOL)isAutoConnectPaused;
48+
49+
/**
50+
If auto connect is paused, returns the duration set by the user
51+
*/
52+
-(NSInteger)currentPauseDuration;
53+
54+
/**
55+
Return YES if at least one VPN service has been set to auto connect
56+
*/
57+
-(BOOL)isAtLeastOneServiceSetToAutoConnect;
58+
59+
60+
/**
61+
Disconnect all the services marked as always auto connect
62+
*/
63+
-(void)disconnectAllAutoConnectedServices;
64+
65+
/**
66+
Connect all the services marked as always auto connect
67+
*/
68+
-(void)connectAllAutoConnectedServices;
69+
70+
@end
71+

Common/ACConnectionManager.m

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
//
2+
// ACConnectionManager.m
3+
// VPN
4+
//
5+
// Created by Alexandre Colucci on 07.07.2018.
6+
// Copyright © 2018 Timac. All rights reserved.
7+
//
8+
9+
#import "ACConnectionManager.h"
10+
#import "ACNEService.h"
11+
#import "ACNEServicesManager.h"
12+
#import "ACPreferences.h"
13+
14+
@interface ACConnectionManager ()
15+
16+
// Timer to try to reconnect services set to always auto connect
17+
@property (strong) NSTimer *alwaysAutoConnectTimer;
18+
19+
// Time when the pause for auto connect was started
20+
@property (assign) CFAbsoluteTime startPauseTime;
21+
22+
// Duration of the pause
23+
@property (assign) NSInteger pauseDuration;
24+
25+
@end
26+
27+
28+
@implementation ACConnectionManager
29+
30+
+ (ACConnectionManager *)sharedManager
31+
{
32+
static ACConnectionManager *sSharedManager = nil;
33+
if(sSharedManager == nil)
34+
{
35+
sSharedManager = [[ACConnectionManager alloc] init];
36+
}
37+
38+
return sSharedManager;
39+
}
40+
41+
- (instancetype)init
42+
{
43+
self = [super init];
44+
if (self)
45+
{
46+
[self startAlwaysAutoConnectTimer];
47+
}
48+
return self;
49+
}
50+
51+
-(void)toggleConnectionForService:(ACNEService *)inService
52+
{
53+
if(inService == nil)
54+
return;
55+
56+
SCNetworkConnectionStatus serviceState = [inService state];
57+
58+
switch(serviceState)
59+
{
60+
case kSCNetworkConnectionDisconnected:
61+
{
62+
// Connect
63+
[inService connect];
64+
}
65+
break;
66+
67+
case kSCNetworkConnectionConnected:
68+
{
69+
// Disconnect
70+
[inService disconnect];
71+
}
72+
break;
73+
74+
default:
75+
break;
76+
}
77+
}
78+
79+
-(void)startConnectionForService:(NSString *)inServiceIdentifier
80+
{
81+
if([inServiceIdentifier length] <= 0)
82+
return;
83+
84+
// Get all services and find the correct NEService
85+
NSArray <ACNEService*>* neServices = [[ACNEServicesManager sharedNEServicesManager] neServices];
86+
87+
ACNEService *foundNEService = nil;
88+
for(ACNEService *neService in neServices)
89+
{
90+
if([inServiceIdentifier isEqualToString:[neService.configuration.identifier UUIDString]])
91+
{
92+
foundNEService = neService;
93+
break;
94+
}
95+
}
96+
97+
// Connect to the service if it is currently disconnected
98+
if(foundNEService != nil)
99+
{
100+
if([foundNEService state] == kSCNetworkConnectionDisconnected)
101+
{
102+
[foundNEService connect];
103+
}
104+
}
105+
}
106+
107+
-(void)startAlwaysAutoConnectTimer
108+
{
109+
// Recreate the timer
110+
if(self.alwaysAutoConnectTimer != nil)
111+
{
112+
[self.alwaysAutoConnectTimer invalidate];
113+
self.alwaysAutoConnectTimer = nil;
114+
}
115+
116+
self.alwaysAutoConnectTimer = [[NSTimer alloc] initWithFireDate:[NSDate date] interval:[[ACPreferences sharedPreferences] alwaysConnectedRetryDelay] repeats:YES block:^(NSTimer * timer)
117+
{
118+
// Each time the timer fires, execute this block
119+
if(![self isAutoConnectPaused])
120+
{
121+
NSArray<NSString *>*alwaysConnectedServicesIdentifiers = [[ACPreferences sharedPreferences] alwaysConnectedServicesIdentifiers];
122+
for(NSString *serviceIdentifier in alwaysConnectedServicesIdentifiers)
123+
{
124+
[self startConnectionForService:serviceIdentifier];
125+
}
126+
}
127+
}];
128+
129+
// Add the timer to the RunLoop
130+
[[NSRunLoop currentRunLoop] addTimer:self.alwaysAutoConnectTimer forMode:NSDefaultRunLoopMode];
131+
}
132+
133+
-(void)setAlwaysAutoConnect:(BOOL)inAlwaysAutoConnect forACNEService:(ACNEService *)inNEService
134+
{
135+
if(inNEService == nil)
136+
return;
137+
138+
// Save the preferences
139+
[[ACPreferences sharedPreferences] setAlwaysConnected:inAlwaysAutoConnect forServicesIdentifier:[inNEService.configuration.identifier UUIDString]];
140+
141+
// Start the Timer
142+
[self startAlwaysAutoConnectTimer];
143+
}
144+
145+
-(void)pauseAutoConnect:(NSInteger)inDuration
146+
{
147+
self.startPauseTime = CFAbsoluteTimeGetCurrent();
148+
self.pauseDuration = inDuration;
149+
}
150+
151+
-(void)resumeAutoConnect
152+
{
153+
self.startPauseTime = 0;
154+
self.pauseDuration = 0;
155+
}
156+
157+
-(BOOL)isAutoConnectPaused
158+
{
159+
if(self.pauseDuration == NSIntegerMax)
160+
{
161+
return YES;
162+
}
163+
else if(self.startPauseTime + self.pauseDuration > CFAbsoluteTimeGetCurrent())
164+
{
165+
return YES;
166+
}
167+
168+
return NO;
169+
}
170+
171+
-(NSInteger)currentPauseDuration
172+
{
173+
return self.pauseDuration;
174+
}
175+
176+
-(BOOL)isAtLeastOneServiceSetToAutoConnect
177+
{
178+
BOOL outCanEnableAutoConnect = NO;
179+
180+
NSArray<NSString *>*alwaysConnectedServicesIdentifiers = [[ACPreferences sharedPreferences] alwaysConnectedServicesIdentifiers];
181+
if([alwaysConnectedServicesIdentifiers count] <= 0)
182+
return outCanEnableAutoConnect;
183+
184+
// Check each service
185+
NSArray <ACNEService*>* neServices = [[ACNEServicesManager sharedNEServicesManager] neServices];
186+
for(ACNEService *neService in neServices)
187+
{
188+
if([alwaysConnectedServicesIdentifiers containsObject:[neService.configuration.identifier UUIDString]])
189+
{
190+
outCanEnableAutoConnect = YES;
191+
break;
192+
}
193+
}
194+
195+
return outCanEnableAutoConnect;
196+
}
197+
198+
-(void)disconnectAllAutoConnectedServices
199+
{
200+
NSArray<NSString *>*alwaysConnectedServicesIdentifiers = [[ACPreferences sharedPreferences] alwaysConnectedServicesIdentifiers];
201+
if([alwaysConnectedServicesIdentifiers count] <= 0)
202+
return;
203+
204+
// Disconnect each service marked as always auto connecting
205+
NSArray <ACNEService*>* neServices = [[ACNEServicesManager sharedNEServicesManager] neServices];
206+
for(ACNEService *neService in neServices)
207+
{
208+
if([alwaysConnectedServicesIdentifiers containsObject:[neService.configuration.identifier UUIDString]])
209+
{
210+
[neService disconnect];
211+
}
212+
}
213+
}
214+
215+
-(void)connectAllAutoConnectedServices
216+
{
217+
NSArray<NSString *>*alwaysConnectedServicesIdentifiers = [[ACPreferences sharedPreferences] alwaysConnectedServicesIdentifiers];
218+
if([alwaysConnectedServicesIdentifiers count] <= 0)
219+
return;
220+
221+
// Connect each service marked as always auto connecting
222+
NSArray <ACNEService*>* neServices = [[ACNEServicesManager sharedNEServicesManager] neServices];
223+
for(ACNEService *neService in neServices)
224+
{
225+
if([alwaysConnectedServicesIdentifiers containsObject:[neService.configuration.identifier UUIDString]])
226+
{
227+
[neService connect];
228+
}
229+
}
230+
}
231+
232+
@end

0 commit comments

Comments
 (0)