Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 8311e6b

Browse files
committed
#522 fix Uwp startup
1 parent 2a57f75 commit 8311e6b

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/Titanium.Web.Proxy/Helpers/RunTime.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
24
#if NETSTANDARD2_0
35
using System.Runtime.InteropServices;
46
#endif
@@ -53,12 +55,51 @@ public static class RunTime
5355
#else
5456
public static bool IsWindows => !IsLinux && !IsMac;
5557
#endif
58+
public static bool IsUwpOnWindows => IsWindows && UwpHelper.IsRunningAsUwp();
5659

5760
#if NETSTANDARD2_0
5861
public static bool IsMac => isRunningOnMac;
5962
#else
6063
public static bool IsMac => isRunningOnMonoMac.Value;
6164
#endif
6265

66+
//https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs
67+
private class UwpHelper
68+
{
69+
const long APPMODEL_ERROR_NO_PACKAGE = 15700L;
70+
71+
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
72+
static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName);
73+
74+
internal static bool IsRunningAsUwp()
75+
{
76+
if (IsWindows7OrLower)
77+
{
78+
return false;
79+
}
80+
else
81+
{
82+
int length = 0;
83+
var sb = new StringBuilder(0);
84+
int result = GetCurrentPackageFullName(ref length, sb);
85+
86+
sb = new StringBuilder(length);
87+
result = GetCurrentPackageFullName(ref length, sb);
88+
89+
return result != APPMODEL_ERROR_NO_PACKAGE;
90+
}
91+
}
92+
93+
private static bool IsWindows7OrLower
94+
{
95+
get
96+
{
97+
int versionMajor = Environment.OSVersion.Version.Major;
98+
int versionMinor = Environment.OSVersion.Version.Minor;
99+
double version = versionMajor + (double)versionMinor / 10;
100+
return version <= 6.1;
101+
}
102+
}
103+
}
63104
}
64105
}

src/Titanium.Web.Proxy/ProxyServer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public ProxyServer(string rootCertificateName, string rootCertificateIssuerName,
102102

103103
ProxyEndPoints = new List<ProxyEndPoint>();
104104
tcpConnectionFactory = new TcpConnectionFactory(this);
105-
if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
105+
if (!RunTime.IsRunningOnMono && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
106106
{
107107
systemProxySettingsManager = new SystemProxyManager();
108108
}
@@ -542,7 +542,7 @@ public void Start()
542542

543543
// clear any system proxy settings which is pointing to our own endpoint (causing a cycle)
544544
// due to ungracious proxy shutdown before or something else
545-
if (systemProxySettingsManager != null && RunTime.IsWindows)
545+
if (systemProxySettingsManager != null && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
546546
{
547547
var proxyInfo = systemProxySettingsManager.GetProxyInfoFromRegistry();
548548
if (proxyInfo.Proxies != null)
@@ -593,7 +593,7 @@ public void Stop()
593593
throw new Exception("Proxy is not running.");
594594
}
595595

596-
if (!RunTime.IsRunningOnMono && RunTime.IsWindows)
596+
if (!RunTime.IsRunningOnMono && RunTime.IsWindows && !RunTime.IsUwpOnWindows)
597597
{
598598
bool setAsSystemProxy = ProxyEndPoints.OfType<ExplicitProxyEndPoint>()
599599
.Any(x => x.IsSystemHttpProxy || x.IsSystemHttpsProxy);

src/Titanium.Web.Proxy/RequestHandler.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace Titanium.Web.Proxy
2525
/// </summary>
2626
public partial class ProxyServer
2727
{
28-
2928
private bool isWindowsAuthenticationEnabledAndSupported =>
3029
EnableWinAuth && RunTime.IsWindows && !RunTime.IsRunningOnMono;
3130

0 commit comments

Comments
 (0)