mirror of
https://github.com/nuclearfog/Shitter.git
synced 2025-01-30 10:55:06 +01:00
finalized proxy function
This commit is contained in:
parent
686c493f56
commit
8c8a07a33a
@ -0,0 +1,79 @@
|
||||
package org.nuclearfog.twidda.backend.engine;
|
||||
|
||||
import org.nuclearfog.twidda.database.GlobalSettings;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Authenticator;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.PasswordAuthentication;
|
||||
import java.net.Proxy;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Creates a https proxy connection for all connections
|
||||
*/
|
||||
class ProxySetup extends ProxySelector {
|
||||
|
||||
private String proxyHost;
|
||||
private int proxyPort;
|
||||
private boolean proxySet;
|
||||
|
||||
|
||||
ProxySetup(GlobalSettings settings) {
|
||||
proxySet = settings.isProxyServerSet();
|
||||
|
||||
if (proxySet) {
|
||||
proxyHost = settings.getProxyHost();
|
||||
proxyPort = Integer.parseInt(settings.getProxyPort());
|
||||
}
|
||||
if (settings.isProxyLoginSet()) {
|
||||
Authenticator.setDefault(new ProxyAuthenticator(settings));
|
||||
} else {
|
||||
Authenticator.setDefault(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Proxy> select(URI uri) {
|
||||
Proxy httpsProxy;
|
||||
if (proxySet) {
|
||||
InetSocketAddress socket = new InetSocketAddress(proxyHost, proxyPort);
|
||||
httpsProxy = new Proxy(Proxy.Type.HTTP, socket);
|
||||
} else {
|
||||
httpsProxy = Proxy.NO_PROXY;
|
||||
}
|
||||
List<Proxy> result = new ArrayList<>(1);
|
||||
result.add(httpsProxy);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates an authenticator for proxy login
|
||||
*/
|
||||
class ProxyAuthenticator extends Authenticator {
|
||||
|
||||
private String username;
|
||||
private char[] password;
|
||||
|
||||
ProxyAuthenticator(GlobalSettings settings) {
|
||||
username = settings.getProxyUser();
|
||||
password = settings.getProxyPass().toCharArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(username, password);
|
||||
}
|
||||
}
|
||||
}
|
@ -25,8 +25,7 @@ import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
@ -93,6 +92,7 @@ public class TwitterEngine {
|
||||
} else {
|
||||
twitter = factory.getInstance();
|
||||
}
|
||||
ProxySelector.setDefault(new ProxySetup(settings));
|
||||
}
|
||||
|
||||
|
||||
@ -1051,17 +1051,8 @@ public class TwitterEngine {
|
||||
@Nullable
|
||||
public Bitmap getImage(String link) throws EngineException {
|
||||
try {
|
||||
Proxy proxy;
|
||||
URL url = new URL(link);
|
||||
if (settings.isProxyServerSet()) {
|
||||
String proxyAddress = settings.getProxyHost();
|
||||
int proxyPort = Integer.parseInt(settings.getProxyPort());
|
||||
InetSocketAddress socket = new InetSocketAddress(proxyAddress, proxyPort);
|
||||
proxy = new Proxy(Proxy.Type.HTTP, socket);
|
||||
} else {
|
||||
proxy = Proxy.NO_PROXY;
|
||||
}
|
||||
InputStream stream = url.openConnection(proxy).getInputStream();
|
||||
InputStream stream = url.openConnection().getInputStream();
|
||||
return BitmapFactory.decodeStream(stream);
|
||||
} catch (IOException err) {
|
||||
throw new EngineException(EngineException.InternalErrorType.BITMAP_FAILURE);
|
||||
|
Loading…
x
Reference in New Issue
Block a user