Untitled

mail@pastecode.io avatar
unknown
powershell
a year ago
4.8 kB
2
Indexable
Never
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12

Function get-telenetStats
    {

    $headers = @{
        "User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36"
        "x-alt-referer" = "https://www2.telenet.be/nl/klantenservice/#/pages=1/menu=selfservice"
    }
    $url = "https://api.prd.telenet.be/ocapi/oauth/userdetails"
    $state, $nonce = ""
    try {
        $response = Invoke-WebRequest -Uri $url -Headers $headers -SessionVariable s -TimeoutSec 10
    }
    catch {
        Write-Host $_.ErrorDetails.Message
        $state = $_.ErrorDetails.Message.Split(",")[0]
        $nonce = $_.ErrorDetails.Message.Split(",")[1]
    }

    $url = "https://login.prd.telenet.be/openid/oauth/authorize?client_id=ocapi&response_type=code&claims={""id_token"":{""http://telenet.be/claims/roles"":null,""http://telenet.be/claims/licenses"":null}}&lang=nl&state=$state&nonce=$nonce&prompt=login"
    $response = Invoke-WebRequest -Uri $url -SessionVariable s -TimeoutSec 10
    $url = "https://login.prd.telenet.be/openid/login.do"
    $data = @{
        "j_username" = ""
        "j_password" = ""
        "rememberme" = $true
    }
    $response = Invoke-WebRequest -Uri $url -Method POST -Body $data -WebSession $s -TimeoutSec 10

    if ($s.Cookies.GetAllCookies() | Where-Object {$_.Name -like "TOKEN-XSRF"}) {
        $headers = @{
            "x-alt-referer" = "https://www2.telenet.be/nl/klantenservice/#/pages=1/menu=selfservice"
            "X-TOKEN-XSRF" = ($s.Cookies.GetAllCookies() | Where-Object {$_.Name -like "TOKEN-XSRF"}).Value
        }
    } else {
        $headers = @{
            "x-alt-referer" = "https://www2.telenet.be/nl/klantenservice/#/pages=1/menu=selfservice"
        }
    }

    $url = "https://api.prd.telenet.be/ocapi/public/?p=internetusage,internetusagereminder"
    $response = Invoke-WebRequest -Uri $url -Method Get -Headers $headers -WebSession $s -TimeoutSec 10

    $telenetValues = $response.Content | ConvertFrom-Json

    $currentPeak = [math]::Round(($telenetValues.internetusage[0].availableperiods[0].usages[0].totalusage.peak / 1024 / 1024))
    $currentoffPeak = [math]::Round(($telenetValues.internetusage[0].availableperiods[0].usages[0].totalusage.offpeak / 1024 / 1024))
    $currentStart = ($telenetValues.internetusage[0].availableperiods[0].usages[0].periodstart).ToString("dd/MM")
    $currentEnd = ($telenetValues.internetusage[0].availableperiods[0].usages[0].periodend).ToString("dd/MM")

    Return "$currentPeak;$currentoffPeak;$currentStart;$currentEnd"
}   

#
# LETS GO
#

$telenetStats = get-telenetStats

$Current_Folder = split-path $MyInvocation.MyCommand.Path
 
# Add assemblies for WPF and Mahapps
[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('presentationframework') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('System.Drawing') | out-null
[System.Reflection.Assembly]::LoadWithPartialName('WindowsFormsIntegration') | out-null
[System.Reflection.Assembly]::LoadFrom("$Current_Folder\MahApps.Metro.dll") | out-null
 
# Add the systray icon 
$Main_Tool_Icon = New-Object System.Windows.Forms.NotifyIcon
$Main_Tool_Icon.Text = "Piekuren: $($telenetStats.Split(";")[0])GB  Daluren: $($telenetStats.Split(";")[1])GB  Reset op: $($telenetStats.Split(";")[3])"
$Main_Tool_Icon.Icon = "$Current_Folder\telenet.ico"
$Main_Tool_Icon.Visible = $true
 
# Make PowerShell Disappear
$windowcode = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
$asyncwindow = Add-Type -MemberDefinition $windowcode -name Win32ShowWindowAsync -namespace Win32Functions -PassThru
$null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0)
 
# Use a Garbage colection to reduce Memory RAM
[System.GC]::Collect()

# Add all menus as context menus
$contextmenu = New-Object System.Windows.Forms.ContextMenuStrip

$Menu_Refresh = New-Object System.Windows.Forms.ToolStripMenuItem
$Menu_Refresh.Text = "Last Refresh: $((Get-Date).ToShortTimeString())"
$contextmenu.Items.Add($Menu_Refresh)
 
$Menu_Exit = New-Object System.Windows.Forms.ToolStripMenuItem
$Menu_Exit.Text = "Exit"
$contextmenu.Items.Add($Menu_Exit)

$Main_Tool_Icon.ContextMenuStrip = $contextmenu

# Action after clicking on the Exit context menu
$Menu_Exit.add_Click({
    $Main_Tool_Icon.Visible = $false
    $window.Close()
    Stop-Process $pid
})

$appContext = New-Object System.Windows.Forms.ApplicationContext
[void][System.Windows.Forms.Application]::Run($appContext)