2

I am trying to use RSelenium with firefox using a local proxy (Tor) on a linux machine.

I had no problem in installing Tor following this tuto, and the command line wget -qO - https://api.ipify.org; echo do get me an new IP.

Now I am willing to use firefox with RSelenium going through the Tor localhost on port 9050:

State       Recv-Q Send-Q                                 Local Address:Port                                                Peer Address:Port
LISTEN      0      128                                        127.0.0.1:9050                                                           *:*
LISTEN      0      128                                        127.0.0.1:9051                                                           *:*

I use a standalone selenium java (selenium-server-standalone-2.53.0.jar), which work fine with regular RSelenium: here is an example getting the ip displayed on ipchicken

library(RSelenium)

remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  port = 4444L,
  browserName = "firefox"
)

remDr$open()
remDr$navigate("https://ipchicken.com/")
ip <- remDr$findElements(using = "css", value ='b')
print(ip[[1]]$getElementText())

And I do get my IP. Now I want to see it happen with Tor. I thus try to add the proxy option when connecting the remotedriver with firefox:

eCaps <- list("moz:firefoxOptions" = list(
  args = c('--proxy-server=localhost:9050'
  )))

remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  port = 4444L,
  browserName = "firefox",
  extraCapabilities = eCaps
)

I tried '--proxy-server=localhost:9050', '--proxy-server=http://localhost:9050','--proxy-server=socks5://localhost:9050', '--proxy-server=127.0.0.1:9050', and it did not output any error and gave me my initial IP. So it is not working. The standalone says it does execute with the options: for example

22:59:10.288 INFO - Executing: [new session: Capabilities [{nativeEvents=true, browserName=firefox, javascriptEnabled=true, moz:firefoxOptions={args=--proxy-server= 127.0.0.1:9050}, version=, platform=ANY}]])
22:59:10.297 INFO - Creating a new session for Capabilities [{nativeEvents=true, browserName=firefox, javascriptEnabled=true, moz:firefoxOptions={args=--proxy-server= 127.0.0.1:9050}, version=, platform=ANY}]
22:59:30.323 INFO - Done: [new session: Capabilities [{nativeEvents=true, browserName=firefox, javascriptEnabled=true, moz:firefoxOptions={args=--proxy-server= 127.0.0.1:9050}, version=, platform=ANY}]]

What Am I doing wrong ?


Edit

After user1207289's answer, and after realizing that you could directly create a firefox profile in RSelenium, I tried:

eCaps <- makeFirefoxProfile(list(network.proxy.type = 1,
                                 network.proxy.socks = "127.0.0.1",
                                 network.proxy.socks_port = 9050,
                                 network.proxy.socks_version = 5))

remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  port = 4444L,
  browserName = "firefox",
  extraCapabilities = eCaps 
)

I used integer for network.proxy.socks_port, network.proxy.socks_port and network.proxy.type because of this question, but tried with character also, without any success. I tried with and without network.proxy.socks_version = 5, and it did not work (I am getting my normal ip). I tried network.proxy.socks_port = 9150, but it did not work.

I also tried

eCaps <- list("moz:firefoxOptions" = list(
  args = c('network.proxy.socks=127.0.0.1:9050' ,'network.proxy.type=1' )
)
)

but that did not work either.

denis
  • 5,580
  • 1
  • 13
  • 40

2 Answers2

2

I could connect to TOR using webdriver and firefox with the below . Just make sure TOR is installed and running. I used it on mac (catalina). You can check port settings according to your OS , in case they are different.

It is in c# but you can pretty much do it for any binding

            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.SetPreference("network.proxy.type", 1);
            firefoxOptions.SetPreference("network.proxy.socks", "127.0.0.1");
            firefoxOptions.SetPreference("network.proxy.socks_port", 9150);

            FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();

            IWebDriver driver = new FirefoxDriver(service, firefoxOptions);

When this opens a firefox browser instance , Just visit https://check.torproject.org/ on the same instance to check if it is connected to TOR. And that will confirm you are connected and will show your new ip also

user1207289
  • 3,060
  • 6
  • 30
  • 66
  • Tor is running, see my question (I tested with `wget -qO - https://api.ipify.org; echo` that I get new IP), and the port is `9050` (verified with `ss -nlt`). I tried your solution with different manners, but I did not work with RSelenium, see my edits. – denis Jun 02 '20 at 21:10
  • Can you try `9150` instead of `9050` as tor browser listens on `9150` as per [this](https://en.wikipedia.org/wiki/Tor_(anonymity_network)) . And it is also mentioned somewhere in [this](https://trac.torproject.org/projects/tor/ticket/33279) that `the default SOCKS port used by Tor Browser when it starts tor is 9150, not 9050` . You can search that line in there. – user1207289 Jun 02 '20 at 21:30
  • I think you should try `9150` . See [this](https://tor.stackexchange.com/questions/7398/connecting-to-torbrowser-socket) and [this](https://tor.stackexchange.com/questions/6939/configure-tor-as-proxy) – user1207289 Jun 02 '20 at 22:22
0

After A lot of searching, I found a way: RSelenium has the getFirefoxProfile function which allows to get a firefox profile.

So I first configured the profile directly from firefox following the same tuto and copied it to my R folder. Using

fprof <- getFirefoxProfile("myprofile.default")

remDr <- remoteDriver(
  remoteServerAddr = "localhost",
  port = 4444L,
  browserName = "firefox",
  extraCapabilities = fprof
)

Did work.

denis
  • 5,580
  • 1
  • 13
  • 40