1package eu.siacs.conversations.http;
2
3import android.util.Log;
4
5import org.apache.http.conn.ssl.StrictHostnameVerifier;
6
7import java.io.IOException;
8import java.io.InputStream;
9import java.net.InetAddress;
10import java.net.InetSocketAddress;
11import java.net.Proxy;
12import java.net.UnknownHostException;
13import java.security.KeyManagementException;
14import java.security.NoSuchAlgorithmException;
15import java.util.ArrayList;
16import java.util.List;
17import java.util.concurrent.Executor;
18import java.util.concurrent.Executors;
19import java.util.concurrent.TimeUnit;
20
21import javax.net.ssl.HostnameVerifier;
22import javax.net.ssl.SSLSocketFactory;
23import javax.net.ssl.X509TrustManager;
24
25import eu.siacs.conversations.Config;
26import eu.siacs.conversations.entities.Account;
27import eu.siacs.conversations.entities.Message;
28import eu.siacs.conversations.services.AbstractConnectionManager;
29import eu.siacs.conversations.services.XmppConnectionService;
30import eu.siacs.conversations.utils.TLSSocketFactory;
31import okhttp3.HttpUrl;
32import okhttp3.OkHttpClient;
33import okhttp3.Request;
34import okhttp3.ResponseBody;
35
36public class HttpConnectionManager extends AbstractConnectionManager {
37
38 private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
39 private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
40
41 public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
42
43 public HttpConnectionManager(XmppConnectionService service) {
44 super(service);
45 }
46
47 public static Proxy getProxy() {
48 try {
49 return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 9050));
50 } catch (final UnknownHostException e) {
51 throw new IllegalStateException(e);
52 }
53 }
54
55 public void createNewDownloadConnection(Message message) {
56 this.createNewDownloadConnection(message, false);
57 }
58
59 public void createNewDownloadConnection(final Message message, boolean interactive) {
60 synchronized (this.downloadConnections) {
61 for(HttpDownloadConnection connection : this.downloadConnections) {
62 if (connection.getMessage() == message) {
63 Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": download already in progress");
64 return;
65 }
66 }
67 final HttpDownloadConnection connection = new HttpDownloadConnection(message, this);
68 connection.init(interactive);
69 this.downloadConnections.add(connection);
70 }
71 }
72
73 public void createNewUploadConnection(final Message message, boolean delay) {
74 synchronized (this.uploadConnections) {
75 for (HttpUploadConnection connection : this.uploadConnections) {
76 if (connection.getMessage() == message) {
77 Log.d(Config.LOGTAG, message.getConversation().getAccount().getJid().asBareJid() + ": upload already in progress");
78 return;
79 }
80 }
81 HttpUploadConnection connection = new HttpUploadConnection(message, Method.determine(message.getConversation().getAccount()), this);
82 connection.init(delay);
83 this.uploadConnections.add(connection);
84 }
85 }
86
87 void finishConnection(HttpDownloadConnection connection) {
88 synchronized (this.downloadConnections) {
89 this.downloadConnections.remove(connection);
90 }
91 }
92
93 void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
94 synchronized (this.uploadConnections) {
95 this.uploadConnections.remove(httpUploadConnection);
96 }
97 }
98
99 OkHttpClient buildHttpClient(final HttpUrl url, final Account account, boolean interactive) {
100 final String slotHostname = url.host();
101 final boolean onionSlot = slotHostname.endsWith(".onion");
102 final OkHttpClient.Builder builder = new OkHttpClient.Builder();
103 //builder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS));
104 builder.writeTimeout(30, TimeUnit.SECONDS);
105 builder.readTimeout(30, TimeUnit.SECONDS);
106 setupTrustManager(builder, interactive);
107 if (mXmppConnectionService.useTorToConnect() || account.isOnion() || onionSlot) {
108 builder.proxy(HttpConnectionManager.getProxy()).build();
109 }
110 return builder.build();
111 }
112
113 private void setupTrustManager(final OkHttpClient.Builder builder, final boolean interactive) {
114 final X509TrustManager trustManager;
115 final HostnameVerifier hostnameVerifier = mXmppConnectionService.getMemorizingTrustManager().wrapHostnameVerifier(new StrictHostnameVerifier(), interactive);
116 if (interactive) {
117 trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
118 } else {
119 trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
120 }
121 try {
122 final SSLSocketFactory sf = new TLSSocketFactory(new X509TrustManager[]{trustManager}, mXmppConnectionService.getRNG());
123 builder.sslSocketFactory(sf, trustManager);
124 builder.hostnameVerifier(hostnameVerifier);
125 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
126 }
127 }
128
129 public static InputStream open(final String url, final boolean tor) throws IOException {
130 return open(HttpUrl.get(url), tor);
131 }
132
133 public static InputStream open(final HttpUrl httpUrl, final boolean tor) throws IOException {
134 final OkHttpClient.Builder builder = new OkHttpClient.Builder();
135 if (tor) {
136 builder.proxy(HttpConnectionManager.getProxy()).build();
137 }
138 final OkHttpClient client = builder.build();
139 final Request request = new Request.Builder().get().url(httpUrl).build();
140 final ResponseBody body = client.newCall(request).execute().body();
141 if (body == null) {
142 throw new IOException("No response body found");
143 }
144 return body.byteStream();
145 }
146}