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.

Configure a proxy for the traffic

See original GitHub issue

Hello, thanks for the amazing library. I was wondering if it is possible to configure a proxy for the player.

I can easily setup a proxy for some HttpURLConnection I’m performing but now I would like to route the traffic of the player itself through the same proxy.

Any advice on the subject will be much appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lpugliacommented, Feb 12, 2021

@kim-vde Thank you very much for your suggestions, they put me on the right direction. I was finally able to make a MediaSource with a proxy connection. I struggled a lot with HttpURLConnection because on android it has limitations that are not present in a normal JVM. In particular the following global variables:

systemProperties.setProperty("http.proxyHost",proxy);
systemProperties.setProperty("http.proxyPort",port);

are ignored in android. I was also trying to use the Proxy class:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port));

for each single connection but I hit a hard wall when i tryed to swith to an authentication proxy:

Authenticator authenticator = new Authenticator() {

    public PasswordAuthentication getPasswordAuthentication() {
        return (new PasswordAuthentication(user,
                password.toCharArray()));
    }
};
Authenticator.setDefault(authenticator);

the Exception I was getting was:

java.net.ProtocolException: unexpected end of stream

In the end, your answer put me on the right track because while i was searching for a way to implement my own DefaultHttpDataSource I came accross OkHttpClient and its ExoPlayer extension. For me it was much easier to setup OkHttpClient with proxy, after adding my client to the mentioned extension everything started to work flawlessy. For future reference here is my code for a proxied HLS stream:

okhttp3.Authenticator proxyAuthenticator = new Authenticator() {
    @Override
    public Request authenticate(Route route, Response response) throws IOException {
        String credential = Credentials.basic(user, password);
        return response.request().newBuilder()
                .header("Proxy-Authorization", credential)
                .build();
    }
};
OkHttpClient client = new OkHttpClient.Builder()
        .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, port)))
        .proxyAuthenticator(proxyAuthenticator)
        .build();

DataSource.Factory dataSourceFactory =
        new OkHttpDataSourceFactory(client);

MediaSource mediaSource =
        new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(uri));

I hope this will help someone else in the future. Regards

0reactions
kim-vdecommented, Feb 10, 2021

Here is some information that could help:

  • ExoPlayer uses a DefaultHttpDataSource by default, which in turn uses an HttpURLConnection. If you set up the proxy for HttpURLConnection by configuring some global properties somewhere, then this approach should also work with ExoPlayer, as long as you are not using an other DataSource.
  • If you need to access the HttpURLConnection instance directly, then you could try to use a custom DataSource extending the DefaultHttpDataSource.

If this doesn’t help, please provide more information, such as:

  • How do you set up the proxy with HttpURLConnection?
  • How do you build the player? More precisely, do you pass a DataSource such as DefaultHttpDataSource, OkHttpDataSource or CronetDataSource?
Read more comments on GitHub >

github_iconTop Results From Across the Web

Use a proxy server in Windows - Microsoft Support
Select the Start button, then select Settings > Network & internet > Proxy. Under Automatic proxy setup, turn on Automatically detect settings.
Read more >
How to Set Up a Proxy Server on Your PC, Mac, or Web Browser
Click Start and select Settings (the gear icon) from the Start menu. · Choose Network & Internet. · Click Proxy in the left...
Read more >
Setting up a Proxy Server for traffic monitoring with Tshark
A proxy is used for more than privacy, this tool helps to bypass filters, logging, and eavesdropping, caching, filtering blocking, manipulate and modify...
Read more >
Forward Proxy — Apache Traffic Server 9.1.3 documentation
Forward proxies act as a gatekeeper between client browsers on your local network and all (or some, at your configuration's discretion) web sites...
Read more >
Traffic Forwarding Via Direct Proxy Or PAC - Comodo Help
Open Internet Explorer · Open 'Tools' > 'Internet Options', open the 'Connections' tab and click 'LAN settings' · Select 'Use a proxy server...
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