@echo off
chcp 65001 >nul

setlocal enabledelayedexpansion
chcp 65001 >nul
cd /d "%~dp0"

set "html_files="

for %%i in (*.html) do (
    if /i not "%%i"=="index.html" (
        set "html_files=!html_files!'%%i', "
    )
)

if defined html_files (
    set "html_files=!html_files:~0,-2!"
) else (
    set "html_files="
)

if not exist "js\" mkdir js

echo let html_files = [%html_files%]; > "js\htmlfiles.js"





@echo off
for /f "tokens=5" %%a in ('netstat -ano ^| findstr ":23456"') do set PID1=%%a
if defined PID1 (
    echo Port 23456 is already in use. Exiting...
) else (
    start "" powershell -NoExit -Command ^
      "$listener = New-Object System.Net.HttpListener;" ^
      "$listener.Prefixes.Add('http://localhost:23456/');" ^
      "$listener.Start();" ^
      "Write-Output 'Listening on port 23456...';" ^
      "while ($true) {" ^
      "  $context = $listener.GetContext();" ^
      "  $request = $context.Request;" ^
      "  $response = $context.Response;" ^
      "  $response.AddHeader('Access-Control-Allow-Origin', '*');" ^
      "  $response.AddHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');" ^
      "  $response.AddHeader('Access-Control-Allow-Headers', 'Content-Type');" ^
      "  if ($request.HttpMethod -eq 'OPTIONS') {" ^
      "    $response.StatusCode = 204;" ^
      "    $response.ContentLength64 = 0;" ^
      "    $response.Close();" ^
      "    continue;" ^
      "  }" ^
      "  $stream = $request.InputStream;" ^
      "  $reader = New-Object System.IO.MemoryStream;" ^
      "  $buffer = New-Object byte[] 4096;" ^
      "  while ($true) {" ^
      "    $bytesRead = $stream.Read($buffer, 0, $buffer.Length);" ^
      "    if ($bytesRead -eq 0) { break }" ^
      "    $reader.Write($buffer, 0, $bytesRead);" ^
      "  }" ^
      "  $rawBytes = $reader.ToArray();" ^
      "  $body = [System.Text.Encoding]::UTF8.GetString($rawBytes);" ^
      "  $queryParams = $request.Url.Query.TrimStart('?').Split('&');" ^
      "  $filePathParam = ($queryParams | Where-Object { $_ -like 'file=*' }) -replace '^file=', '';" ^
      "  if ($filePathParam) {" ^
      "    $decodedFilePath = [System.Uri]::UnescapeDataString($filePathParam);" ^
      "    $utf8Encoding = New-Object System.Text.UTF8Encoding($true);" ^
      "    [System.IO.File]::WriteAllText($decodedFilePath, $body);" ^
      "  }" ^
      "  $response.ContentType = 'text/plain; charset=utf-8';" ^
      "  $buffer = [System.Text.Encoding]::UTF8.GetBytes('true');" ^
      "  $response.ContentLength64 = $buffer.Length;" ^
      "  $response.OutputStream.Write($buffer, 0, $buffer.Length);" ^
      "  $response.OutputStream.Close();" ^
      "}"
)



powershell -ExecutionPolicy Bypass -Command "$path = (Get-Item '%cd%').FullName; $listener = New-Object System.Net.HttpListener; $listener.Prefixes.Add('http://localhost:9998/'); $listener.Start(); Start-Process 'http://localhost:9998/index.html'; while ($listener.IsListening) { $context = $listener.GetContext(); $url = $context.Request.Url.LocalPath.Substring(1); $fullpath = [System.IO.Path]::Combine($path, $url); if ($context.Request.HttpMethod -ne 'GET') { $context.Response.StatusCode = 405; $context.Response.Close(); continue }; if (Test-Path $fullpath -PathType Container) { $dir = [System.IO.Directory]::GetFiles($path); $html = '<html><body><ul>' + ($dir | %% { '<li><a href="' + $_.Substring($path.Length) + '">' + $_.Substring($path.Length) + '</a></li>' }) + '</ul></body></html>'; $buffer = [Text.Encoding]::UTF8.GetBytes($html); $context.Response.ContentType = 'text/html'; } elseif (Test-Path $fullpath) { $buffer = [System.IO.File]::ReadAllBytes($fullpath); $context.Response.ContentType = @{'.html'='text/html';'.txt'='text/plain';'.css'='text/css';'.js'='application/javascript'}[[System.IO.Path]::GetExtension($fullpath)]; } else { $context.Response.StatusCode = 404; }; $context.Response.OutputStream.Write($buffer, 0, $buffer.Length); $context.Response.Close(); }"






endlocal
pause
