Skip to content

Commit 85df64f

Browse files
authored
Merge pull request #1 from lysiszegerman/master
added list and status functions
2 parents 5ab8a33 + 1509e9e commit 85df64f

File tree

1 file changed

+95
-29
lines changed

1 file changed

+95
-29
lines changed

vpnutil/main.m

+95-29
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ static void PrintUsage()
2222
fprintf(stderr, "\t To stop the VPN called 'MyVPN':\n");
2323
fprintf(stderr, "\t vpnutil stop MyVPN\n");
2424
fprintf(stderr, "\n");
25+
fprintf(stderr, "\t To list all available VPNs and their state:\n");
26+
fprintf(stderr, "\t vpnutil list\n");
27+
fprintf(stderr, "\n");
28+
fprintf(stderr, "\t To get the status of the VPN called 'MyVPN':\n");
29+
fprintf(stderr, "\t vpnutil status MyVPN\n");
30+
fprintf(stderr, "\n");
2531
fprintf(stderr, "Copyright © 2018 Alexandre Colucci\nblog.timac.org\n");
2632
fprintf(stderr, "\n");
2733

@@ -77,38 +83,73 @@ int main(int argc, const char * argv[])
7783
{
7884
@autoreleasepool
7985
{
80-
if (argc != 3)
81-
{
82-
PrintUsage();
83-
}
8486

8587
// Do we want to start or stop the service?
8688
BOOL shouldStartService = YES;
87-
NSString *parameter1 = [NSString stringWithUTF8String:argv[1]];
89+
BOOL statusService = NO;
90+
BOOL needServiceName = NO;
91+
BOOL listService = NO;
92+
int numArgs = 0;
93+
94+
if (argc <= 1)
95+
{
96+
PrintUsage();
97+
}
98+
99+
NSString *parameter1 = [NSString stringWithUTF8String:argv[1]];
88100
if ([parameter1 isEqualToString:@"start"])
89101
{
90102
shouldStartService = YES;
103+
needServiceName = YES;
104+
numArgs = 3;
91105
}
92106
else if ([parameter1 isEqualToString:@"stop"])
93107
{
94108
shouldStartService = NO;
109+
needServiceName = YES;
110+
numArgs = 3;
95111
}
112+
else if ([parameter1 isEqualToString:@"list"])
113+
{
114+
needServiceName = NO;
115+
shouldStartService = NO;
116+
listService = YES;
117+
numArgs = 2;
118+
}
119+
else if ([parameter1 isEqualToString:@"status"])
120+
{
121+
statusService = YES;
122+
needServiceName = YES;
123+
shouldStartService = NO;
124+
listService = NO;
125+
numArgs = 3;
126+
}
96127
else
97128
{
98129
PrintUsage();
99130
}
100131

132+
if (argc != numArgs)
133+
{
134+
PrintUsage();
135+
}
136+
101137
// Get the VPN name?
102-
NSString *vpnName = [NSString stringWithUTF8String:argv[2]];
103-
if ([vpnName length] <= 0)
104-
{
105-
PrintUsage();
106-
}
138+
__block NSString *vpnName;
139+
if (needServiceName)
140+
{
141+
vpnName = [NSString stringWithUTF8String:argv[2]];
142+
if ([vpnName length] <= 0)
143+
{
144+
PrintUsage();
145+
}
146+
}
107147

108148
// Since this is a command line tool, we manually run an NSRunLoop
109149
__block ACNEService *foundNEService = NULL;
110150
__block BOOL keepRunning = YES;
111-
151+
__block NSArray <ACNEService*> *neServices = NULL;
152+
112153
// Make sure that the ACNEServicesManager singleton is created and load the configurations
113154
[[ACNEServicesManager sharedNEServicesManager] loadConfigurationsWithHandler:^(NSError * error)
114155
{
@@ -117,22 +158,22 @@ int main(int argc, const char * argv[])
117158
NSLog(@"Failed to load the configurations - %@", error);
118159
}
119160

120-
NSArray <ACNEService*>* neServices = [[ACNEServicesManager sharedNEServicesManager] neServices];
161+
neServices = [[ACNEServicesManager sharedNEServicesManager] neServices];
121162
if([neServices count] <= 0)
122163
{
123164
NSLog(@"Could not find any VPN");
124165
}
125166

126-
for(ACNEService *neService in neServices)
127-
{
128-
if([neService.name isEqualToString:vpnName])
129-
{
130-
foundNEService = neService;
131-
break;
132-
}
133-
}
167+
for(ACNEService *neService in neServices)
168+
{
169+
if([neService.name isEqualToString:vpnName])
170+
{
171+
foundNEService = neService;
172+
break;
173+
}
174+
}
134175

135-
if(!foundNEService)
176+
if(needServiceName && !foundNEService)
136177
{
137178
// Stop running the NSRunLoop
138179
keepRunning = NO;
@@ -167,24 +208,49 @@ int main(int argc, const char * argv[])
167208
// Ensure we wait at least 1s
168209
if(startWaiting + 1.0 < CFAbsoluteTimeGetCurrent())
169210
{
170-
if(foundNEService != nil && (foundNEService.gotInitialSessionStatus))
171-
{
172-
//NSLog(@"Found NEService and session state");
173-
keepRunning = NO;
174-
}
211+
if (needServiceName)
212+
{
213+
if(foundNEService != nil && (foundNEService.gotInitialSessionStatus))
214+
{
215+
//NSLog(@"Found NEService and session state");
216+
keepRunning = NO;
217+
}
218+
}
219+
else
220+
{
221+
// go through all services and keep running if we don't have a state for each service
222+
keepRunning = NO;
223+
for(ACNEService *neService in neServices)
224+
{
225+
if (!neService.gotInitialSessionStatus)
226+
{
227+
keepRunning = YES;
228+
}
229+
}
230+
}
175231
}
176232
else
177233
{
178234
//NSLog(@"Need to wait more...");
179235
}
180236
}
181237

182-
if(foundNEService)
238+
if (listService)
239+
{
240+
for(ACNEService *neService in neServices)
241+
{
242+
printf("%s %s\n", [neService.name UTF8String], [GetDescriptionForSCNetworkConnectionStatus(neService.state) UTF8String]);
243+
}
244+
}
245+
else if(foundNEService)
183246
{
184247
SCNetworkConnectionStatus currentState = foundNEService.state;
185248
//NSLog(@"Got status %@", GetDescriptionForSCNetworkConnectionStatus(currentState));
186-
187-
if(shouldStartService)
249+
if(statusService)
250+
{
251+
printf("%s %s\n", [foundNEService.name UTF8String], [GetDescriptionForSCNetworkConnectionStatus(foundNEService.state) UTF8String]);
252+
}
253+
else if(shouldStartService)
188254
{
189255
if(currentState == kSCNetworkConnectionDisconnected)
190256
{

0 commit comments

Comments
 (0)