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;
|
||||
@ -138,7 +134,7 @@ public class ProxyDialog {
|
||||
etUsername.addTextChangedListener(requireTestOnChange);
|
||||
etPassword = content.findViewById(R.id.etPassword);
|
||||
if (!TextUtils.isEmpty(proxyConfig.password)) {
|
||||
etPassword.setText(proxyConfig.username);
|
||||
etPassword.setText(proxyConfig.password);
|
||||
}
|
||||
etPassword.addTextChangedListener(requireTestOnChange);
|
||||
if (proxyConfig.type == Proxy.Type.DIRECT) {
|
||||
@ -185,7 +181,7 @@ public class ProxyDialog {
|
||||
private boolean checkValidity() {
|
||||
boolean valid = true;
|
||||
if (spType.getSelectedItemPosition() > 0) {
|
||||
valid &= checkHost();
|
||||
valid = checkHost();
|
||||
}
|
||||
valid &= checkPort();
|
||||
return valid;
|
||||
@ -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,27 +259,25 @@ 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);
|
||||
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);
|
||||
}
|
||||
@ -291,31 +285,18 @@ public class ProxyDialog {
|
||||
.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…
x
Reference in New Issue
Block a user