Monday 14 November 2016

Private Internet Access - detect VPN disconnect and automatically reconnect

So after finally settling on choosing a new VPN provider, (being Private Internet Access from the previous which was BTGuard), I noticed that every now and again the PIA VPN drops.

The VPN client comes with a KillSwitch which automatically kills all network connections as soon as this happens - but it's not really what I'm after. I'm not THAT paranoid about online connectivity, I just want a UK breakout - so basically just reconnect and carry on would be nice. You would think they would have an 'automatic reconnect' option or something but unfortunately not - so I created one.

I have to admit, I spent quite a bit of time messing about with PowerShell and WMIObject to try and catch the NetEnabled trigger. I'm running the VPN client on a Windows 7 laptop so I don't have access to the NetAdapter module (boohoo). If you want THAT solution there are plenty of google articles out there. 

The laptop is a dedicated VPN machine I intend to use as a VPN Gateway, so I'm happy to leave it on Windows 7 for now.

The VPN client isn't kind enough to trigger an event log when it disconnects, so I started with the idea of monitoring the TAP NIC created by the PIA Client on install - but let me save you the time and tell you, don't waste your time with the WMIObjects - yes it does work, but it's a lot of work and you have to keep polling the status to see if it's up or down. This is about as pretty as a looping ping command continuously in the background parsing the output for timeouts....ugh!

Something I found more elegant is a task schedule that triggers when the VPN disconnects. Then you just set the scheduled action to run the damn thing again which, if it's set to connect on startup, reconnects the VPN. Much prettier I think.

But wait - didn't I just say the VPN client isn't kind enough to trigger an event log when it disconnects?  True - but what the disconnect does trigger almost immediately is a Browser election when the interface drops.
We use this as our disconnect trigger.

STEP 1: Configure the client
  • Right-click the private internet access client from your system tray and select Settings (you will notice you can't change settings while it's connected, so stop your "Loony Tunes Bumper Pack" torrent download, disconnect the VPN client, then try again)
  • Tick the box labelled "Auto-connect on launch"
  • Check your region if it's relevant to you. I also have it to "Start application at login" but that's just because the laptop is dedicated to the VPN.


STEP 2: Run the task scheduler
  • Start button, type "Task" and you should see "Task Scheduler" appear at the top.
  • Create a Task and under the General Tab;
  • Give it a name - mine is "VPN disconnect trigger" Tick "Run with highest privileges" and leave the rest of the settings on this tab.

  • Under the Triggers Tab;
  • Click New... Set "Begin the task:" to "On an Event"
  • Leave the Settings as Basic and select the System log (dropdown riiiiight to the bottom to find "System")
  • Set the Source to Browser
  • Set the EventID to 8033
  • Ensure "Enabled" is ticked Click "OK"

Now head to the "Actions" tab;
  • Click New...
  • Make sure the Action is "Start a program"
  • At this point it helps to grab the PIA shortcut - leave the "Edit Action" windows open, click your Start button, type in "Private Internet Access", and you should see the "Private Internet Access" program pop up at the top of the list.
  • Right click "Private Internet Access" program, select Properties
  • The shortcut windows opens up with the shortcut already selected - so all you have to do is hit CTRL + C, OR right-click the selected text and select "Copy" Close the shortcut window and get back to your Task Scheduler "Edit Action" window and in the Program/Script text box hit CTRL + V, OR right click and select "Paste"




  • In the "Add Arguments (optional)" text box type: --startup
  • Make sure it's all in lower case
  • Click OK


The SettingsTab;
When you set the VPN client to start when windows starts, it sets up a task to trigger the VPN client on log on.  This task stays running for as long as the pi_manager.exe is running, whether it's connected or not.

To try and avoid any conflicts or multiple threads (I didn't bother testing whether it's an issue or not) I just set the reconnect trigger Settings to kill the task if it's still running.  I'm not sure if this applies to the action or just the task, but at the end of the day only one vpn client ends up running, so either the .exe sorts itself out, or this setting helps.  If you're really pedantic you can test it and decide if you need it.


The rest;
I didn't have anything to add to the defaults, so I left the rest of the tabs. You can fiddle, but I'd suggest make sure it's working with the above settings first, then fiddle.

