@@ -22,6 +22,12 @@ static void PrintUsage()
22
22
fprintf (stderr, " \t To stop the VPN called 'MyVPN':\n " );
23
23
fprintf (stderr, " \t vpnutil stop MyVPN\n " );
24
24
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 " );
25
31
fprintf (stderr, " Copyright © 2018 Alexandre Colucci\n blog.timac.org\n " );
26
32
fprintf (stderr, " \n " );
27
33
@@ -77,38 +83,73 @@ int main(int argc, const char * argv[])
77
83
{
78
84
@autoreleasepool
79
85
{
80
- if (argc != 3 )
81
- {
82
- PrintUsage ();
83
- }
84
86
85
87
// Do we want to start or stop the service?
86
88
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 ]];
88
100
if ([parameter1 isEqualToString: @" start" ])
89
101
{
90
102
shouldStartService = YES ;
103
+ needServiceName = YES ;
104
+ numArgs = 3 ;
91
105
}
92
106
else if ([parameter1 isEqualToString: @" stop" ])
93
107
{
94
108
shouldStartService = NO ;
109
+ needServiceName = YES ;
110
+ numArgs = 3 ;
95
111
}
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
+ }
96
127
else
97
128
{
98
129
PrintUsage ();
99
130
}
100
131
132
+ if (argc != numArgs)
133
+ {
134
+ PrintUsage ();
135
+ }
136
+
101
137
// 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
+ }
107
147
108
148
// Since this is a command line tool, we manually run an NSRunLoop
109
149
__block ACNEService *foundNEService = NULL ;
110
150
__block BOOL keepRunning = YES ;
111
-
151
+ __block NSArray <ACNEService*> *neServices = NULL ;
152
+
112
153
// Make sure that the ACNEServicesManager singleton is created and load the configurations
113
154
[[ACNEServicesManager sharedNEServicesManager ] loadConfigurationsWithHandler: ^(NSError * error)
114
155
{
@@ -117,22 +158,22 @@ int main(int argc, const char * argv[])
117
158
NSLog (@" Failed to load the configurations - %@ " , error);
118
159
}
119
160
120
- NSArray <ACNEService*>* neServices = [[ACNEServicesManager sharedNEServicesManager ] neServices ];
161
+ neServices = [[ACNEServicesManager sharedNEServicesManager ] neServices ];
121
162
if ([neServices count ] <= 0 )
122
163
{
123
164
NSLog (@" Could not find any VPN" );
124
165
}
125
166
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
+ }
134
175
135
- if (!foundNEService)
176
+ if (needServiceName && !foundNEService)
136
177
{
137
178
// Stop running the NSRunLoop
138
179
keepRunning = NO ;
@@ -167,24 +208,49 @@ int main(int argc, const char * argv[])
167
208
// Ensure we wait at least 1s
168
209
if (startWaiting + 1.0 < CFAbsoluteTimeGetCurrent ())
169
210
{
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
+ }
175
231
}
176
232
else
177
233
{
178
234
// NSLog(@"Need to wait more...");
179
235
}
180
236
}
181
237
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)
183
246
{
184
247
SCNetworkConnectionStatus currentState = foundNEService.state ;
185
248
// 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)
188
254
{
189
255
if (currentState == kSCNetworkConnectionDisconnected )
190
256
{
0 commit comments