Created
March 25, 2023 23:47
-
-
Save hazulifidastian/880279d67b71fd1b75ad7274bbfc213c to your computer and use it in GitHub Desktop.
Script powershell UDP server di windows sebagai aplikasi remote yang bisa menerima perintah untuk me-reset aplikasi lain.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$port = 8080 | |
$targetProcessName = "FService.exe" | |
$targetProcessPath = "C:\\Program Files (x86)\\EasyLink SDK\\FService.exe" | |
$endpoint = New-Object System.Net.IPEndPoint ([IPAddress]::Any, $port) | |
function KillPort { | |
param ( | |
$port | |
) | |
$foundProcesses = netstat -ano | findstr :$port | |
# $activePortPattern = ":$port\s.+LISTENING\s+\d+$" | |
$activePortPattern = ":$port\s.+\d+$" | |
$pidNumberPattern = "\d+$" | |
IF ($foundProcesses | Select-String -Pattern $activePortPattern -Quiet) { | |
$matches = $foundProcesses | Select-String -Pattern $activePortPattern | |
$firstMatch = $matches.Matches.Get(0).Value | |
$pidNumber = [regex]::match($firstMatch, $pidNumberPattern).Value | |
taskkill /pid $pidNumber /f | |
} | |
} | |
KillPort $port | |
Try { | |
while($true) { | |
$socket = New-Object System.Net.Sockets.UdpClient $port | |
$contentByte = $socket.Receive([ref]$endpoint) | |
$enc = [system.Text.Encoding]::UTF8 | |
$contentStr = $enc.GetString($contentByte) | |
$token, $command = $contentStr.split(':') | |
$dateText = Get-Date -Format "yyyyMMdd" | |
$tokenValidator = "6f797fcc675fe0f411ef56931936c63b8a026660"+ $dateText | |
$sha1 = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider | |
$hashByteArray = $sha1.computeHash($enc.GetBytes($tokenValidator)) | |
$validToken = "" | |
foreach ($byte in $hashByteArray) { | |
$validToken += "{0:X2}".ToLower() -f $byte | |
} | |
if ($token -eq $validToken) { | |
if ($command -eq "RESTART") { | |
taskkill /IM $targetProcessName /F | |
Start-Process $targetProcessPath | |
} | |
$text = $enc.GetBytes("OK") | |
[void]$socket.Send($text, $text.Length, $endpoint) | |
} else { | |
$text = $enc.GetBytes("FAILED") | |
[void]$socket.Send($text, $text.Length, $endpoint) | |
} | |
$socket.Close() | |
} | |
} Catch { | |
"$($Error[0])" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment