From 544f145f4f3c6a846452bbc22c05979654a5d5ed Mon Sep 17 00:00:00 2001 From: uduki Date: Sat, 5 Sep 2026 02:56:01 +0900 Subject: [PATCH] =?UTF-8?q?IIS=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WebImageViewer/Program.cs | 49 ++++++++++--------- .../PublishProfiles/FolderProfile.pubxml | 21 +++++++- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/WebImageViewer/Program.cs b/WebImageViewer/Program.cs index dca81a2..f444ce3 100644 --- a/WebImageViewer/Program.cs +++ b/WebImageViewer/Program.cs @@ -6,24 +6,27 @@ using WebImageViewer.Class.Services; var builder = WebApplication.CreateBuilder(args); -// Docker/Caddyリバースプロキシからの転送(host.docker.internal:5000)を受け付けるため全インターフェースでリッスン -if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ASPNETCORE_URLS"))) +// ========================================================================= +// 【重要】IIS ホスト時のポート設定 +// IIS (AspNetCoreModuleV2) がポートを動的に割り当てて制御するため、 +// builder.WebHost.UseUrls(...) は呼び出さず、IIS のバインディング (8080) に委ねます。 +// ※ローカルで IIS を介さず exe 単体でデバッグ起動したい場合のみ、環境変数がない時に UseUrls を有効にします。 +if (!builder.Environment.IsProduction() && string.IsNullOrEmpty(Environment.GetEnvironmentVariable("ASPNETCORE_PORT"))) { builder.WebHost.UseUrls("http://0.0.0.0:5000"); } - +// ========================================================================= // ========================================== // サービスの登録 (DIコンテナ) // ========================================== - -// メモリキャッシュ機能の追加(VPSのトラフィック削減・高速化) +// メモリキャッシュ機能の追加 builder.Services.AddMemoryCache(); // HTTP通信用ヘルパーの登録 builder.Services.AddHttpClient(); -// 各種スクレイパークラスの登録(Class/Scrapers フォルダより) +// 各種スクレイパークラスの登録 builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -33,17 +36,18 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); - - - -// ギャラリー統括管理サービスの登録(Class/Services フォルダより) +// ギャラリー統括管理サービスの登録 builder.Services.AddScoped(); -// VPS(リバースプロキシ環境: https://udukipc-web.armonica-vps.work/)対応のフォワードヘッダー設定 +// VPS / Caddy リバースプロキシ環境対応のフォワードヘッダー設定 builder.Services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost; - // リバースプロキシ背後での動作を許可 + + // 多段プロキシ(Caddy -> IIS -> Kestrel)経由でもヘッダーを最後まで反映させる + options.ForwardLimit = null; + + // 非推奨となった KnownNetworks は使わず、KnownIPNetworks をクリア options.KnownIPNetworks.Clear(); options.KnownProxies.Clear(); }); @@ -54,16 +58,17 @@ var app = builder.Build(); // ミドルウェアパイプラインの設定 // ========================================== +// プロキシヘッダーの反映(ルーティング等の前で必ず実行) app.UseForwardedHeaders(); // ========================================== // ルーティング・エンドポイントの定義 (統一アクセス方式) // ========================================== -// トップページ(URL入力・ギャラリー検索UI) +// トップページ app.MapGet("/", () => Results.Content(HtmlRenderer.RenderHomePage(), "text/html; charset=utf-8")); -// 汎用ビューアー画面(URLクエリパラメータ経由: 例 /view?url=https://aurahentai.com/gallery/013597 または /view?site=aurahentai&id=013597) +// 汎用ビューアー画面 app.MapGet("/view", async (string? url, string? site, string? id, string? category, GalleryService galleryService, CancellationToken ct) => { var target = url ?? id; @@ -75,13 +80,13 @@ app.MapGet("/view", async (string? url, string? site, string? id, string? catego return await RenderGalleryResponseAsync(site, target, category, galleryService, ct); }); -// 統一ビューアー画面(3階層: /{site}/{category}/{id} 例: /momonga/fanzine/mo3469109) +// 統一ビューアー画面(3階層) app.MapGet("/{site}/{category}/{id}", async (string site, string category, string id, GalleryService galleryService, CancellationToken ct) => { return await RenderGalleryResponseAsync(site, id, category, galleryService, ct); }); -// 統一ビューアー画面(2階層: /{site}/{id} 例: /momonga/mo3469109, /aurahentai/013597, /fanzine/mo3469109, /gallery/013597) +// 統一ビューアー画面(2階層) app.MapGet("/{site}/{id}", async (string site, string id, GalleryService galleryService, CancellationToken ct) => { return await RenderGalleryResponseAsync(site, id, null, galleryService, ct); @@ -100,19 +105,19 @@ app.MapGet("/api/sites", (GalleryService galleryService) => return Results.Ok(sites); }); -// 統一 JSON API(3階層: /api/{site}/{category}/{id} 例: /api/momonga/fanzine/mo3469109) +// 統一 JSON API(3階層) app.MapGet("/api/{site}/{category}/{id}", async (string site, string category, string id, GalleryService galleryService, CancellationToken ct) => { return await RenderJsonResponseAsync(site, id, category, galleryService, ct); }); -// 統一 JSON API(2階層: /api/{site}/{id} 例: /api/momonga/mo3469109, /api/aurahentai/013597, /api/fanzine/mo3469109) +// 統一 JSON API(2階層) app.MapGet("/api/{site}/{id}", async (string site, string id, GalleryService galleryService, CancellationToken ct) => { return await RenderJsonResponseAsync(site, id, null, galleryService, ct); }); -// 汎用スクレイピング JSON API(URLまたはパラメータ直接指定) +// 汎用スクレイピング JSON API app.MapGet("/api/scrape", async (string? url, string? site, string? id, string? category, GalleryService galleryService, CancellationToken ct) => { var target = url ?? id; @@ -124,7 +129,7 @@ app.MapGet("/api/scrape", async (string? url, string? site, string? id, string? return await RenderJsonResponseAsync(site, target, category, galleryService, ct); }); -// 画像プロキシエンドポイント(CORSや外部Referer制約を回避する必要がある場合に使用) +// 画像プロキシエンドポイント app.MapGet("/api/proxy-image", async (string url, string? referer, HttpHelper httpHelper, CancellationToken ct) => { if (string.IsNullOrWhiteSpace(url)) @@ -146,7 +151,6 @@ app.MapGet("/api/proxy-image", async (string url, string? referer, HttpHelper ht // ========================================== // 共通レスポンス生成ヘルパー関数 // ========================================== - static async Task RenderGalleryResponseAsync( string? site, string idOrUrl, @@ -189,5 +193,4 @@ static async Task RenderJsonResponseAsync( return Results.Ok(gallery); } -app.Run(); - +app.Run(); \ No newline at end of file diff --git a/WebImageViewer/Properties/PublishProfiles/FolderProfile.pubxml b/WebImageViewer/Properties/PublishProfiles/FolderProfile.pubxml index 92637e6..ea4871d 100644 --- a/WebImageViewer/Properties/PublishProfiles/FolderProfile.pubxml +++ b/WebImageViewer/Properties/PublishProfiles/FolderProfile.pubxml @@ -1,15 +1,32 @@  - false false - true + false Release Any CPU FileSystem C:\docker\web\ FileSystem <_TargetId>Folder + net10.0 + false + + + + + + + + + + + + + + \ No newline at end of file