8
8
using System . Security . Principal ;
9
9
using Newtonsoft . Json . Linq ;
10
10
11
+
12
+ using System . Collections . Generic ;
13
+ using System . Net . Http ;
14
+ using System . Threading . Tasks ;
15
+ using Newtonsoft . Json ;
16
+
11
17
namespace BrowserStack
12
18
{
13
19
public enum LocalState { Idle , Connecting , Connected , Error , Disconnected } ;
@@ -16,7 +22,11 @@ public class BrowserStackTunnel : IDisposable
16
22
{
17
23
static readonly string uname = Util . GetUName ( ) ;
18
24
static readonly string binaryName = GetBinaryName ( ) ;
19
- static readonly string downloadURL = "https://www.browserstack.com/local-testing/downloads/binaries/" + binaryName ;
25
+
26
+ static readonly string userAgent = "" ;
27
+ static readonly string sourceUrl = null ;
28
+ private bool isFallbackEnabled = false ;
29
+ private Exception downloadFailureException = null ;
20
30
21
31
static readonly string homepath = ! IsWindows ( ) ?
22
32
Environment . GetFolderPath ( Environment . SpecialFolder . Personal ) :
@@ -41,7 +51,7 @@ static bool IsDarwin(string osName)
41
51
{
42
52
return osName . Contains ( "darwin" ) ;
43
53
}
44
-
54
+
45
55
46
56
static bool IsWindows ( )
47
57
{
@@ -102,8 +112,9 @@ public virtual void addBinaryArguments(string binaryArguments)
102
112
this . binaryArguments = binaryArguments ;
103
113
}
104
114
105
- public BrowserStackTunnel ( )
115
+ public BrowserStackTunnel ( string userAgent )
106
116
{
117
+ userAgent = userAgent ;
107
118
localState = LocalState . Idle ;
108
119
output = new StringBuilder ( ) ;
109
120
}
@@ -121,7 +132,7 @@ public virtual void fallbackPaths()
121
132
public void modifyBinaryPermission ( )
122
133
{
123
134
if ( ! IsWindows ( ) )
124
- {
135
+ {
125
136
try
126
137
{
127
138
using ( Process proc = Process . Start ( "/bin/bash" , $ "-c \" chmod 0755 { this . binaryAbsolute } \" ") )
@@ -143,16 +154,59 @@ public void modifyBinaryPermission()
143
154
}
144
155
}
145
156
146
- public void downloadBinary ( )
157
+ private string fetchSourceUrl ( string accessKey )
158
+ {
159
+ var url = "https://local.browserstack.com/binary/api/v1/endpoint" ;
160
+
161
+ using ( var client = new HttpClient ( ) )
162
+ {
163
+ var data = new Dictionary < string , object >
164
+ {
165
+ { "auth_token" , accessKey }
166
+ } ;
167
+ client . DefaultRequestHeaders . Add ( "Content-Type" , "application/json" ) ;
168
+ client . DefaultRequestHeaders . Add ( "user-agent" , userAgent ) ;
169
+
170
+ if ( isFallbackEnabled )
171
+ {
172
+ data [ "error_message" ] = downloadFailureException . Message ;
173
+ client . DefaultRequestHeaders . Add ( "X-Local-Fallback-Cloudflare" , "true" ) ;
174
+ }
175
+
176
+ string jsonData = JsonConvert . SerializeObject ( data ) ;
177
+ var content = new StringContent ( jsonData , Encoding . UTF8 , "application/json" ) ;
178
+
179
+ HttpResponseMessage response = await client . PostAsync ( url , content ) ;
180
+
181
+ response . EnsureSuccessStatusCode ( ) ;
182
+
183
+ string responseString = await response . Content . ReadAsStringAsync ( ) ;
184
+
185
+ dynamic jsonResponse = JsonConvert . DeserializeObject ( responseString ) ;
186
+
187
+ Console . WriteLine ( "Response JSON:" ) ;
188
+ Console . WriteLine ( jsonResponse ) ;
189
+
190
+ if ( jsonResponse . error != null )
191
+ {
192
+ throw new Exception ( jsonResponse . error ) ;
193
+ }
194
+ return jsonResponse . data . endpoint ;
195
+ }
196
+ }
197
+
198
+ public void downloadBinary ( string accessKey )
147
199
{
148
200
string binaryDirectory = Path . Combine ( this . binaryAbsolute , ".." ) ;
149
201
//string binaryAbsolute = Path.Combine(binaryDirectory, binaryName);
150
202
203
+ string sourceDownloadUrl = fetchSourceUrl ( accessKey ) ;
204
+
151
205
Directory . CreateDirectory ( binaryDirectory ) ;
152
206
153
207
using ( var client = new WebClient ( ) )
154
208
{
155
- client . DownloadFile ( downloadURL , this . binaryAbsolute ) ;
209
+ client . DownloadFile ( sourceDownloadUrl + "/" + binaryName , this . binaryAbsolute ) ;
156
210
}
157
211
158
212
if ( ! File . Exists ( binaryAbsolute ) )
@@ -163,7 +217,7 @@ public void downloadBinary()
163
217
modifyBinaryPermission ( ) ;
164
218
}
165
219
166
- public virtual void Run ( string accessKey , string folder , string logFilePath , string processType )
220
+ public virtual void Run ( string accessKey , string folder , string logFilePath , string processType , bool fallbackEnabled , Exception failureException )
167
221
{
168
222
string arguments = "-d " + processType + " " ;
169
223
if ( folder != null && folder . Trim ( ) . Length != 0 )
@@ -176,7 +230,9 @@ public virtual void Run(string accessKey, string folder, string logFilePath, str
176
230
}
177
231
if ( ! File . Exists ( binaryAbsolute ) )
178
232
{
179
- downloadBinary ( ) ;
233
+ isFallbackEnabled = fallbackEnabled ;
234
+ downloadFailureException = failureException ;
235
+ downloadBinary ( accessKey ) ;
180
236
}
181
237
182
238
if ( process != null )
0 commit comments