-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGhostBridge.java
104 lines (93 loc) · 4.21 KB
/
GhostBridge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// go-ghostbridge - React Native to Go bridge
// Copyright (c) 2019 Péter Szilágyi. All rights reserved.
package io.ipsn.ghostbridge;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.ReactCookieJarContainer;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
// GhostBridge is a utility class to retrieve the security parameters of an already
// established bridge and inject the certificate, token and domain interceptor into
// React Native's HTTP client.
public class GhostBridge {
// GhostBridge creates a new GhostBridge, both constructing the server side as
// well as authenticating and authorizing the client HTTP library.
public static void init(final long port, final String cert, final String token) throws Exception {
final X509Certificate bridgeCert = (X509Certificate)CertificateFactory.getInstance("X.509")
.generateCertificate(new StringBufferInputStream(cert));
// Wrap all the trust managers so they each trust out self-signer certificate
TrustManagerFactory factory = TrustManagerFactory.getInstance("X509");
factory.init((KeyStore) null);
TrustManager[] trustManagers = factory.getTrustManagers();
for (int i = 0; i < trustManagers.length; i++) {
if (trustManagers[i] instanceof X509TrustManager) {
final X509TrustManager current = (X509TrustManager)trustManagers[i];
// TLS trust manager found, add security exception for own certificate
trustManagers[i] = new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return current.getAcceptedIssuers();
}
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
current.checkClientTrusted(x509Certificates, s);
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
// If the server authenticated itself with the trusted certificate, accept
for (X509Certificate cert : chain) {
if (cert.equals(bridgeCert)) {
return;
}
}
// Certificate chain not the self-signed one, delegate to system authority
current.checkServerTrusted(chain, authType);
}
};
}
}
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagers, new SecureRandom());
// Replace React Native's stock socket factory with the trusted self-signed version
OkHttpClientProvider.setOkHttpClientFactory(new OkHttpClientFactory() {
public OkHttpClient createNewNetworkModuleClient() {
return new OkHttpClient.Builder()
.cookieJar(new ReactCookieJarContainer())
.sslSocketFactory(sslContext.getSocketFactory())
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
// If the request is not addressed the bridge, execute directly
Request original = chain.request();
if (!original.url().host().equals("ghost-bridge")) {
return chain.proceed(original);
}
// Request sent to the ghost-bridge, redirect to localhost
Request redirected = original.newBuilder()
.url(original.url().newBuilder()
.host("localhost")
.port((int)port)
.build())
.header("Authorization", "Bearer " + token)
.build();
return chain.proceed(redirected);
}
})
.build();
}
});
}
}