/// <summary> /// Read settings from the config into an easy to use class /// </summary> public static bool Load() { try { CaptureMacAddress = (PhysicalAddress)GetValue("captureMacAddress", false, (value) => { return(PhysicalAddress.Parse(value.ToUpper())); }); EnableLocalProxy = (bool)GetValue("enableLocalProxy", true, (value) => { return(bool.Parse(value)); }); DeviceNumber = (int?)GetValue("deviceNumber", false, (value) => { return(int.Parse(value)); }); PacFileHostPort = (int)GetValue("pacFileHostPort", true, (value) => { return(int.Parse(value)); }); ProxyPort = (int)GetValue("proxyPort", true, (value) => { return(int.Parse(value)); }); CaptureReadTimeout = (int)GetValue("captureReadTimeout", true, (value) => { return(int.Parse(value)); }); ProxyServer = (IPAddress)GetValue("proxyServer", false, (value) => { return(IPAddress.Parse(value)); }); ProcessToDisplay = (string)GetValue("processToDisplay", true, (value) => { return(value.ToString()); }); HostsToProxy = (string[])GetValue("hostsToProxy", false, (value) => { if (!string.IsNullOrEmpty(value)) { return(value.Split(',')); } else { return(null); } }); // Device number needs to be configured at this point // in order for the proxy server endpoint to be set if (DeviceNumber.HasValue) { if (EnableLocalProxy) { ProxyServerEndPoint = new IPEndPoint(NetworkCapture.GetDeviceIp(DeviceNumber.Value), ProxyPort); } else { ProxyServerEndPoint = new IPEndPoint(ProxyServer, ProxyPort); } } return(true); } catch (Exception ex) { Logger.AddToErrorView("Configuration", ex); return(false); } }
/// <summary> /// If a device number is not specified in the config then a UI is presented /// to the user to select a device /// </summary> /// <returns></returns> private static int GetDeviceNumber() { int deviceNumber = 0; if (!Configuration.DeviceNumber.HasValue) { for (; ;) { List <string> devices = NetworkCapture.GetDevices().ToList(); Console.Clear(); Console.WriteLine("Please select a network device to monitor\r\n"); for (int index = 0; index < devices.Count; index++) { Console.ForegroundColor = ConsoleColor.White; Console.Out.Write("\t{0} - ", index); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(devices[index]); } ConsoleKeyInfo key = Console.ReadKey(); if (!int.TryParse(key.KeyChar.ToString(), out deviceNumber) || deviceNumber > devices.Count()) { Logger.AddToInfoView("\r\n{0} is an invalid device number", key.KeyChar); continue; } Configuration.SetupLocalProxyEndPoint(deviceNumber); break; } } return(Configuration.DeviceNumber.Value); }
/// <summary> /// Used by the GUI when the user selects a device /// </summary> /// <param name="deviceNumber"></param> public static void SetupLocalProxyEndPoint(int deviceNumber) { DeviceNumber = deviceNumber; ProxyServer = NetworkCapture.GetDeviceIp(DeviceNumber.Value); ProxyServerEndPoint = new IPEndPoint(ProxyServer, ProxyPort); }
/// <summary> /// Main entry point to get things rockin /// </summary> /// <param name="args"></param> static void Main(string[] args) { List <BaseWorker> workers = null; try { if (Configuration.Load()) { int deviceNumber = GetDeviceNumber(); Console.Clear(); Logger.AddToInfoView("Once the server has started press any key to stop it."); IPEndPoint pacFileHost = new IPEndPoint(NetworkCapture.GetDeviceIp(deviceNumber), Configuration.PacFileHostPort); workers = new List <BaseWorker>() { new PacFileHost(pacFileHost, Configuration.ProxyServerEndPoint, Configuration.HostsToProxy), new Proxy(), new NetworkCapture(deviceNumber, Configuration.PacFileHostPort, Configuration.ProxyServerEndPoint), }; List <Task> tasks = new List <Task>(); workers.ForEach(worker => { if (worker.Enabled()) { tasks.Add(worker.Start()); } }); tasks.Add(Task.Factory.StartNew(() => HandleUserInput(), BaseWorker.StopToken.Token)); if (NameServices.FlushNameServices()) { Task.WaitAny(tasks.ToArray()); } } } catch (Exception ex) { Logger.AddToErrorView("Program.Main", ex); } finally { if (workers != null) { workers.ForEach(worker => { if (worker != null) { worker.Stop(); } }); } NameServices.FlushNameServices(); } }