question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Setting up WebSocket protocol

See original GitHub issue

Hi, when I use browsermob-proxy all connections goes through https:// even WebSocket methods. For example: GET /echo HTTP/1.1 returns WebSocket-Location: wss://site.com/echo/123/websocket, after that initiates GET wss😕/site.com/echo/123/websocket with response code 101 switching protocols. But with browsermob-proxy initiates request GET https😕/site.com/echo/123/websocket

I’ve try to change protocol with addRequestFilter() but there is only http version of protocol:

proxy.addRequestFilter(new RequestFilter() {
   @Override
    public HttpResponse filterRequest(HttpRequest request, HttpMessageContents contents,    HttpMessageInfo messageInfo) {
       if (request.getUri().contains("echo/") && request.getUri().contains("/websocket")) {
           request.setProtocolVersion(new HttpVersion("WSS/1.1", true));
       }
       return null;
   }
});

There is no info about WebSocket and browsermob-proxy just this self-reply https://groups.google.com/forum/#!topic/browsermob-proxy/JzTHxje0qP4

I need proxy to monitor HTTP and WebSocket, there are a lot of autotests that I could make with it, but if I run tests with WebSocket it does not work at all.

Kind regards!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14

github_iconTop GitHub Comments

3reactions
ineverovcommented, Aug 10, 2018

@jekh Looks like littleproxy supports WebSockets already https://github.com/adamfisk/LittleProxy/releases/tag/littleproxy-1.1.0 Could somebody take a look at this issue?

1reaction
apregcommented, Feb 9, 2021

Hi, I use PAC file // Start BrowserMobProxy server BrowserMobProxy proxyServer = new BrowserMobProxyServer(); proxyServer.setTrustAllServers(true); proxyServer.setRequestTimeout(120, TimeUnit.SECONDS); proxyServer.setConnectTimeout(120, TimeUnit.SECONDS); proxyServer.enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes()); proxyServer.start(0);

// Get BrowserMobProxy server port for PAC file int proxyPort = proxyServer.getPort();

// Create PAC file to send WebSocket requests direct but other protocols throught BrowserMobProxy server String pacFunction = "function FindProxyForURL(url, host) { if (url.substring(0, 3) === “ws:” || url.substring(0, 4) === “wss:”) { " + “return “DIRECT”; } else { return “PROXY localhost:” + proxyPort + “”; } }”; String pacLocation = String.format(“target/pac/websocket-proxy-%s.pac”, proxyPort); writeStringToFile(new File(pacLocation), pacFunction, “UTF-8”);

// Set PAC file path to Selenium proxy Proxy seleniumProxy = new Proxy(); seleniumProxy.setProxyType(Proxy.ProxyType.PAC); seleniumProxy.setProxyAutoconfigUrl(String.format(System.getProperty(“base-dir”), pacLocation));

// Add Selenium proxy to DesiredCapabilities DesiredCapabilities desiredCapabilities = capabilities; desiredCapabilities.setCapability(CapabilityType.PROXY, seleniumProxy); desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

// Start driver with desiredCapabilities WebDriver driver = new ChromeDriver(capabilities);

But it works only in Chrome and does not work in Firefox.

Thank you for this workaround. I had to tweak it a little bit since Chrome and Windows blocks the file:// schema usage for pac file url and I did not want to start a webserver to host the pac file. I found the solution here: https://superuser.com/questions/1405499/chrome-72-seems-to-no-longer-accept-proxy-pac-url --proxy-pac-url='data:application/x-javascript-config;base64,'$(base64 -w0 /path/to/local/proxy.pac) The code after this trick has been applied: // Create PAC file to send WebSocket requests direct but other protocols throught BrowserMobProxy server String pacFunction = "function FindProxyForURL(url, host) { if (url.substring(0, 3) === "ws:" || url.substring(0, 4) === "wss:") { " + "return "DIRECT"; } else { return "PROXY localhost:" + proxyPort + ""; } }"; seleniumProxy.setProxyAutoconfigUrl("data:application/x-javascript-config;base64," + Base64.getUrlEncoder().encodeToString(pacFunction.getBytes()));

As you can see I encoded the script not the location because that way it did not work for me. Note: Actually I used BrowserUp proxy but I think this should work with BrowserMob also.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Writing WebSocket servers - Web APIs | MDN
First, the server must listen for incoming socket connections using a standard TCP socket. Depending on your platform, this may be handled for ......
Read more >
WebSocket - The Modern JavaScript Tutorial
To open a websocket connection, we need to create new WebSocket using the special protocol ws in the url: let socket = new...
Read more >
WebSocket <webSocket> | Microsoft Learn
How to configure webSocket. Open Internet Information Services (IIS) Manager: If you are using Windows Server 2012 or later: On the ...
Read more >
HTML5 - WebSockets - Tutorialspoint
A WebSocket is a standard bidirectional TCP socket between the client and the server. The socket starts out as a HTTP connection and...
Read more >
Using WebSocket to build an interactive web application
In this guide, we use STOMP messaging with Spring to create an interactive web application. STOMP is a subprotocol operating on top of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found