That's pretty much it.  When you click OK and Click OK again it should save the Task and ask you for a username and password.  If you're an admin user, use yours - if not, specify an admin user (otherwise you get the UAC popping up asking if it's allowed to let the program run).

Now might be a good time to bounce your machine.  Once you've logged in again your PIA-VPN should auto-connect (if you're doing the start with Windows option)
Once it's happily connected, disconnect it and wait about 10-15 seconds - you should see your PIA VPN start up again.

If it doesn't you may want to add a display message action that says something like "Triggered!" to help troubleshoot.  If it pops up but the VPN client doesn't reconnect, make sure you typed --startup all in lowercase.  

If you're super-nervous about being 'public' for even one second (which makes me think you probably should be arrested anyway) then use the VPN killswitch.  If you really want me to blog and automatic restart for that too, leave a comment or something.


Tuesday 25 June 2013

Windows 7 Ultimate 64bit - Network stack Crash and Burn

Short version:

If your Windows 7 crashes with services refusing to start and your network card or network connectivity is doing weird stuff, won't reinstall drivers, complaining about arb crap like "the parameter is invalid" and netsh gives you the 'initiating helper dll' finger every time you try reset TCP or Winsock, AND the system file checker says everything is fine and importing new Winsock and Winsock2 registry entries only sort-of fixes some of your issues but you get the feeling it's kinda like putting a plaster on a severed limb and you REALLY REALLY REALLY do NOT want to have to reinstall a FRESH copy of Windows and all the application reinstall and system configuration hell that goes with it - let me save you some trouble and offer three words of condolence;


In. Place. Upgrade.
Stop trying to fix it.  You can't.  Ok, I can't, but I live according to my ego which says if I can't, you can't.
So stop - relax - and reinstall your current version of windows over your current version of windows as an 'upgrade' - not a fresh install.
Not ideal.
No "Root Cause Analysis" resolutions or epiphanies about what cause it.
But if none of the google-fixes mentioned above work, just do it.  

Of course, this may not be easy, but it eventually worked for me.

Long version:

On my main PC I run Windows 7 Ultimate 64bit.  
No clue why I'm running 64bit as I only have 3GB RAM, but I run a little web dev with MySQL DB and PHP on IIS plus some other interesting combo's, suffice it say the system config is not something I really want to have to redo at any point.

The other day I was messing around and things just seemed sluggish.  I noticed the committed memory was way over what was currently in use and figured the poor thing could use a reboot. (It usually gets it's weekly reboot from either the power company blacking out or the maid running the dish washer, washing machine, tumble drier, kettle and iron at the same time, which generally results in a nice little 'click' as the DB boards melt and my computers cry in vain for a UPS before dwindling into nothingness...fortunately the power company hasn't blacked us out in a while and unfortunately we no longer have a maid, so, for a change, graceful shutdown and reboot in order!)

After the reboot, I log in and receive a funny error message saying that a system service didn't start.  It was funny because it said the service that didn't start was the Event log service, and I should go check out the Event log to see why....(nice one, William)


