Skip to content

Commit ba3ae90

Browse files
committed
Added disconnect from network
1 parent 0257738 commit ba3ae90

8 files changed

Lines changed: 323 additions & 0 deletions

File tree

lib/darwin.js

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/linux.js

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/wifi-control.js

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/win32.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/darwin.coffee

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,21 @@ module.exports =
135135
stdout = @execSync COMMANDS[com]
136136
_msg = "Success!"
137137
@WiFiLog _msg
138+
139+
disconnectFromAP: ( _ap ) ->
140+
COMMANDS =
141+
disconnect: "networksetup -removepreferredwirelessnetwork #{@WiFiControlSettings.iface} \"#{_ap.ssid}\""
142+
disconnectFromAPChain = [ "connect" ]
143+
for com in disconnectFromAPChain
144+
@WiFiLog "Executing:\t#{COMMANDS[com]}"
145+
try
146+
stdout = @execSync COMMANDS[com]
147+
catch error
148+
if stdout is "Could not find network #{_ap.ssid}."
149+
_msg = "Error: No network called #{_ap.ssid} could be found."
150+
@WiFiLog _msg, true
151+
return {
152+
success: false
153+
msg: _msg
154+
}
155+
@WiFiLog "Success!"

src/linux.coffee

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,65 @@ module.exports =
221221
stdout = @execSync COMMANDS[com]
222222
_msg = "Success!"
223223
@WiFiLog _msg
224+
225+
#
226+
# With Linux, we can use nmcli to do the heavy lifting.
227+
#
228+
disconnectFromAP: ( _ap ) ->
229+
#
230+
# (1) Does a connection that matches the name of the ssid
231+
# already exist?
232+
#
233+
COMMANDS =
234+
delete: "nmcli connection delete \"#{_ap.ssid}\""
235+
try
236+
stdout = @execSync "nmcli connection show \"#{_ap.ssid}\""
237+
ssidExist = true if stdout.length
238+
catch error
239+
ssidExist = false
240+
241+
#
242+
# (2) Delete the old connection, if there is one.
243+
# Then, create a new connection.
244+
#
245+
disconnectFromAPChain = []
246+
if ssidExist
247+
@WiFiLog "It appears there is already a connection for this SSID."
248+
disconnectFromAPChain.push "delete"
249+
250+
#
251+
# (3) Connect to AP using using the above constructed
252+
# command chain.
253+
#
254+
for com in disconnectFromAPChain
255+
@WiFiLog "Executing:\t#{COMMANDS[com]}"
256+
#
257+
# Run the command, handle any errors that get thrown.
258+
#
259+
try
260+
stdout = @execSync COMMANDS[com]
261+
catch error
262+
if error.stderr.toString().trim() is "Error: No network with SSID '#{_ap.ssid}' found."
263+
_msg = "Error: No network called #{_ap.ssid} could be found."
264+
@WiFiLog _msg, true
265+
return {
266+
success: false
267+
msg: _msg
268+
}
269+
else if error.stderr.toString().search /Error:/ != -1
270+
_msg = error.stderr.toString().trim()
271+
@WiFiLog _msg, true
272+
return {
273+
success: false
274+
msg: _msg
275+
}
276+
# Ignore nmcli's add/modify errors, this is a system bug
277+
@WiFiLog error, true
278+
return {
279+
success: false
280+
msg: error
281+
}
282+
#
283+
# Otherwise, so far so good!
284+
#
285+
@WiFiLog "Success!"

src/wifi-control.coffee

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,98 @@ module.exports =
286286
success: false
287287
msg: _msg
288288

289+
#
290+
# disconnectFromAP: Direct the host machine to disconnect from a specific WiFi AP
291+
#
292+
disconnectFromAP: ( _ap, cb ) ->
293+
unless CXT.WiFiControlSettings.iface?
294+
_msg = "You cannot disconnect from a WiFi network without a valid wireless interface."
295+
CXT.WiFiLog _msg, true
296+
return {
297+
success: false
298+
msg: _msg
299+
}
300+
try
301+
#
302+
# (1) Verify there is a valid SSID
303+
#
304+
unless _ap.ssid.length
305+
return {
306+
success: false
307+
msg: "Please provide a non-empty SSID."
308+
}
309+
310+
#
311+
# (3) Do the OS-specific dirty work
312+
#
313+
os_instructions.disconnectFromAP.call CXT, _ap
314+
315+
#
316+
# (4) Now we keep checking the state of the network interface
317+
# to make sure it ends up actually being connected to the
318+
# desired SSID.
319+
#
320+
request_msg = "WiFi disconnection request to \"#{_ap.ssid}\" has been processed."
321+
CXT.WiFiLog request_msg
322+
323+
#
324+
# (5) check_iface is a helper function we use to repeatedly
325+
# check on the ifaceState using setTimeouts. This containment
326+
# is important because it makes it possible to implement
327+
# connectionTimeout.
328+
#
329+
t0 = new Date()
330+
check_iface = (_ap, cb) =>
331+
ifaceState = @getIfaceState()
332+
# If the connection is settled, check if we're connected
333+
# to the requested _ap.
334+
if ifaceState.success and (ifaceState.connection is "disconnected")
335+
if !ifaceState.ssid?
336+
#
337+
# We're disconnected! Success.
338+
#
339+
_msg = "Successfully disconnected from \"#{_ap.ssid}\""
340+
CXT.WiFiLog _msg
341+
cb null,
342+
success: true
343+
msg: _msg
344+
else
345+
#
346+
# We're connected, but to the wrong SSID!
347+
#
348+
_msg = "Error: Interface is currently connected to \"#{ifaceState.ssid}\""
349+
CXT.WiFiLog _msg, true
350+
connect_to_ap_result =
351+
success: false
352+
msg: _msg
353+
cb _msg,
354+
success: false
355+
msg: "Error: Could not connect to #{_ap.ssid}"
356+
return
357+
358+
# Attempt to confirm connection up to connectionTimeout milliseconds
359+
if (new Date() - t0) < CXT.WiFiControlSettings.connectionTimeout
360+
setTimeout ->
361+
check_iface _ap, cb
362+
, 250
363+
else
364+
cb "Disconnection confirmation timed out. (#{CXT.WiFiControlSettings.connectionTimeout}ms)",
365+
success: false,
366+
msg: "Error: Could not disconnect from #{_ap.ssid}"
367+
368+
#
369+
# (6) Start the check_iface loop
370+
# This will eventually return the user's callback "cb".
371+
#
372+
check_iface _ap, cb
373+
374+
catch error
375+
_msg = "Encountered an error while disconnecting from \"#{_ap.ssid}\": #{error}"
376+
CXT.WiFiLog _msg, true
377+
cb error,
378+
success: false
379+
msg: _msg
380+
289381
#
290382
# resetWiFi: Attempt to return the host machine's wireless to whatever
291383
# network it connects to by default.

src/win32.coffee

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,6 @@ module.exports =
242242
stdout = @execSync COMMANDS[com]
243243
_msg = "Success!"
244244
@WiFiLog _msg
245+
246+
connectToAP: ( _ap ) ->
247+
throw new Error("Not yet implemented")

0 commit comments

Comments
 (0)