This repository has been archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Invoke-Login.ps1
77 lines (63 loc) · 2.9 KB
/
Invoke-Login.ps1
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
[CmdletBinding()]
param(
[Parameter(Mandatory, Position = 0)]
[string]
$UUID,
[Parameter(Mandatory, Position = 1)]
[string]
$Password,
[Parameter(Position = 2)]
[ValidateSet('cmcc', 'telecom', 'unicom')]
[string]
$Provider
)
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 Green
} else {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Currently logged out." -ForegroundColor Blue
if ($Provider -and $isDorm) { $UUID = $UUID + '@' + $Provider }
$urlLogin = $serverHostAlt + "eportal/?c=Portal&a=login&callback=dr1001&login_method=1&user_account=%2C0%2C$($UUID)&user_password=$($Password)&wlan_user_ip=$($status.v46ip)&wlan_user_ipv6=&wlan_user_mac=$($status.ss4)&wlan_ac_ip=&wlan_ac_name=&jsVersion=3.3.3"
Write-Debug "Connecting to $($urlLogin)"
Invoke-WebRequest -URI $urlLogin -Headers $headers -UserAgent $userAgent -WebSession $session -ErrorAction Stop |
Get-ResponseJson | ConvertFrom-Json -OutVariable 'login' | Write-Debug
if ($login.result -eq 1) {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Authentication succeeded. ($($login.msg))" -ForegroundColor Green
} else {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Authentication failed with code $($login.ret_code). ($($login.msg))" -ForegroundColor Red
}
}
} catch {
Write-Host "$(Get-Date -Format 'yyyy-MM-ddTHH:mmZK')|Failed to connect to authentication server." -ForegroundColor Red
Write-Host $_.Exception -ForegroundColor Red
}