using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Threading; using Sog; using SimpleHttpServer; using LitJson; using ProtoCSStruct; using System.Net.Http; using Sog.IO; using System.Text; using System.Collections; using System.Reflection; using Org.BouncyCastle.Ocsp; using static Sog.IO.GmCommandMgr; namespace Operation { public class HttpApiInfo { public string httpApiCmd; //http api 接口命令 public Func httpApiFunction;//http api 接口函数 public Func httpApiDbReqFunction;//http api db req函数 public ExtData extData; } public class ExtData { public string token; public string method; } //http api 指令注册和管理 public class HttpApiMgr : Singleton { private Dictionary m_allHttpApi; public HttpApiMgr() { m_allHttpApi = new Dictionary(); } public void Register(string httpApiCmd, Func httpApiFunction, Func httpApiDbReqFunction) { if (httpApiCmd == null) { TraceLog.Error("HttpApiMgr.Register httpApiCmd {0} invalid gmFun!", httpApiCmd); return; } string lowcaseCmd = httpApiCmd.ToLower(); if (m_allHttpApi.ContainsKey(lowcaseCmd)) { TraceLog.Error("HttpApiMgr.Register httpApiCmd {0} lowcaseCmd {1} already registed!", httpApiCmd, lowcaseCmd); return; } TraceLog.Debug("HttpApiMgr.Register httpApiCmd {0} lowcaseCmd {1} success", httpApiCmd, lowcaseCmd); HttpApiInfo info = new HttpApiInfo(); info.httpApiCmd = httpApiCmd; info.httpApiFunction = httpApiFunction; info.httpApiDbReqFunction = httpApiDbReqFunction; m_allHttpApi.Add(lowcaseCmd, info); Resolver.Register(httpApiCmd,httpApiFunction); } public void ClearAll() { TraceLog.Debug("HttpApiMgr.ClearAll clear all registed httpApiCmd"); m_allHttpApi.Clear(); } public int HandlerHttpApiFunction(string httpApiCmd, JsonData jsondata, HttpResponse rsp, HttpRequest request, HttpQueryParams query, uint httpContextId) { string lowcaseCmd = httpApiCmd.ToLower(); if (!m_allHttpApi.ContainsKey(lowcaseCmd)) { TraceLog.Error("HttpApiMgr.HandlerHttpApiFunction httpApiCmd {0} lowcaseCmd {1} not be registed,please check", httpApiCmd, lowcaseCmd); return -1; } HttpApiInfo info = m_allHttpApi[lowcaseCmd]; int ret = 0; if (info.httpApiFunction != null) ret = info.httpApiFunction(lowcaseCmd, jsondata, rsp, request, query, httpContextId); TraceLog.Debug("HttpApiMgr.HandlerHttpApiFunction httpApiCmd {0} lowcaseCmd {1} ret {2}", httpApiCmd, lowcaseCmd, ret); if (info.extData == null) { info.extData = new ExtData(); } info.extData.method = request.Method; info.extData.token = query.GetValue("token"); HandlerHttpAlterLog(httpApiCmd, request.Method,query); return ret; } public int HandlerHttpApiDbReqFunction(string httpApiCmd, SSHttpApiDbReq req, DBOperator dbOperator) { string lowcaseCmd = httpApiCmd.ToLower(); if (!m_allHttpApi.ContainsKey(lowcaseCmd)) { TraceLog.Error("HttpApiMgr.HandlerHttpApiDbReqFunction httpApiCmd {0} lowcaseCmd {1} not be registed,please check", httpApiCmd, lowcaseCmd); return -1; } HttpApiInfo info = m_allHttpApi[lowcaseCmd]; int ret = 0; if (info.httpApiDbReqFunction != null) ret = info.httpApiDbReqFunction(lowcaseCmd, req, dbOperator); TraceLog.Debug("HttpApiMgr.HandlerHttpApiDbReqFunction httpApiCmd {0} lowcaseCmd {1} ret {2}", httpApiCmd, lowcaseCmd, ret); return ret; } private int HandlerHttpAlterLog(string httpApiCmd,string method, HttpQueryParams query) { string lowcaseCmd = httpApiCmd.ToLower(); try { if (Resolver.IsWritLog(httpApiCmd)) { SSHttpApiDbReq reqBak = new SSHttpApiDbReq(); string account = OperationServerUtils.GetAccount(query.GetValue("token")); if (query.Count == 0) { return 0; } if (httpApiCmd == "server_monitor" && !query.m_map.ContainsKey("GmUid")) { return 0; } foreach (var keyValuePair in query.m_map) { if (reqBak.Data.Count > reqBak.Data.GetMaxCount()) { break; } //不要把密码打到日志中 if (keyValuePair.Key == "password") { reqBak.Data.Add("*****"); continue; } reqBak.Data.Add(keyValuePair.Value); } byte[] gameDataByte = StructMessageParseUtils.ToByteArray(ref reqBak); gameDataByte = ZipUtils.CompressBytes(gameDataByte); OperationServerUtils.GetDbOp().InsertOperationLog(account,method, lowcaseCmd, gameDataByte); } } catch (System.Exception e) { TraceLog.Debug( "HttpApiMgr.HandlerHttpApiDbReqFunction push operation log error {0} lowcaseCmd {1} ret {2},err={3}", httpApiCmd, lowcaseCmd, 0, e.Message); } return 0; } } }