-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetAuthLite.ps1
More file actions
314 lines (257 loc) · 9.67 KB
/
Copy pathNetAuthLite.ps1
File metadata and controls
314 lines (257 loc) · 9.67 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<#
Author : Mohammed Abdul Raqeeb
GitHub : https://github.com/Raqeeb27
Date : 07/01/2026
Script : NetAuthLite.ps1
Purpose : Campus Wi-Fi login/logout automation with background keep-alive
#>
Param(
[Parameter(Mandatory=$false)]
[string]$Command,
[Parameter(Mandatory=$false)]
[string]$Username,
[Parameter(Mandatory=$false)]
[string]$Password,
[Parameter(ValueFromRemainingArguments=$true)]
[string[]]$ExtraArgs
)
$ErrorActionPreference = "Stop"
# -------------------------------
# -------- CONFIGURATION --------
# -------------------------------
$GATEWAY = "10.100.100.1"
$LOGIN_URL = "https://10.100.100.1:8090/login.xml"
$LIVE_URL = "https://10.100.100.1:8090/live"
$LOGOUT_URL = "https://10.100.100.1:8090/logout.xml"
# -------------------------------
function Show-Usage {
Write-Host ""
Write-Host "Usage:" -ForegroundColor Yellow
Write-Host " ./wifi-connect.ps1 login <username> <password>" -ForegroundColor Yellow
Write-Host " ./wifi-connect.ps1 logout <username>" -ForegroundColor Yellow
exit 1
}
function Try-ParseXml {
param([string]$Content)
try {
return [xml]$Content
} catch {
return $null
}
}
# -------------------------------
# -------- Check Wi-Fi ----------
# -------------------------------
function Check-WiFi {
Write-Host "`nChecking campus Wi-Fi gateway..."
try {
$reachable = Test-Connection -Count 1 -Quiet $GATEWAY -ErrorAction Stop
} catch {
Write-Host "`nError: Test-Connection failed unexpectedly." -ForegroundColor Red
Write-Host "`nUniversity Wi-Fi gateway unreachable." -ForegroundColor Red
Write-Host "Ensure you're connected to the campus Wi-Fi.`n" -ForegroundColor Red
exit 1
}
if (-not $reachable) {
Write-Host "`nUniversity Wi-Fi gateway unreachable." -ForegroundColor Red
Write-Host "Ensure you're connected to the campus Wi-Fi.`n" -ForegroundColor Red
exit 1
}
Write-Host "Campus Wi-Fi gateway verified.`n" -ForegroundColor Green
}
# -------------------------------------------------------
# ----- Start detached keepalive ------
# -------------------------------------------------------
function Start-KeepAliveDetached($User) {
$TempScript = "$env:TEMP\wifi_keepalive_$User.ps1"
@'
while ($true) {
try {
$ts = [int][double]::Parse((Get-Date -UFormat '%s'))
$url = "https://10.100.100.1:8090/live?mode=192&username=__USER__&a=$ts&producttype=0"
curl.exe -k $url | Out-Null
} catch {}
Start-Sleep -Seconds 150
}
'@ -replace "__USER__", $User | Out-File $TempScript -Encoding UTF8 -Force
# Start detached background PowerShell (survives closing this window)
Start-Process powershell.exe `
-ArgumentList "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$TempScript`"" `
-WindowStyle Hidden
}
# -------------------------------------------------------
# -------- Stop detached process for this user ----------
# -------------------------------------------------------
function Stop-KeepAliveDetached($User) {
$pattern = "wifi_keepalive_$User.ps1"
# Use Win32_Process to inspect commandlines reliably
$procs = Get-CimInstance Win32_Process -Filter "Name = 'powershell.exe'" |
Where-Object { $_.CommandLine -like "*$pattern*" }
if ($procs) {
Write-Host "Stopping keep-alive background processes for '$User'..."
foreach ($p in $procs) {
try {
Stop-Process -Id $p.ProcessId -Force -ErrorAction SilentlyContinue
} catch {}
}
Write-Host "Stopped." -ForegroundColor Green
} else {
Write-Host "No detached keep-alive process running for '$User'." -ForegroundColor Yellow
}
$TempScript = Join-Path $env:TEMP "wifi_keepalive_$User.ps1"
if (Test-Path $TempScript) {
try {
Remove-Item $TempScript -Force -ErrorAction Stop
Write-Host "`nRemoved detached script file: Local\Temp\$pattern" -ForegroundColor Green
} catch {
Write-Host "`nWarning: Could not remove detached script file: Local\Temp\$pattern" -ForegroundColor Yellow
}
}
}
# -------------------------------
# -------- Login Function -------
# -------------------------------
function Login($User, $Pass) {
Check-WiFi
Write-Host "Terminating any existing keep-alive sessions for '$User'..."
# Kill old keepalive processes for same user
Stop-KeepAliveDetached $User
Write-Host "`nLogging in as '$User'..."
# Generate timestamp
$TS = [int][double]::Parse((Get-Date -UFormat %s))
# Send login request via curl and capture XML response
$output = curl.exe -k -s -X POST $LOGIN_URL `
-d "mode=191" `
-d "username=$User" `
-d "password=$Pass" `
-d "a=$TS" `
-d "producttype=0"
if ($LASTEXITCODE -ne 0 -or -not $output) {
Write-Host "Error: curl login request failed." -ForegroundColor Red
return
}
$xml = Try-ParseXml $output
if (-not $xml) {
Write-Host "Error: Server returned invalid XML." -ForegroundColor Red
Write-Host $output
return
}
$status = $xml.requestresponse.status.'#cdata-section'
$message = $xml.requestresponse.message.'#cdata-section'
# ---- Interpret result ----
if ($status -eq "LIVE") {
Write-Host "`nLogin Successful" -ForegroundColor Green
if ($message) {
$displayMessage = $message -replace [regex]::Escape('{username}'), "'$User'"
Write-Host $displayMessage
}
Write-Host "`nStarting detached keep-alive process..."
Start-KeepAliveDetached $User
Write-Host "Keep-alive running in background. You can close this window." -ForegroundColor Green
return
}
if ($message -match "Invalid user name") {
Write-Host "Login failed: Invalid username or password." -ForegroundColor Red
return
}
if ($message -match "maximum login limit") {
Write-Host "Login failed: Maximum login limit reached (already logged in elsewhere)." -ForegroundColor Yellow
return
}
# Fallback for other messages
Write-Host "Login not successful." -ForegroundColor Red
if ($message) {
Write-Host "Server message:"
Write-Host $message
} else {
Write-Host $output
}
}
# -------------------------------
# -------- Logout Function ------
# -------------------------------
function Logout($User) {
Check-WiFi
Write-Host "Logging out '$User'..."
$TS = [int][double]::Parse((Get-Date -UFormat %s))
$output = curl.exe -k -s -X POST $LOGOUT_URL `
-d "mode=193" `
-d "username=$User" `
-d "a=$TS" `
-d "producttype=0"
if ($LASTEXITCODE -ne 0 -or -not $output) {
Write-Host "Error: The logout request through commandline has failed." -ForegroundColor Red
Write-Host "You may need to logout manually through the web portal." -ForegroundColor Yellow
return
} else {
try {
$xml = Try-ParseXml $output
if (-not $xml) {
Write-Host "Error: Server returned invalid XML." -ForegroundColor Red
Write-Host $output
return
}
$logoutMsg = $xml.requestresponse.logoutmessage.'#cdata-section'
$message = $xml.requestresponse.message.'#cdata-section'
if ($logoutMsg) {
Write-Host "$logoutMsg"
} elseif ($message) {
if ($message -match "You've signed out"){
Write-Host "You have signed out successfully`n" -ForegroundColor Green
}
else{
Write-Host "$message"
}
}
} catch {
Write-Host "Server response (unparsed):" -ForegroundColor Red
Write-Host $output
}
}
# Stop background keep-alive
Stop-KeepAliveDetached $User
}
# -------------------------------
# -------- Command Router -------
# -------------------------------
if (-not $Command) {
Show-Usage
}
if ($ExtraArgs.Count -gt 0) {
Write-Host "Error: Too many arguments supplied: $($ExtraArgs -join ', ')" -ForegroundColor Red
Write-Host "`nExpected format:" -ForegroundColor Yellow
Show-Usage
exit 1
}
switch ($Command.ToLower()) {
"login" {
if ($Username -and $Password -and ($PSCmdlet.MyInvocation.BoundParameters.Count -eq 3)) {
if ($Username -match '^\d{12}$') {
Login $Username $Password
} else {
Write-Host "Error: Username must be exactly 12 digits." -ForegroundColor Red
Show-Usage
}
} else {
Write-Host "Error: login requires exactly <username> <password>" -ForegroundColor Red
Show-Usage
}
}
"logout" {
if ($Username -and ($PSCmdlet.MyInvocation.BoundParameters.Count -eq 2)) {
if ($Username -match '^\d{12}$') {
Logout $Username
} else {
Write-Host "Error: Username must be exactly 12 digits." -ForegroundColor Red
Show-Usage
}
} else {
Write-Host "Error: logout requires exactly <username>" -ForegroundColor Red
Show-Usage
}
}
default {
Write-Host "Invalid command." -ForegroundColor Red
Show-Usage
}
}