From 9ad444762a4105c7fc30e8a4c5969c73099dc3cc Mon Sep 17 00:00:00 2001 From: Sean Date: Sun, 27 Apr 2025 00:33:08 -0400 Subject: [PATCH] Add IPMonitor --- .gitignore | 3 + IPMonitor/CommandArgs.cs | 86 +++++ IPMonitor/Executor.cs | 26 ++ IPMonitor/IPMonitor.csproj | 23 ++ IPMonitor/IPMonitor.sln | 22 ++ IPMonitor/Interface/IArguments.cs | 7 + IPMonitor/Interface/IMainService.cs | 9 + IPMonitor/Program.cs | 22 ++ IPMonitor/Services/Arguments.cs | 17 + IPMonitor/Services/MainService.cs | 348 ++++++++++++++++++ IPMonitor/ZoneEditResponse.cs | 79 ++++ IPMonitor/appsettings.json | 10 + MarketData.sln | 2 + .../Integration/HttpNetRequest.cs | 97 +++-- 14 files changed, 699 insertions(+), 52 deletions(-) create mode 100644 IPMonitor/CommandArgs.cs create mode 100755 IPMonitor/Executor.cs create mode 100755 IPMonitor/IPMonitor.csproj create mode 100755 IPMonitor/IPMonitor.sln create mode 100755 IPMonitor/Interface/IArguments.cs create mode 100755 IPMonitor/Interface/IMainService.cs create mode 100755 IPMonitor/Program.cs create mode 100755 IPMonitor/Services/Arguments.cs create mode 100755 IPMonitor/Services/MainService.cs create mode 100755 IPMonitor/ZoneEditResponse.cs create mode 100644 IPMonitor/appsettings.json diff --git a/.gitignore b/.gitignore index 6419d53..1ec1b80 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,6 @@ eNavigator/MarketDataLib/obj/** MarketDataServer/bin/** MarketDataServer/obj/** MarketDataServer/logs/** +IPMonitor/bin/** +IPMonitor/obj/** +IPMonitor/obj/logs/** diff --git a/IPMonitor/CommandArgs.cs b/IPMonitor/CommandArgs.cs new file mode 100644 index 0000000..54c1c58 --- /dev/null +++ b/IPMonitor/CommandArgs.cs @@ -0,0 +1,86 @@ +using System.Text; +using MarketData.Utils; + +namespace IPMonitor +{ + public class CommandArgs : Dictionary + { + private String[] arguments = default; + + public String[] GetArguments() + { + return arguments; + } +// While the commands are converted to uppercase care must be taken to preserve the case of the arguments. + public CommandArgs(String[] args) + { + arguments = args; + for(int index=1;index2) + { + StringBuilder sb=new StringBuilder(); + for(int subIndex=1;subIndex(String name) + { + T result=default(T); + try {result = (T)Convert.ChangeType(this[name].Value, typeof(T));} + catch {result = default(T);} + return result; + } + + public T Coalesce(String name,T coalesce) + { + T result=default(T); + try + { + if(!Contains(name))result=coalesce; + else result = (T)Convert.ChangeType(this[name].Value, typeof(T)); + } + catch {result = default(T);} + return result; + } + + public T Coalesce(String name) + { + T result=default(T); + try {result = (T)Convert.ChangeType(this[name].Value, typeof(T));} + catch {result = default(T);} + return result; + } + } +} \ No newline at end of file diff --git a/IPMonitor/Executor.cs b/IPMonitor/Executor.cs new file mode 100755 index 0000000..7db0312 --- /dev/null +++ b/IPMonitor/Executor.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Configuration; + +namespace IPMonitor +{ + public class Executor + { + private readonly IConfiguration _configuration; + private readonly IMainService _mainService; + private readonly IArguments _arguments; + + public Executor(IMainService mainService,IConfigurationRoot configuration,IArguments arguments) + { + _configuration = configuration ?? throw new ArgumentException(nameof(configuration)); + _mainService = mainService ?? throw new ArgumentException(nameof(mainService)); + _arguments = arguments ?? throw new ArgumentException(nameof(arguments)); + } + + /// + /// This is essentially the starting point where we bootstrap the legacy entry point + /// + public void Execute() + { + _mainService.RunService(_arguments.GetArguments(), _configuration); + } + } +} diff --git a/IPMonitor/IPMonitor.csproj b/IPMonitor/IPMonitor.csproj new file mode 100755 index 0000000..bd41737 --- /dev/null +++ b/IPMonitor/IPMonitor.csproj @@ -0,0 +1,23 @@ + + + + Exe + net8.0 + ipmonitor + enable + disable + CA1416;CS8769;CS0108;CS8602;CS8601;CS8620;CS8618;CS8603;CS8767;CS8625;CS8604;CS8600;CS8604 + + + + + + + + + + + PreserveNewest + + + diff --git a/IPMonitor/IPMonitor.sln b/IPMonitor/IPMonitor.sln new file mode 100755 index 0000000..b35b50a --- /dev/null +++ b/IPMonitor/IPMonitor.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.21005.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IPMonitor", "IPMonitor.csproj", "{45646B53-1805-4678-AC93-20F87BE46A95}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {45646B53-1805-4678-AC93-20F87BE46A95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {45646B53-1805-4678-AC93-20F87BE46A95}.Debug|Any CPU.Build.0 = Debug|Any CPU + {45646B53-1805-4678-AC93-20F87BE46A95}.Release|Any CPU.ActiveCfg = Release|Any CPU + {45646B53-1805-4678-AC93-20F87BE46A95}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/IPMonitor/Interface/IArguments.cs b/IPMonitor/Interface/IArguments.cs new file mode 100755 index 0000000..04461d4 --- /dev/null +++ b/IPMonitor/Interface/IArguments.cs @@ -0,0 +1,7 @@ +namespace IPMonitor +{ + public interface IArguments + { + public String[] GetArguments(); + } +} \ No newline at end of file diff --git a/IPMonitor/Interface/IMainService.cs b/IPMonitor/Interface/IMainService.cs new file mode 100755 index 0000000..0feb023 --- /dev/null +++ b/IPMonitor/Interface/IMainService.cs @@ -0,0 +1,9 @@ +using Microsoft.Extensions.Configuration; + +namespace IPMonitor +{ + public interface IMainService + { + public void RunService(String[] args,IConfiguration configuration); + } +} \ No newline at end of file diff --git a/IPMonitor/Program.cs b/IPMonitor/Program.cs new file mode 100755 index 0000000..187d9d6 --- /dev/null +++ b/IPMonitor/Program.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + +namespace IPMonitor +{ + class Program + { + static void Main(string[] args) + { + IConfigurationBuilder builder = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); + IConfigurationRoot configurationRoot = builder.Build(); + Arguments arguments = new Arguments(args); + IServiceCollection services = new ServiceCollection(); + services.AddSingleton(arguments); + services.AddSingleton(configurationRoot); + services.AddSingleton(); + services.AddSingleton(); + services.BuildServiceProvider().GetService().Execute(); + } + } +} \ No newline at end of file diff --git a/IPMonitor/Services/Arguments.cs b/IPMonitor/Services/Arguments.cs new file mode 100755 index 0000000..df31e79 --- /dev/null +++ b/IPMonitor/Services/Arguments.cs @@ -0,0 +1,17 @@ +namespace IPMonitor +{ + public class Arguments : IArguments + { + private readonly String[] _args; + + public Arguments(String[] args) + { + _args = args ?? throw new ArgumentNullException(nameof(args)); + } + + public String[] GetArguments() + { + return _args; + } + } +} \ No newline at end of file diff --git a/IPMonitor/Services/MainService.cs b/IPMonitor/Services/MainService.cs new file mode 100755 index 0000000..82ca0c2 --- /dev/null +++ b/IPMonitor/Services/MainService.cs @@ -0,0 +1,348 @@ +using System.Diagnostics; +using System.Net; +using System.Text; +using System.Web; +using MarketData; +using MarketData.Configuration; +using MarketData.Integration; +using MarketData.Utils; +using Microsoft.Extensions.Configuration; + +namespace IPMonitor +{ + public class MainService : IMainService + { + /// + /// This is the main entry point. + /// Arguments: /FORCE:true|false if true forces a refresh on ZoneEdit. + /// Force an update to ZoneEdit at the top of every hour + /// cron every hour + /// DOTNET_ROOT=/opt/dotnet + /// CRON_DIR_IPMONITOR=/opt/MarketData/IPMonitor + /// 0 * * * * cd $CRON_DIR_IPMONITOR ; /opt/MarketData/IPMonitor/ipmonitor /FORCE:true > /dev/null 2>&1 + /// cron 10 minute intervals except top of hour + /// 10,20,30,40,50 * * * * cd $CRON_DIR_IPMONITOR ; /opt/MarketData/IPMonitor/ipmonitor > /dev/null 2>&1 + /// + /// + /// + public void RunService(String[] args,IConfiguration configuration) + { + DateTime currentDate = DateTime.Now; + GlobalConfig.Instance.Configuration = configuration; // This call sets up configuration stuff so it needs to be first. + + string arg = "ipmonitor"; + CommandArgs commandArgs = new CommandArgs(args); + + if(!CreateLogging(arg)) + { + Console.WriteLine("CreateLogging returned false."); + return; + } + + try + { + Profiler profiler=new Profiler(); + profiler.Start(); + MDTrace.WriteLine(LogLevel.DEBUG,$"[RunService] Started @ {Utility.DateTimeToStringYYYYHMMHDDHHMMSSTT(currentDate)} in {Directory.GetCurrentDirectory()}"); + MDTrace.WriteLine(LogLevel.DEBUG,$"[RunService] Argument {arg}"); + + bool force=false; + if(commandArgs.Has("FORCE")) + { + force=commandArgs.Coalesce("FORCE"); + MDTrace.WriteLine(LogLevel.DEBUG,$"FORCE={force}"); + } + String ipAddress=GetPublicIPAddress(); + UpdateIPAddress(ipAddress,force); + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString())); + } + } + + private static bool CreateLogging(String task) + { + if(String.IsNullOrEmpty(task))return false; + task=task.ToLower(); + MDTrace.LogLevel = LogLevel.DEBUG; + String logFolder = "/logs"; + DateTime currentDate=DateTime.Now; + String strLogFile = "marketdata_" + task + ".log"; + String currentWorkingDirectory = Directory.GetCurrentDirectory(); + Console.WriteLine($"Current directory is {currentWorkingDirectory}"); + Utility.EnsureLogFolder(currentWorkingDirectory+logFolder); + Utility.ExpireLogs(currentWorkingDirectory+logFolder,1); + Trace.Listeners.Remove("Default"); + Console.WriteLine($"Adding Trace Listener :{currentWorkingDirectory+logFolder+"/"+strLogFile}"); + Trace.Listeners.Add(new TextWriterTraceListener(currentWorkingDirectory+logFolder+"/"+strLogFile)); + MDTrace.WriteLine($"Trace Listener added."); + Utility.ShowLogs(currentWorkingDirectory + logFolder); + return true; + } + + /// + /// Retains a record of current ip address in ipaddress.txt file and updates that ip address to ZoneEdit for DNS + /// + /// + public static void UpdateIPAddress(String ipAddress,bool force) + { + try + { + String strPathFileName="ipaddress.txt"; + if(null==ipAddress)return; + if(force) + { + File.Delete(GetPathFileName(strPathFileName)); + } + if(!File.Exists(GetPathFileName(strPathFileName))) + { + WriteFile(GetPathFileName(strPathFileName), ipAddress); + ZoneEditResponses zoneEditResponses=UpdateZoneEditRecord(ipAddress); + MDTrace.WriteLine(LogLevel.DEBUG,$"IPAddress {ipAddress}. ZoneEditUpdated={zoneEditResponses.IsSuccess()}"); + SendSMSEmail(String.Format("IPAddress {0}. ZoneEditUpdated={1}",ipAddress,zoneEditResponses.IsSuccess())); + if(!zoneEditResponses.IsSuccess()) + { + File.Delete(GetPathFileName(strPathFileName)); + } + } + else + { + String currentIPAddress=ReadFile(GetPathFileName(strPathFileName)); + if(null!=currentIPAddress && !currentIPAddress.Equals(ipAddress)) + { + WriteFile(GetPathFileName(strPathFileName),ipAddress); + ZoneEditResponses zoneEditResponses=UpdateZoneEditRecord(ipAddress); + SendSMSEmail(String.Format("IPAddress {0}. ZoneEditUpdated={1}",ipAddress,zoneEditResponses.IsSuccess())); + } + else if(null!=currentIPAddress && currentIPAddress.Equals(ipAddress)) + { + MDTrace.WriteLine(LogLevel.DEBUG,$"Public IP:{ipAddress} matches latest fetched IP:{currentIPAddress}"); + } + else if(null==currentIPAddress) + { + SendSMSEmail("IPMonitor "+ipAddress+ ". IPMonitor encountered an issue reading the IPAddress file."); + } + } + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString())); + } + } + + /// + /// Retrieves the public IP Address + /// + /// + public static String GetPublicIPAddress() + { + int MAX_RETRIES=5; + int TIMEOUT_BETWEEN_ATTEMPTS=30000; + String request="http://checkip.dyndns.org/"; + + try + { + String address = null; + for(int index=0;index"); + address = address.Substring(first, last - first); + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Request succeeded: {0} -> {1}",request,address)); + return address; + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception parsing address: {0}",exception.ToString())); + return null; + } + } + catch(Exception exception) + { + String message = String.Format("Exception:{0}",exception.ToString()); + MDTrace.WriteLine(LogLevel.DEBUG,message); + SendSMSEmail("IPMonitor encountered an issue retrieving public IPAddress."); + return null; + } + } + + /// + /// Sends an HttpRequest and receives the response string. Used by GetPublicIPAddress + /// + /// + public static String GetHttpRequest(String strRequest) + { + try + { + WebRequest request = WebRequest.Create(strRequest); + using WebResponse response = request.GetResponse(); + using (StreamReader stream = new StreamReader(response.GetResponseStream())) + { + return stream.ReadToEnd(); + } + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Request {strRequest} Failed with {1}",exception.ToString())); + return null; + } + finally + { + } + } + + /// + /// Updates ZoneEdit with the given ipaddress. + /// + /// + /// ZoneEditResponses + public static ZoneEditResponses UpdateZoneEditRecord(String ipAddress) + { + HttpClient client = default; + HttpNetResponse httpNetResponse=default; + + try + { + client = new HttpClient(); + + String user=GlobalConfig.Instance.Configuration["zoneedit_user"]; + String password=GlobalConfig.Instance.Configuration["zoneedit_password"]; + String host=GlobalConfig.Instance.Configuration["zoneedit_host"]; + + String strRequest=null; + StringBuilder sb=new StringBuilder(); + + sb = new StringBuilder(); + sb.Append("https://"); + sb.Append("dynamic.zoneedit.com/auth/dynamic.html"); + sb.Append("?host=").Append(HttpUtility.UrlEncode(host)); + sb.Append("&dnsto=").Append(HttpUtility.UrlEncode(ipAddress)); + strRequest=sb.ToString(); + httpNetResponse = HttpNetRequest.GetRequestNoEncodingZoneEdit(strRequest,user,password); + + if(!httpNetResponse.Success) + { + return new ZoneEditResponses(); + } + + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("{0}",httpNetResponse.ResponseString)); + return new ZoneEditResponses(httpNetResponse.ResponseString); + } + finally + { + if(default!=client) + { + client.Dispose(); + } + if(default!=httpNetResponse) + { + httpNetResponse.Dispose(); + } + } + } + + /// + /// Writes the given ip address into the specified file + /// + /// + /// + public static void WriteFile(String strPathFileName,String ipAddress) + { + try + { + if(File.Exists(strPathFileName))File.Delete(strPathFileName); + using FileStream fileStream=new FileStream(strPathFileName,FileMode.Create,FileAccess.Write,FileShare.Read); + using StreamWriter streamWriter=new StreamWriter(fileStream); + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Creating address file:{0}",strPathFileName)); + streamWriter.WriteLine(ipAddress); + streamWriter.Flush(); + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString())); + return; + } + } + + /// + /// Reads a string from the specified file + /// + /// + /// + public static String ReadFile(String strPathFileName) + { + try + { + using FileStream fileStream=new FileStream(strPathFileName,FileMode.Open,FileAccess.ReadWrite,FileShare.Read); + using StreamReader streamReader=new StreamReader(fileStream); + String item=null; + item=streamReader.ReadLine(); + return item; + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception:{0}",exception.ToString())); + return null; + } + } + + /// + /// Determines the fully qualified path file name for the provided file based on current directory and working folder + /// + /// + /// + public static String GetPathFileName(String fileName) + { + String currentWorkingDirectory = Directory.GetCurrentDirectory(); + String workingFolder = GlobalConfig.Instance.Configuration["working_folder"]; + if(workingFolder.StartsWith("/")) + { + String fullPath = currentWorkingDirectory+workingFolder; + if(!Directory.Exists(fullPath))Directory.CreateDirectory(fullPath); + return fullPath+"/"+fileName; + } + else + { + String fullPath = currentWorkingDirectory+"/"+workingFolder; + if(!Directory.Exists(fullPath))Directory.CreateDirectory(fullPath); + return fullPath+"/"+fileName; + } + } + + /// + /// Sends an email. + /// + /// + public static void SendSMSEmail(string message) + { + String smsSMTPAddress = GlobalConfig.Instance.Configuration["sms_smtpaddress"]; + String smsUserName = GlobalConfig.Instance.Configuration["sms_smsusername"]; + String smsPassword = GlobalConfig.Instance.Configuration["sms_smspassword"]; + String[] smsRecipients = GlobalConfig.Instance.Configuration["sms_smsrecipients"].Split(','); + SMSClient.SendSMSEmail(message, smsUserName, smsRecipients, smsSMTPAddress, smsUserName, smsPassword); + } + } +} \ No newline at end of file diff --git a/IPMonitor/ZoneEditResponse.cs b/IPMonitor/ZoneEditResponse.cs new file mode 100755 index 0000000..c0bf222 --- /dev/null +++ b/IPMonitor/ZoneEditResponse.cs @@ -0,0 +1,79 @@ +using System.Text; +using MarketData.Utils; + +namespace IPMonitor +{ + public class ZoneEditResponses : List + { + public ZoneEditResponses() + { + } + + public bool IsSuccess() + { + if(Count==0)return false; + return this.Any(x => x.SuccessCode.Equals("200") || x.SuccessCode.Equals("201")); + } + + public override string ToString() + { + StringBuilder sb=new StringBuilder(); + for(int index=0;index\n\n" string + + public class ZoneEditResponse + { + public String SuccessCode { get; set; } + public String Text { get; set; } + public String Zone { get; set; } + + public ZoneEditResponse() + { + } + + public ZoneEditResponse(String successCode, String text, String zone) + { + SuccessCode=successCode; + Text=text; + Zone=zone; + } + + public override String ToString() + { + StringBuilder sb = new StringBuilder(); + sb.Append("SUCCESS CODE=").Append(SuccessCode); + sb.Append(","); + sb.Append("TEXT=").Append(Text); + sb.Append(","); + sb.Append("ZONE=").Append(Zone); + return sb.ToString(); + } + } +} diff --git a/IPMonitor/appsettings.json b/IPMonitor/appsettings.json new file mode 100644 index 0000000..13fe206 --- /dev/null +++ b/IPMonitor/appsettings.json @@ -0,0 +1,10 @@ +{ +"working_folder" : "/ipaddress", +"sms_smtpaddress" : "smtp.gmail.com", +"sms_smsusername" : "skessler1964@gmail.com", +"sms_smspassword" : "xjfo isnf gmyi zovr", +"sms_smsrecipients" : "skessler1964@gmail.com", +"zoneedit_user" : "skessler1964", +"zoneedit_password" : "4A536A7920309A11", +"zoneedit_host" : "diversified-software.com" +} diff --git a/MarketData.sln b/MarketData.sln index e87661d..1e1de7d 100755 --- a/MarketData.sln +++ b/MarketData.sln @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketData", "MarketData\Ma EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MarketDataServer", "MarketDataServer\MarketDataServer.csproj", "{EAC3FB58-9112-4446-98BF-DC33F96357B5}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IPMonitor", "IPMonitor\IPMonitor.csproj", "{EAC3FB58-9112-4446-98BF-DC33F96357B5}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/MarketData/MarketDataLib/Integration/HttpNetRequest.cs b/MarketData/MarketDataLib/Integration/HttpNetRequest.cs index fd2d618..278a1c8 100755 --- a/MarketData/MarketDataLib/Integration/HttpNetRequest.cs +++ b/MarketData/MarketDataLib/Integration/HttpNetRequest.cs @@ -216,8 +216,6 @@ namespace MarketData.Integration HttpWebResponse webResponse=null; try { - // ServicePointManager.Expect100Continue=true; - // ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestStreamCSV[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; @@ -300,7 +298,6 @@ namespace MarketData.Integration try { ServicePointManager.Expect100Continue = true; -// ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestNoEncoding[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; @@ -355,7 +352,6 @@ namespace MarketData.Integration public static HttpNetResponse GetRequestNoEncodingV1(String strRequest,WebProxy webProxy=null) { ServicePointManager.Expect100Continue=true; -// ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; HttpWebResponse webResponse=null; try { @@ -626,7 +622,6 @@ namespace MarketData.Integration HttpWebResponse webResponse=null; WebException lastWebException=null; ServicePointManager.Expect100Continue=true; -// ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; Random random=new Random(); try @@ -733,7 +728,6 @@ namespace MarketData.Integration HttpWebResponse webResponse=null; WebException lastWebException=null; ServicePointManager.Expect100Continue=true; -// ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; try { @@ -832,7 +826,6 @@ namespace MarketData.Integration HttpWebResponse webResponse=null; WebException lastWebException=null; ServicePointManager.Expect100Continue=true; -// ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; Random random=new Random(); try @@ -1001,10 +994,7 @@ namespace MarketData.Integration int charCount=0; byte[] buffer=new byte[8192]; StringBuilder sb=new StringBuilder(); -// bool expect100Condition=ServicePointManager.Expect100Continue; -// SecurityProtocolType securityProtocolType=ServicePointManager.SecurityProtocol; ServicePointManager.Expect100Continue=true; -// ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12; HttpWebRequest webRequest=(HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout=REQUEST_TIMEOUT; @@ -1042,8 +1032,6 @@ namespace MarketData.Integration sb.Append(Encoding.ASCII.GetString(buffer,0,charCount)); } } -// ServicePointManager.Expect100Continue=expect100Condition; -// ServicePointManager.SecurityProtocol=securityProtocolType; return new HttpNetResponse(sb.ToString(),strRequest,webResponse,webResponse.Cookies,true); } catch(WebException webException) @@ -1074,10 +1062,6 @@ namespace MarketData.Integration int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); -// bool expect100Condition = ServicePointManager.Expect100Continue; -// SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; -// ServicePointManager.Expect100Continue = true; -// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout = webRequestTimeoutMS; @@ -1104,8 +1088,6 @@ namespace MarketData.Integration if (0 == charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } -// ServicePointManager.Expect100Continue = expect100Condition; -// ServicePointManager.SecurityProtocol = securityProtocolType; return new HttpNetResponse(sb.ToString(), strRequest, webResponse, webResponse.Cookies, true); } catch (WebException webException) @@ -1203,10 +1185,6 @@ namespace MarketData.Integration int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); - // bool expect100Condition = ServicePointManager.Expect100Continue; - // SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; - // ServicePointManager.Expect100Continue = true; - // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout = webRequestTimeoutMS; @@ -1235,8 +1213,6 @@ namespace MarketData.Integration if (0 == charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } - // ServicePointManager.Expect100Continue = expect100Condition; - // ServicePointManager.SecurityProtocol = securityProtocolType; return new HttpNetResponse(sb.ToString(), strRequest, webResponse, webResponse.Cookies, true); } catch (WebException webException) @@ -1268,9 +1244,6 @@ namespace MarketData.Integration byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); bool expect100Condition = ServicePointManager.Expect100Continue; - // SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; - // ServicePointManager.Expect100Continue = true; - // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout = webRequestTimeoutMS; @@ -1302,8 +1275,6 @@ namespace MarketData.Integration else throw; } HttpNetResponse httpNetResponse = ProcessWebResponse(strRequest, webResponse); - // ServicePointManager.Expect100Continue = expect100Condition; - // ServicePointManager.SecurityProtocol = securityProtocolType; return httpNetResponse; } catch (WebException webException) @@ -1333,10 +1304,6 @@ namespace MarketData.Integration MDTrace.WriteLine(LogLevel.VERBOSE, String.Format("GetRequestNoEncodingV5C[ENTER]{0}", strRequest)); byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); - // bool expect100Condition = ServicePointManager.Expect100Continue; - // SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; - // ServicePointManager.Expect100Continue = true; - // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout = webRequestTimeoutMS; @@ -1372,8 +1339,6 @@ namespace MarketData.Integration HttpNetResponse httpNetResponse = ProcessWebResponse(strRequest, webResponse); httpNetResponse.CookieCollection = new CookieCollection(); httpNetResponse.CookieCollection.Add(webResponse.Cookies); - // ServicePointManager.Expect100Continue = expect100Condition; - // ServicePointManager.SecurityProtocol = securityProtocolType; return httpNetResponse; } catch (WebException webException) @@ -1444,10 +1409,6 @@ namespace MarketData.Integration int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); - // bool expect100Condition = ServicePointManager.Expect100Continue; - // SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; - // ServicePointManager.Expect100Continue = true; - // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); webRequest.Timeout = webRequestTimeoutMS; webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); @@ -1474,8 +1435,6 @@ namespace MarketData.Integration if (0 == charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } - // ServicePointManager.Expect100Continue = expect100Condition; - // ServicePointManager.SecurityProtocol = securityProtocolType; return new HttpNetResponse(sb.ToString(), strRequest, webResponse, webResponse.Cookies, true); } catch (WebException webException) @@ -1507,9 +1466,6 @@ namespace MarketData.Integration byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); bool expect100Condition = ServicePointManager.Expect100Continue; - // SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; - // ServicePointManager.Expect100Continue = true; - // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.KeepAlive=true; @@ -1555,8 +1511,6 @@ namespace MarketData.Integration sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } } - // ServicePointManager.Expect100Continue = expect100Condition; - // ServicePointManager.SecurityProtocol = securityProtocolType; return new HttpNetResponse(sb.ToString(), strRequest, webResponse, webResponse.Cookies, true); } catch (WebException webException) @@ -1577,6 +1531,51 @@ namespace MarketData.Integration MDTrace.WriteLine(LogLevel.VERBOSE, "GetRequestNoEncodingV7[LEAVE]"); } } + + public static HttpNetResponse GetRequestNoEncodingZoneEdit(String strRequest,String userName, String password) + { + HttpWebResponse webResponse = null; + try + { + MDTrace.WriteLine(LogLevel.VERBOSE, String.Format("GetRequestNoEncodingZoneEdit[ENTER]{0}", strRequest)); + int charCount = 0; + byte[] buffer = new byte[8192]; + StringBuilder sb = new StringBuilder(); + HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); + webRequest.Credentials=new NetworkCredential(userName,password); + webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); + webRequest.Headers.Add("Accept-Encoding: None"); + webRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; + webRequest.KeepAlive = true; + webResponse = (HttpWebResponse)webRequest.GetResponse(); + Stream responseStream = webResponse.GetResponseStream(); + while (true) + { + charCount = responseStream.Read(buffer, 0, buffer.Length); + if (0 == charCount) break; + sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); + } + return new HttpNetResponse(sb.ToString(), strRequest, webResponse, webResponse.Cookies, true); + } + catch (WebException webException) + { + return new HttpNetResponse((HttpWebResponse)webException.Response, strRequest, false, webException.Message); + } + catch (Exception exception) + { + return new HttpNetResponse(webResponse, strRequest, false, exception.Message); + } + finally + { + if(null!=webResponse) + { + webResponse.Close(); + webResponse.Dispose(); + } + MDTrace.WriteLine(LogLevel.VERBOSE, "GetRequestNoEncodingZoneEdit[LEAVE]"); + } + } + // This is specifically taylored to Morningstar site for retrieval of historical data public static HttpNetResponse GetRequestNoEncodingMStar(String strRequest,WebProxy webProxy=null) { @@ -1587,10 +1586,6 @@ namespace MarketData.Integration int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); - // bool expect100Condition = ServicePointManager.Expect100Continue; - // SecurityProtocolType securityProtocolType = ServicePointManager.SecurityProtocol; - // ServicePointManager.Expect100Continue = true; - // ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); webRequest.KeepAlive=true; webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); @@ -1645,8 +1640,6 @@ namespace MarketData.Integration sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } } - // ServicePointManager.Expect100Continue = expect100Condition; - // ServicePointManager.SecurityProtocol = securityProtocolType; return new HttpNetResponse(sb.ToString(), strRequest, webResponse, webResponse.Cookies, true); } catch (WebException webException)