Skip to content

Commit

Permalink
Use ProxySelector to point HTTP clients at Lantern
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Jun 14, 2021
1 parent c442fc2 commit 21f1a1a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
54 changes: 50 additions & 4 deletions android/app/src/main/java/org/getlantern/lantern/LanternApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.getlantern.lantern.model.LanternHttpClient;
import org.getlantern.lantern.model.LanternSessionManager;
import org.getlantern.lantern.model.MessagingHolder;
import org.getlantern.lantern.model.ProPlan;
import org.getlantern.lantern.model.Utils;
import org.getlantern.lantern.model.VpnState;
import org.getlantern.lantern.model.WelcomeDialog;
Expand All @@ -34,8 +33,16 @@
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LanternApp extends Application implements ActivityLifecycleCallbacks {
Expand Down Expand Up @@ -95,12 +102,12 @@ public void onCreate() {
messaging.init(this);
Logger.debug(TAG, "messaging.init() finished at " + (System.currentTimeMillis() - start));
session = new LanternSessionManager(this);
configureProxySelector();
Logger.debug(TAG, "new LanternSessionManager finished at " + (System.currentTimeMillis() - start));
if (LanternApp.getSession().isPlayVersion()) {
inAppBilling = new InAppBilling(this);
}
lanternHttpClient = new LanternHttpClient(session.getSettings().getHttpProxyHost(),
(int) session.getSettings().getHttpProxyPort());
lanternHttpClient = new LanternHttpClient();
Logger.debug(TAG, "new LanternHttpClient finished at " + (System.currentTimeMillis() - start));
initFirebase();
Logger.debug(TAG, "initFirebase() finished at " + (System.currentTimeMillis() - start));
Expand Down Expand Up @@ -263,7 +270,9 @@ public static LanternHttpClient getLanternHttpClient() {
return lanternHttpClient;
}

public static HttpClient getHttpClient() { return lanternHttpClient; }
public static HttpClient getHttpClient() {
return lanternHttpClient;
}

@Override
protected void attachBaseContext(Context base) {
Expand Down Expand Up @@ -296,4 +305,41 @@ public static InAppBilling getInAppBilling() {
public static void getPlans(LanternHttpClient.PlansCallback cb) {
lanternHttpClient.getPlans(cb, inAppBilling);
}

/**
* Configures the default ProxySelector to send all traffic to the embedded Lantern proxy.
*/
private void configureProxySelector() {
final SocketAddress proxyAddress = addrFromString(session.getSettings().getHttpProxyHost() + ":" +
session.getSettings().getHttpProxyPort());
ProxySelector.setDefault(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
final List<Proxy> proxiesList = new ArrayList();
proxiesList.add(new Proxy(Proxy.Type.HTTP, proxyAddress));
return proxiesList;
}

@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
}
});
}

/**
* Converts a host:port string into an InetSocketAddress by first making a fake URL using that
* address.
*
* @param addr
* @return
*/
private static InetSocketAddress addrFromString(String addr) {
try {
URI uri = new URI("my://" + addr);
return new InetSocketAddress(uri.getHost(), uri.getPort());
} catch (Exception e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ public class LanternHttpClient extends HttpClient {

/**
* Creates a new HTTP client
*
* @param proxyHost The host of the local proxy.
* @param proxyPort The port of the local proxy.
*/
public LanternHttpClient(final String proxyHost, final int proxyPort) {
super(proxyHost, proxyPort);
public LanternHttpClient() {
super();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ open class HttpClient(@JvmField val httpClient: OkHttpClient) {
/**
* Creates a new HTTP client
*
* @param proxyHost The host of the local proxy.
* @param proxyPort The port of the local proxy.
*/
constructor(proxyHost: String, proxyPort: Int) : this(
constructor() : this(
OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.proxy(Proxy(Proxy.Type.HTTP, InetSocketAddress(proxyHost, proxyPort)))
.build()
)

Expand Down

0 comments on commit 21f1a1a

Please # to comment.