{"id":1250,"date":"2015-08-06T00:26:52","date_gmt":"2015-08-05T23:26:52","guid":{"rendered":"http:\/\/www.phillips321.co.uk\/?p=1250"},"modified":"2015-08-06T00:26:52","modified_gmt":"2015-08-05T23:26:52","slug":"os-x-autovpn-reconnection-app","status":"publish","type":"post","link":"https:\/\/www.phillips321.co.uk\/2015\/08\/06\/os-x-autovpn-reconnection-app\/","title":{"rendered":"OS X AutoVPN Reconnection App"},"content":{"rendered":"<p>So I travel around and this year I was at <a href=\"https:\/\/www.bsideslv.org\" target=\"_blank\">BSidesLV<\/a> and <a href=\"https:\/\/www.defcon.org\" target=\"_blank\">Defcon23<\/a> which are dodgy places to use wifi.<\/p>\n<p>I always connect to my VPN endpoint so that all my traffic leaves my devices over a secure tunnel. However, the biggest flaw with the inbuilt Apple VPN client is that it won&#8217;t auto reestablish the VPN session if it dies or you wake the laptop from sleep. The wifi at conferences is pretty flakey and I loose the connection regularly, as such when the connection is reestablished the VPN tunnel does not get reopened. This can lead to me sending data over the open air.<\/p>\n<p>I decided to create a simple app using AppleScript and loosely based it on the code found <a href=\"http:\/\/lifehacker.com\/5932886\/force-mac-os-x-to-automatically-reconnect-to-vpn\" target=\"_blank\">here<\/a>. I improved the code to make sure that there was network connectivity before attempting to start the VPN session.<\/p>\n<p>The code is here should you wish to make it yourself:<\/p>\n<div class=\"codecolorer-container applescript vibrant\" style=\"overflow:auto;white-space:nowrap;width:100%;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/><\/div><\/td><td><div class=\"applescript codecolorer\">delay <span class=\"nu0\">10<\/span><br \/>\n<span class=\"kw3\">on<\/span> <span class=\"kw1\">idle<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">tell<\/span> <span class=\"kw1\">application<\/span> <span class=\"st0\">&quot;System Events&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">tell<\/span> current location <span class=\"kw3\">of<\/span> network preferences<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> myConnection <span class=\"kw3\">to<\/span> <span class=\"kw2\">the<\/span> service <span class=\"st0\">&quot;home VPN&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">if<\/span> myConnection <span class=\"kw3\">is<\/span> <span class=\"kw2\">not<\/span> null <span class=\"kw3\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> networkUp <span class=\"kw3\">to<\/span> <span class=\"kw1\">true<\/span> <span class=\"co1\">-- assume the best<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">try<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">do shell script<\/span> <span class=\"st0\">&quot;ping -c 1 -t 2 www.google.com&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">on<\/span> <span class=\"kw3\">error<\/span> <span class=\"co1\">-- network is down<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">set<\/span> networkUp <span class=\"kw3\">to<\/span> <span class=\"kw1\">false<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">try<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">if<\/span> current configuration <span class=\"kw3\">of<\/span> myConnection <span class=\"kw3\">is<\/span> <span class=\"kw2\">not<\/span> connected <span class=\"kw2\">and<\/span> networkUp <span class=\"kw3\">then<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; say <span class=\"st0\">&quot;Reconnecting VPN&quot;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; connect myConnection<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">if<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">if<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">tell<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw3\">return<\/span> <span class=\"nu0\">10<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">end<\/span> <span class=\"kw3\">tell<\/span><br \/>\n<span class=\"kw3\">end<\/span> <span class=\"kw1\">idle<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>What should be noted is that my VPN connection is called &#8220;home VPN&#8221; so you will need to change this yourself to match the name of your VPN connection name. To do this just open the script using AppleScript.<br \/>\n<a href=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.15.08.png\"><img loading=\"lazy\" src=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.15.08-300x90.png\" alt=\"Screen Shot 2015-08-05 at 16.15.08\" width=\"300\" height=\"90\" class=\"aligncenter size-medium wp-image-1254\" srcset=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.15.08-300x90.png 300w, https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.15.08.png 340w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Here&#8217;s the app for your use <a href=\"https:\/\/www.phillips321.co.uk\/downloads\/AutoVPN.app.zip\" target=\"_blank\">https:\/\/www.phillips321.co.uk\/downloads\/AutoVPN.app.zip<\/a> be sure to rename the VPN connection name in the script from &#8220;home VPN&#8221; to the name of your connection.<\/p>\n<p>You can set the script to run at start as well by using the normal dock option of &#8220;Open at Login&#8221;<br \/>\n<a href=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.16.14.png\"><img loading=\"lazy\" src=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.16.14-300x205.png\" alt=\"Screen Shot 2015-08-05 at 16.16.14\" width=\"300\" height=\"205\" class=\"aligncenter size-medium wp-image-1253\" srcset=\"https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.16.14-300x205.png 300w, https:\/\/www.phillips321.co.uk\/wp-content\/uploads\/2015\/08\/Screen-Shot-2015-08-05-at-16.16.14.png 906w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I travel around and this year I was at BSidesLV and Defcon23 which are dodgy places to use wifi. I always connect to my VPN endpoint so that all my traffic leaves my devices over a secure tunnel. However, the biggest flaw with the inbuilt Apple VPN client is that it won&#8217;t auto reestablish [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1251,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[435,434,369,436,433,437,25],"_links":{"self":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/1250"}],"collection":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/comments?post=1250"}],"version-history":[{"count":3,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/1250\/revisions"}],"predecessor-version":[{"id":1256,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/posts\/1250\/revisions\/1256"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/media\/1251"}],"wp:attachment":[{"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/media?parent=1250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/categories?post=1250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phillips321.co.uk\/wp-json\/wp\/v2\/tags?post=1250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}