/
Program.cs
73 lines (69 loc) · 2.74 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using NativeWifi;
namespace LockOut
{
class Program
{
private static WlanClient wlan;
private static string[] safeSSIDs;
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Arguments: a list of safe SSIDs: e.g. home work friendsWlan");
return;
}
safeSSIDs = args;
wlan = new WlanClient();
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(PowerModeChanged);
Console.WriteLine("The program will check the SSID of the connected WLAN after resume from standby and lock the computer if it is not in the list of safe SSIDs.");
Console.WriteLine("Press any key to exit.\n");
Console.ReadKey();
}
[DllImport("user32.dll")]
public static extern void LockWorkStation();
static void PowerModeChanged(Object sender, PowerModeChangedEventArgs e)
{
if (e.Mode == PowerModes.Suspend)
{
Console.WriteLine("PowerMode: suspend");
}
if (e.Mode == PowerModes.Resume)
{
Console.WriteLine("PowerMode: resume");
// give it some time to connect. wasn't necessary for me, but might be for some configurations. adjust if needed.
System.Threading.Thread.Sleep(500);
var connectedSsids = new List<string>();
foreach (WlanClient.WlanInterface wlanInterface in wlan.Interfaces)
{
if (wlanInterface.InterfaceState != Wlan.WlanInterfaceState.Connected)
{
Console.WriteLine(wlanInterface.InterfaceName + " is not connected!");
}
else
{
Wlan.Dot11Ssid ssid = wlanInterface.CurrentConnection.wlanAssociationAttributes.dot11Ssid;
connectedSsids.Add(new String(Encoding.ASCII.GetChars(ssid.SSID, 0, (int)ssid.SSIDLength)));
}
}
Console.WriteLine("Connected SSIDs:");
foreach (var ssid in connectedSsids)
Console.WriteLine("\t" + ssid);
if (connectedSsids.Intersect(safeSSIDs).Count() > 0)
{
Console.WriteLine("-> save");
}
else
{
Console.WriteLine("-> unsafe. Locking workstation now!");
LockWorkStation();
}
}
}
}
}