Anyway, before dissolving into a panicky mess I figured perhaps I should just reboot again, because I had seen a java-update pop up just before I rebooted and had to cancel the process from the shutdown menu.  (Okay, so it wasn't entirely graceful)

Ground 0:
Upon reboot system goes into a boot loop - windows logo, reboot.  Windows logo, reboot.  Starting Windows, repair mode!
What?
Um, ok.
I let it do it's thing (something along the lines of "Window is automatically fixing startup errors.")  
It reboots.
Repair mode.
Then it kindly tells me it couldn't automatically repair anything, and offers me some of my own choices - like a system restore point.
At this point I'm thinking, rather uncharacteristically "Hell yeah!  System restore point!  Go windows!" - mainly because back in the day I'd be pulling out the 'Windows XP boot USB Flash Drive' with fdisk and whatnot installed for your pleasure.
I select the last restore point (of about three that I can see), which I notice was just prior to a 'critical system update'.   The system works for a while and then reboots.
Repair mode.  
Weird...maybe I should have chosen another restore point.  Go back to restore point menu and....there are no restore points! (As an aside, I found out LATER that windows applies the FIFO, Fit In or F**k Off rule...wait...sorry, the First In First Out rule to restore points based on available space.  When you don't have it, it deletes the oldest restore point.  If you completely whack all available free space, say goodbye to your restore points.  As an aside, aside, still haven't figured out how to put system restores on another volume - will check it out after writing this...)

Level 1 - HP 0, INT 0, WIS 0, Magic - definitely 0:
After trying fitfully to use anything GUI-based to recover from the recovery menus, numbers of times, I eventually descend into console territory (Recovery Console / DOS Window).
A thought that had been sitting with me since watching that less than graceful shutdown surfaces - system file corruption somewhere.
I run the system file checker, which requires  the /offbootdir and /offwindir switches when running it from the repair console ("Offline mode" they call it).
So,  "SFC /offbootdir:c:\ /offwindir:c:\windows /scannow" scans the system files for corruption and presumably fixes them, assuming it finds them.
It reboots - didn't get a chance to see if it fixed anything or not, but I'm back in repair mode.
I try change my HDD BIOS setting from IDE to AHPI, (which was stupid because I never changed it FROM AHCI to IDE and Windows was installed with it on IDE, so obviously this didn't work).
I sheepishly put it back to IDE, and decide to try safe mode with boot logging.
System hangs for a bit after classpnp.sys then reboots.  I go online and realise how many people out there don't realise that when bootlog says "loaded classpnp.sys" and then hangs - this isn't a problem with classpnp.sys but whatever comes after it.  After boggling at people who can't understand why copying classpnp.sys over itself doesn't solve anything, I move on...
I SOMEHOW manage to get into safe mode (don't ask - I may have just selected safe mode...one second I was looking through tear-filled eyes at a repair menu, the next I was in safe mode)
I frantically disable all my non-windows startup services ("clean boot") - nada.
I run SFC in safe mode again just for kicks.
Reboot again - wait a second - I'm back in windows!  Fantastic!  Awesome!  Except when I log in I get the stupid Event log service message again....and my network isn't working....and when I look at running services, a whole tonne of 'Automatic' services haven't started, including DHCP client and Server which are in "starting" mode...so, awesome to have Windows back - not so awesome to have it buggered.

Level 10:  HP 5, INT 9, WIS 9, Magic...still 0:
Through some fiddling I somehow decided that based on the hierarchy of dependency services that are failing to start, it all looks rooted in the network stack.
I do a 
'netsh reset winsock'
and 
'netsh int ip reset' from a command prompt to try and reset the stack - I'm already logged in as Administrator so I don't have to mess about with "Run As Administrator", but still I'm not winning.
With both commands I get a weird "Initialization Function InitHelperDll in NSHTTP.DLL failed to start with error code 11003" before it said it was reset.
Reboot - no change.  Winsock resets still give the helperdll error.
After hours of fiddling I finally give in and try something that made very little sense to me - removing the winsock and winsock2 registry keys and replacing them with others exported from another copy of windows.
It didn't make sense, because I didn't HAVE another copy of Windows 7 Ultimate 64 - only Windows Professional on a laptop.  Plus the TCP service thingies enumerate in different orders in the LSP (Layered Service Protocol)....Nevertheless I was desperate, so I backed up the 
HKLM/System/CurrentControlSet/Services/Winsock
HKLM/System/CurrentControlSet/Services/Winsock2
entries and replaces them with the same keys exported from my laptop.

Reboot....

Level 20:  HP 14, INT 13, WIS 14, Magic...call it 5, but a very unimpressed 5:
Services start!
Well - some of them...the event log service for one - so now I can actually start looking at event logs (thanks William)
NIC is - just not starting.  I try updating/reinstalling the drivers - nada "The parameter is incorrect".
I try installing the vendor's specific driver (which is older than the MS one) - same problem.

I try uninstalling it.
Reboot - no more NIC.

Now the NIC shows up as not working in Device Manager, doesn't appear AT ALL in network management, and refuses to install anything driver-like.
I try flashing the BIOS (really desperate at this point). nada.
I try put in another NIC, but unfortunately the only one I could get my hands on was an OLD 3Com 3c905cx 10/100 PCI - which Windows 7 has unfortunately never heard of, cannot recognise, and won't install, no matter what drivers I try fiddle with.
Unfortunately, I'm looking at a rebuild....but I'm hoping an in-place upgrade will save the day.
More unfortunately, I don't have any OEM discs!  What the hell is this crap that vendors sell OEM windows without discs!?
Eventually I manage to get my hands on a Windows 7 Ultimate CD.  When I try upgrade however, it tells me I need to install Service Pack 1 before I can upgrade.
Not too convinced, I download and run SP1.  Sure enough, after hours of progress bars, it cranks and fails. I go through a whole tonne of "how to successfully install SP1" unsuccessfully.
Eventually I decide I'm going to dual-boot and just SEE if a fresh install will work, before I wipe my current version...

Level 35:  HP 17, INT 17, WIS 17, Magic....getting there with about a 12:
I run an install on D:\Windows and after a surprisingly short time I'm booting into a fresh install of Windows 7 ...and the NIC is FINE!  (if you hadn't been thinking NIC failure by now, well I was)...except I seem to have installed 32bit....weird.  It's not like it asked my opinion on anything, it just installed - I thought it was native 64bit....oh well.
Interestingly enough, this annoyed me more than anything else.  Now I KNEW it was system file or registry corruption - so I bounced out of my fresh install and flew back into damage inc. determined to nail it.
I uninstalled iTunes and Bonjour (suspect just based on the MDNSResponder values it pops into the winsock registry keys - and removing it wasn't easy which made it more suspect!) - no change.
Kept reimporting my old keys now and again to check, and sure enough, every time I reimported my old winsock registry keys the system burst into flames with any number of issues.  Every time I used the imported Windows Professional winsock keys, I got a sort-of healthier startup, but no network and network-stack-dependent services still crashing.
I thought perhaps the windows install failed previously because it was 32bit (don't ask me why I thought it, it makes no sense because it complained about SP1 - not 64bit or anything...but there you go) so I went hunting for a confirmed 64bit version (boy was that an adventure reminiscent of captain jack sparrow) and eventually came up with something I was fairly sure was indeed the 64bit version
.

Level 50: HP 21, INT 25, WIS 20, MAG 18 - bring on the carrion crawler!
I run the 64bit setup (after clearing a few GB of space - you'd think the sudden lack of restore points would have been enough, but no!) and lo-and-behold it bitches about SP1.
Groaning, I prepare for another bout of attempting to install SP1, feeling pretty certain I'm headed for complete fresh reinstall and weeks of IIS config to work through.
But then I saw some arb comment on some arb forum that nobody even bothered to rate.
Chuckling evilly at the irony, I run setup again - this time in COMPATIBILITY MODE.
I tell compatibility mode this definitely used to run on an earlier version of windows, and that was VISTA SP2.  I Start the setup...
SETUP RUNS!  for two and a half days.... but it runs.
(By this time we'd gotten a new maid - yes it's been that long - and I'm praying the electricity company doesn't black out while I'm systematically severing every high-wattage appliance plug from its power cable.)
Eventually I get to log into windows again AND....

My network icon is still sporting a rather sad little red cross....but there were no service startup errors AT ALL...plus I notice I'm now running SP1...(Seriously William - WTF?)

Almost sobbing I double click the NIC to confirm the inevitable....but wait.  The NIC is visible, it's just disabled.  I remembered that somewhere in my troubleshooting, rebooting madness I had disabled the NIC to see if it made a difference.  It didn't, except for the ability for it to f**k with my emotions at THIS point!
Like a loyal owner stroking their rabid pet, (that is, very carefully while using a long stick and holding a 9mm behind your back) I right-click the NIC and click Enable...


IT'S ALIVE!


Words to the wise:

  • Make those repair discs.
  • Get Windows Backup Running.  Back that sucker up.
  • Create Restore Points and watch your available space.
Disclaimer:
All stolen images are copyright of their respective owners and I did not get permission to reproduce them.  However, considering I just did a search on Google Images and fiddled with photoshop I figured nobody would mind.  If you do mind, please talk to Google seeing as they were kind enough to reproduce them for me in the first place, so I'll take whatever legal stance they take.

Tuesday 13 March 2012

Outlook favourites disappearing

After being a long-standing user of Microsoft's Outlook email program, I recently upgraded to 2010 and set it up to access my multitude of gmail accounts.

As I'm a fan of the tree-view, this became somewhat cluttered, so I thought I'd make 1000 MSDevs dreams come true and put the "Favourites" section to good use.  Simply find the folder you want to add to favourites and either Right-Click, "Show in Favourites" or just drag it there.




To order them nicely once they're in the favourites, right click the favourites folder and select "Move up", "Move down" or just drag the thing where you want it.

Wonderful!  I've learnt something new and helpful - so I start using my favourites :)

A while later I go into outlook to check a calendar entry, then head to my email folders and...dammit, where'd the favourites go?  Try as I might I can't even get the original favourites back!After fiddling for a while I resort to Google "outlook 2010 favourites folder missing".  Wow, common problem.  The resolution from most of them is to run Outlook.exe from the command line (or Start, run/search line) with a parameter of /resetnavpane.

This works.  But sooner or later while I'm working, I get back to my Folders and the damn things favourites are missing again.  So now I look carefully at what I'm doing versus what starting outlook with the /resetnavpane option is doing - and then the penny drops...quite far, but it drops :)
Whenever I want to get back to my Mail folders (normally from working in my Calendar or Tasks) I use the Navpane icons below all my folders to get there.

What I was doing was using the Folder List option to get back to my mail folders - whereas when you run Outlook /resetnavpane, outlook starts with the Mail option selected.

Sure enough, when I clicked on the Folder List option my Favourites immediately disappeared - it was a different view, but so similar to the Mail option with one exception - no Favourites.
When I clicked on the Mail option - tadaa - my Favourites came back!

So now I just use the Mail option to navigate back to my Mail folders.  You can also just push Ctrl+1 and it will do the same thing.


Monday 12 March 2012

Notebook volume button issue

I have a basic philosophy about old computers - "If it can still run Windows 95, it's still worth something to someone."

Unfortunately this has resulted in my house looking rather like an elephant graveyard, if elephants were old XT's with CRT monitors for heads and PS2 keyboards for legs.
Whenever I move I manage to throw some of it away, but for some reason I've never been able to bring myself to throw out a laptop. (Forget about selling, if I can sell it, it means someone can use it and I'd prefer that someone to be me - so either it's FUBAR, or it's mine!)

Which brings me to the point of this particular topic - I have a very old HP nx9110 laptop (bought around 2004), but it's still a beaut.  15.4" display, P4, 3.0 GHz, 1.5Gb RAM.  I bought some cheap power supply from 'alternative sources' for it when the cable eventually frayed off the original two years ago, so other than losing the ability to charge the battery and having a power supply that threatens to burn its way back to Taiwan through my teak parquet floor if left on too long - it works like a charm.

A while ago I fired it up to work in bed because I was sick.  Everything went fine until I stopped working and decided I wanted to watch movies instead.  Damn, no volume!

TroubleShooting 101: HP Compaq NX9110 - no volume
So I check the Volume by clicking once on the taskbar speaker icon in Windows XP...The volume is set to zero. 
Duh. 


I drag the volume to MAX, hear satisfying "DING!" from Windows and then watch in amazement as the bar proceeds to slide itself back to zero. 

What the...

I drag it up, "Ding!", it slides back down by itself.
I try the hardware “Volume Up” button on the side of the laptop - nothing.  Then I notice every time I push it, it toggles mute.  The mute button also still toggles mute. 
I open audio properties and drag the slider there to max volume - "Ding!", then it slides back down.
I drag it up again but this time, as it starts sliding back down, I quickly push and hold the hardware “Volume Up” button and it stops mid-slide!  I let the button go and it continues its descent.  Push the button again and it stops.
Ok, so clearly there's something somewhere sending this volume down which the Volume Up button cancels, provided it’s held in.  Troubleshooting begins in earnest;

TroubleShooting 2.0:

16 gazillion Ctrl+alt+del solutions in my past have burnt certain synapses in the brain, so first order of business is a reboot.  Problem persists.
I disable, enable, then reinstall audio drivers.  Same story.
I reinstall about five or six HP-laptop system apps, from HP config Utility to OS Enhancements and more.  Problem persists!
I upgrade my firmware.  Problem persists.
I (eventually) reinstall XP from scratch.  OS reinstall is a major admission of defeat for me – luckily for my ego the problem persists!
I upgrade XP to SP3.  Still there.
I upgrade my BIOS (we won't bother going into the three-day episode of hell that turned out to be).  About the only thing that does remain the same is the volume issue.
Throughout this I'm SCOURING the net and finding tonnes of people complaining about the same thing, none of them with a solution or explanation;
"Volume button stuck"
"Volume control keeps going down"
"How can I disable hardware volume controls?"
After hours and hours and hours and hours and then days of troubleshooting and trying stuff I eventually gave up, shoved a pin in the volume-up button to wedge it and managed to watch half a movie to convince myself it had all been worth it.

Of course, I wouldn’t have gone to all this trouble of writing this if the final solution was “stick a pin in it”, but being sick and frustrated I have to admit I gave up the first time and just couldn't be bothered.
About six months later I wind up sick again - so I pull out the old laptop and spend the day working in bed.  Afternoon comes and I'm keen to watch a movie – where’s the volu- oh crap..
Back to square one, and now I refuse to use the old pin trick because, well, to be honest...I can't find a pin.

So I start again.  After trying to reload stuff in different orders, spending hours trawling through the same old posts from people who had the same problem (and never found a solution, other than something like my pin or, in one case a rather popular solution which involved ripping open the laptop and physically short-circuiting or removing the buttons from the laptop completely, soldering iron and all!) I eventually decided to try figure it out myself.
This is what I did.

Troubleshooting 4.0:

It seemed from all my fiddling that it was more likely a hardware problem in that the "volume down" button was physically stuck, as opposed to a software problem - but regardless of the cause, I had to figure out if Windows was detecting a keypress or whether something else was forcing the volume down.  I did a quick google and downloaded and installed a basic keyboard monitor program called KeyboardTest.
Sure enough, it showed a constant input of a keycode 174 (0xae)
I checked the code and confirmed that this was Windows speak for "Volume Down" - so the issue was definitely emotionally hardware related in that the volume down button was permanently depressed. [see 2016 Post Script edit below]
Not willing to take a soldering iron to my laptop I decided I had to find the software equivalent of my pin - i.e. pushing in the volume UP button all the time, which seemed to cancel out the Volume Down.
Another short google search later and I came across something called AutoHotKey.  AutoHotKey is a freeware app that allows you to customise and configure keyboard shortcuts - while it's running, it intercepts all keyboard keypresses, checks to see if you have commands or macros set up for them and if so executes them instead of sending the keypress to Windows.
I downloaded AutoHotKey, installed it, quick-read the helpfile and there it was - interception keys for Volume!
I edited the default script and set it to send "Volume Up" every time "Volume Down" was pressed.  I ran it and it completely reversed my problem! 


Wait, let's be clear here - not fixed exactly, reversed - my volume now went all the way up continuously - not perfect, but still a major improvement from no sound.

After giving it some thought I realised that pressing the Volume Up key physically may have been cancelling out the Volume Down input, but AutoHotkey was intercepting this and instead of sending the Down command, sent the Up command - which meant there was no cancelling out.  For the software version of my pin, I didn't have to counter the "Volume Down" keypress with a "Volume Up" - I just had to intercept the "Volume Down" and discard it.

I edited the script and a few seconds later it worked 100%!  Now when I drag the volume bar it stays where I put it! 

Ok, so the hardware buttons still do weird things if I try use them, but I don't need ‘em so who cares?  At least I didn’t take a soldering iron to my laptop!



After getting it right I wanted to go back to all those forums and tell people - there is a solution that doesn't involve melting lead!
But thinking of having to register on each forum, verify email, then post, then move to the next - well I figured it would be easier to post a blog and hopefully if someone does a search like "nx9110 volume demon" or "hp laptop volume stuck" they'll find my blog :)
Lemme know if you found it!

Disclaimer:
All stolen images are copyright of their respective owners and I did not get permission to reproduce them.  However, considering I just did a search on Google Images and fiddled with photoshop I figured nobody would mind.  If you do mind, please talk to Google seeing as they were kind enough to reproduce them for me in the first place, so I'll take whatever legal stance they take.
POST SCRIPT EDIT: 2016
A few years later I rebuilt the laptop with Linux.  Then I rebuilt it with Windows XP.  Then Linux again.  Then Windows 7.
I realised the sound problem no longer persists - meaning a rebuild would have fixed it - meaning, probably, that it was a software problem after all.  As I'm not someone who accepts "rebuild" as a viable solution to anything other than a corrupted OS beyond repair, the above blog remains :)