1package eu.siacs.conversations.http;
2
3import static eu.siacs.conversations.utils.Random.SECURE_RANDOM;
4
5import android.content.Context;
6import android.os.Build;
7import android.util.Log;
8
9import androidx.core.util.Consumer;
10
11import de.gultsch.common.TrustManagers;
12import eu.siacs.conversations.BuildConfig;
13import eu.siacs.conversations.Config;
14import eu.siacs.conversations.entities.Account;
15import eu.siacs.conversations.entities.DownloadableFile;
16import eu.siacs.conversations.entities.Message;
17import eu.siacs.conversations.services.AbstractConnectionManager;
18import eu.siacs.conversations.services.XmppConnectionService;
19import eu.siacs.conversations.utils.TLSSocketFactory;
20import java.io.IOException;
21import java.io.InputStream;
22import java.net.InetAddress;
23import java.net.InetSocketAddress;
24import java.net.Proxy;
25import java.net.UnknownHostException;
26import java.security.KeyManagementException;
27import java.security.KeyStoreException;
28import java.security.NoSuchAlgorithmException;
29import java.security.cert.CertificateException;
30import java.util.ArrayList;
31import java.util.List;
32import java.util.concurrent.Executor;
33import java.util.concurrent.Executors;
34import java.util.concurrent.TimeUnit;
35import javax.net.ssl.SSLSocketFactory;
36import javax.net.ssl.X509TrustManager;
37import okhttp3.HttpUrl;
38import okhttp3.OkHttpClient;
39import okhttp3.Request;
40import okhttp3.ResponseBody;
41import org.apache.http.conn.ssl.StrictHostnameVerifier;
42
43public class HttpConnectionManager extends AbstractConnectionManager {
44
45 private final List<HttpDownloadConnection> downloadConnections = new ArrayList<>();
46 private final List<HttpUploadConnection> uploadConnections = new ArrayList<>();
47
48 public static final Executor EXECUTOR = Executors.newFixedThreadPool(4);
49
50 private static final OkHttpClient OK_HTTP_CLIENT;
51
52 static {
53 OK_HTTP_CLIENT =
54 new OkHttpClient.Builder()
55 .addInterceptor(
56 chain -> {
57 final Request original = chain.request();
58 final Request modified =
59 original.newBuilder()
60 .header("User-Agent", getUserAgent())
61 .build();
62 return chain.proceed(modified);
63 })
64 .build();
65 }
66
67 public static String getUserAgent() {
68 return String.format("%s/%s", BuildConfig.APP_NAME, BuildConfig.VERSION_NAME);
69 }
70
71 public HttpConnectionManager(XmppConnectionService service) {
72 super(service);
73 }
74
75 public static Proxy getProxy() {
76 final InetAddress localhost;
77 try {
78 localhost = InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
79 } catch (final UnknownHostException e) {
80 throw new IllegalStateException(e);
81 }
82 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
83 return new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(localhost, 9050));
84 } else {
85 return new Proxy(Proxy.Type.HTTP, new InetSocketAddress(localhost, 8118));
86 }
87 }
88
89 public void createNewDownloadConnection(Message message) {
90 this.createNewDownloadConnection(message, false);
91 }
92
93 public void createNewDownloadConnection(final Message message, boolean interactive) {
94 createNewDownloadConnection(message, interactive, null);
95 }
96
97 public void createNewDownloadConnection(final Message message, boolean interactive, Consumer<DownloadableFile> cb) {
98 synchronized (this.downloadConnections) {
99 for (HttpDownloadConnection connection : this.downloadConnections) {
100 if (connection.getMessage() == message) {
101 Log.d(
102 Config.LOGTAG,
103 message.getConversation().getAccount().getJid().asBareJid()
104 + ": download already in progress");
105 return;
106 }
107 }
108 final HttpDownloadConnection connection = new HttpDownloadConnection(message, this, cb);
109 connection.init(interactive);
110 this.downloadConnections.add(connection);
111 }
112 }
113
114 public void createNewUploadConnection(final Message message, boolean delay) {
115 createNewUploadConnection(message, delay, null);
116 }
117
118 public void createNewUploadConnection(final Message message, boolean delay, final Runnable cb) {
119 synchronized (this.uploadConnections) {
120 for (HttpUploadConnection connection : this.uploadConnections) {
121 if (connection.getMessage() == message) {
122 Log.d(
123 Config.LOGTAG,
124 message.getConversation().getAccount().getJid().asBareJid()
125 + ": upload already in progress");
126 return;
127 }
128 }
129 HttpUploadConnection connection =
130 new HttpUploadConnection(
131 message,
132 Method.determine(message.getConversation().getAccount()),
133 this, cb);
134 connection.init(delay);
135 this.uploadConnections.add(connection);
136 }
137 }
138
139 void finishConnection(HttpDownloadConnection connection) {
140 synchronized (this.downloadConnections) {
141 this.downloadConnections.remove(connection);
142 }
143 }
144
145 void finishUploadConnection(HttpUploadConnection httpUploadConnection) {
146 synchronized (this.uploadConnections) {
147 this.uploadConnections.remove(httpUploadConnection);
148 }
149 }
150
151 public OkHttpClient buildHttpClient(final HttpUrl url, final Account account, boolean interactive) {
152 return buildHttpClient(url, account, 30, interactive);
153 }
154
155 public OkHttpClient buildHttpClient(
156 final HttpUrl url, final Account account, int readTimeout, boolean interactive) {
157 final String slotHostname = url.host();
158 final boolean onionSlot = slotHostname.endsWith(".onion");
159 final OkHttpClient.Builder builder = newBuilder(mXmppConnectionService.useTorToConnect() || account.isOnion() || onionSlot);
160 builder.readTimeout(readTimeout, TimeUnit.SECONDS);
161 setupTrustManager(builder, interactive);
162 return builder.build();
163 }
164
165 private void setupTrustManager(final OkHttpClient.Builder builder, final boolean interactive) {
166 final X509TrustManager trustManager;
167 if (interactive) {
168 trustManager = mXmppConnectionService.getMemorizingTrustManager().getInteractive();
169 } else {
170 trustManager = mXmppConnectionService.getMemorizingTrustManager().getNonInteractive();
171 }
172 try {
173 final SSLSocketFactory sf =
174 new TLSSocketFactory(new X509TrustManager[] {trustManager}, SECURE_RANDOM);
175 builder.sslSocketFactory(sf, trustManager);
176 builder.hostnameVerifier(new StrictHostnameVerifier());
177 } catch (final KeyManagementException | NoSuchAlgorithmException ignored) {
178 }
179 }
180
181 public static OkHttpClient.Builder newBuilder(final boolean tor) {
182 final OkHttpClient.Builder builder = OK_HTTP_CLIENT.newBuilder();
183 builder.writeTimeout(30, TimeUnit.SECONDS);
184 builder.readTimeout(30, TimeUnit.SECONDS);
185 if (tor) {
186 builder.proxy(HttpConnectionManager.getProxy()).build();
187 }
188 return builder;
189 }
190
191 public static InputStream open(final String url, final boolean tor) throws IOException {
192 return open(HttpUrl.get(url), tor);
193 }
194
195 public static InputStream open(final HttpUrl httpUrl, final boolean tor) throws IOException {
196 final OkHttpClient client = newBuilder(tor).build();
197 final Request request = new Request.Builder().get().url(httpUrl).build();
198 final ResponseBody body = client.newCall(request).execute().body();
199 if (body == null) {
200 throw new IOException("No response body found");
201 }
202 return body.byteStream();
203 }
204
205 public static String extractFilenameFromResponse(okhttp3.Response response) {
206 String filename = null;
207
208 // Try to extract filename from the Content-Disposition header
209 String contentDisposition = response.header("Content-Disposition");
210 if (contentDisposition != null && contentDisposition.contains("filename=")) {
211 String[] parts = contentDisposition.split(";");
212 for (String part : parts) {
213 if (part.trim().startsWith("filename=")) {
214 filename = part.substring("filename=".length()).trim().replace("\"", "");
215 break;
216 }
217 }
218 }
219
220 // If filename is not found in the Content-Disposition header, try to get it from the URL
221 if (filename == null || filename.isEmpty()) {
222 HttpUrl httpUrl = response.request().url();
223 List<String> pathSegments = httpUrl.pathSegments();
224 if (!pathSegments.isEmpty()) {
225 filename = pathSegments.get(pathSegments.size() - 1);
226 }
227 }
228
229 return filename;
230 }
231
232 public static OkHttpClient okHttpClient(final Context context) {
233 final OkHttpClient.Builder builder = HttpConnectionManager.OK_HTTP_CLIENT.newBuilder();
234 try {
235 final X509TrustManager trustManager = TrustManagers.createForAndroidVersion(context);
236 final SSLSocketFactory socketFactory =
237 new TLSSocketFactory(new X509TrustManager[] {trustManager}, SECURE_RANDOM);
238 builder.sslSocketFactory(socketFactory, trustManager);
239 } catch (final IOException
240 | KeyManagementException
241 | NoSuchAlgorithmException
242 | KeyStoreException
243 | CertificateException e) {
244 Log.d(Config.LOGTAG, "not reconfiguring service to work with bundled LetsEncrypt");
245 throw new RuntimeException(e);
246 }
247 return builder.build();
248 }
249}