Skip to content
This repository has been archived by the owner on Jul 6, 2020. It is now read-only.

Commit

Permalink
Improved active lists handling
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Apr 3, 2017
1 parent 8211784 commit 7e396b4
Show file tree
Hide file tree
Showing 15 changed files with 1,493 additions and 1,486 deletions.
125 changes: 60 additions & 65 deletions ProcessProxifier/Core/ProcessesListManager.cs
Original file line number Diff line number Diff line change
@@ -1,66 +1,61 @@
using System;
using System.Linq;
using ProcessProxifier.Models;
using ProcessProxifier.Utils;

namespace ProcessProxifier.Core
{
public static class ProcessesListManager
{
public static AsyncObservableCollection<Process> UpdateProcesses(ProxifierSettings guiModelData, ProxifierSettings settings)
{
var processesList = System.Diagnostics.Process.GetProcesses().OrderBy(x => x.ProcessName).ToList();

var finishedProcesses = guiModelData.ProcessesList
.Where(x => !processesList.Select(p => p.Id).Contains(x.Pid)
&& !x.IsEnabled)
.ToList();
foreach (var process in finishedProcesses)
{
guiModelData.ProcessesList.Remove(process);
}

var newProcesses = processesList.Where(x => !guiModelData.ProcessesList.Select(p => p.Pid).Contains(x.Id)).ToList();

foreach (var process in newProcesses)
{
var path = getPath(process);
var newProcess = new Process
{
Name = process.ProcessName,
Pid = process.Id,
Path = path
};

var settingsProcess = settings.ProcessesList
.FirstOrDefault(x => x.Path.Equals(path, StringComparison.InvariantCultureIgnoreCase));
if (settingsProcess != null)
{
newProcess.IsEnabled = settingsProcess.IsEnabled;
newProcess.ServerInfo = new ServerInfo
{
ServerIP = settingsProcess.ServerInfo.ServerIP,
ServerPort = settingsProcess.ServerInfo.ServerPort,
ServerType = settingsProcess.ServerInfo.ServerType
};
}

guiModelData.ProcessesList.Add(newProcess);
}

return guiModelData.ProcessesList;
}

private static string getPath(System.Diagnostics.Process process)
{
try
{
return process.MainModule.FileName;
}
catch
{
return string.Empty;
}
}
}
using System;
using System.Linq;
using ProcessProxifier.Models;
using ProcessProxifier.Utils;

namespace ProcessProxifier.Core
{
public static class ProcessesListManager
{
public static AsyncObservableCollection<Process> UpdateProcesses(
ProxifierSettings guiModelData,
ProxifierSettings settings)
{
var systemProcessList = System.Diagnostics.Process.GetProcesses().OrderBy(x => x.ProcessName).ToList();
var systemProcessIds = systemProcessList.Select(p => p.Id).ToList();
var finishedProcesses = guiModelData.ProcessesList
.Where(process => !systemProcessIds.Contains(process.Pid))
.ToList();

if (finishedProcesses.Any())
{
SettingsManager.SaveSettings(guiModelData, settings);
}

foreach (var process in finishedProcesses)
{
guiModelData.ProcessesList.Remove(process);
}

var guiProcessIds = guiModelData.ProcessesList.Select(process => process.Pid).ToList();
var newSystemProcesses = systemProcessList.Where(process => !guiProcessIds.Contains(process.Id)).ToList();
foreach (var systemProcess in newSystemProcesses)
{
var path = systemProcess.GetPath();
var newProcess = new Process
{
Name = systemProcess.ProcessName,
Pid = systemProcess.Id,
Path = path
};

var settingsProcess = settings.ActiveProcessesList
.FirstOrDefault(x => x.Path.Equals(path, StringComparison.InvariantCultureIgnoreCase));
if (settingsProcess != null)
{
newProcess.IsEnabled = settingsProcess.IsEnabled;
newProcess.ServerInfo = new ServerInfo
{
ServerIP = settingsProcess.ServerInfo.ServerIP,
ServerPort = settingsProcess.ServerInfo.ServerPort,
ServerType = settingsProcess.ServerInfo.ServerType
};
}

guiModelData.ProcessesList.Add(newProcess);
}

return guiModelData.ProcessesList;
}
}
}
182 changes: 87 additions & 95 deletions ProcessProxifier/Core/ProxyRouter.cs
Original file line number Diff line number Diff line change
@@ -1,96 +1,88 @@
using System.Linq;
using System.Net.Security;
using System.Threading;
using Fiddler;
using ProcessProxifier.Models;
using ProcessProxifier.Utils;

namespace ProcessProxifier.Core
{
using System.Globalization;

public class ProxyRouter
{
#region Properties (4)

public ServerInfo DefaultServerInfo { set; get; }

public int FiddlerPort { set; get; }

public AsyncObservableCollection<Process> ProcessesList { set; get; }

public AsyncObservableCollection<RoutedConnection> RoutedConnectionsList { set; get; }

#endregion Properties

#region Methods (5)

// Public Methods (2) 

public void Shutdown()
{
FiddlerApplication.BeforeRequest -= beforeRequest;
FiddlerApplication.OnValidateServerCertificate -= onValidateServerCertificate;

if (FiddlerApplication.oProxy != null)
FiddlerApplication.oProxy.Detach();

FiddlerApplication.Shutdown();

Thread.Sleep(1000);
}

public void Start()
{
FiddlerApplication.BeforeRequest += beforeRequest;
FiddlerApplication.OnValidateServerCertificate += onValidateServerCertificate;

FiddlerApplication.Startup(FiddlerPort,
FiddlerCoreStartupFlags.RegisterAsSystemProxy |
FiddlerCoreStartupFlags.MonitorAllConnections |
FiddlerCoreStartupFlags.CaptureFTP);
}
// Private Methods (3) 

void beforeRequest(Session oSession)
{
var process = ProcessesList.FirstOrDefault(p => p.Pid == oSession.LocalProcessID && p.IsEnabled);
if (process == null)
return;

var useDefaultServerInfo = string.IsNullOrWhiteSpace(process.ServerInfo.ServerIP);
if (useDefaultServerInfo)
{
oSession["X-OverrideGateway"] = (DefaultServerInfo.ServerType == ServerType.Socks ? "socks=" : "") + DefaultServerInfo.ServerIP + ":" + DefaultServerInfo.ServerPort;
}
else
{
oSession["X-OverrideGateway"] = (process.ServerInfo.ServerType == ServerType.Socks ? "socks=" : "") + process.ServerInfo.ServerIP + ":" + process.ServerInfo.ServerPort;
}

RoutedConnectionsList.Add(new RoutedConnection
{
ProcessPid = oSession.LocalProcessID,
Url = oSession.fullUrl,
ProcessName = getProcessName(oSession),
ProcessPath = process.Path
});
}

private string getProcessName(Session oSession)
{
var process = ProcessesList.FirstOrDefault(x => x.Pid == oSession.LocalProcessID);
return process == null ? oSession.LocalProcessID.ToString(CultureInfo.InvariantCulture) : process.Name;
}

static void onValidateServerCertificate(object sender, ValidateServerCertificateEventArgs e)
{
if (SslPolicyErrors.None == e.CertificatePolicyErrors)
return;

e.ValidityState = CertificateValidity.ForceValid;
}

#endregion Methods
}
using System.Linq;
using System.Net.Security;
using System.Threading;
using Fiddler;
using ProcessProxifier.Models;
using ProcessProxifier.Utils;

namespace ProcessProxifier.Core
{
using System.Globalization;

public class ProxyRouter
{
public ServerInfo DefaultServerInfo { set; get; }

public int FiddlerPort { set; get; }

public AsyncObservableCollection<Process> ProcessesList { set; get; }

public AsyncObservableCollection<RoutedConnection> RoutedConnectionsList { set; get; }

// Public Methods (2) 

public void Shutdown()
{
FiddlerApplication.BeforeRequest -= beforeRequest;
FiddlerApplication.OnValidateServerCertificate -= onValidateServerCertificate;

if (FiddlerApplication.oProxy != null)
FiddlerApplication.oProxy.Detach();

FiddlerApplication.Shutdown();

Thread.Sleep(1000);
}

public void Start()
{
FiddlerApplication.BeforeRequest += beforeRequest;
FiddlerApplication.OnValidateServerCertificate += onValidateServerCertificate;

FiddlerApplication.Startup(FiddlerPort,
FiddlerCoreStartupFlags.RegisterAsSystemProxy |
FiddlerCoreStartupFlags.MonitorAllConnections |
FiddlerCoreStartupFlags.CaptureFTP);
}
// Private Methods (3) 

void beforeRequest(Session oSession)
{
var process = ProcessesList.FirstOrDefault(p => p.Pid == oSession.LocalProcessID && p.IsEnabled);
if (process == null)
return;

var useDefaultServerInfo = string.IsNullOrWhiteSpace(process.ServerInfo.ServerIP);
if (useDefaultServerInfo)
{
oSession["X-OverrideGateway"] = (DefaultServerInfo.ServerType == ServerType.Socks ? "socks=" : "") + DefaultServerInfo.ServerIP + ":" + DefaultServerInfo.ServerPort;
}
else
{
oSession["X-OverrideGateway"] = (process.ServerInfo.ServerType == ServerType.Socks ? "socks=" : "") + process.ServerInfo.ServerIP + ":" + process.ServerInfo.ServerPort;
}

RoutedConnectionsList.Add(new RoutedConnection
{
ProcessPid = oSession.LocalProcessID,
Url = oSession.fullUrl,
ProcessName = getProcessName(oSession),
ProcessPath = process.Path
});
}

private string getProcessName(Session oSession)
{
var process = ProcessesList.FirstOrDefault(x => x.Pid == oSession.LocalProcessID);
return process == null ? oSession.LocalProcessID.ToString(CultureInfo.InvariantCulture) : process.Name;
}

static void onValidateServerCertificate(object sender, ValidateServerCertificateEventArgs e)
{
if (SslPolicyErrors.None == e.CertificatePolicyErrors)
return;

e.ValidityState = CertificateValidity.ForceValid;
}
}
}
Loading

0 comments on commit 7e396b4

Please sign in to comment.