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