Merge pull request #5151 from ByteHamster/fix-proxy
Fixed proxy support
This commit is contained in:
commit
65bf8bf70c
|
@ -30,8 +30,7 @@ import de.danoeh.antennapod.R;
|
|||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
|
||||
import de.danoeh.antennapod.core.service.download.ProxyConfig;
|
||||
import io.reactivex.Single;
|
||||
import io.reactivex.SingleOnSubscribe;
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
@ -41,9 +40,6 @@ import okhttp3.Request;
|
|||
import okhttp3.Response;
|
||||
|
||||
public class ProxyDialog {
|
||||
|
||||
private static final String TAG = "ProxyDialog";
|
||||
|
||||
private final Context context;
|
||||
|
||||
private AlertDialog dialog;
|
||||
|
@ -122,26 +118,26 @@ public class ProxyDialog {
|
|||
ProxyConfig proxyConfig = UserPreferences.getProxyConfig();
|
||||
spType.setSelection(adapter.getPosition(proxyConfig.type.name()));
|
||||
etHost = content.findViewById(R.id.etHost);
|
||||
if(!TextUtils.isEmpty(proxyConfig.host)) {
|
||||
if (!TextUtils.isEmpty(proxyConfig.host)) {
|
||||
etHost.setText(proxyConfig.host);
|
||||
}
|
||||
etHost.addTextChangedListener(requireTestOnChange);
|
||||
etPort = content.findViewById(R.id.etPort);
|
||||
if(proxyConfig.port > 0) {
|
||||
if (proxyConfig.port > 0) {
|
||||
etPort.setText(String.valueOf(proxyConfig.port));
|
||||
}
|
||||
etPort.addTextChangedListener(requireTestOnChange);
|
||||
etUsername = content.findViewById(R.id.etUsername);
|
||||
if(!TextUtils.isEmpty(proxyConfig.username)) {
|
||||
if (!TextUtils.isEmpty(proxyConfig.username)) {
|
||||
etUsername.setText(proxyConfig.username);
|
||||
}
|
||||
etUsername.addTextChangedListener(requireTestOnChange);
|
||||
etPassword = content.findViewById(R.id.etPassword);
|
||||
if(!TextUtils.isEmpty(proxyConfig.password)) {
|
||||
etPassword.setText(proxyConfig.username);
|
||||
if (!TextUtils.isEmpty(proxyConfig.password)) {
|
||||
etPassword.setText(proxyConfig.password);
|
||||
}
|
||||
etPassword.addTextChangedListener(requireTestOnChange);
|
||||
if(proxyConfig.type == Proxy.Type.DIRECT) {
|
||||
if (proxyConfig.type == Proxy.Type.DIRECT) {
|
||||
enableSettings(false);
|
||||
setTestRequired(false);
|
||||
}
|
||||
|
@ -184,8 +180,8 @@ public class ProxyDialog {
|
|||
|
||||
private boolean checkValidity() {
|
||||
boolean valid = true;
|
||||
if(spType.getSelectedItemPosition() > 0) {
|
||||
valid &= checkHost();
|
||||
if (spType.getSelectedItemPosition() > 0) {
|
||||
valid = checkHost();
|
||||
}
|
||||
valid &= checkPort();
|
||||
return valid;
|
||||
|
@ -193,11 +189,11 @@ public class ProxyDialog {
|
|||
|
||||
private boolean checkHost() {
|
||||
String host = etHost.getText().toString();
|
||||
if(host.length() == 0) {
|
||||
if (host.length() == 0) {
|
||||
etHost.setError(context.getString(R.string.proxy_host_empty_error));
|
||||
return false;
|
||||
}
|
||||
if(!"localhost".equals(host) && !Patterns.DOMAIN_NAME.matcher(host).matches()) {
|
||||
if (!"localhost".equals(host) && !Patterns.DOMAIN_NAME.matcher(host).matches()) {
|
||||
etHost.setError(context.getString(R.string.proxy_host_invalid_error));
|
||||
return false;
|
||||
}
|
||||
|
@ -215,10 +211,10 @@ public class ProxyDialog {
|
|||
|
||||
private int getPort() {
|
||||
String port = etPort.getText().toString();
|
||||
if(port.length() > 0) {
|
||||
if (port.length() > 0) {
|
||||
try {
|
||||
return Integer.parseInt(port);
|
||||
} catch(NumberFormatException e) {
|
||||
} catch (NumberFormatException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
@ -226,7 +222,7 @@ public class ProxyDialog {
|
|||
}
|
||||
|
||||
private void setTestRequired(boolean required) {
|
||||
if(required) {
|
||||
if (required) {
|
||||
testSuccessful = false;
|
||||
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setText(R.string.proxy_test_label);
|
||||
} else {
|
||||
|
@ -240,7 +236,7 @@ public class ProxyDialog {
|
|||
if (disposable != null) {
|
||||
disposable.dispose();
|
||||
}
|
||||
if(!checkValidity()) {
|
||||
if (!checkValidity()) {
|
||||
setTestRequired(true);
|
||||
return;
|
||||
}
|
||||
|
@ -251,7 +247,7 @@ public class ProxyDialog {
|
|||
txtvMessage.setTextColor(textColorPrimary);
|
||||
txtvMessage.setText("{fa-circle-o-notch spin} " + checking);
|
||||
txtvMessage.setVisibility(View.VISIBLE);
|
||||
disposable = Single.create((SingleOnSubscribe<Response>) emitter -> {
|
||||
disposable = Completable.create(emitter -> {
|
||||
String type = (String) spType.getSelectedItem();
|
||||
String host = etHost.getText().toString();
|
||||
String port = etPort.getText().toString();
|
||||
|
@ -263,59 +259,44 @@ public class ProxyDialog {
|
|||
}
|
||||
SocketAddress address = InetSocketAddress.createUnresolved(host, portValue);
|
||||
Proxy.Type proxyType = Proxy.Type.valueOf(type.toUpperCase(Locale.US));
|
||||
Proxy proxy = new Proxy(proxyType, address);
|
||||
OkHttpClient.Builder builder = AntennapodHttpClient.newBuilder()
|
||||
.connectTimeout(10, TimeUnit.SECONDS)
|
||||
.proxy(proxy);
|
||||
builder.interceptors().clear();
|
||||
OkHttpClient client = builder.build();
|
||||
.proxy(new Proxy(proxyType, address));
|
||||
if (!TextUtils.isEmpty(username)) {
|
||||
builder.proxyAuthenticator((route, response) -> {
|
||||
String credentials = Credentials.basic(username, password);
|
||||
client.interceptors().add(chain -> {
|
||||
Request request = chain.request().newBuilder()
|
||||
.header("Proxy-Authorization", credentials).build();
|
||||
return chain.proceed(request);
|
||||
return response.request().newBuilder()
|
||||
.header("Proxy-Authorization", credentials)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
Request request = new Request.Builder()
|
||||
.url("http://www.google.com")
|
||||
.head()
|
||||
.build();
|
||||
try {
|
||||
Response response = client.newCall(request).execute();
|
||||
emitter.onSuccess(response);
|
||||
} catch(IOException e) {
|
||||
OkHttpClient client = builder.build();
|
||||
Request request = new Request.Builder().url("https://www.example.com").head().build();
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
if (response.isSuccessful()) {
|
||||
emitter.onComplete();
|
||||
} else {
|
||||
emitter.onError(new IOException(response.message()));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
emitter.onError(e);
|
||||
}
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
response -> {
|
||||
int colorId;
|
||||
String icon;
|
||||
String result;
|
||||
if(response.isSuccessful()) {
|
||||
colorId = R.color.download_success_green;
|
||||
icon = "{fa-check}";
|
||||
result = context.getString(R.string.proxy_test_successful);
|
||||
} else {
|
||||
colorId = R.color.download_failed_red;
|
||||
icon = "{fa-close}";
|
||||
result = context.getString(R.string.proxy_test_failed);
|
||||
}
|
||||
int color = ContextCompat.getColor(context, colorId);
|
||||
txtvMessage.setTextColor(color);
|
||||
String message = String.format("%s %s: %s", icon, result, response.message());
|
||||
() -> {
|
||||
txtvMessage.setTextColor(ContextCompat.getColor(context, R.color.download_success_green));
|
||||
String message = String.format("%s %s", "{fa-check}",
|
||||
context.getString(R.string.proxy_test_successful));
|
||||
txtvMessage.setText(message);
|
||||
setTestRequired(!response.isSuccessful());
|
||||
setTestRequired(false);
|
||||
},
|
||||
error -> {
|
||||
String icon = "{fa-close}";
|
||||
String result = context.getString(R.string.proxy_test_failed);
|
||||
int color = ContextCompat.getColor(context, R.color.download_failed_red);
|
||||
txtvMessage.setTextColor(color);
|
||||
String message = String.format("%s %s: %s", icon, result, error.getMessage());
|
||||
error.printStackTrace();
|
||||
txtvMessage.setTextColor(ContextCompat.getColor(context, R.color.download_failed_red));
|
||||
String message = String.format("%s %s: %s", "{fa-close}",
|
||||
context.getString(R.string.proxy_test_failed), error.getMessage());
|
||||
txtvMessage.setText(message);
|
||||
setTestRequired(true);
|
||||
}
|
||||
|
|
|
@ -118,14 +118,13 @@ public class AntennapodHttpClient {
|
|||
if (config.type != Proxy.Type.DIRECT) {
|
||||
int port = config.port > 0 ? config.port : ProxyConfig.DEFAULT_PORT;
|
||||
SocketAddress address = InetSocketAddress.createUnresolved(config.host, port);
|
||||
Proxy proxy = new Proxy(config.type, address);
|
||||
builder.proxy(proxy);
|
||||
builder.proxy(new Proxy(config.type, address));
|
||||
if (!TextUtils.isEmpty(config.username) && config.password != null) {
|
||||
builder.proxyAuthenticator((route, response) -> {
|
||||
String credentials = Credentials.basic(config.username, config.password);
|
||||
builder.interceptors().add(chain -> {
|
||||
Request request = chain.request().newBuilder()
|
||||
.header("Proxy-Authorization", credentials).build();
|
||||
return chain.proceed(request);
|
||||
return response.request().newBuilder()
|
||||
.header("Proxy-Authorization", credentials)
|
||||
.build();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue