MINI
C:
/
siak
/
app
/
dukcapil
/
remote
/
Upload File:
files >> C:/siak/app/dukcapil/remote/Controller.ps1
# ========================================================== # DUKCAPIL PROXY REMOTE CONTROLLER # SERVER : 192.168.25.15 # PROXY : 3030 # CONTROLLER : 38080 # ========================================================== $AppPath = "C:\Siak_Terpusat_versi_12.4.1_WAR\index-x86.exe" $ProcessName = "index-x86" $ProxyIP = "192.168.25.15" $ProxyPort = 3030 $ControllerPort = 38080 $Token = "DUKCAPIL-2026" $listener = New-Object System.Net.HttpListener $listener.Prefixes.Add("http://+:$ControllerPort/") function Get-ProxyProcess { return Get-Process -Name $ProcessName -ErrorAction SilentlyContinue | Where-Object { try { $_.Path -eq $AppPath } catch { $false } } | Select-Object -First 1 } function Get-Status { $process = Get-ProxyProcess if ($process) { return @{ status = "ONLINE" pid = $process.Id ip = $ProxyIP port = $ProxyPort } } return @{ status = "OFFLINE" pid = 0 ip = $ProxyIP port = $ProxyPort } } # PowerShell lama/Windows lama: JSON dibuat manual agar tidak # bergantung pada ConvertTo-Json. function Escape-JsonString($Value) { if ($null -eq $Value) { return "" } $s = [string]$Value $s = $s.Replace("\", "\\") $s = $s.Replace('"', '\"') $s = $s.Replace("`r", "\r") $s = $s.Replace("`n", "\n") $s = $s.Replace("`t", "\t") return $s } function Convert-ObjectToJson($ResponseObject) { $parts = @() foreach ($key in $ResponseObject.Keys) { $value = $ResponseObject[$key] $safeKey = Escape-JsonString $key if ($value -is [int] -or $value -is [long] -or $value -is [double] -or $value -is [decimal]) { $parts += '"' + $safeKey + '":' + ([string]$value) } elseif ($value -is [bool]) { if ($value) { $parts += '"' + $safeKey + '":true' } else { $parts += '"' + $safeKey + '":false' } } else { $safeValue = Escape-JsonString $value $parts += '"' + $safeKey + '":"' + $safeValue + '"' } } return "{" + ($parts -join ",") + "}" } function Send-Json($ResponseObject) { $json = Convert-ObjectToJson $ResponseObject $buffer = [System.Text.Encoding]::UTF8.GetBytes($json) try { $response = $context.Response $response.StatusCode = 200 $response.ContentType = "application/json" $response.ContentEncoding = [System.Text.Encoding]::UTF8 $response.ContentLength64 = $buffer.Length $output = $response.OutputStream $output.Write($buffer, 0, $buffer.Length) $output.Flush() $output.Close() } catch { Write-Host "SEND JSON ERROR: $($_.Exception.Message)" try { $context.Response.Close() } catch {} } } function Start-Proxy { $process = Get-ProxyProcess if ($process) { return Get-Status } if (!(Test-Path $AppPath)) { return @{ status = "ERROR" message = "index-x86.exe tidak ditemukan" } } try { Start-Process ` -FilePath $AppPath ` -WorkingDirectory (Split-Path $AppPath) Start-Sleep -Seconds 2 return Get-Status } catch { return @{ status = "ERROR" message = $_.Exception.Message } } } function Stop-Proxy { $process = Get-ProxyProcess if ($process) { try { Stop-Process -Id $process.Id -Force Start-Sleep -Seconds 1 } catch { return @{ status = "ERROR" message = $_.Exception.Message } } } return Get-Status } function Restart-Proxy { Stop-Proxy | Out-Null Start-Sleep -Seconds 2 return Start-Proxy } # ========================================================== # START SERVER # ========================================================== try { $listener.Start() Write-Host "" Write-Host "============================================" Write-Host " DUKCAPIL PROXY REMOTE CONTROLLER" Write-Host "============================================" Write-Host "" Write-Host "SERVER : $ProxyIP" Write-Host "PROXY : $ProxyPort" Write-Host "CONTROLLER : $ControllerPort" Write-Host "" Write-Host "API:" Write-Host "http://$ProxyIP`:$ControllerPort/status" Write-Host "" Write-Host "Controller ONLINE" Write-Host "============================================" Write-Host "" while ($listener.IsListening) { $context = $null try { $context = $listener.GetContext() $request = $context.Request $response = $context.Response $path = $request.Url.AbsolutePath.ToLower() Write-Host "$(Get-Date -Format 'HH:mm:ss') $($request.HttpMethod) $path" # -------------------------------------------------- # TOKEN # -------------------------------------------------- $clientToken = $request.Headers["X-Dukcapil-Token"] if ($clientToken -ne $Token) { $json = Convert-ObjectToJson @{ status = "UNAUTHORIZED" message = "Token salah" } $buffer = [System.Text.Encoding]::UTF8.GetBytes($json) try { $response.StatusCode = 401 $response.ContentType = "application/json" $response.ContentEncoding = [System.Text.Encoding]::UTF8 $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) $response.OutputStream.Flush() $response.OutputStream.Close() } catch { try { $response.Close() } catch {} } continue } # -------------------------------------------------- # STATUS # -------------------------------------------------- if ($path -eq "/status") { Send-Json (Get-Status) } # -------------------------------------------------- # START # -------------------------------------------------- elseif ($path -eq "/start") { Send-Json (Start-Proxy) } # -------------------------------------------------- # STOP # -------------------------------------------------- elseif ($path -eq "/stop") { Send-Json (Stop-Proxy) } # -------------------------------------------------- # RESTART # -------------------------------------------------- elseif ($path -eq "/restart") { Send-Json (Restart-Proxy) } else { $json = Convert-ObjectToJson @{ status = "ERROR" message = "Endpoint tidak ditemukan" } $buffer = [System.Text.Encoding]::UTF8.GetBytes($json) try { $response.StatusCode = 404 $response.ContentType = "application/json" $response.ContentEncoding = [System.Text.Encoding]::UTF8 $response.ContentLength64 = $buffer.Length $response.OutputStream.Write($buffer, 0, $buffer.Length) $response.OutputStream.Flush() $response.OutputStream.Close() } catch { try { $response.Close() } catch {} } } } catch { Write-Host "ERROR: $($_.Exception.Message)" if ($context -and $context.Response) { try { $json = Convert-ObjectToJson @{ status = "ERROR" message = $_.Exception.Message } $buffer = [System.Text.Encoding]::UTF8.GetBytes($json) $context.Response.StatusCode = 500 $context.Response.ContentType = "application/json" $context.Response.ContentEncoding = [System.Text.Encoding]::UTF8 $context.Response.ContentLength64 = $buffer.Length $context.Response.OutputStream.Write($buffer, 0, $buffer.Length) $context.Response.OutputStream.Flush() $context.Response.OutputStream.Close() } catch {} } } } } finally { if ($listener) { try { $listener.Stop() } catch {} try { $listener.Close() } catch {} } }
Copyright ©2021 || Defacer Indonesia