-
-
Save orange-in-space/d63862f0eda54fe3edd0d4f5b8fce297 to your computer and use it in GitHub Desktop.
.bat .cs .html combined, Single file Web Server Demo. Windowsバッチファイル 兼 C# ソースコード 兼 HTMLを単一のファイルにまとめて、自分でcsc.exeを探してコンパイルできるウェブサーバー兼コンテンツ><
This file contains 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
/* | |
@echo off | |
@goto bat_begin | |
Windows-batch-file, C-Sharp-Source, HTML Combined File DEMO | |
(C) orange_in_space, 2022 | |
License: CC0 | |
This is a simple demonstration And probably contains security issues. | |
Working, but Not practical code. | |
これは実証デモであってセキュリティー的にはたぶん問題があるかも><; | |
あくまでこういうことも出来るよ!>< って簡素で雑なお手本的なやつ>< | |
:bat_begin | |
setlocal | |
for /f "usebackq delims=" %%A in (`dir /s /b %WINDIR%\Microsoft.NET\CSC.EXE`) do set CSCFULLPATH=%%A | |
echo csc fullpath is %CSCFULLPATH% | |
@echo on | |
%CSCFULLPATH% /platform:x64 /target:exe %~n0.bat | |
@if %errorlevel% equ 0 ( echo %~n0.exe was generated? maybe... ) | |
@openfiles > NUL 2>&1 | |
@if not %ERRORLEVEL% == 0 ( | |
@echo requires administrator for server | |
@rem powershell start-process \"%~f0\" -Verb runas | |
goto :eof | |
) | |
@echo -------- | |
@rem Server Start | |
%~n0.exe %~n0.bat | |
goto :eof | |
*/ | |
// cs part | |
using System; | |
using System.IO; | |
using System.Net; | |
using System.Text; | |
namespace MinimumWebServer | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string Combined_src_path = ""; | |
if (args.Length>0) | |
{ | |
Combined_src_path = args[0]; | |
} | |
using (MinimumWebServerEngine engine = new MinimumWebServerEngine(Combined_src_path)) | |
{ | |
while (true) | |
{ | |
engine.Loop(); | |
} | |
} | |
} | |
} | |
class MinimumWebServerEngine : IDisposable | |
{ | |
HttpListener httpListener; | |
public MinimumWebServerEngine(string CombinedSourceFilePath) | |
{ | |
Console.WriteLine("MinimumWebServerEngine"); | |
string prefix = @"http://+:8080/"; | |
try | |
{ | |
Console.WriteLine("ImplantHTMLSourceReader"); | |
Console.WriteLine("SRC IS " + CombinedSourceFilePath); | |
ReadImplantHTMLSource(CombinedSourceFilePath); | |
if (ImplantHTMLReady) | |
{ | |
Console.WriteLine("ImplantHTMLSourceReady"); | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("ImplantHTMLSourceReader Error"); | |
Console.WriteLine(e.Message); | |
} | |
httpListener = new HttpListener(); | |
httpListener.Prefixes.Clear(); | |
httpListener.Prefixes.Add(prefix); | |
Console.WriteLine("MinimumWebServerEngine Start"); | |
httpListener.Start(); | |
Console.WriteLine("Server Running"); | |
Console.WriteLine(""); | |
} | |
public void Dispose() | |
{ | |
httpListener.Close(); | |
} | |
private bool ImplantHTMLReady = false; | |
private byte[] html_root_srcBytes; | |
private Encoding ImprantHTML_src_encoding; | |
private enum _IHRState | |
{ | |
Cs, | |
Html | |
} | |
private void ReadImplantHTMLSource(string source_filepath) | |
{ | |
if (File.Exists(source_filepath)) | |
{ | |
using (StreamReader sr = new StreamReader(source_filepath)) | |
{ | |
ImprantHTML_src_encoding = sr.CurrentEncoding; | |
StringBuilder html_src_sb = new StringBuilder(); | |
_IHRState state = _IHRState.Cs; | |
while (!sr.EndOfStream) | |
{ | |
string src_line = sr.ReadLine(); | |
if (state == _IHRState.Cs && src_line.Trim() == @"#if html_source_begin") | |
{ | |
state = _IHRState.Html; | |
} | |
else if (state == _IHRState.Html && src_line.Trim() == @"#endif") | |
{ | |
state = _IHRState.Cs; | |
} | |
else if (state == _IHRState.Html) | |
{ | |
html_src_sb.AppendLine(src_line); | |
} | |
} | |
if (html_src_sb.Length > 0) | |
{ | |
using (MemoryStream ms = new MemoryStream()) | |
{ | |
html_root_srcBytes = ImprantHTML_src_encoding.GetBytes(html_src_sb.ToString()); | |
ImplantHTMLReady = true; | |
} | |
} | |
} | |
} | |
} | |
public void Loop() | |
{ | |
string root = @"D:\_web_root"; | |
bool fileserverEnabled = false; | |
HttpListenerContext context = httpListener.GetContext(); | |
HttpListenerRequest req = context.Request; | |
HttpListenerResponse res = context.Response; | |
Console.WriteLine("User Host Address: "+req.UserHostAddress); | |
Console.WriteLine("Request HttpMethod: "+req.HttpMethod); | |
Console.WriteLine("Request URL: "+req.RawUrl); | |
if (ImplantHTMLReady && req.RawUrl == @"/" | req.RawUrl == @"/index.html") | |
{ | |
//res.RedirectLocation = @"/index.html"; | |
res.OutputStream.Write(html_root_srcBytes, 0, html_root_srcBytes.Length); | |
res.ContentType = @"text/html"; | |
res.ContentEncoding = ImprantHTML_src_encoding; | |
Console.WriteLine(res.Headers); | |
} | |
else | |
{ | |
if (fileserverEnabled) | |
{ | |
string path = root + req.RawUrl.Replace("/", "\\"); | |
if (req.RawUrl == @"/") | |
{ | |
path = root + @"\index.html"; | |
} | |
if (File.Exists(path)) | |
{ | |
byte[] content = File.ReadAllBytes(path); | |
res.OutputStream.Write(content, 0, content.Length); | |
} | |
else | |
{ | |
res.StatusCode = 404; | |
} | |
} | |
else | |
{ | |
res.StatusCode = 404; | |
} | |
} | |
res.Close(); | |
} | |
} | |
} | |
//HTML | |
#if html_source_begin | |
<html> | |
<head> | |
<title> | |
Combined File DEMO | |
</title> | |
</head> | |
<body> | |
<h1>Windows-batch-file, C-Sharp-Source, HTML Combined File DEMO</h1> | |
Hello!>< | |
</body> | |
</html> | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment