chore: 初始提交 - 移除所有硬编码凭据,统一从环境变量读取
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<%@ WebHandler Language="C#" Class="SaveArchive" %>
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
|
||||
public class SaveArchive : IHttpHandler
|
||||
{
|
||||
public bool IsReusable { get { return true; } }
|
||||
public void ProcessRequest(HttpContext context)
|
||||
{
|
||||
context.Response.ContentType = "application/json";
|
||||
try
|
||||
{
|
||||
string body;
|
||||
using (var reader = new StreamReader(context.Request.InputStream, Encoding.UTF8)) body = reader.ReadToEnd();
|
||||
var url = ResolveNodeUrl();
|
||||
var req = (HttpWebRequest)WebRequest.Create(url);
|
||||
req.Method = "POST";
|
||||
req.ContentType = "application/json";
|
||||
var data = Encoding.UTF8.GetBytes(body);
|
||||
using (var s = req.GetRequestStream()) s.Write(data, 0, data.Length);
|
||||
using (var resp = (HttpWebResponse)req.GetResponse())
|
||||
using (var rs = resp.GetResponseStream())
|
||||
using (var r = new StreamReader(rs, Encoding.UTF8))
|
||||
{
|
||||
var txt = r.ReadToEnd();
|
||||
context.Response.Write(txt);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
context.Response.StatusCode = 500;
|
||||
var msg = "{\"ok\":false,\"error\":\"" + Escape(e.Message) + "\"}";
|
||||
context.Response.Write(msg);
|
||||
}
|
||||
}
|
||||
string ResolveNodeUrl()
|
||||
{
|
||||
var ports = new int[] { 8976, 5050 };
|
||||
foreach (var p in ports)
|
||||
{
|
||||
var u = "http://localhost:" + p + "/api/archives/save";
|
||||
if (Probe(u)) return u;
|
||||
}
|
||||
return "http://localhost:5050/api/archives/save";
|
||||
}
|
||||
bool Probe(string url)
|
||||
{
|
||||
try
|
||||
{
|
||||
var req = (HttpWebRequest)WebRequest.Create(url.Replace("/api/archives/save", "/api/ping"));
|
||||
req.Method = "GET";
|
||||
req.Timeout = 1500;
|
||||
using (var resp = (HttpWebResponse)req.GetResponse()) return resp.StatusCode == HttpStatusCode.OK;
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
string Escape(string s)
|
||||
{
|
||||
return s.Replace("\\", "\\\\").Replace("\"", "\\\"");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user