Skip to content

Commit aa77d92

Browse files
committed
add support for Flashpoint Proxy preferences
1 parent 93d29ce commit aa77d92

5 files changed

Lines changed: 141 additions & 25 deletions

File tree

FlashpointSecurePlayer/FlashpointProxy.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public static class FlashpointProxy {
1515

1616
private const uint INTERNET_OPEN_TYPE_DIRECT = 1;
1717

18+
private const bool FP_PROXY_DEFAULT = true;
19+
private const int FP_PROXY_PORT_DEFAULT = 22500;
20+
21+
private const string FP_PROXY = nameof(FP_PROXY);
22+
private const string FP_PROXY_PORT = nameof(FP_PROXY_PORT);
23+
1824
[DllImport("WinInet.dll", SetLastError = true, CharSet = CharSet.Ansi)]
1925
private static extern IntPtr InternetOpen(string lpszAgent, uint dwAccessType, IntPtr lpszProxy, IntPtr lpszProxyBypass, uint dwFlags);
2026

@@ -129,8 +135,71 @@ private static void GetSystemProxy(ref INTERNET_PER_CONN_OPTION_LIST internetPer
129135
throw;
130136
}
131137
}
132-
133-
public static void Enable(string proxyServer) {
138+
139+
public static void GetPreferences(out bool proxy, out int port) {
140+
FlashpointSecurePlayerSection.FlashpointProxyElement flashpointProxyElement = null;
141+
142+
try {
143+
FlashpointSecurePlayerSection flashpointSecurePlayerSection = GetFlashpointSecurePlayerSection(false, ACTIVE_EXE_CONFIGURATION_NAME);
144+
flashpointProxyElement = flashpointSecurePlayerSection.FlashpointProxy;
145+
} catch {
146+
// fail silently
147+
}
148+
149+
bool? proxyFile = null;
150+
int? portFile = null;
151+
152+
if (flashpointProxyElement != null
153+
&& flashpointProxyElement.ElementInformation.IsPresent) {
154+
proxyFile = flashpointProxyElement.Proxy;
155+
portFile = flashpointProxyElement.Port;
156+
}
157+
158+
// try getting from the proxy element
159+
// if that fails, try from the environment variables
160+
// if that fails, use the default
161+
if (proxyFile == null) {
162+
string proxyEnvironmentVariable = null;
163+
164+
try {
165+
proxyEnvironmentVariable = Environment.GetEnvironmentVariable(FP_PROXY, EnvironmentVariableTarget.Process);
166+
} catch {
167+
// fail silently
168+
}
169+
170+
if (!bool.TryParse(proxyEnvironmentVariable, out proxy)) {
171+
proxy = FP_PROXY_DEFAULT;
172+
}
173+
} else {
174+
proxy = proxyFile.GetValueOrDefault();
175+
}
176+
177+
if (portFile == null) {
178+
string portEnvironmentVariable = null;
179+
180+
try {
181+
portEnvironmentVariable = Environment.GetEnvironmentVariable(FP_PROXY_PORT, EnvironmentVariableTarget.Process);
182+
} catch {
183+
// fail silently
184+
}
185+
186+
if (!int.TryParse(portEnvironmentVariable, out port)) {
187+
port = FP_PROXY_PORT_DEFAULT;
188+
}
189+
} else {
190+
port = portFile.GetValueOrDefault();
191+
}
192+
}
193+
194+
public static void Enable() {
195+
GetPreferences(out bool proxy, out int port);
196+
197+
if (!proxy) {
198+
return;
199+
}
200+
201+
string proxyServer = "http=127.0.0.1:" + port + ";https=127.0.0.1:" + port + ";ftp=127.0.0.1:" + port;
202+
134203
IntPtr internetHandle = InternetOpen(AGENT, INTERNET_OPEN_TYPE_DIRECT, IntPtr.Zero, IntPtr.Zero, 0);
135204

136205
if (internetHandle == IntPtr.Zero) {

FlashpointSecurePlayer/FlashpointSecurePlayerGUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ private async Task ActivateModeAsync(TemplateElement templateElement, ErrorDeleg
708708

709709
try {
710710
// StartProcessCreateBreakawayFromJob required for Process Sync on Windows 7
711-
StartProcessCreateBreakawayFromJob(softwareProcessStartInfo, out softwareProcess);
711+
softwareProcess = StartProcessCreateBreakawayFromJob(softwareProcessStartInfo);
712712
} catch (JobObjectException ex) {
713713
// popup message box and blow up
714714
LogExceptionToLauncher(ex);

FlashpointSecurePlayer/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("2.2.3.0")]
37-
[assembly: AssemblyFileVersion("2.2.3.0")]
36+
[assembly: AssemblyVersion("2.2.4.0")]
37+
[assembly: AssemblyFileVersion("2.2.4.0")]
3838
[assembly: NeutralResourcesLanguage("en")]
3939

FlashpointSecurePlayer/Shared.cs

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -958,12 +958,32 @@ public enum MODIFICATIONS_REVERT_METHOD {
958958

959959
// there should be only one HTTP Client per application
960960
// (as of right now though this is exclusively used by DownloadsBefore class)
961-
private static readonly HttpClientHandler httpClientHandler = new HttpClientHandler {
962-
Proxy = new WebProxy("127.0.0.1", 22500),
963-
UseProxy = true
964-
};
961+
private static HttpClientHandler httpClientHandler = null;
962+
963+
private static HttpClientHandler HttpClientHandler {
964+
get {
965+
if (httpClientHandler == null) {
966+
FlashpointProxy.GetPreferences(out bool proxy, out int port);
967+
968+
httpClientHandler = new HttpClientHandler {
969+
Proxy = proxy ? new WebProxy("127.0.0.1", port) : null,
970+
UseProxy = proxy
971+
};
972+
}
973+
return httpClientHandler;
974+
}
975+
}
976+
977+
private static HttpClient httpClient = null;
965978

966-
public static readonly HttpClient httpClient = new HttpClient(httpClientHandler);
979+
public static HttpClient HttpClient {
980+
get {
981+
if (httpClient == null) {
982+
httpClient = new HttpClient(HttpClientHandler);
983+
}
984+
return httpClient;
985+
}
986+
}
967987

968988
// no parallel downloads
969989
// if parallel downloads are ever supported, the max value should
@@ -1264,7 +1284,7 @@ public ModeElement Mode {
12641284
if (String.IsNullOrEmpty(Name) || _mode == null) {
12651285
return null;
12661286
}
1267-
return base[_mode] as ModeElement;
1287+
return (ModeElement)base[_mode];
12681288
}
12691289

12701290
set {
@@ -1400,7 +1420,7 @@ protected override ConfigurationPropertyCollection Properties {
14001420
}
14011421

14021422
protected override object GetElementKey(ConfigurationElement configurationElement) {
1403-
EnvironmentVariablesElement environmentVariablesElement = configurationElement as EnvironmentVariablesElement;
1423+
EnvironmentVariablesElement environmentVariablesElement = (EnvironmentVariablesElement)configurationElement;
14041424
return environmentVariablesElement._Key;
14051425
}
14061426

@@ -1444,7 +1464,7 @@ public string Name {
14441464
}
14451465

14461466
protected override object GetElementKey(ConfigurationElement configurationElement) {
1447-
DownloadBeforeElement downloadBeforeElement = configurationElement as DownloadBeforeElement;
1467+
DownloadBeforeElement downloadBeforeElement = (DownloadBeforeElement)configurationElement;
14481468
return downloadBeforeElement.Name;
14491469
}
14501470

@@ -1653,7 +1673,7 @@ protected override ConfigurationPropertyCollection Properties {
16531673
}
16541674

16551675
protected override object GetElementKey(ConfigurationElement configurationElement) {
1656-
RegistryStateElement registryStateElement = configurationElement as RegistryStateElement;
1676+
RegistryStateElement registryStateElement = (RegistryStateElement)configurationElement;
16571677
return registryStateElement.Name;
16581678
}
16591679

@@ -1848,10 +1868,10 @@ public OldCPUSimulatorElement OldCPUSimulator {
18481868
}
18491869
}
18501870
}
1851-
1871+
18521872
public ModificationsElement Modifications {
18531873
get {
1854-
return base[_modifications] as ModificationsElement;
1874+
return (ModificationsElement)base[_modifications];
18551875
}
18561876

18571877
set {
@@ -1867,7 +1887,7 @@ protected override ConfigurationPropertyCollection Properties {
18671887
}
18681888

18691889
protected override object GetElementKey(ConfigurationElement configurationElement) {
1870-
TemplateElement templateElement = configurationElement as TemplateElement;
1890+
TemplateElement templateElement = (TemplateElement)configurationElement;
18711891
return templateElement.Name;
18721892
}
18731893

@@ -1884,6 +1904,22 @@ public override void Remove(string name) {
18841904
}
18851905
}
18861906

1907+
public class FlashpointProxyElement : ConfigurationElement {
1908+
[ConfigurationProperty("proxy", IsRequired = false)]
1909+
public bool? Proxy {
1910+
get {
1911+
return base["proxy"] as bool?;
1912+
}
1913+
}
1914+
1915+
[ConfigurationProperty("port", IsRequired = false)]
1916+
public int? Port {
1917+
get {
1918+
return base["port"] as int?;
1919+
}
1920+
}
1921+
}
1922+
18871923
[ConfigurationProperty("templates", IsDefaultCollection = true, IsRequired = true)]
18881924
[ConfigurationCollection(typeof(TemplatesElementCollection), AddItemName = "template")]
18891925
public TemplatesElementCollection Templates {
@@ -1895,6 +1931,13 @@ public TemplatesElementCollection Templates {
18951931
base["templates"] = value;
18961932
}
18971933
}
1934+
1935+
[ConfigurationProperty("flashpointProxy", IsDefaultCollection = false, IsRequired = false)]
1936+
public FlashpointProxyElement FlashpointProxy {
1937+
get {
1938+
return (FlashpointProxyElement)base["flashpointProxy"];
1939+
}
1940+
}
18981941
}
18991942

19001943
public const string ACTIVE_EXE_CONFIGURATION_NAME = "";
@@ -2227,7 +2270,7 @@ public static async Task<Uri> DownloadAsync(string name) {
22272270
await downloadSemaphoreSlim.WaitAsync().ConfigureAwait(true);
22282271

22292272
try {
2230-
using (HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(name, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(true)) {
2273+
using (HttpResponseMessage httpResponseMessage = await HttpClient.GetAsync(name, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(true)) {
22312274
if (!httpResponseMessage.IsSuccessStatusCode) {
22322275
throw new Exceptions.DownloadFailedException("The HTTP Response Message failed with a Status Code of \"" + httpResponseMessage.StatusCode + "\".");
22332276
}
@@ -2510,8 +2553,11 @@ public static async Task DownloadFlashpointSecurePlayerSectionAsync(string name)
25102553
public static FlashpointSecurePlayerSection.TemplatesElementCollection.TemplateElement GetTemplateElement(bool createTemplateElement, string exeConfigurationName) {
25112554
// need the section to operate on
25122555
FlashpointSecurePlayerSection flashpointSecurePlayerSection = GetFlashpointSecurePlayerSection(createTemplateElement, exeConfigurationName);
2556+
25132557
// get the element
2514-
FlashpointSecurePlayerSection.TemplatesElementCollection.TemplateElement templateElement = flashpointSecurePlayerSection.Templates.Get(exeConfigurationName) as FlashpointSecurePlayerSection.TemplatesElementCollection.TemplateElement;
2558+
FlashpointSecurePlayerSection.TemplatesElementCollection.TemplateElement templateElement
2559+
= flashpointSecurePlayerSection.Templates.Get(exeConfigurationName)
2560+
as FlashpointSecurePlayerSection.TemplatesElementCollection.TemplateElement;
25152561

25162562
// create it if it doesn't exist...
25172563
if (createTemplateElement && templateElement == null) {
@@ -2614,9 +2660,9 @@ public static StringBuilder GetCreateProcessCommandLine(ProcessStartInfo process
26142660
return commandLine;
26152661
}
26162662

2617-
public static void StartProcessCreateBreakawayFromJob(ProcessStartInfo processStartInfo, out Process process) {
2618-
process = null;
2619-
2663+
public static Process StartProcessCreateBreakawayFromJob(ProcessStartInfo processStartInfo) {
2664+
Process process = null;
2665+
26202666
if (processStartInfo == null) {
26212667
throw new ArgumentNullException("The processStartInfo is null.");
26222668
}
@@ -2651,7 +2697,7 @@ public static void StartProcessCreateBreakawayFromJob(ProcessStartInfo processSt
26512697

26522698
if (!CreateProcess(null, commandLine, IntPtr.Zero, IntPtr.Zero, false, creationFlags, IntPtr.Zero, currentDirectory, ref startupInfo, out processInformation)) {
26532699
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
2654-
return;
2700+
return process;
26552701
}
26562702

26572703
try {
@@ -2666,7 +2712,7 @@ public static void StartProcessCreateBreakawayFromJob(ProcessStartInfo processSt
26662712

26672713
if (ResumeThread(processInformation.hThread) == -1) {
26682714
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
2669-
return;
2715+
return process;
26702716
}
26712717
} catch {
26722718
process.Kill();
@@ -2683,6 +2729,7 @@ public static void StartProcessCreateBreakawayFromJob(ProcessStartInfo processSt
26832729
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
26842730
}
26852731
}
2732+
return process;
26862733
}
26872734

26882735
public static BINARY_TYPE GetLibraryBinaryType(string libFileName) {

FlashpointSecurePlayer/WebBrowserMode.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ private void WebBrowserMode_Load(object sender, EventArgs e) {
567567
}
568568

569569
try {
570-
FlashpointProxy.Enable("http=127.0.0.1:22500;https=127.0.0.1:22500;ftp=127.0.0.1:22500");
570+
FlashpointProxy.Enable();
571571
} catch (FlashpointProxyException ex) {
572572
// popup message box but allow through anyway
573573
LogExceptionToLauncher(ex);

0 commit comments

Comments
 (0)