# VOW webcam bridge — run on the shop PC (Chester counter). # Contabo ERP is HTTP, so the browser cannot open the webcam in-page. # This serves a capture page on http://127.0.0.1:5516 (secure context) and posts the photo back. $ErrorActionPreference = "Stop" $Port = 5516 function Send-Response( [System.Net.Sockets.NetworkStream]$ns, [int]$code, [string]$contentType, [byte[]]$body ) { if ($null -eq $body) { $body = [byte[]]@() } $reason = switch ($code) { 200 { "OK" } 204 { "No Content" } 404 { "Not Found" } default { "OK" } } $sb = New-Object System.Text.StringBuilder [void]$sb.Append("HTTP/1.1 $code $reason`r`n") [void]$sb.Append("Access-Control-Allow-Origin: *`r`n") [void]$sb.Append("Access-Control-Allow-Methods: GET,POST,OPTIONS`r`n") [void]$sb.Append("Access-Control-Allow-Headers: Authorization,Content-Type,Access-Control-Request-Private-Network`r`n") [void]$sb.Append("Access-Control-Allow-Private-Network: true`r`n") [void]$sb.Append("Cache-Control: no-store`r`n") [void]$sb.Append("Connection: close`r`n") if ($contentType) { [void]$sb.Append("Content-Type: $contentType`r`n") } [void]$sb.Append("Content-Length: $($body.Length)`r`n`r`n") $hdr = [Text.Encoding]::ASCII.GetBytes($sb.ToString()) $ns.Write($hdr, 0, $hdr.Length) if ($body.Length -gt 0) { $ns.Write($body, 0, $body.Length) } $ns.Flush() } function Send-Json($ns, $code, $obj) { $json = ($obj | ConvertTo-Json -Compress) Send-Response $ns $code "application/json; charset=utf-8" ([Text.Encoding]::UTF8.GetBytes($json)) } $listener = New-Object System.Net.Sockets.TcpListener([System.Net.IPAddress]::Loopback, $Port) try { $listener.Start() } catch { Write-Host "Could not bind 127.0.0.1:$Port — already running?" throw } Write-Host "VOW webcam bridge on http://127.0.0.1:$Port/" Write-Host "Leave this window open. Scrap buy / orders / repair will open the camera here." Write-Host "" while ($true) { $client = $listener.AcceptTcpClient() try { $ns = $client.GetStream() $ns.ReadTimeout = 15000 $buf = New-Object byte[] 65536 $ms = New-Object System.IO.MemoryStream while ($true) { $n = $ns.Read($buf, 0, $buf.Length) if ($n -le 0) { break } $ms.Write($buf, 0, $n) $text = [Text.Encoding]::ASCII.GetString($ms.ToArray()) if ($text -match "`r`n`r`n") { break } if ($ms.Length -gt 1024 * 1024) { break } } $req = [Text.Encoding]::ASCII.GetString($ms.ToArray()) $first = ($req -split "`r`n")[0] if ($first -notmatch "^(GET|POST|OPTIONS)\s+(\S+)") { Send-Response $ns 404 "text/plain" ([Text.Encoding]::UTF8.GetBytes("bad request")) continue } $method = $Matches[1] $rawPath = $Matches[2] $path = ($rawPath.Split("?")[0]) if (-not $path) { $path = "/" } if ($method -eq "OPTIONS") { Send-Response $ns 204 $null $null continue } if ($path -eq "/health") { Send-Json $ns 200 @{ ok = $true; camera = "webcam" } continue } if (($path -eq "/" -or $path -eq "/capture" -or $path -eq "/capture.html") -and $method -eq "GET") { $html = @' VOW camera

VOW camera

Starting camera…

'@ Send-Response $ns 200 "text/html; charset=utf-8" ([Text.Encoding]::UTF8.GetBytes($html)) continue } Send-Response $ns 404 "text/plain; charset=utf-8" ([Text.Encoding]::UTF8.GetBytes("not found")) } catch { try { Send-Json $ns 500 @{ message = $_.Exception.Message } } catch {} } finally { try { $client.Close() } catch {} } }