Class: BlackStack::BrowserFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/browserfactory.rb

Constant Summary collapse

LOCKING_FILENAME =

manejo de concurrencia en la creación de browsers

BlackStack::MyProcess.macaddress
PROFILE_PID_LIST_FILENAME =

manejo de concurrencia en la creación de browsers

"./browserfactory.%PRFILE_NAME%.list"
PROFILE_PID_LOCK_FILENAME =

manejo de concurrencia en la creación de browsers

"./browserfactory.%PRFILE_NAME%.lock"
CHROME_EXTENSION_DIRECTORY =
File.absolute_path('./chrome_extension')
DEFAULT_LOAD_TIMEOUT =
360
DEFAULT_CHROMEDRIVER_PATH =
'./chromedriver.exe'
TYPE_PHANTOMJS =

discontinued!

0
TYPE_FIREFOX =
1
TYPE_CHROME =
2
TYPE_MIMIC =
3
@@fd =
File.open(LOCKING_FILENAME,"w")
@@fd_profile =
nil
@@driver =
nil
@@browser =
nil
@@pid =
nil
@@profile_name =
nil
@@type =
nil
@@arAgents =

NOTA: esta lista debe estar permitida por LinkedIn. De caso contrario, aparecera el mensaje “Upgrad your browser” Se puede obtener una lista actualizada de este sitio: techblog.willshouse.com/2012/01/03/most-common-user-agents/

[
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",  
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
"Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063", 
]

Class Method Summary collapse

Class Method Details

.addPidToProfileList(profile_name, pid) ⇒ Object



256
257
258
259
260
261
# File 'lib/browserfactory.rb', line 256

def self.addPidToProfileList(profile_name, pid)
  fname = PROFILE_PID_LIST_FILENAME.gsub('%PRFILE_NAME%', profile_name)
  f = File.open(fname, 'a')
  f.write("#{pid},")
  f.close
end

.agentsObject



339
340
341
# File 'lib/browserfactory.rb', line 339

def self.agents()
  return @@arAgents
end

.browserObject



302
303
304
# File 'lib/browserfactory.rb', line 302

def self.browser()
  @@browser
end

.chrome(h) ⇒ Object



439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/browserfactory.rb', line 439

def self.chrome(h)
  # TODO: check if h is a hash

  # TODO: check the variable type of each param

  # TODO: check mandatory params 

  self.launch_chrome(
    h[:proxy], 
    h[:load_timeout].nil? ? DEFAULT_LOAD_TIMEOUT : h[:load_timeout], 
    h[:user_agent],
    h[:profile_name],
    h[:chromedriver_path],
    h[:chromedriver_signature]
  )
end

.chrome_versionObject



422
423
424
425
426
427
# File 'lib/browserfactory.rb', line 422

def self.chrome_version
  res = `reg query \"HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon\" /v version`.scan(/REG_SZ    (.*)$/).first
  return nil if res.nil?
  return res[0] if res.size > 0
  return nil
end

.chromedriver_version(filename = nil) ⇒ Object



430
431
432
433
434
435
436
# File 'lib/browserfactory.rb', line 430

