This repository was archived by the owner on Sep 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-Logout.ps1
More file actions
76 lines (60 loc) · 3.37 KB
/
Invoke-Logout.ps1
File metadata and controls
76 lines (60 loc) · 3.37 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
[CmdletBinding()]
param ()
function Get-ResponseJson {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[Microsoft.PowerShell.Commands.WebResponseObject]$Response
)
Process { $Response.Content.Substring($Response.Content.IndexOf('{')).Trim().TrimEnd(')') }
}
$userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0'
if ([System.Environment]::OSVersion.Platform -eq 'Win32NT') {
$ssid = (Get-NetConnectionProfile -InterfaceAlias 'WLAN*').Name
} else {
if (Get-Command 'iwgetid') {
$ssid = iwgetid -r
}
}
$isDorm = $ssid -and $ssid -cne 'seu-wlan'
if ($isDorm) {
$serverHost = 'http://10.80.128.2/'
$serverHostAlt = 'http://10.80.128.2:801/'
Write-Debug 'Dormitory environment detected'
} else {
$serverHost = 'https://w.seu.edu.cn/'
$serverHostAlt = 'https://w.seu.edu.cn:801/'
Write-Debug 'Non-dormitory environment detected.'
}
$headers = @{ 'Referer' = $serverHost }
$urlQuery = $serverHost + 'drcom/chkstatus?callback=dr1000'
Write-Debug "Connecting to $($urlQuery)"
try {
Invoke-WebRequest -URI $urlQuery -Headers $headers -UserAgent $userAgent -SessionVariable 'session' -ErrorAction Stop |
Get-ResponseJson | ConvertFrom-Json -OutVariable 'status' | Write-Debug
if ($status.result -eq 1) {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Currently logged in as $($status.uid) $($status.NID)." -ForegroundColor Blue
$urlUnbind = $serverHostAlt + "eportal/?c=Portal&a=unbind_mac&callback=dr1005&user_account=$($UUID)&wlan_user_mac=$($status.olmac)&wlan_user_ip=$($status.v46ip)&jsVersion=3.3.3"
Write-Debug "Connecting to $($urlUnbind)"
Invoke-WebRequest -URI $urlUnbind -Headers $headers -UserAgent $userAgent -WebSession $session -ErrorAction Stop |
Get-ResponseJson | ConvertFrom-Json -OutVariable 'unbind' | Write-Debug
if ($unbind.result -eq 1) {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Unbind succeeded. ($($unbind.msg))" -ForegroundColor Green
} else {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Unbind failed with code $($unbind.ret_code). ($($unbind.msg))" -ForegroundColor Red
}
$urlLogout = $serverHostAlt + "eportal/?c=Portal&a=logout&callback=dr1001&login_method=1&user_account=drcom&user_password=123&ac_logout=1®ister_mode=1&wlan_user_ip=$($status.v46ip)&wlan_user_ipv6=&wlan_vlan_id=0&wlan_user_mac=$($status.olmac)&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3"
Write-Debug "Connecting to $($urlLogout)"
Invoke-WebRequest -URI $urlLogout -Headers $headers -UserAgent $userAgent -WebSession $session -ErrorAction Stop |
Get-ResponseJson | ConvertFrom-Json -OutVariable 'logout' | Write-Debug
if ($logout.result -eq 1) {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Logout succeeded. ($($logout.msg))" -ForegroundColor Green
} else {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Logout failed with code $($logout.ret_code). ($($logout.msg))" -ForegroundColor Red
}
} else {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Currently logged out." -ForegroundColor Green
}
} catch {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Failed to connect to authentication server." -ForegroundColor Red
Write-Host $_.Exception -ForegroundColor Red
}