private static string _GetIPFLocation(stCoCServerConfig.CoCServerConfigData.Configuration conf, stNet.WebHandleTypes type) { int idx = conf.Opt.IPFType.FindIndex(o => o.value.Equals(type.ToString())); if (idx != -1) { if (conf.Opt.IPFLocationEnable[idx].bval) { return(conf.Opt.IPFLocation[idx].value); } } return(String.Empty); }
public static byte[] GetJsonSetup(stCoCServerConfig.CoCServerConfigData.Configuration conf) { try { if (CoCClientSetup._setupJsonByte == null) { string ver = stApp.AppInformation.GetAppVersion(); DataTable dt = SqliteConvertExtension.MapToDataTable <ServerSetup>(); DataRow dr = dt.NewRow(); dr["IRCServer"] = conf.Opt.IRCServer.value; dr["IRCPort"] = (System.Int64)conf.Opt.IRCPort.num; dr["IRCChannel"] = conf.Opt.IRCChannel.value; dr["IRCLanguage"] = conf.Opt.IRCLanguage.value; dr["NotifyUpdateTime"] = (System.Int64)conf.Opt.SQLDBUpdateTime.num; dr["URLClan"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.JsonWebRequest); dr["URLNotify"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.ServerSentEventWebRequest); dr["URLInformer"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.InformerWebRequest); dr["URLIrcLog"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.TemplateWebRequest); dr["URLWiki"] = CoCClientSetup._GetIPFLocation(conf, stNet.WebHandleTypes.WikiWebRequest); dr["ServerVersion"] = ver; dr["ServerMagic"] = ver.ToMD5(); dt.Rows.Add(dr); if ((CoCClientSetup._setupJsonByte = Encoding.UTF8.GetBytes( dt.ToJson(false, true, (conf.Opt.SQLDBUpdateTime.num * 60)) )) == null) { throw new ArgumentNullException(); } } return(CoCClientSetup._setupJsonByte); } catch (Exception e) { conf.ILog.LogError(e.Message); return(default(byte[])); } }
public static void SaveJsonSetup(stCoCServerConfig.CoCServerConfigData.Configuration conf) { try { CoCClientSetup._setupJsonByte = CoCClientSetup.GetJsonSetup(conf); if (CoCClientSetup._setupJsonByte == null) { throw new ArgumentNullException(); } File.WriteAllBytes( Path.Combine( conf.Opt.SYSROOTPath.value, conf.Opt.IPFLocation[0].value, Properties.Settings.Default.setJsonSetupFile ), CoCClientSetup._setupJsonByte ); } catch (Exception e) { throw e; } }
private static void _ErrorHtmlDefault( stCoCServerConfig.CoCServerConfigData.Configuration conf, HttpStatusCode code, string reason, HttpListenerContext context ) { conf.HttpSrv.BadRequestRaw( Encoding.UTF8.GetBytes( string.Format( Properties.Resources.httpHtmlError, conf.HttpSrv.wUserAgent, (int)code, code.ToString(), ((string.IsNullOrWhiteSpace(reason)) ? "" : reason + @".<br/>"), DateTime.Now.ToString(), @"/assets/html/ClanInfo.html" ) ), context, (int)code, HttpUtil.MimeType.MimeHtml ); }
public static void MainStart(string[] args) { const string className = "[Main]: "; IMessage iLog = new IMessage() { LogInfo = CoCServerMain.PrnInfo, LogError = CoCServerMain.PrnError, LogNetSyslog = CoCServerMain.PrnNetLog, ProgressBar = stConsole.ProgressTxt }; Func <uint, List <int>, int, bool, bool> geoCheckFun = null; Func <string, int> geoCountryFun = null; /// Thread.CurrentThread.Name = stCore.IOBaseAssembly.BaseName(Assembly.GetExecutingAssembly()); CoCServerMain.PrnInfo( string.Format( Properties.Resources.PrnRun, stApp.AppInformation.GetAppVersion(), DateTime.Now ) ); /// try { if ((CoCServerMain._Opt = stCoCServerConfig.CoCServerConfiguration.BuildConfig.InitOption(args, Assembly.GetExecutingAssembly(), iLog)) == null) { throw new ArgumentNullException( string.Format( Properties.Resources.fmtConfErrorException, "CoCServerMain.Opt" ) ); } if (string.IsNullOrWhiteSpace(CoCServerMain.Opt.CLANTag.value)) { throw new ArgumentNullException( string.Format( Properties.Resources.fmtConfMissException, "CLAN Tag" ) ); } if (string.IsNullOrWhiteSpace(CoCServerMain.Opt.CLANAPIKey.value)) { throw new ArgumentNullException( string.Format( Properties.Resources.fmtConfMissException, "CLAN API Key" ) ); } if ((CoCServerMain._Conf = stCoCServerConfig.CoCServerConfiguration.BuildConfig.InitConfiguration(CoCServerMain.Opt, iLog)) == null) { throw new ArgumentNullException( string.Format( Properties.Resources.fmtConfErrorException, "CoCServerMain.Conf" ) ); } if (!string.IsNullOrWhiteSpace(CoCServerMain.Conf.Opt.SYSLANGConsole.value)) { try { CultureInfo ci = null; if ((ci = CultureInfo.GetCultureInfo(CoCServerMain.Conf.Opt.SYSLANGConsole.value)) != null) { System.Threading.Thread.CurrentThread.CurrentCulture = ci; System.Threading.Thread.CurrentThread.CurrentUICulture = ci; } } catch (Exception e) { CoCServerMain.PrnError( string.Format( Properties.Resources.prnLangCultureError, CoCServerMain.Conf.Opt.SYSLANGConsole.value, e.Message ) ); } } CoCServerMain.PrnInfo( string.Format( Properties.Resources.prnLangCulture, CultureInfo.CurrentCulture.DisplayName ) ); if (CoCServerMain.Conf.Opt.LOGRemoteServerEnable.bval) { try { CoCServerMain.Conf.SysLog = new stNet.Syslog.stSysLogNG( new stNet.Syslog.SysLogSenderUdp( new IPEndPoint( IPAddress.Parse(CoCServerMain.Conf.Opt.LOGRemoteServerAddress.value), CoCServerMain.Conf.Opt.LOGRemoteServerPort.num ) ) ); CoCServerMain.Conf.SysLog.isUTF8Bom = true; CoCServerMain.PrnInfo(Properties.Resources.serviceSysLogStarted); } catch (Exception e) { CoCServerMain.Conf.Opt.LOGRemoteServerEnable.bval = false; CoCServerMain.PrnError( string.Format( Properties.Resources.serviceSysLogStartFailed, e.Message ) ); } } else { CoCServerMain.PrnInfo(Properties.Resources.SysLogDisabled); } /// IP Filter bool isFilterEnable = BuildConfig.isFilterAll(ref CoCServerMain.Conf.Opt); if (isFilterEnable) { try { CoCServerMain.Conf.Geo = new GeoFilter(iLog, true); CoCServerMain.Conf.Geo.InitBase( CoCServerMain.Conf.Opt.SYSGEOPath.value, false, stConsole.GetCursorAlign(2) ); geoCheckFun = CoCServerMain.Conf.Geo.InGeoRange; geoCountryFun = stGeo.MaxMindUtil.GetCountryId; CoCServerMain.PrnInfo(Properties.Resources.serviceGeoStarted); } catch (Exception e) { if (CoCServerMain.Conf.Geo != null) { CoCServerMain.Conf.Geo.Dispose(); } CoCServerMain.Conf.Geo = null; CoCServerMain.PrnError( string.Format( Properties.Resources.fmtMainError, className, e.GetType().Name, e.Message ) ); } } else { CoCServerMain.PrnInfo(Properties.Resources.GeoFilterDisabled); } /// CoC API CoCServerMain.Conf.Api = new stCoCAPI.CoCAPI( CoCServerMain.Conf.Opt.SQLDBPath.value, CoCServerMain.Conf.Opt.SQLDBUri.value, CoCServerMain.Conf.Opt.CLANAPIKey.value, CoCServerMain.Conf.Opt.CLANTag.value, CoCServerMain.Conf.Opt.SYSROOTPath.value, iLog ); CoCServerMain.Conf.Api.FilterMemberTag = CoCServerMain.Conf.Opt.SQLDBFilterMemberTag.collection; CoCServerMain.Conf.Api.DefaultLang = CoCServerMain.Conf.Opt.WEBLANGDefault.value; CoCServerMain.Conf.Api.InformerStaticEnable = CoCServerMain.Conf.Opt.CLANInformerStaticEnable.bval; CoCServerMain.Conf.Api.AssetsPath = CoCServerMain.Conf.Opt.IPFLocation[0].value; /// Integrate DokuWiki API Auth method if (CoCServerMain.Conf.Opt.DOKUWikiAuthEnable.bval) { CoCServerMain.Conf.Api.DokuWikiAuthInit( Opt.DOKUWikiRootPath.value, Opt.DOKUWikiDefaultGroup.value ); CoCServerMain.PrnInfo(Properties.Resources.serviceWikiAuthStarted); } /// CoC Api start CoCServerMain.Conf.Api.Start(CoCServerMain.Conf.Opt.SQLDBUpdateTime.num); CoCServerMain.PrnInfo(Properties.Resources.serviceCoCApiStarted); /// Json Client setup stCoCServer.CoCAPI.CoCClientSetup.SaveJsonSetup(CoCServerMain.Conf); /// Web server if ( (!string.IsNullOrWhiteSpace(CoCServerMain.Conf.Opt.WEBRootUri.value)) && ((int)CoCServerMain.Conf.Opt.WEBRootPort.num > 0) ) { /// Web server Template CoCServerMain.Conf.HtmlTemplate = new stNet.stWebServerUtil.HtmlTemplate( Path.Combine( CoCServerMain.Conf.Opt.SYSROOTPath.value, CoCServerMain.Conf.Opt.SYSTMPLPath.value ) ); CoCServerMain.Conf.HtmlTemplate.InsertFileNotFound = Properties.Resources.httpLogNotFound; CoCServerMain.PrnInfo(Properties.Resources.serviceTemplateStarted); /// Web server Wiki Engine if ( (CoCServerMain.Conf.Opt.IPFLocationEnable.Count > 6) && (CoCServerMain.Conf.Opt.IPFLocationEnable[6].bval) ) { try { CoCServerMain.Conf.WikiEngine = new stDokuWiki.WikiEngine.WikiFile( ((string.IsNullOrWhiteSpace(CoCServerMain.Conf.Opt.DOKUWikiRootPath.value)) ? Path.Combine( CoCServerMain.Conf.Opt.SYSROOTPath.value, stDokuWiki.WikiEngine.WikiFile.wikiLocalPath ) : CoCServerMain.Conf.Opt.DOKUWikiRootPath.value ) ); CoCServerMain.Conf.WikiEngine.OnProcessError += (o, e) => { CoCServerMain.PrnError( string.Format( Properties.Resources.fmtMainError, className, e.ex.GetType().Name, e.ex.Message ) ); }; // external DokuWiki auth disaled // CoCServerMain.Conf.Opt.DOKUWikiAuthEnable.bval = false; // TODO: normalize this CoCServerMain.PrnInfo(Properties.Resources.serviceWikiStarted); } catch (Exception e) { CoCServerMain.PrnError( string.Format( Properties.Resources.fmtMainError, className, e.GetType().Name, e.Message ) ); CoCServerMain.Conf.WikiEngine = null; CoCServerMain.Conf.Opt.IPFLocationEnable[6].bval = false; } } else { CoCServerMain.Conf.WikiEngine = null; } /// Web server Engine CoCServerMain.Conf.HttpSrv = new stWebServer( CoCServerMain.Conf.Opt.WEBRootUri.value, CoCServerMain.Conf.Opt.WEBRootPort.num, iLog ); CoCServerMain.Conf.HttpSrv.wUserData = Conf; CoCServerMain.Conf.HttpSrv.isConcat = true; CoCServerMain.Conf.HttpSrv.isMinify = true; CoCServerMain.Conf.HttpSrv.isFrontEnd = CoCServerMain.Conf.Opt.WEBFrontEndEnable.bval; CoCServerMain.Conf.HttpSrv.DefaultLang = CoCServerMain.Conf.Opt.WEBLANGDefault.value; CoCServerMain.Conf.HttpSrv.wBadRequestDebugOut = CoCServerMain.Conf.Opt.WEBRequestDebugEnable.bval; /// Web server Locations/IPFilter for (int i = 0; i < BuildConfig.numFilters; i++) { stIPFilter ipFilter = null; if (!CoCServerMain.Conf.Opt.IPFLocationEnable[i].bval) { continue; } if ( (isFilterEnable) && (BuildConfig.isFilter(ref CoCServerMain.Conf.Opt, i)) ) { ipFilter = stACL.IpFilterCreate( CoCServerMain.Conf.Opt.IPFIpList[i].collection, CoCServerMain.Conf.Opt.IPFGeoListASN[i].collection, CoCServerMain.Conf.Opt.IPFGeoListCountry[i].collection, CoCServerMain.Conf.Opt.IPFIsIpBlackList[i].bval, CoCServerMain.Conf.Opt.IPFIsGeoAsnBlackList[i].bval, CoCServerMain.Conf.Opt.IPFIsGeoCountryBlackList[i].bval, geoCheckFun, geoCountryFun ); } Conf.HttpSrv.AddHandler( CoCWebSrvHandleSettings.HttpHandleType(CoCServerMain.Conf.Opt.IPFType[i].value), CoCWebSrvHandleSettings.HttpHandleAction(CoCServerMain.Conf.Opt.IPFType[i].value), CoCServerMain.Conf.Opt.IPFLocation[i].value, ipFilter, ((CoCWebSrvHandleSettings.HttpHandleBool(CoCServerMain.Conf.Opt.IPFType[i].value)) ? CoCServerMain.Conf.Opt.SYSROOTPath.value : null) ); } #if DEBUG_HTTPResorceLocation Conf.HttpSrv.PrintResorceLocation(); #endif Conf.HttpSrv.Start(ref CoCServerMain.Conf.Opt.IsRun.bval); CoCServerMain.PrnInfo(Properties.Resources.serviceHttpStarted); } else { CoCServerMain.PrnInfo(Properties.Resources.httpServerDisabled); } /// IRC bot if ( (!string.IsNullOrWhiteSpace(CoCServerMain.Conf.Opt.IRCChannel.value)) && (!string.IsNullOrWhiteSpace(CoCServerMain.Conf.Opt.IRCServer.value)) && ((int)CoCServerMain.Conf.Opt.IRCPort.num > 0) ) { Conf.LogDump = new stCore.IOFile( CoCServerMain.Conf.Opt.IRCSOutDirName.value, CoCServerMain.Conf.Opt.IRCSOutFileName.value, CoCServerMain.Conf.Opt.SYSIRCLOGPath.value, CoCServerMain.PrnInfo ); CoCServerMain.PrnInfo(Properties.Resources.serviceIrcLogStarted); Conf.Irc = new IrcClient(CoCServerMain.Conf.Opt.IRCServer.value, (int)CoCServerMain.Conf.Opt.IRCPort.num); Conf.Irc.Nick = Conf.Opt.IRCNik.value; Conf.Irc.ConsoleOutput = false; Conf.Irc.KickRespawn = Conf.Opt.IRCKickRespawn.bval; Conf.Irc.ServerPass = ((!string.IsNullOrWhiteSpace(CoCServerMain.Conf.Opt.IRCPassword.value)) ? CoCServerMain.Conf.Opt.IRCPassword.value : ""); Conf.Irc.iLog = iLog; Conf.Irc.Connect(); Conf.IrcCmd = new IrcCommand(CoCServerMain.Conf); CoCServerMain.InitIrcCallBack(); CoCServerMain.PrnInfo(Properties.Resources.serviceIrcBotStarted); Conf.IrcCmd.PluginsPrint(); } else { CoCServerMain.PrnInfo(Properties.Resources.ircBootDisabled); } #region Cancel Key Press / SYSTEM CoCServerMain.cwtoken = new CancellationTokenSource(); Console.CancelKeyPress += delegate(object sender, ConsoleCancelEventArgs e) { e.Cancel = true; CoCServerMain.Conf.Opt.IsRun.bval = false; if ((CoCServerMain.cwtoken != null) && (!CoCServerMain.cwtoken.IsCancellationRequested)) { CoCServerMain.cwtoken.Cancel(); } }; #endregion } catch (Exception e) { CoCServerMain.PrnError( string.Format( Properties.Resources.fmtMainError, className, e.GetType().Name, e.Message ) ); #if DEBUG_ExtendedError stConsole.WriteHeader(e.ToString()); #endif if (CoCServerMain.CheckConf) { CoCServerMain.Conf.Dispose(); } #if DEBUG Console.ReadLine(); #endif return; } CoCServerMain.PrnInfo( string.Format( Properties.Resources.PrnJob, CoCServerMain.Conf.Opt.SYSAppName.value, DateTime.Now ) ); while (CoCServerMain.Conf.Opt.IsRun.bval) { try { CoCServerMain.cwtoken.Token.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { break; } catch (ObjectDisposedException) { break; } try { CoCServerMain.cwtoken.Token.WaitHandle.WaitOne(TimeSpan.FromSeconds(1)); } catch (ObjectDisposedException) { break; } } if (CoCServerMain.cwtoken != null) { CoCServerMain.cwtoken.Dispose(); } CoCServerMain.Conf.Dispose(); CoCServerMain.PrnInfo(Properties.Resources.PrnExit); }
public static void WikiWebRequest(string url, object ctx, object udata) { byte[] msg; stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } if (conf.HtmlTemplate == null) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, String.Empty, context ); return; } try { if ((msg = CoCWebSrv.WebWikiRouteTree(url, conf)) == null) { throw new ArgumentException("Create Wiki page error"); } } catch (Exception e) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, e.Message, context ); return; } try { if (context.Response.StatusCode == (int)HttpStatusCode.OK) { context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "max-age=2629000, public"); } else { context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "no-cache"); } context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimeHtml)); context.Response.ContentLength64 = msg.Length; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "Wiki"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); return; /* * /////// * //////// Go this! * ////// * string[] urlPart = url.Split('/'); * * if ( * (urlPart.Length < 5) || * ( * (!urlPart[2].All(char.IsDigit)) || * (!urlPart[3].All(char.IsDigit)) || * (!urlPart[4].All(char.IsDigit)) * ) * ) * { * DateTime cdate = DateTime.Now.AddDays(-1); * urlPart = new string[] { * "", * "", * cdate.ToString("yyyy"), * cdate.ToString("MM"), * cdate.ToString("dd") * }; * } * try * { * cacheid = @"irc" + urlPart[4] + urlPart[3] + urlPart[2]; * * if ( * (!conf.Opt.WEBCacheEnable.bval) || * (!stCore.stCache.GetCacheObject<byte[]>(cacheid, out msg)) * ) * { * filePath = conf.LogDump.GetFilePath(urlPart[4], urlPart[3], urlPart[2]); * * if (!File.Exists(filePath)) * { * string title = string.Format( * Properties.Resources.httpLogNotFound, * urlPart[2], * urlPart[3], * urlPart[4] * ); * msg = Encoding.UTF8.GetBytes( * conf.HtmlTemplate.Render( * new * { * LANG = conf.Opt.SYSLANGConsole.value.ToLower(), * TITLE = title, * INSERTHTMLDATA = title, * TOPMENU = string.Format( * Properties.Resources.ircLogArchive, * conf.Opt.IRCChannel.value * ), * DATE = DateTime.Now.ToString(), * GENERATOR = conf.HttpSrv.wUserAgent, * DATEY = urlPart[2], * DATEM = urlPart[3], * DATED = urlPart[4] * }, * "IrcLogTemplate.html", * filePath * ) * ); * context.Response.StatusCode = ((conf.Opt.WEBFrontEndEnable.bval) ? (int)HttpStatusCode.OK : (int)HttpStatusCode.NotFound); * } * else * { * msg = Encoding.UTF8.GetBytes( * conf.HtmlTemplate.Render( * new * { * LANG = conf.Opt.SYSLANGConsole.value.ToLower(), * TITLE = string.Format( * Properties.Resources.httpDateLogTitle, * urlPart[2], * urlPart[3], * urlPart[4] * ), * TOPMENU = string.Format( * Properties.Resources.ircLogArchive, * conf.Opt.IRCChannel.value * ), * DATE = DateTime.Now.ToString(), * GENERATOR = conf.HttpSrv.wUserAgent, * DATEY = urlPart[2], * DATEM = urlPart[3], * DATED = urlPart[4] * }, * "IrcLogTemplate.html", * filePath * ) * ); * if (conf.Opt.WEBCacheEnable.bval) * { * stCore.stCache.SetCacheObject<byte[]>(cacheid, msg, DateTime.Now.AddHours(6)); * } * context.Response.StatusCode = (int)HttpStatusCode.OK; * } * } * else * { * context.Response.StatusCode = (int)HttpStatusCode.OK; #if DEBUG_isCached * stConsole.WriteHeader("IRC LOG content is cached: " + cacheid + " : " + msg.Length.ToString()); #endif * } * } * catch (Exception e) * { * CoCWebSrv._ErrorHtmlDefault( * conf, * HttpStatusCode.InternalServerError, * e.Message, * context * ); * return; * } * try * { * if (context.Response.StatusCode == (int)HttpStatusCode.OK) * { * context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "max-age=2629000, public"); * } * else * { * context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "no-cache"); * } * context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimeHtml)); * context.Response.ContentLength64 = msg.Length; * context.Response.OutputStream.Write(msg, 0, msg.Length); * context.Response.OutputStream.Close(); * } #if DEBUG * catch (Exception e) * { * conf.ILog.LogError("[Template Web Request]: " + e.GetType().Name + " -> " + e.Message); #else * catch (Exception) * { #endif * context.Response.Abort(); * return; * } * context.Response.Close(); */ }
public static void InformerWebRequest(string url, object ctx, object udata) { stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq reqtype = stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.None; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, String.Empty, context ); return; } Int32 idx = 0, resize = 2; byte[] msg = null; string[] urlPart = url.Split('/'); urlPart = urlPart.Skip(1).Concat(urlPart.Take(1)).ToArray(); Array.Resize(ref urlPart, urlPart.Length - 1); string memberId = "", langid = stNet.stWebServerUtil.HttpUtil.GetHttpPreferedLanguage(context.Request.UserLanguages); try { if (urlPart.Length < 3) { throw new ArgumentOutOfRangeException(); } if (!Int32.TryParse(urlPart[2], out idx)) { throw new ArgumentOutOfRangeException(); } switch (urlPart[1]) { case "info": case "clan": { reqtype = stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Clan; if (urlPart.Length != 4) { throw new ArgumentOutOfRangeException(); } break; } case "player": case "member": { reqtype = stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Members; if (urlPart.Length != 5) { throw new ArgumentOutOfRangeException(); } if (urlPart[3].Equals("random")) { urlPart[1] = urlPart[3]; resize = 3; } else { if ((urlPart[3].Length > 12) || (urlPart[3].Length < 6)) { throw new ArgumentOutOfRangeException(); } urlPart[2] = urlPart[3]; } memberId = urlPart[3]; break; } default: { throw new ArgumentOutOfRangeException(); } } Array.Resize(ref urlPart, urlPart.Length - resize); } catch (Exception) { conf.HttpSrv.BadRequestRaw(conf.Api.InformerImageError(reqtype, langid), context, (int)HttpStatusCode.BadRequest, HttpUtil.MimeType.MimePng); return; } try { stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq cReq = stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None; string query = string.Empty; try { query = conf.Api.GetQueryString(urlPart, ref cReq, conf.Opt.SQLDBFilterMemberTag.collection, conf.ILog.LogError); if ( (cReq == stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None) || (string.IsNullOrWhiteSpace(query)) ) { throw new ArgumentNullException(); } } catch (Exception) { throw new ArgumentException(); } #if DEBUG_PrintWebRequest stConsole.WriteHeader("InformerWebRequest -> URL: (" + url + ") Query: (" + query + ")"); #endif DataTable dt = conf.Api.QueryData(query); if ((dt == null) || (dt.Rows.Count == 0)) { throw new ArgumentNullException(); } #if DEBUG_PrintJson stConsole.WriteHeader("InformerWebRequest -> JSON: " + dt.ToJson(false, false)); #endif #if DEBUG_PrintDataTable dt.DataTableToPrint(); #endif msg = conf.Api.InformerImageGet(dt.Rows[0], reqtype, idx, memberId, langid, conf.Opt.WEBCacheEnable.bval); #if DEBUG_PrintImageInfo stConsole.WriteHeader("InformerWebRequest -> ImageInfo: " + msg.Length); #endif } catch (Exception) { conf.HttpSrv.BadRequestRaw( conf.Api.InformerImageError(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.Auth, langid), context, (int)HttpStatusCode.InternalServerError, HttpUtil.MimeType.MimePng ); return; } try { #if DEBUG_NoCache context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "no-cache"); context.Response.AddHeader(conf.HttpSrv.httpAccelBuffering, "no"); #else context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "max-age=" + conf.Api.UpdateNextSeconds.ToString() + ", public"); context.Response.AddHeader(conf.HttpSrv.httpAccelBuffering, "yes"); context.Response.AddHeader(conf.HttpSrv.httpLastModified, conf.Api.UpdateLastTime.ToString("R")); #endif context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimePng)); context.Response.AddHeader(conf.HttpSrv.httpAccessControlAllowOrigin, "*"); context.Response.ContentLength64 = msg.Length; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "Informer"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); }
public static void SseWebRequest(string url, object ctx, object udata) { stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } string[] urlPart = url.Split('/'); if (urlPart.Length < 3) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.BadRequest, Properties.Resources.fmtNotifyUrlError, context ); return; } stCoCAPI.CoCAPI.CoCNotifyHost client = new stCoCAPI.CoCAPI.CoCNotifyHost() { Response = context.Response, IpAddress = ((IPAddress)stNet.stWebServerUtil.HttpUtil.GetHttpClientIP(context.Request)).ToString(), Language = stNet.stWebServerUtil.HttpUtil.GetHttpPreferedLanguage(context.Request.UserLanguages) }; switch (urlPart[2]) { case "sse": { if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { if (conf.Api.NotifySendSseStreamComplette( client, stCoCAPI.CoCAPI.CoCEnum.EventNotify.ServerError.ToString(), HttpStatusCode.InternalServerError.ToString() )) { context.Response.Close(); } else { context.Response.Abort(); } return; } if (!context.Request.KeepAlive) { if (conf.Api.NotifySendSseStreamComplette( client, stCoCAPI.CoCAPI.CoCEnum.EventNotify.ServerError.ToString(), stCoCAPI.CoCAPI.CoCEnum.EventNotify.NoKeepAlive.ToString() + " : " + DateTime.Now.ToString() )) { context.Response.Close(); } else { context.Response.Abort(); } return; } conf.Api.NotifySseAdd(client); break; } case "json": { if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { conf.HttpSrv.BadRequestJson(stCoCAPI.CoCAPI.CoCEnum.EventNotify.ServerError.ToString(), context, (int)HttpStatusCode.InternalServerError); return; } bool isfull = true; if ( (urlPart.Length > 3) && (urlPart[3].Equals("list")) ) { isfull = false; } if (conf.Api.NotifySendJsonComplette(client, isfull)) { context.Response.Close(); } else { context.Response.Abort(); } break; } case "xml": case "rss": { if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { conf.HttpSrv.BadRequestXml(stCoCAPI.CoCAPI.CoCEnum.EventNotify.ServerError.ToString(), context, (int)HttpStatusCode.InternalServerError); return; } if (conf.Api.NotifySendRssComplette(client)) { context.Response.Close(); } else { context.Response.Abort(); } break; } default: { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, Properties.Resources.fmtNotifyUrlError, context ); break; } } }
public static void JsonWebRequest(string url, object ctx, object udata) { byte[] msg; stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } if ( (conf.Api == null) || (!conf.Api.DBCheck()) ) { conf.HttpSrv.BadRequestJson(HttpStatusCode.InternalServerError.ToString(), context, (int)HttpStatusCode.InternalServerError); return; } string [] urlPart = url.Split('/'); urlPart = urlPart.Skip(1).Concat(urlPart.Take(1)).ToArray(); Array.Resize(ref urlPart, urlPart.Length - 1); if ((urlPart.Length < 3) || (urlPart.Length > 7)) { conf.HttpSrv.BadRequestJson(HttpStatusCode.BadRequest.ToString(), context, (int)HttpStatusCode.BadRequest); return; } try { stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq cReq = stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None; string query = string.Empty; query = conf.Api.GetQueryString(urlPart, ref cReq, conf.Opt.SQLDBFilterMemberTag.collection, conf.ILog.LogError); switch (cReq) { case stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.None: { throw new ArgumentNullException(); } case stCoCAPI.CoCAPI.CoCEnum.ClanTypeReq.ServerSetup: { msg = CoCClientSetup.GetJsonSetup(conf); break; } default: { if (string.IsNullOrWhiteSpace(query)) { throw new ArgumentNullException(); } try { #if DEBUG_PrintWebRequest stConsole.WriteHeader("JsonWebRequest -> URL: (" + url + ") Query: (" + query + ")"); #endif msg = Encoding.UTF8.GetBytes( conf.Api.QueryData(query).ToJson(true, true) ); #if DEBUG_PrintJson stConsole.WriteHeader("JsonWebRequest -> JSON: " + Encoding.UTF8.GetString(msg)); #endif } catch (Exception) { throw new ArgumentException(HttpStatusCode.BadRequest.ToString()); } break; } } } catch (Exception e) { conf.HttpSrv.BadRequestJson(e.Message, context, (int)HttpStatusCode.InternalServerError); return; } try { context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimeJson)); context.Response.ContentLength64 = msg.Length; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "Json"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); }
public static void FileWebRequest(string url, object ctx, object udata) { stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } byte[] msg = null; long msgsize = 0; string modify = String.Empty; string filePath = conf.Opt.SYSROOTPath.value; if (url.Contains("?")) { string [] urlPart = url.Split('?'); if ((urlPart.Length == 0) || (string.IsNullOrWhiteSpace(urlPart[0]))) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.BadRequest, String.Empty, context ); return; } url = urlPart[0]; } foreach (string part in url.Split('/')) { if (part.Equals("..")) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.BadRequest, String.Empty, context ); return; } filePath = Path.Combine(filePath, part); } if (!File.Exists(filePath)) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.NotFound, HttpStatusCode.NotFound.ToString() + @" path: '<b>" + url + @"</b>'", context ); return; } try { msg = File.ReadAllBytes(filePath); switch (context.Request.HttpMethod) { case "HEAD": { FileInfo fi = new FileInfo(filePath); modify = fi.LastWriteTimeUtc.ToLongDateString(); break; } case "GET": { msg = File.ReadAllBytes(filePath); msgsize = msg.Length; modify = File.GetLastWriteTimeUtc(filePath).ToLongDateString(); break; } default: { throw new ArgumentOutOfRangeException(Properties.Resources.httpMethodNotSupport); } } } catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, ""), e.GetType().Name, e.Message ) ); CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, e.Message, context ); return; } try { context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType(url)); context.Response.AddHeader(conf.HttpSrv.httpContentDisposition, string.Format( Properties.Settings.Default.setHttpContentDisposition, Path.GetFileName(filePath) ) ); context.Response.AddHeader(conf.HttpSrv.httpLastModified, modify); context.Response.ContentLength64 = msgsize; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "File"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); }
public static void TemplateWebRequest(string url, object ctx, object udata) { byte[] msg; string filePath, cacheid; stCoCServerConfig.CoCServerConfigData.Configuration conf = udata as stCoCServerConfig.CoCServerConfigData.Configuration; HttpListenerContext context = ctx as HttpListenerContext; if ( (udata == null) || (conf.HttpSrv == null) ) { context.Response.Abort(); return; } if ( (conf.HtmlTemplate == null) || (conf.LogDump == null) ) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, String.Empty, context ); return; } string [] urlPart = url.Split('/'); if ( (urlPart.Length < 5) || ( (!urlPart[2].All(char.IsDigit)) || (!urlPart[3].All(char.IsDigit)) || (!urlPart[4].All(char.IsDigit)) ) ) { DateTime cdate = DateTime.Now.AddDays(-1); urlPart = new string[] { "", "", cdate.ToString("yyyy"), cdate.ToString("MM"), cdate.ToString("dd") }; } try { cacheid = @"irc" + urlPart[4] + urlPart[3] + urlPart[2]; if ( (!conf.Opt.WEBCacheEnable.bval) || (!stCore.stCache.GetCacheObject <byte[]>(cacheid, out msg)) ) { filePath = conf.LogDump.GetFilePath(urlPart[4], urlPart[3], urlPart[2]); if (!File.Exists(filePath)) { string title = string.Format( Properties.Resources.httpLogNotFound, urlPart[2], urlPart[3], urlPart[4] ); msg = Encoding.UTF8.GetBytes( conf.HtmlTemplate.Render( new { LANG = conf.Opt.SYSLANGConsole.value.ToLower(), TITLE = title, INSERTHTMLDATA = title, TOPMENU = string.Format( Properties.Resources.ircLogArchive, conf.Opt.IRCChannel.value ), DATE = DateTime.Now.ToString(), GENERATOR = conf.HttpSrv.wUserAgent, DATEY = urlPart[2], DATEM = urlPart[3], DATED = urlPart[4] }, "IrcLogTemplate.html", filePath ) ); context.Response.StatusCode = ((conf.Opt.WEBFrontEndEnable.bval) ? (int)HttpStatusCode.OK : (int)HttpStatusCode.NotFound); } else { msg = Encoding.UTF8.GetBytes( conf.HtmlTemplate.Render( new { LANG = conf.Opt.SYSLANGConsole.value.ToLower(), TITLE = string.Format( Properties.Resources.httpDateLogTitle, urlPart[2], urlPart[3], urlPart[4] ), TOPMENU = string.Format( Properties.Resources.ircLogArchive, conf.Opt.IRCChannel.value ), DATE = DateTime.Now.ToString(), GENERATOR = conf.HttpSrv.wUserAgent, DATEY = urlPart[2], DATEM = urlPart[3], DATED = urlPart[4] }, "IrcLogTemplate.html", filePath ) ); if (conf.Opt.WEBCacheEnable.bval) { stCore.stCache.SetCacheObject <byte[]>(cacheid, msg, DateTime.Now.AddHours(6)); } context.Response.StatusCode = (int)HttpStatusCode.OK; } } else { context.Response.StatusCode = (int)HttpStatusCode.OK; #if DEBUG_isCached stConsole.WriteHeader("IRC LOG content is cached: " + cacheid + " : " + msg.Length.ToString()); #endif } } catch (Exception e) { CoCWebSrv._ErrorHtmlDefault( conf, HttpStatusCode.InternalServerError, e.Message, context ); return; } try { if (context.Response.StatusCode == (int)HttpStatusCode.OK) { context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "max-age=2629000, public"); } else { context.Response.AddHeader(conf.HttpSrv.httpCacheControl, "no-cache"); } context.Response.AddHeader(conf.HttpSrv.httpContentType, HttpUtil.GetMimeType("", HttpUtil.MimeType.MimeHtml)); context.Response.ContentLength64 = msg.Length; context.Response.OutputStream.Write(msg, 0, msg.Length); context.Response.OutputStream.Close(); } #if DEBUG catch (Exception e) { conf.ILog.LogError( string.Format( Properties.Resources.fmtMainError, string.Format(fmtClassName, "Template"), e.GetType().Name, e.Message ) ); #else catch (Exception) { #endif context.Response.Abort(); return; } context.Response.Close(); }