Update src/WinBGP-API.ps1
This commit is contained in:
@@ -363,6 +363,8 @@ if ($Configuration) {
|
|||||||
# Write-Log "API request received: $FullRequest" -EventLogSource 'WinBGP-API'
|
# Write-Log "API request received: $FullRequest" -EventLogSource 'WinBGP-API'
|
||||||
$FullPath=($request.RawUrl).substring(1)
|
$FullPath=($request.RawUrl).substring(1)
|
||||||
$Path=$FullPath.Split('?')[0]
|
$Path=$FullPath.Split('?')[0]
|
||||||
|
switch -wildcard ($FullPath) {
|
||||||
|
'api*' {
|
||||||
switch ($request.HttpMethod) {
|
switch ($request.HttpMethod) {
|
||||||
'GET' {
|
'GET' {
|
||||||
if ($FullPath -eq 'api') {
|
if ($FullPath -eq 'api') {
|
||||||
@@ -454,7 +456,55 @@ if ($Configuration) {
|
|||||||
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif ($FullPath -eq 'metrics') {
|
}
|
||||||
|
}
|
||||||
|
'POST' {
|
||||||
|
$RouteName = $request.QueryString.Item("RouteName")
|
||||||
|
$Path=$Path.replace('api/','')
|
||||||
|
Write-Log "API received POST request '$Path' from '$RequestUser' - Source IP: '$RequestHost'" -AdditionalFields $RouteName
|
||||||
|
switch ($Path) {
|
||||||
|
'Reload' {
|
||||||
|
[string]$ActionOutput=WinBGP -Reload
|
||||||
|
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
||||||
|
$outputHeader.Add('Content-Type', 'application/json')
|
||||||
|
}
|
||||||
|
'StartMaintenance' {
|
||||||
|
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StartMaintenance
|
||||||
|
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
||||||
|
$outputHeader.Add('Content-Type', 'application/json')
|
||||||
|
}
|
||||||
|
'StartRoute' {
|
||||||
|
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StartRoute
|
||||||
|
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
||||||
|
$outputHeader.Add('Content-Type', 'application/json')
|
||||||
|
}
|
||||||
|
'StopMaintenance' {
|
||||||
|
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StopMaintenance
|
||||||
|
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
||||||
|
$outputHeader.Add('Content-Type', 'application/json')
|
||||||
|
}
|
||||||
|
'StopRoute' {
|
||||||
|
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StopRoute
|
||||||
|
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
||||||
|
$outputHeader.Add('Content-Type', 'application/json')
|
||||||
|
}
|
||||||
|
Default {
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch ($commandOutput.output) {
|
||||||
|
'Success' { $statusCode = [System.Net.HttpStatusCode]::OK }
|
||||||
|
'WinBGP not ready' { $statusCode = [System.Net.HttpStatusCode]::InternalServerError }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Default {
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'metrics' {
|
||||||
|
switch ($request.HttpMethod) {
|
||||||
|
'GET' {
|
||||||
# Define WinBGP Prometheus metrics
|
# Define WinBGP Prometheus metrics
|
||||||
$WinBGP_metrics=@()
|
$WinBGP_metrics=@()
|
||||||
|
|
||||||
@@ -497,13 +547,87 @@ if ($Configuration) {
|
|||||||
# Add header
|
# Add header
|
||||||
$outputHeader.Add('Content-Type', 'text/plain; version=0.0.4; charset=utf-8')
|
$outputHeader.Add('Content-Type', 'text/plain; version=0.0.4; charset=utf-8')
|
||||||
$statusCode = [System.Net.HttpStatusCode]::OK
|
$statusCode = [System.Net.HttpStatusCode]::OK
|
||||||
} else {
|
}
|
||||||
|
Default {
|
||||||
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
'mcp' {
|
||||||
|
# Always send json header for MCP
|
||||||
|
$outputHeader.Add('Content-Type', 'application/json')
|
||||||
|
# Body request
|
||||||
|
$requestBodyReader = New-Object System.IO.StreamReader $request.InputStream
|
||||||
|
$requestBody=$requestBodyReader.ReadToEnd()
|
||||||
|
$jsonRequestBody=$requestBody | convertfrom-json
|
||||||
|
switch ($request.HttpMethod) {
|
||||||
|
'GET' {
|
||||||
|
# Send not allowed (405) to avoid switching to SSE
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::MethodNotAllowed
|
||||||
|
}
|
||||||
'POST' {
|
'POST' {
|
||||||
|
switch ($jsonRequestBody.method) {
|
||||||
|
'initialize' {
|
||||||
|
$commandOutput = '{"jsonrpc":"2.0","id":' + ($jsonRequestBody.id | ConvertTo-Json -Depth 10 -Compress) + ',"result":{"protocolVersion":"2025-03-26","capabilities":{"tools":{"listChanged":false}},"serverInfo":{"name":"WinBGP MCP Server","version":"0.1.0"}}}'
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::OK
|
||||||
|
}
|
||||||
|
'notifications/initialized' {
|
||||||
|
# Accept the initialization request
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::Accepted
|
||||||
|
}
|
||||||
|
'notifications/responses' {
|
||||||
|
# Accept the notification request
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::Accepted
|
||||||
|
}
|
||||||
|
'tools/list' {
|
||||||
|
$toolsListJson = '{"name":"Get-WinBGPRoute","description":"Get-WinBGPRoute","inputSchema":{"type":"object","properties":{"ComputerName":{"type":"string","description":"Single or multiple ComputerName (Default: localhost)"},"Name":{"type":"string","description":"Single or multiple Route Name (IntelliSense availalble)"}},"required":[]},"returns":{"type":"string","description":"Get-WinBGPRoute"}}'
|
||||||
|
$commandOutput = '{"jsonrpc":"2.0","id":' + ($jsonRequestBody.id | ConvertTo-Json -Depth 10 -Compress) + ',"result":{"tools":' + $toolsListJson + '}}'
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::OK
|
||||||
|
}
|
||||||
|
'tools/call' {
|
||||||
|
#$toolName = $jsonRequestBody.params.name
|
||||||
|
#$Arguments = $jsonRequestBody.params.arguments
|
||||||
|
|
||||||
|
#$result = & $toolName @targetArgs
|
||||||
|
$result=[PSCustomObject]@{
|
||||||
|
Name = 'demo.webalex.lab'
|
||||||
|
Network = '172.16.10.10/32'
|
||||||
|
Status = 'up'
|
||||||
|
MaintenanceTimestamp = $null
|
||||||
|
ComputerName = 'server1.webalex.lab'
|
||||||
|
}
|
||||||
|
|
||||||
|
$callresponse = [ordered]@{
|
||||||
|
jsonrpc = "2.0"
|
||||||
|
id = $jsonRequestBody.id
|
||||||
|
result = @{
|
||||||
|
content = @(
|
||||||
|
[ordered]@{
|
||||||
|
type = "text"
|
||||||
|
text = $result | Out-String
|
||||||
|
}
|
||||||
|
)
|
||||||
|
isError = $false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$commandOutput=$callresponse | ConvertTo-Json -Depth 10 -Compress
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::OK
|
||||||
|
}
|
||||||
|
Default {
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Default {
|
||||||
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
# Add stop method to stop API (TO IMPROVE)
|
# Add stop method to stop API (TO IMPROVE)
|
||||||
if ($FullPath -eq 'stop') {
|
'stop' {
|
||||||
|
switch ($request.HttpMethod) {
|
||||||
|
'POST' {
|
||||||
# Only local request are authorized
|
# Only local request are authorized
|
||||||
if ($request.IsLocal) {
|
if ($request.IsLocal) {
|
||||||
$keepListening = $false
|
$keepListening = $false
|
||||||
@@ -512,47 +636,10 @@ if ($Configuration) {
|
|||||||
$statusCode = [System.Net.HttpStatusCode]::Forbidden
|
$statusCode = [System.Net.HttpStatusCode]::Forbidden
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
elseif ($FullPath -like 'api/*') {
|
|
||||||
$RouteName = $request.QueryString.Item("RouteName")
|
|
||||||
$Path=$Path.replace('api/','')
|
|
||||||
Write-Log "API received POST request '$Path' from '$RequestUser' - Source IP: '$RequestHost'" -AdditionalFields $RouteName
|
|
||||||
switch ($Path) {
|
|
||||||
'Reload' {
|
|
||||||
[string]$ActionOutput=WinBGP -Reload
|
|
||||||
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
|
||||||
$outputHeader.Add('Content-Type', 'application/json')
|
|
||||||
}
|
|
||||||
'StartMaintenance' {
|
|
||||||
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StartMaintenance
|
|
||||||
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
|
||||||
$outputHeader.Add('Content-Type', 'application/json')
|
|
||||||
}
|
|
||||||
'StartRoute' {
|
|
||||||
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StartRoute
|
|
||||||
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
|
||||||
$outputHeader.Add('Content-Type', 'application/json')
|
|
||||||
}
|
|
||||||
'StopMaintenance' {
|
|
||||||
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StopMaintenance
|
|
||||||
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
|
||||||
$outputHeader.Add('Content-Type', 'application/json')
|
|
||||||
}
|
|
||||||
'StopRoute' {
|
|
||||||
[string]$ActionOutput=WinBGP -RouteName "$RouteName" -StopRoute
|
|
||||||
$commandOutput = ConvertTo-Json -InputObject @{'output'=$ActionOutput}
|
|
||||||
$outputHeader.Add('Content-Type', 'application/json')
|
|
||||||
}
|
|
||||||
Default {
|
Default {
|
||||||
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
switch ($commandOutput.output) {
|
|
||||||
'Success' { $statusCode = [System.Net.HttpStatusCode]::OK }
|
|
||||||
'WinBGP not ready' { $statusCode = [System.Net.HttpStatusCode]::InternalServerError }
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Default {
|
Default {
|
||||||
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
$statusCode = [System.Net.HttpStatusCode]::NotImplemented
|
||||||
|
|||||||
Reference in New Issue
Block a user