private async Task ProcessRequest(HttpListenerContext context) { if (context.Request.HttpMethod.ToUpperInvariant() != "POST") { await WriteNotFound(context); return; } //var urlPath = context.Request.Url.AbsolutePath.Substring(this.prefixPath.Length) // .ToLowerInvariant(); var url = context.Request.Url; var urlPath = url.PathAndQuery; if (urlPath.StartsWith("/api/")) { //限制请求体大小100k以内 if (context.Request.ContentLength64 < 100 * 1000) { var q = url.Query; var param = ParseQueryString(q); //byte[] byts = new byte[context.Request.InputStream.Length]; //await context.Request.InputStream.ReadAsync(byts, 0, byts.Length); //string req = System.Text.Encoding.UTF8.GetString(byts); //Console.WriteLine(urlPath); StringRequest sq = new StringRequest(); sq.token = param.Get("token"); sq.content = param.Get("content"); sq.stamp = param.Get("stamp"); TaskCompletionSource <string> tcs = new TaskCompletionSource <string>(); await srv.Execute(async() => { try { string rsp = await srv.OnHandleHttpRequest(sq); tcs.SetResult(rsp); } catch (Exception e) { tcs.SetException(e); } }); if (tcs.Task.IsFaulted) { throw tcs.Task.Exception; } await WriteString(context, tcs.Task.Result, "application/json"); return; } } await WriteNotFound(context); }
public async virtual Task <string> OnHandleHttpRequest(StringRequest msg) { log.Info("[Admin] " + msg.token); if (!VerifyStamp(msg.stamp)) { throw new Exception("VerifyStamp failed"); } if (!VerifyContent(msg, out string json)) { throw new Exception("VerifyContent failed"); } //TODO在这里分发处理不同的需求 var jsonObj = JsonConvert.DeserializeObject(json) as JObject; switch (jsonObj["cmd"].ToString()) { case CmdType.ServerMailType: return(await OnHandleServerMail(jsonObj)); case CmdType.ServerAnnouncement: return(await OnHandleServerAnnouncement(jsonObj)); case CmdType.ServerRoleBlacklist: return(await OnHandleServerBlacklist(jsonObj)); case CmdType.ServerRoleBan: return(await OnHandleServerBan(jsonObj)); case CmdType.ServerRoleToUUID: return(await OnHandleServerRoleNameToUUID(jsonObj)); case CmdType.ServerRolePrivilege: return(await OnHandleServerRolePrivilege(jsonObj)); case CmdType.ServerItemBase: return(OnHandleServerItemBase(jsonObj)); case CmdType.ServerRoleBagQuery: return(await OnHandleServerRoleBagQuery(jsonObj)); case CmdType.ServerRoleBagModify: return(await OnHandleServerRoleBagModify(jsonObj)); case CmdType.ServerAccountQuery: return(await OnHandleServerAccountQuery(jsonObj)); default: break; } return(ResponseResult(false)); }
private bool VerifyContent(StringRequest sr, out string content) { sr.content = sr.content.Replace(" ", "%2B"); var bytes = Convert.FromBase64String(Uri.UnescapeDataString(sr.content)); content = System.Text.Encoding.UTF8.GetString(bytes); //log.Info(json + " " + api_key + " " + sr.stamp); string s = api_key + content + sr.stamp; var my = SHA1HashStringForUTF8String(s); //log.Info(my + " " + sr.token); return(my == sr.token); }