def self.chromedriver_version(filename=nil)
  res = `chromedriver -v`.scan(/ChromeDriver\s(.*)\s\(/).first if filename.nil?
  res = `#{filename} -v`.scan(/ChromeDriver\s(.*)\s\(/).first if !filename.nil?
  return nil if res.nil?
  return res[0] if res.size > 0
  return nil
end

.destroyObject



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/browserfactory.rb', line 380

def self.destroy()
  if @@type == BlackStack::BrowserFactory::TYPE_CHROME
    self.lockProfileList()
    self.readPidToProfileList(@@profile_name).each { |pid|
      MyProcess.kill(pid)
      self.removePidToProfileList(@@profile_name, pid)      
    }
    self.releaseProfileList()
    begin
      @@browser.close if !@@browser.nil?
    rescue => e
      # 

    end
  elsif @@type == BlackStack::BrowserFactory::TYPE_MIMIC
    @@browser.close if !@@browser.nil?
    begin
      BlackStack::BrowserFactory.mimic(@@profile_name)
    rescue
      # nothing here

    end
    i = 0
    max = 3
    success = false
    while i<max && !success
      i+=1
      url = "http://127.0.0.1:#{BlackStack::StealthBrowserAutomation::Multilogin::mla_local_port}/api/v1/profile/stop?profileId=#{@@profile_name}"
      uri = URI.parse(url)
      body = Net::HTTP.get(uri)
      res = JSON.parse(body)
      success = true if res['status'].to_s == 'OK'
    end # while i<max

    raise 'Error requesting Mimic to stop (tried #{i.to_s} times)' if !success
  end
  # reseteo las variables    

  @@browser = nil
  @@pid = nil
  @@profile_name = nil
  @@driver = nil
  @@type = nil
end

.getPidObject



343
344
345
# File 'lib/browserfactory.rb', line 343

def self.getPid()
  return @@pid
end

.isCreatedObject



347
348
349
350
351
352
353
354
355
356
# File 'lib/browserfactory.rb', line 347

def self.isCreated()
  if (self.readPidToProfileList(@@profile_name).size>0)
    return true
  end
  if (@@browser != nil)
    return true
  end
  
  return false
end

.isCreated?Boolean

Returns:

  • (Boolean)


358
359
360
# File 'lib/browserfactory.rb', line 358

def self.isCreated?
  self.isCreated()
end

.launch_chrome(proxy = nil, load_timeout = DEFAULT_LOAD_TIMEOUT, user_agent = nil, profile_name = nil, chromedriver_path = nil, chromedriver_signature = nil) ⇒ Object



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/browserfactory.rb', line 524

def self.launch_chrome(proxy=nil, load_timeout=DEFAULT_LOAD_TIMEOUT, user_agent=nil, profile_name=nil, chromedriver_path=nil, chromedriver_signature=nil)
  #

  @@browser = nil
  @@driver = nil
  @@type = BlackStack::BrowserFactory::TYPE_CHROME
  @@profile_name = profile_name

  #

  fname = PROFILE_PID_LOCK_FILENAME.gsub('%PRFILE_NAME%', @@profile_name)
  @@fd_profile = File.open(fname,"w")
  
  # 

  agent = @@arAgents[0] if user_agent.nil?
  agent = user_agent if !user_agent.nil?
  
  # #959 - valido que el parametro agents sea un array - porque si se pasa un string se lo procesa como array y se toma un UA name de un char.

  #raise "Array expected in parameter agents in BrowserFactory.create." if !(agent.is_a? String) 

  
  # 

  while (@@browser == nil)            
    
    # Levantar el flag de reserva a mi favor

    self.lock()
    
    begin
      client = Selenium::WebDriver::Remote::Http::Default.new
      begin
        client.read_timeout = load_timeout # for newest selenium webdriver version 

      rescue => e
        client.timeout = load_timeout # deprecated in newest selenium webdriver version

      end
  
      # se agregan extensiones por el issue #1171

      #

      # UPDATE: Las extensiones en JavaScript para eliminar rastros, dejan rastros en si mismas.

      # 

      switches = ["lang=en", "--log-level=3", "--user-agent=#{agent}", "disable-infobars", "load-extension=#{File.absolute_path('./chrome_extension')}"]
  
      # 

      if (profile_name!=nil)
            
        # issue #964

        filename = "chrome_profiles/#{profile_name}/#{profile_name}/Default/Preferences"
        if ( File.file?(filename) == true )
          File.delete(filename)
        end
  
        # issue #964

        filename = "chrome_profiles/#{profile_name}/#{profile_name}/Local State"
        if ( File.file?(filename) == true )
          File.delete(filename)
        end
            
        # configuro el browser para que use este perfil

        switches += ["--user-data-dir=chrome_profiles/#{profile_name}/#{profile_name}"]
            
      end
          
      if (proxy!=nil)
        switches += proxy.chrome_switches
      end
  
      self.lockProfileList()
  
      begin

        Selenium::WebDriver::Chrome.driver_path = chromedriver_path if !chromedriver_path.nil?
        Selenium::WebDriver::Chrome.driver_path = DEFAULT_CHROMEDRIVER_PATH if chromedriver_path.nil?
        
        @@driver = Selenium::WebDriver.for :chrome, :switches => switches
        bridge = @@driver.instance_variable_get(:@bridge)
        service = bridge.instance_variable_get(:@service)
        process = service.instance_variable_get(:@process)
        @@pid = process.pid.to_s # es el PID del chromedriver

        @@driver.manage.window().maximize()
        @@browser = PampaBrowser.new(@@driver)
        @@browser.proxy = proxy.clone if !proxy.nil?    
        @@browser.agent_name = agent
        @@browser.profile_name = profile_name
        self.updateProfileList              
      rescue => e
        p1 = PROCESS.list().select { |p| p[:ppid] == PROCESS.pid && p[:executablepath] =~ /chromedriver(.*)\.exe/ }.first
        if (p1 != nil)
          @@pid = p1[:pid]
          self.updateProfileList              
        end              
        self.releaseProfileList()
        raise e
      end
      self.releaseProfileList()
        
      # Liberar el flag de reserva de creacion del browser

      self.release()
  
    # 

    rescue => e # TODO: Se atrapa una expecion porque sigue ocurreiendo el error reportado en el issue #134 y #129

      # Liberar el flag de reserva de creacion del browser

      self.release()
  
      # disparar la exepcion

      raise e          
    end                  
  end # while

  
  #

  @@browser
end

.lockObject



372
373
374
# File 'lib/browserfactory.rb', line 372

def self.lock()
  @@fd.flock(File::LOCK_EX)
end

.lockProfileListObject



306
307
308
# File 'lib/browserfactory.rb', line 306

def self.lockProfileList()
  #    @@fd_profile.flock(File::LOCK_EX)

end

.mimic(mimic_profile_id) ⇒ Object



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/browserfactory.rb', line 453

def self.mimic(mimic_profile_id)

  # Levantar el flag de reserva a mi favor

  self.lock()

  begin  
    @@type = BlackStack::BrowserFactory::TYPE_MIMIC
    @@profile_name = mimic_profile_id

    #self.lockProfileList()


    url_2 = nil
    i = 0
    max = 3
    while (i<max && url_2.nil?)
      i += 1

      url = "http://127.0.0.1:#{BlackStack::StealthBrowserAutomation::Multilogin::mla_local_port}/api/v1/profile/start?automation=true&profileId=#{@@profile_name}"
      uri = URI.parse(url)
      body = Net::HTTP.get(uri)
      res = JSON.parse(body)

      if !res.has_key?('status')
        if i>=max
          self.release() # libero el flag de reserva de turno para crar un browser

          raise "Error requesting Mimic to start (try #{i.to_s}): #{res.to_s}"
        end
      elsif res['status'] != 'OK'
        if i>=max
          self.release() # libero el flag de reserva de turno para crar un browser

          raise "Error requesting Mimic to start (try #{i.to_s}): #{res.to_s}"
        end
      else
        url_2 = res['value']
      end # if !res.has_key?('status')

    end # while

  
    if !url_2.nil?
      #uri_2 = URI.parse(url_2)

      #req_2 = Net::HTTP::Get.new(url_2)

      #res_2 = Net::HTTP.start(uri_2.host, uri_2.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) {|http|

      #  http.request(req_2)

      #}        

      @@driver = Selenium::WebDriver.for(:remote, :url => url_2)
      #bridge = @@driver.instance_variable_get(:@bridge)

      #service = bridge.instance_variable_get(:@service)

      #process = service.instance_variable_get(:@process)

      #@@pid = process.pid.to_s # es el PID del chromedriver

      @@driver.manage.window().maximize()
      @@browser = PampaBrowser.new(@@driver) # Watir::Browser.new(@@driver)

    end
  rescue => e
    # 

    #self.releaseProfileList()              

    # libero el flag de reserva de turno para crar un browser

    self.release() 
    # 

    raise e
  end

  # 

  #self.releaseProfileList()              


  # libero el flag de reserva de turno para crar un browser

  self.release() 

  # 

  @@browser
end

.readPidToProfileList(profile_name) ⇒ Object



272
273
274
275
276
277
278
279
280
# File 'lib/browserfactory.rb', line 272

def self.readPidToProfileList(profile_name)
  if (profile_name != nil)
    fname = PROFILE_PID_LIST_FILENAME.gsub('%PRFILE_NAME%', profile_name)
    s = File.read(fname)
    return s.split(",")
  else
    return []
  end
end

.releaseObject



376
377
378
# File 'lib/browserfactory.rb', line 376

def self.release()
  @@fd.flock(File::LOCK_UN)
end

.releaseProfileListObject



310
311
312
313
314
315
316
317
318
319
# File 'lib/browserfactory.rb', line 310

def self.releaseProfileList()
  #    @@fd_profile.flock(File::LOCK_UN)

=begin
  begin
    @@fd_profile.close
  rescue
  end
  @@fd_profile = nil
=end

end

.removePidToProfileList(profile_name, pid) ⇒ Object



263
264
265
266
267
268
269
270
# File 'lib/browserfactory.rb', line 263

def self.removePidToProfileList(profile_name, pid)
  fname = PROFILE_PID_LIST_FILENAME.gsub('%PRFILE_NAME%', profile_name)
  s = File.read(fname)
  s = s.gsub("#{pid},", "")
  f = File.open(fname, 'w')
  f.write(s)
  f.close
end

.reservation_idObject



362
363
364
365
366
367
368
369
370
# File 'lib/browserfactory.rb', line 362

def self.reservation_id()
  q = "SELECT TOP 1 reservation_id FROM browserfactory WHERE host_name='#{Socket.gethostname}'"
  row = DB[q].first
  if (row == nil)
    DB.execute("INSERT INTO browserfactory (reservation_id, reservation_time, host_name) VALUES (NULL, NULL, '#{Socket.gethostname}')")
  end
  row = DB[q].first
  return row[:reservation_id]
end

.screenshot(filename) ⇒ Object

toma una captura del area visible de la pagina mas informacion: stackoverflow.com/questions/25543651/screenshot-of-entire-webpage-using-selenium-webdriver-rc-rails



335
336
337
# File 'lib/browserfactory.rb', line 335

def self.screenshot(filename)
  @@driver.save_screenshot(filename)
end

.updateProfileListObject



282
283
284
285
286
287
# File 'lib/browserfactory.rb', line 282

def self.updateProfileList()
  addPidToProfileList(@@profile_name, @@pid)
  PROCESS.list().select { |p| p[:ppid] == @@pid && (p[:executablepath] =~ /chrome\.exe/ || p[:executablepath] =~ /mimic/) }.each { |p2|
    addPidToProfileList(@@profile_name, p2[:pid])                  
  }
end