|
@@ -15,11 +15,11 @@ using System.Web;
|
|
|
|
|
|
|
|
namespace Fine.HttpServer
|
|
namespace Fine.HttpServer
|
|
|
{
|
|
{
|
|
|
- public class HttpServer<T> where T: Fine.IHttpServer.IHttpHandler
|
|
|
|
|
|
|
+ public class HttpServer<T> where T : Fine.IHttpServer.IHttpHandler
|
|
|
{
|
|
{
|
|
|
HttpListener listener;
|
|
HttpListener listener;
|
|
|
HttpServerParams _serverParams;
|
|
HttpServerParams _serverParams;
|
|
|
- public bool Status { get; set; } = false;
|
|
|
|
|
|
|
+ public bool Status { get; set; } = false;
|
|
|
public HttpServer(HttpServerParams serverparams)
|
|
public HttpServer(HttpServerParams serverparams)
|
|
|
{
|
|
{
|
|
|
_serverParams = serverparams;
|
|
_serverParams = serverparams;
|
|
@@ -27,15 +27,15 @@ namespace Fine.HttpServer
|
|
|
}
|
|
}
|
|
|
public void Start()
|
|
public void Start()
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- listener.Prefixes.Add("http://"+_serverParams.Host+":"+_serverParams.Port+"/");
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ listener.Prefixes.Add("http://" + _serverParams.Host + ":" + _serverParams.Port + "/");
|
|
|
//listener.Prefixes.Add("https://" + _serverParams.Host + ":" + (_serverParams.Port + 1) + "/");
|
|
//listener.Prefixes.Add("https://" + _serverParams.Host + ":" + (_serverParams.Port + 1) + "/");
|
|
|
if (_serverParams.SSL == true)
|
|
if (_serverParams.SSL == true)
|
|
|
{
|
|
{
|
|
|
listener.Prefixes.Add("https://" + _serverParams.Host + ":" + _serverParams.HttpsPort + "/");
|
|
listener.Prefixes.Add("https://" + _serverParams.Host + ":" + _serverParams.HttpsPort + "/");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
//启动监听器
|
|
//启动监听器
|
|
|
listener.Start();
|
|
listener.Start();
|
|
|
Status = true;
|
|
Status = true;
|
|
@@ -44,35 +44,36 @@ namespace Fine.HttpServer
|
|
|
{
|
|
{
|
|
|
Console.WriteLine($"Start up Http Server:https://{_serverParams.Host}:{_serverParams.HttpsPort}");
|
|
Console.WriteLine($"Start up Http Server:https://{_serverParams.Host}:{_serverParams.HttpsPort}");
|
|
|
}
|
|
}
|
|
|
- listener.BeginGetContext(Result,null);
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+ listener.BeginGetContext(Result, null);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
if (_serverParams.SSL)
|
|
if (_serverParams.SSL)
|
|
|
{
|
|
{
|
|
|
- AddCertificate(_serverParams.Host+":"+ _serverParams.HttpsPort);
|
|
|
|
|
|
|
+ AddCertificate(_serverParams.Host + ":" + _serverParams.HttpsPort);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void AddCertificate(string ipport)
|
|
private void AddCertificate(string ipport)
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
- X509Certificate2 cert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory+"/config/fine.pfx", _serverParams.CertPassword, X509KeyStorageFlags.MachineKeySet |
|
|
|
|
|
|
|
+
|
|
|
|
|
+ X509Certificate2 cert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + "/config/fine.pfx", _serverParams.CertPassword, X509KeyStorageFlags.MachineKeySet |
|
|
|
X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
|
|
X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
|
|
|
- string hashstr=cert.GetCertHashString();
|
|
|
|
|
|
|
+ string hashstr = cert.GetCertHashString();
|
|
|
Console.WriteLine("CertHash:" + hashstr);
|
|
Console.WriteLine("CertHash:" + hashstr);
|
|
|
object[] attrs = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
|
|
object[] attrs = System.Reflection.Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
|
|
|
Guid id = new Guid(((System.Runtime.InteropServices.GuidAttribute)attrs[0]).Value);
|
|
Guid id = new Guid(((System.Runtime.InteropServices.GuidAttribute)attrs[0]).Value);
|
|
|
string appid = id.ToString();
|
|
string appid = id.ToString();
|
|
|
- Console.WriteLine("App ID:"+appid);
|
|
|
|
|
|
|
+ Console.WriteLine("App ID:" + appid);
|
|
|
//95df4f3c-8179-4576-91d6-2dd20bba5942
|
|
//95df4f3c-8179-4576-91d6-2dd20bba5942
|
|
|
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
|
|
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
store.Open(OpenFlags.ReadWrite);
|
|
store.Open(OpenFlags.ReadWrite);
|
|
|
store.Add(cert);
|
|
store.Add(cert);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
store.Close();
|
|
store.Close();
|
|
|
delnetsh(ipport);
|
|
delnetsh(ipport);
|
|
|
addnetsh(ipport, hashstr, appid);
|
|
addnetsh(ipport, hashstr, appid);
|
|
@@ -80,12 +81,12 @@ namespace Fine.HttpServer
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void addnetsh(string ipport,string certhash,string appid)
|
|
|
|
|
|
|
+ private void addnetsh(string ipport, string certhash, string appid)
|
|
|
{
|
|
{
|
|
|
//netsh http del sslcert ipport=0.0.0.0:9003 certhash=E2D1C410BDFBFBD7E85368C2419F1B06078D4F7C appid={95df4f3c-8179-4576-91d6-2dd20bba5942}
|
|
//netsh http del sslcert ipport=0.0.0.0:9003 certhash=E2D1C410BDFBFBD7E85368C2419F1B06078D4F7C appid={95df4f3c-8179-4576-91d6-2dd20bba5942}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
ExcuteCMD("netsh", $"http add sslcert ipport={ipport} certhash={certhash} appid={{{appid}}}");
|
|
ExcuteCMD("netsh", $"http add sslcert ipport={ipport} certhash={certhash} appid={{{appid}}}");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void ExcuteCMD(string cmd, string arg)
|
|
private void ExcuteCMD(string cmd, string arg)
|
|
@@ -94,7 +95,7 @@ namespace Fine.HttpServer
|
|
|
ProcessStartInfo psi = new ProcessStartInfo();
|
|
ProcessStartInfo psi = new ProcessStartInfo();
|
|
|
psi.FileName = cmd;
|
|
psi.FileName = cmd;
|
|
|
psi.Arguments = arg;
|
|
psi.Arguments = arg;
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
Process proc = Process.Start(psi);
|
|
Process proc = Process.Start(psi);
|
|
|
proc.StartInfo.RedirectStandardOutput = true;
|
|
proc.StartInfo.RedirectStandardOutput = true;
|
|
|
proc.StartInfo.UseShellExecute = false;
|
|
proc.StartInfo.UseShellExecute = false;
|
|
@@ -120,41 +121,51 @@ namespace Fine.HttpServer
|
|
|
private void Result(IAsyncResult ar)
|
|
private void Result(IAsyncResult ar)
|
|
|
{
|
|
{
|
|
|
//var httpcontext = listener.GetContext();
|
|
//var httpcontext = listener.GetContext();
|
|
|
-
|
|
|
|
|
DateTime stime = DateTime.Now;
|
|
DateTime stime = DateTime.Now;
|
|
|
- if (listener.IsListening)
|
|
|
|
|
|
|
+ try
|
|
|
{
|
|
{
|
|
|
- listener.BeginGetContext(Result, null);
|
|
|
|
|
- var httpcontext = listener.EndGetContext(ar);
|
|
|
|
|
|
|
|
|
|
- httpcontext.Response.Headers.Add("Content-type", "text/html;charset=UTF-8");
|
|
|
|
|
- httpcontext.Response.ContentEncoding = Encoding.UTF8;
|
|
|
|
|
|
|
|
|
|
- httpcontext.Response.AppendHeader("Access-Control-Allow-Origin", "*");//后台跨域请求,通常设置为配置文件
|
|
|
|
|
- httpcontext.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
|
|
|
|
- httpcontext.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); //后台跨域请求
|
|
|
|
|
- //httpcontext.Response.AppendHeader("Server", "MyIIS");//后台跨域请求,通常设置为配置文件
|
|
|
|
|
- httpcontext.Response.AppendHeader("Authorize", ConfigurationManager.AppSettings["auth"]);
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
- Fine.IHttpServer.HttpResult result = null;
|
|
|
|
|
- try
|
|
|
|
|
|
|
+ if (listener.IsListening)
|
|
|
{
|
|
{
|
|
|
- result = ProcessRequest(httpcontext);
|
|
|
|
|
- }
|
|
|
|
|
- catch (Exception ee)
|
|
|
|
|
- {
|
|
|
|
|
- result = Fine.IHttpServer.HttpResult.CreateError(ee);
|
|
|
|
|
- }
|
|
|
|
|
- string json = JsonUtil.SerializeObject(result);
|
|
|
|
|
|
|
+ listener.BeginGetContext(Result, null);
|
|
|
|
|
+ var httpcontext = listener.EndGetContext(ar);
|
|
|
|
|
+
|
|
|
|
|
+ httpcontext.Response.Headers.Add("Content-type", "text/html;charset=UTF-8");
|
|
|
|
|
+ httpcontext.Response.ContentEncoding = Encoding.UTF8;
|
|
|
|
|
|
|
|
- byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
|
|
|
|
|
- httpcontext.Response.OutputStream.Write(data, 0, data.Length);
|
|
|
|
|
- httpcontext.Response.OutputStream.Flush();
|
|
|
|
|
- httpcontext.Response.OutputStream.Close();
|
|
|
|
|
- Console.WriteLine($"process request cost {(DateTime.Now - stime).TotalMilliseconds}");
|
|
|
|
|
|
|
+ httpcontext.Response.AppendHeader("Access-Control-Allow-Origin", "*");//后台跨域请求,通常设置为配置文件
|
|
|
|
|
+ httpcontext.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
|
|
|
|
|
+ httpcontext.Response.AppendHeader("Access-Control-Allow-Credentials", "true"); //后台跨域请求
|
|
|
|
|
+ //httpcontext.Response.AppendHeader("Server", "MyIIS");//后台跨域请求,通常设置为配置文件
|
|
|
|
|
+ //httpcontext.Response.AppendHeader("Authorize", ConfigurationManager.AppSettings["auth"]);
|
|
|
|
|
+
|
|
|
|
|
+ Fine.IHttpServer.HttpResult result = null;
|
|
|
|
|
+ try
|
|
|
|
|
+ {
|
|
|
|
|
+ result = ProcessRequest(httpcontext);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (Exception ee)
|
|
|
|
|
+ {
|
|
|
|
|
+ result = Fine.IHttpServer.HttpResult.CreateError(ee);
|
|
|
|
|
+ }
|
|
|
|
|
+ string json = JsonUtil.SerializeObject(result);
|
|
|
|
|
+
|
|
|
|
|
+ byte[] data = System.Text.Encoding.UTF8.GetBytes(json);
|
|
|
|
|
+ httpcontext.Response.OutputStream.Write(data, 0, data.Length);
|
|
|
|
|
+ httpcontext.Response.OutputStream.Flush();
|
|
|
|
|
+ httpcontext.Response.OutputStream.Close();
|
|
|
|
|
+ Console.WriteLine($"process request cost {(DateTime.Now - stime).TotalMilliseconds}");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ catch
|
|
|
|
|
+ {
|
|
|
|
|
+ LogUtil.WriteLog("网络连接异常,服务将重启" + stime);
|
|
|
|
|
+ this.Start();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private Fine.IHttpServer.HttpResult ProcessRequest(HttpListenerContext context)
|
|
private Fine.IHttpServer.HttpResult ProcessRequest(HttpListenerContext context)
|
|
@@ -173,7 +184,7 @@ namespace Fine.HttpServer
|
|
|
urlpars = url.Substring(url.IndexOf('?') + 1, url.Length - (url.IndexOf('?') + 1));
|
|
urlpars = url.Substring(url.IndexOf('?') + 1, url.Length - (url.IndexOf('?') + 1));
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
- if (Convert.ToBoolean(ConfigurationManager.AppSettings["isauth"])&&!string.IsNullOrEmpty(request.Headers["auth"]) && request.Headers["auth"].ToLower() != ConfigurationManager.AppSettings["auth"].ToLower())
|
|
|
|
|
|
|
+ if (Convert.ToBoolean(ConfigurationManager.AppSettings["isauth"]) && !string.IsNullOrEmpty(request.Headers["auth"]) && request.Headers["auth"].ToLower() != ConfigurationManager.AppSettings["auth"].ToLower())
|
|
|
{
|
|
{
|
|
|
rlt = new { code = 401, msg = "无权限" };
|
|
rlt = new { code = 401, msg = "无权限" };
|
|
|
return Fine.IHttpServer.HttpResult.CreateSucess(rlt);
|
|
return Fine.IHttpServer.HttpResult.CreateSucess(rlt);
|
|
@@ -193,22 +204,22 @@ namespace Fine.HttpServer
|
|
|
byte[] content = new byte[1024];
|
|
byte[] content = new byte[1024];
|
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
|
|
|
|
|
|
|
- int readlen = 0;
|
|
|
|
|
|
|
+ int readlen = 0;
|
|
|
do
|
|
do
|
|
|
{
|
|
{
|
|
|
readlen = request.InputStream.Read(content, 0, content.Length);
|
|
readlen = request.InputStream.Read(content, 0, content.Length);
|
|
|
-
|
|
|
|
|
- ms.Write(content,0,readlen);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ ms.Write(content, 0, readlen);
|
|
|
} while (readlen != 0);
|
|
} while (readlen != 0);
|
|
|
|
|
|
|
|
var requestdata = ms.ToArray();
|
|
var requestdata = ms.ToArray();
|
|
|
- ms.Close();
|
|
|
|
|
|
|
+ ms.Close();
|
|
|
par = System.Text.Encoding.UTF8.GetString(requestdata);
|
|
par = System.Text.Encoding.UTF8.GetString(requestdata);
|
|
|
}
|
|
}
|
|
|
rlt = handler.Post(action, par);
|
|
rlt = handler.Post(action, par);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return Fine.IHttpServer.HttpResult.CreateSucess(rlt) ;
|
|
|
|
|
|
|
+ return Fine.IHttpServer.HttpResult.CreateSucess(rlt);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private string UrlParamsToJson(string urlpars)
|
|
private string UrlParamsToJson(string urlpars)
|
|
@@ -246,7 +257,8 @@ namespace Fine.HttpServer
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
- public void Close() {
|
|
|
|
|
|
|
+ public void Close()
|
|
|
|
|
+ {
|
|
|
if (Status)
|
|
if (Status)
|
|
|
{
|
|
{
|
|
|
listener.Stop();
|
|
listener.Stop();
|
|
@@ -254,7 +266,7 @@ namespace Fine.HttpServer
|
|
|
Status = false;
|
|
Status = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|