@@ -23,8 +23,8 @@ public class BrowserStackTunnel : IDisposable
2323 static readonly string uname = Util . GetUName ( ) ;
2424 static readonly string binaryName = GetBinaryName ( ) ;
2525
26- static readonly string userAgent = "" ;
27- static readonly string sourceUrl = null ;
26+ private static string userAgent = "" ;
27+ private string sourceUrl = null ;
2828 private bool isFallbackEnabled = false ;
2929 private Exception downloadFailureException = null ;
3030
@@ -94,8 +94,15 @@ static string GetBinaryName()
9494 return "BrowserStackLocal.exe" ;
9595 }
9696
97- public virtual void addBinaryPath ( string binaryAbsolute )
97+ public virtual void addBinaryPath ( string binaryAbsolute , string accessKey , bool fallbackEnabled = false , Exception failureException = null )
9898 {
99+ if ( basePathsIndex == - 1 )
100+ {
101+ /* Called at most twice (primary & a fallback) */
102+ isFallbackEnabled = fallbackEnabled ;
103+ downloadFailureException = failureException ;
104+ fetchSourceUrl ( accessKey ) ;
105+ }
99106 if ( binaryAbsolute == null || binaryAbsolute . Trim ( ) . Length == 0 )
100107 {
101108 binaryAbsolute = Path . Combine ( basePaths [ ++ basePathsIndex ] , binaryName ) ;
@@ -112,15 +119,19 @@ public virtual void addBinaryArguments(string binaryArguments)
112119 this . binaryArguments = binaryArguments ;
113120 }
114121
115- public BrowserStackTunnel ( string userAgent )
122+ public BrowserStackTunnel ( string userAgentParam )
116123 {
117- userAgent = userAgent ;
124+ userAgent = userAgentParam ;
118125 localState = LocalState . Idle ;
119126 output = new StringBuilder ( ) ;
120127 }
121128
122129 public virtual void fallbackPaths ( )
123130 {
131+ if ( File . Exists ( binaryAbsolute ) )
132+ {
133+ File . Delete ( binaryAbsolute ) ;
134+ }
124135 if ( basePathsIndex >= basePaths . Length - 1 )
125136 {
126137 throw new Exception ( "Binary not found or failed to launch. Make sure that BrowserStackLocal is not already running." ) ;
@@ -164,49 +175,48 @@ private string fetchSourceUrl(string accessKey)
164175 {
165176 { "auth_token" , accessKey }
166177 } ;
167- client . DefaultRequestHeaders . Add ( "Content-Type" , "application/json" ) ;
168- client . DefaultRequestHeaders . Add ( "user-agent" , userAgent ) ;
169178
170179 if ( isFallbackEnabled )
171180 {
172181 data [ "error_message" ] = downloadFailureException . Message ;
173- client . DefaultRequestHeaders . Add ( "X-Local-Fallback-Cloudflare" , "true" ) ;
174182 }
175183
176- string jsonData = JsonConvert . SerializeObject ( data ) ;
184+ var jsonData = JsonConvert . SerializeObject ( data ) ;
177185 var content = new StringContent ( jsonData , Encoding . UTF8 , "application/json" ) ;
178186
179- HttpResponseMessage response = await client . PostAsync ( url , content ) ;
187+ client . DefaultRequestHeaders . Add ( "User-Agent" , userAgent ) ;
188+ if ( isFallbackEnabled )
189+ {
190+ client . DefaultRequestHeaders . Add ( "X-Local-Fallback-Cloudflare" , "true" ) ;
191+ }
192+
193+ var response = client . PostAsync ( url , content ) . Result ;
180194
181195 response . EnsureSuccessStatusCode ( ) ;
182196
183- string responseString = await response . Content . ReadAsStringAsync ( ) ;
184-
185- dynamic jsonResponse = JsonConvert . DeserializeObject ( responseString ) ;
197+ var responseString = response . Content . ReadAsStringAsync ( ) . Result ;
186198
187- Console . WriteLine ( "Response JSON:" ) ;
188- Console . WriteLine ( jsonResponse ) ;
199+ var jsonResponse = JObject . Parse ( responseString ) ;
189200
190- if ( jsonResponse . error != null )
201+ if ( jsonResponse [ " error" ] != null )
191202 {
192- throw new Exception ( jsonResponse . error ) ;
203+ throw new Exception ( ( string ) jsonResponse [ " error" ] ) ;
193204 }
194- return jsonResponse . data . endpoint ;
205+
206+ sourceUrl = jsonResponse [ "data" ] ? [ "endpoint" ] ? . ToString ( ) ;
207+ return sourceUrl ;
195208 }
196209 }
197210
198211 public void downloadBinary ( string accessKey )
199212 {
200213 string binaryDirectory = Path . Combine ( this . binaryAbsolute , ".." ) ;
201- //string binaryAbsolute = Path.Combine(binaryDirectory, binaryName);
202-
203- string sourceDownloadUrl = fetchSourceUrl ( accessKey ) ;
204214
205215 Directory . CreateDirectory ( binaryDirectory ) ;
206216
207217 using ( var client = new WebClient ( ) )
208218 {
209- client . DownloadFile ( sourceDownloadUrl + "/" + binaryName , this . binaryAbsolute ) ;
219+ client . DownloadFile ( sourceUrl + "/" + binaryName , this . binaryAbsolute ) ;
210220 }
211221
212222 if ( ! File . Exists ( binaryAbsolute ) )
@@ -217,7 +227,7 @@ public void downloadBinary(string accessKey)
217227 modifyBinaryPermission ( ) ;
218228 }
219229
220- public virtual void Run ( string accessKey , string folder , string logFilePath , string processType , bool fallbackEnabled , Exception failureException )
230+ public virtual void Run ( string accessKey , string folder , string logFilePath , string processType )
221231 {
222232 string arguments = "-d " + processType + " " ;
223233 if ( folder != null && folder . Trim ( ) . Length != 0 )
@@ -230,8 +240,6 @@ public virtual void Run(string accessKey, string folder, string logFilePath, str
230240 }
231241 if ( ! File . Exists ( binaryAbsolute ) )
232242 {
233- isFallbackEnabled = fallbackEnabled ;
234- downloadFailureException = failureException ;
235243 downloadBinary ( accessKey ) ;
236244 }
237245
0 commit comments