Debug: proxy connections

This commit is contained in:
M66B 2023-12-02 12:39:00 +01:00
parent af9fb287cf
commit 47ec1ade23
4 changed files with 36 additions and 2 deletions

View File

@ -312,7 +312,7 @@ public class SocketFetcher {
} else {
SocketFactory f = (SocketFactory) props.get("fairemail.factory");
eu.faircode.email.Log.i("Using socket factory=" + f);
socket = (f == null ? new Socket() : f.createSocket());
socket = (f == null ? eu.faircode.email.ConnectionHelper.getSocket(host, port) : f.createSocket());
}
}
if (to >= 0) {

View File

@ -125,6 +125,8 @@ public class ApplicationEx extends Application
" process=" + android.os.Process.myPid());
Log.logMemory(this, "App");
ConnectionHelper.setupProxy(this);
if (BuildConfig.DEBUG)
UriHelper.test(this);

View File

@ -48,8 +48,12 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.net.UnknownHostException;
@ -906,4 +910,32 @@ public class ConnectionHelper {
Log.w(ex);
}
}
static void setupProxy(Context context) {
if (!BuildConfig.DEBUG)
return;
// https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html
ProxySelector.setDefault(new ProxySelector() {
@Override
public List<Proxy> select(URI uri) {
// new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("", 0));
Log.i("PROXY uri=" + uri);
return Arrays.asList(Proxy.NO_PROXY);
}
@Override
public void connectFailed(URI uri, SocketAddress sa, IOException ex) {
Log.e("PROXY uri=" + uri + " sa=" + sa, ex);
}
});
}
public static Socket getSocket(String host, int port) {
if (BuildConfig.DEBUG) {
Proxy proxy = ProxySelector.getDefault().select(URI.create("socket://" + host + ":" + port)).get(0);
return new Socket(proxy);
} else
return new Socket();
}
}

View File

@ -42,7 +42,7 @@ public class Whois {
}
static String get(String domain, String host, int port) throws IOException {
Socket socket = new Socket();
Socket socket = ConnectionHelper.getSocket(host, port);
socket.connect(new InetSocketAddress(host, port), WHOIS_TIMEOUT);
try {
byte[] request = (domain + "\r\n").getBytes(StandardCharsets.UTF_8);