public static bool GetUrlToFile(string url, string file, string path, stCore.IMessage iLog) { if ( (string.IsNullOrWhiteSpace(url)) || (string.IsNullOrWhiteSpace(file)) || (string.IsNullOrWhiteSpace(path)) ) { throw new ArgumentNullException(); } stNet.stWebClient wcl = null; string geoPath = Path.Combine( path, file ); GeoUpdateMain.downloadComplete = false; var cursor = stConsole.WriteGetPosition( string.Format( Properties.Resources.GeoFileDownload, " ", file ) ); try { wcl = new stWebClient(); wcl.wUserAgent = stNet.stWebServerUtil.HttpUtil.GetHttpUA(@"stGeoUpdate Client"); wcl.iLog = iLog; wcl.DownloadProgressChanged += (s, e) => { stConsole.WriteToPosition(" " + e.ProgressPercentage + "% ", cursor); }; wcl.DownloadFileCompleted += (s, e) => { GeoUpdateMain.downloadComplete = true; }; wcl.DownloadFileAsync(new Uri(url + file), geoPath, null); while (!GeoUpdateMain.downloadComplete) { Thread.Sleep(1000); } stConsole.NewLine(); return(true); } catch (Exception e) { GeoUpdateMain.PrnError(e.Message); if (!File.Exists(geoPath)) { File.Delete(geoPath); } return(false); } finally { if (wcl != null) { wcl.Dispose(); } } }
private static List <stGeoRange> _getGeoData(string path, int skipline, Action <int, int, int, string, bool> pb, bool isGzip, int top, bool isLoadDb, stCore.IMessage ilog) { int cnt = 1; string pathSource1 = String.Empty, pathSource2 = String.Empty; List <stGeoRange> DataASN = null, DataCountry = null; if (pb != null) { MaxMindUtil._iMsg.ProgressBar = pb; } pathSource1 = Path.Combine(path, MaxMindUtil.GetGeoDataFileName); if (File.Exists(pathSource1)) { if (!isLoadDb) { return(new List <stGeoRange>()); } try { if (ilog != null) { ilog.LogInfo( string.Format( Properties.Resources.GeoBaseLoad, pathSource1 ) ); } DataASN = pathSource1.stDeserialize <stGeoRange>(isGzip); return(DataASN); } catch (Exception e) { if (DataASN != null) { DataASN.Clear(); } throw new ArgumentException(e.Message); } } pathSource1 = Path.Combine(path, MaxMindUtil.GetASNFileName); if (!File.Exists(pathSource1)) { if (!MaxMindUtil.unzipMaxmindData( path, MaxMindUtil.GetASNZipFileName, MaxMindUtil.GetASNFileName )) { throw new FileNotFoundException( string.Format( Properties.Resources.GeoMaxMindFileNotFound, MaxMindUtil.GetASNFileName ) ); } } pathSource2 = Path.Combine(path, MaxMindUtil.GetCountryFileName); if (!File.Exists(pathSource2)) { if (!MaxMindUtil.unzipMaxmindData( path, MaxMindUtil.GetCountryZipFileName, MaxMindUtil.GetCountryFileName )) { throw new FileNotFoundException( string.Format( Properties.Resources.GeoMaxMindFileNotFound, MaxMindUtil.GetCountryFileName ) ); } } Task <List <stGeoRange> > t1 = Task <List <stGeoRange> > .Factory.StartNew(() => { return(MaxMindUtil._formatToList( pathSource1, Properties.Settings.Default.stMaxMindASNPrefix, skipline, top, MaxMindUtil._ConvertASNGeoData )); }); Task <List <stGeoRange> > t2 = Task <List <stGeoRange> > .Factory.StartNew(() => { return(MaxMindUtil._formatToList( pathSource2, "", skipline, (top + 1), MaxMindUtil._ConvertCountryGeoData )); }); try { t2.Wait(); DataCountry = t2.Result; if ((t2.IsFaulted) || (t2.IsCanceled) || (DataCountry == null) || (DataCountry.Count == 0)) { t2.Dispose(); throw new ArgumentException( string.Format( Properties.Resources.GeoMaxMindListEmpty, "Country" ) ); } t2.Dispose(); } catch (Exception e) { throw new ArgumentException("DATA Country: " + e.Message); } try { t1.Wait(); DataASN = t1.Result; if ((t1.IsFaulted) || (t1.IsCanceled) || (DataASN == null) || (DataASN.Count == 0)) { t1.Dispose(); throw new ArgumentException( string.Format( Properties.Resources.GeoMaxMindListEmpty, "ASN" ) ); } t1.Dispose(); } catch (Exception e) { throw new ArgumentException("DATA ASN: " + e.Message); } MaxMindUtil._iMsg.ProgressBar((top + 1), 0, 0, null, true); DataASN.Where(a => ((a.ipStart > 0) && (a.ipEnd > 0))).Select(a => { stGeoRange country = DataCountry.Where(c => ( ((c.ipStart >= a.ipStart) && (c.ipEnd <= a.ipEnd)) || ((a.ipStart >= c.ipStart) && (a.ipEnd <= c.ipEnd)) ) ).FirstOrDefault(); MaxMindUtil._iMsg.ProgressBar(-1, cnt++, DataASN.Count, Properties.Resources.GeoPbMerge, false); a.ipCountry = ((country == null) ? 0 : country.ipCountry); return(a); }).ToList(); MaxMindUtil._iMsg.ProgressBar(-1, 0, 0, null, true); try { DataASN.stSerialize( Path.Combine( path, MaxMindUtil.GetGeoDataFileName ), isGzip ); } catch (Exception e) { if (DataASN != null) { DataASN.Clear(); } throw new ArgumentException("DATA Serialize: " + e.Message); } finally { if (DataCountry != null) { DataCountry.Clear(); } } #if DEBUGEO foreach (var data in DataASN) { Console.WriteLine(data.ToString()); Console.Write(" ipStart: " + data.ipStart + "\r\n"); Console.Write(" ipEnd: " + data.ipEnd + "\r\n"); Console.Write(" ipASNumber: " + data.ipASNumber + "\r\n"); Console.Write(" ipCountry: " + data.ipCountry + "\r\n"); } #endif return(DataASN); }
static void Main(string[] args) { bool _isRemove = false, _isCreate = false; string _geoPath = String.Empty, _geoLock = String.Empty; stGeo.GeoFilter _geof = null; stCore.IniConfig.IniFile _iniFile = null; stCore.IMessage _iLog = null; GeoUpdateMain.rootAppPath = stCore.IOBaseAssembly.BaseDataDir(); Thread.CurrentThread.Name = stCore.IOBaseAssembly.BaseName(Assembly.GetExecutingAssembly()); args.Process( () => { }, new CommandLine.Switch("geodir", val => _geoPath = string.Join("", val), "-d"), new CommandLine.Switch("clear", val => _isRemove = true, "-c"), new CommandLine.Switch("create", val => _isCreate = true, "-a"), new CommandLine.Switch("quiet", val => GeoUpdateMain.isQuiet = true, "-q") ); if (GeoUpdateMain.isQuiet) { _iLog = new IMessage(); } else { _iLog = new IMessage() { LogInfo = GeoUpdateMain.PrnInfo, LogError = GeoUpdateMain.PrnError, ProgressBar = stConsole.ProgressTxt }; string AppDesc = IOBaseAssembly.BaseDescription(Assembly.GetExecutingAssembly()); stApp.AppInformation.PrnBanner( new string[] { string.Format( "{0}: {1}", Thread.CurrentThread.Name, ((string.IsNullOrWhiteSpace(AppDesc)) ? "" : AppDesc) ), Properties.Resources.banRun } ); } try { if (string.IsNullOrWhiteSpace(_geoPath)) { try { string iniPath = Path.Combine( GeoUpdateMain.rootAppPath, "stCoCServer.ini" ); if (!File.Exists(iniPath)) { throw new FileNotFoundException( string.Format( Properties.Resources.GeoIniNotFound, iniPath ) ); } _iniFile = new stCore.IniConfig.IniFile(iniPath); if (_iniFile == null) { throw new FileLoadException( string.Format( Properties.Resources.GeoInErrorLoad, iniPath ) ); } string lng = _iniFile.Section("SYS").Get("SYSLANGConsole"); if (!string.IsNullOrWhiteSpace(lng)) { try { CultureInfo ci = null; if ((ci = CultureInfo.GetCultureInfo(lng)) != null) { System.Threading.Thread.CurrentThread.CurrentCulture = ci; System.Threading.Thread.CurrentThread.CurrentUICulture = ci; } } catch (Exception) { } } _geoPath = _iniFile.Section("SYS").Get("SYSGEOPath"); if (_geoPath == null) { throw new ArgumentNullException(Properties.Resources.GeoConfigNotFound); } } catch (Exception e) { _geoPath = Path.Combine( GeoUpdateMain.rootAppPath, "geo" ); if (!Directory.Exists(_geoPath)) { _iLog.LogError(e.Message); return; } } finally { if (_iniFile != null) { _iniFile.Clear(); } } } if (!Directory.Exists(_geoPath)) { if (_isCreate) { Directory.CreateDirectory(_geoPath); } else { throw new DirectoryNotFoundException(Properties.Resources.GeoDirNotFound); } } string[] allFiles = GeoUpdateMain.MakeFileList(_geoPath); if (_isRemove) { foreach (string fn in allFiles) { if (File.Exists(fn)) { File.Delete(fn); } } } if ( (!File.Exists(allFiles[0])) && (!File.Exists(allFiles[1])) ) { if (!GeoUpdateMain.GetUrlToFile( stGeo.MaxMindUtil.MaxMindDownloadASNURL, stGeo.MaxMindUtil.GetASNZipFileName, _geoPath, _iLog) ) { throw new FileNotFoundException(Properties.Resources.GeoFileNotFound); } } if ( (!File.Exists(allFiles[2])) && (!File.Exists(allFiles[3])) ) { if (!GeoUpdateMain.GetUrlToFile( stGeo.MaxMindUtil.MaxMindDownloadCountryURL, stGeo.MaxMindUtil.GetCountryZipFileName, _geoPath, _iLog) ) { throw new FileNotFoundException(Properties.Resources.GeoFileNotFound); } } if (File.Exists(allFiles[4])) { File.Delete(allFiles[4]); } _geof = new GeoFilter(_iLog, false); if (_geof == null) { throw new ArgumentNullException(Properties.Resources.GeoClassInitError); } _geoLock = Path.Combine(_geoPath, MaxMindUtil.GetGeoDataLockFileName); File.Create(_geoLock); if (!_geof.InitBase(_geoPath, false, stConsole.GetCursorAlign(2))) { GeoUpdateMain.PrnError(Properties.Resources.GeoClassInitError); } else { GeoUpdateMain.PrnInfo( string.Format( Properties.Resources.GeoComplette, _geoPath ) ); } } catch (Exception e) { GeoUpdateMain.PrnError(e.Message); } finally { try { if (_geof != null) { _geof.Dispose(); } if ( (!string.IsNullOrWhiteSpace(_geoLock)) && (File.Exists(_geoLock)) ) { File.Delete(_geoLock); string geoDb = Path.Combine(_geoPath, MaxMindUtil.GetGeoDataFileName); if (File.Exists(geoDb)) { File.SetCreationTime(geoDb, DateTime.Now); } } } catch (Exception e) { GeoUpdateMain.PrnError(e.Message); } } #if DEBUG Console.ReadLine(); #endif return; }
public static List <stGeoRange> getGeoData(string path, bool isGzip = false, int top = -1, bool isLoadDb = true, stCore.IMessage ilog = null) { return(MaxMindUtil._getGeoData(path, 0, null, isGzip, top, isLoadDb, ilog)); }
public static List <stGeoRange> getGeoData(string path, int skipline, Action <int, int, int, string, bool> pb, bool isGzip = false, int top = -1, bool isLoadDb = true, stCore.IMessage ilog = null) { return(MaxMindUtil._getGeoData(path, skipline, pb, isGzip, top, isLoadDb, ilog)); }
public UPNP(stCore.IMessage iMsg) { this.IMsg = ((iMsg == null) ? (new stCore.IMessage()) : iMsg); this._InitUPNP(Type.Broadcast); }
public UPNP(Type type, stCore.IMessage imsg) { this.IMsg = imsg; this._InitUPNP(type); }
private static bool _CheckArgs(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string key, string clanTag, stCore.IMessage iLog = null) { if (type == stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq.None) { stCore.LogException.Error(Properties.Resources.CoCRequestIdEmpty, iLog); return(false); } if (string.IsNullOrWhiteSpace(clanTag)) { stCore.LogException.Error(Properties.Resources.CoCClanIdEmpty, iLog); return(false); } if (string.IsNullOrWhiteSpace(key)) { stCore.LogException.Error(Properties.Resources.CoCKeyEmpty, iLog); return(false); } return(true); }
private static bool _CheckReturnString(string jsonOut, stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, stCore.IMessage iLog = null) { if (string.IsNullOrWhiteSpace(jsonOut)) { string serverError = string.Format( Properties.Resources.CoCGetUrlEmpty, type.ToString() ); stCore.LogException.Error(serverError, iLog); return(false); } #if DEBUG_CHECKCURL stConsole.WriteHeader("Json Out (CheckReturnString): " + jsonOut); #endif if (jsonOut.Contains("\"reason\":")) { string serverError = String.Empty; DataTable dt1 = jsonOut.JsonToDataTable(); #if DEBUG_CHECKCURL dt1.DataTableToPrint(); #endif if ((dt1 != null) && (dt1.Rows.Count > 0)) { serverError = string.Format( Properties.Resources.CoCGetUrlServerError, ((dt1.Rows[0].Table.Columns.Contains("reason")) ? stConsole.SplitCapitalizeString((string)dt1.Rows[0]["reason"]) : Properties.Resources.CurlReturnReason ), ((dt1.Rows[0].Table.Columns.Contains("message")) ? dt1.Rows[0]["message"] : Properties.Resources.CurlReturnReason ) ); } else { serverError = string.Format( Properties.Resources.CoCGetUrlEmpty, type.ToString() ); } throw new CoCDBExceptionReason(serverError); } return(true); }
public static string GetCoCUrlSingle(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string key, string clanTag, string curlexe, string rootpath, stCore.IMessage iLog = null) { if (!CoCRequest._CheckArgs(type, key, clanTag, iLog)) { return(String.Empty); } string reqUrl = CoCRequest.MakeCoCUrl(type, clanTag); if (string.IsNullOrWhiteSpace(reqUrl)) { stCore.LogException.Error(Properties.Resources.CoCUrlEmpty, iLog); return(String.Empty); } #if DEBUG_CHECKCURL stConsole.WriteHeader("Get URL: " + reqUrl); #endif dynamic ccl = null; try { ccl = CoCRequest._InitCUrlObj(curlexe, key, rootpath, iLog); string jsonOut = ccl.GetJson(reqUrl, ((stRuntime.isRunTime()) ? null : key)); if (!CoCRequest._CheckReturnString(jsonOut, type, iLog)) { return(String.Empty); } return(jsonOut); } catch (Exception e) { stCore.LogException.Error(e, iLog); return(String.Empty); } finally { if (ccl != null) { ccl.Dispose(); } } }
public static string GetCoCUrlMulti(stCoCAPI.CoCAPI.CoCEnum.CoCFmtReq type, string key, string clanTag, dynamic curlobj, stCore.IMessage iLog = null) { if (curlobj == null) { stCore.LogException.Error(Properties.Resources.CurlObjEmpty, iLog); return(String.Empty); } if (!CoCRequest._CheckArgs(type, key, clanTag, iLog)) { return(String.Empty); } string reqUrl = CoCRequest.MakeCoCUrl(type, clanTag); if (string.IsNullOrWhiteSpace(reqUrl)) { stCore.LogException.Error(Properties.Resources.CoCUrlEmpty, iLog); return(String.Empty); } #if DEBUG_CHECKCURL stConsole.WriteHeader("Get URL: " + reqUrl); #endif try { string jsonOut = curlobj.GetJson(reqUrl, ((stRuntime.isRunTime()) ? null : key)); if (!CoCRequest._CheckReturnString(jsonOut, type, iLog)) { return(String.Empty); } return(jsonOut); } catch (Exception e) { stCore.LogException.Error(e, iLog); return(String.Empty); } }
private static dynamic _InitCUrlObj(string curlexe, string key, string rootpath, stCore.IMessage iLog) { dynamic ccl = null; try { // curl is use on native Win32/64, libcurl use in Mono Linux/OSX // for use libcurl for WIN32, needed compile static libcurl library with OpenSSL, // and remove flag LIBCURLSELECTOR and/or LIBCURLBINARY // // Curl help: Legacy Windows and SSL // WinSSL (specifically SChannel from Windows SSPI), is the native SSL library // in Windows. However, WinSSL in Windows <= XP is unable to connect to servers // that no longer support the legacy handshakes and algorithms used by those // versions. If you will be using curl in one of those earlier versions of // Windows you should choose another SSL backend such as OpenSSL. #if LIBCURLSELECTOR //HACK: Compile stCoCAPI is use libcurl selector (curl/libcurl), Mono compatible if (stRuntime.isRunTime()) { ccl = new stNet.stCurlClientShare(key, stNet.stCurlClientSet.jsonHeaderArgs); } else { ccl = new stNet.stCurlClientBinary(curlexe); ccl.RootPath = rootpath; ccl.UTF8Out = true; } #elif LIBCURLBINARY //HACK: Compile stCoCAPI is use curl binary ccl = new stNet.stCurlClientBiary(curlexe); #else //HACK: Compile stCoCAPI is use libcurl shared library, Mono compatible ccl = new stNet.stCurlClientShare(key, stNet.stCurlClientSet.jsonHeaderArgs); #endif if (ccl == null) { throw new ArgumentNullException(Properties.Resources.CurlObjEmpty); } return(ccl); } catch (Exception e) { stCore.LogException.Error(e, iLog); return(null); } }
/// <summary> /// Initialize cURL instance /// </summary> /// <param name="curlexe">name and path of curl binary, is null - auto detect</param> /// <param name="key">Bearer auth key</param> /// <param name="iLog">log instance, <see cref="stCore.IMessage"/></param> /// <returns></returns> public static dynamic InitCUrlObj(string curlexe, string key, string rootpath, stCore.IMessage iLog = null) { return(CoCRequest._InitCUrlObj(curlexe, key, rootpath, iLog)); }
/// <summary> /// Initialize cURL instance /// </summary> /// <param name="key">Bearer auth key</param> /// <param name="iLog">log instance, <see cref="stCore.IMessage"/></param> /// <returns></returns> public static dynamic InitCUrlObj(string key, stCore.IMessage iLog = null) { return(CoCRequest._InitCUrlObj(null, key, null, iLog)); }