Skip to content

Commit 2f5a383

Browse files
committed
update: keep compatibility in mind
1 parent 3ebbd23 commit 2f5a383

File tree

3 files changed

+68
-19
lines changed

3 files changed

+68
-19
lines changed

BrowserStackLocal/BrowserStackLocal/BrowserStackLocal.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<RootNamespace>BrowserStackRootNamespace>
44
<AssemblyName>BrowserStackLocalAssemblyName>
5-
<TargetFrameworks>net48;netstandard2.0TargetFrameworks>
5+
<TargetFrameworks>net20;netstandard2.0TargetFrameworks>
66
<GenerateAssemblyInfo>falseGenerateAssemblyInfo>
77
<Title>BrowserStackLocalTitle>
88
<Product>BrowserStackLocalProduct>

BrowserStackLocal/BrowserStackLocal/BrowserStackTunnel.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
using System.Security.AccessControl;
88
using System.Security.Principal;
99
using Newtonsoft.Json.Linq;
10-
using System.Runtime.InteropServices;
1110

1211
namespace BrowserStack
1312
{
1413
public enum LocalState { Idle, Connecting, Connected, Error, Disconnected };
1514

1615
public class BrowserStackTunnel : IDisposable
1716
{
18-
static string binaryName = GetBinaryName();
17+
static readonly string uname = Util.GetUName();
18+
static readonly string binaryName = GetBinaryName();
1919
static readonly string downloadURL = "https://www.browserstack.com/local-testing/downloads/binaries/" + binaryName;
20-
static readonly string homepath = IsDarwin() || IsLinux() ?
20+
21+
static readonly string homepath = !IsWindows() ?
2122
Environment.GetFolderPath(Environment.SpecialFolder.Personal) :
2223
Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");
2324
public static readonly string[] basePaths = new string[] {
@@ -36,9 +37,9 @@ public class BrowserStackTunnel : IDisposable
3637

3738
Process process = null;
3839

39-
static bool IsDarwin()
40+
static bool IsDarwin(string osName)
4041
{
41-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
42+
if(osName.Contains("darwin"))
4243
{
4344
return true;
4445
}
@@ -48,16 +49,16 @@ static bool IsDarwin()
4849

4950
static bool IsWindows()
5051
{
51-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
52+
if (Environment.OSVersion.VersionString?.ToLower().Contains("windows") ?? false)
5253
{
5354
return true;
5455
}
5556
return false;
5657
}
5758

58-
static bool IsLinux()
59+
static bool IsLinux(string osName)
5960
{
60-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
61+
if(osName.Contains("linux"))
6162
{
6263
return true;
6364
}
@@ -69,7 +70,7 @@ static bool IsAlpine()
6970
try
7071
{
7172
string[] output = Util.RunShellCommand("grep -w 'NAME' /etc/os-release");
72-
if (string.IsNullOrEmpty(output[0]) && output[0].Contains("Alpine"))
73+
if (output[0]?.ToLower()?.Contains("alpine") ?? false)
7374
{
7475
return true;
7576
}
@@ -83,17 +84,17 @@ static bool IsAlpine()
8384

8485
static string GetBinaryName()
8586
{
86-
if (IsDarwin())
87+
if (IsWindows())
8788
{
88-
return "BrowserStackLocal-darwin-x64";
89+
return "BrowserStackLocal.exe";
8990
}
90-
else if (IsWindows())
91+
else if (IsDarwin(uname))
9192
{
92-
return "BrowserStackLocal.exe";
93+
return "BrowserStackLocal-darwin-x64";
9394
}
94-
else if (IsLinux())
95+
else if (IsLinux(uname))
9596
{
96-
if (Environment.Is64BitOperatingSystem)
97+
if (Util.Is64BitOS())
9798
{
9899
if (IsAlpine())
99100
{
@@ -148,7 +149,7 @@ public virtual void fallbackPaths()
148149

149150
public void modifyBinaryPermission()
150151
{
151-
if (IsDarwin() || IsLinux())
152+
if (!IsWindows())
152153
{
153154
try
154155
{

BrowserStackLocal/BrowserStackLocal/Util.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
using System;
12
using System.Diagnostics;
3+
using System.Runtime.InteropServices;
24

35
namespace BrowserStack
46
{
57
public class Util {
68

9+
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
10+
[return: MarshalAs(UnmanagedType.Bool)]
11+
private static extern bool IsWow64Process(
12+
[In] IntPtr hProcess,
13+
[Out] out bool wow64Process
14+
);
15+
716
// Only Unix Support
8-
public static string[] RunShellCommand(string command) {
17+
public static string[] RunShellCommand(string command)
18+
{
919
ProcessStartInfo psi = new ProcessStartInfo("bash", $"-c \"{command}\"") {
1020
RedirectStandardOutput = true,
1121
RedirectStandardError = true,
@@ -20,7 +30,45 @@ public static string[] RunShellCommand(string command) {
2030
process.WaitForExit();
2131
return new string[]{output, error};
2232
}
23-
33+
34+
public static string GetUName()
35+
{
36+
string osName = "";
37+
try
38+
{
39+
string[] output = RunShellCommand("uname");
40+
osName = output[0]?.ToLower();
41+
}
42+
catch (System.Exception) {}
43+
return osName;
44+
}
45+
46+
public static bool Is64BitOS()
47+
{
48+
bool is64BitProcess = IntPtr.Size == 8;
49+
return is64BitProcess || InternalCheckIsWow64();
50+
}
51+
52+
public static bool InternalCheckIsWow64()
53+
{
54+
if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
55+
Environment.OSVersion.Version.Major >= 6)
56+
{
57+
using (Process p = Process.GetCurrentProcess())
58+
{
59+
bool retVal;
60+
if (!IsWow64Process(p.Handle, out retVal))
61+
{
62+
return false;
63+
}
64+
return retVal;
65+
}
66+
}
67+
else
68+
{
69+
return false;
70+
}
71+
}
2472
}
2573
}
2674

0 commit comments

Comments
 (0)