@xdcobra/react-native-grpc 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +22 -0
- package/README.md +114 -0
- package/android/build.gradle +127 -0
- package/android/gradle.properties +4 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/java/com/reactnativegrpc/GrpcMarshaller.java +27 -0
- package/android/src/main/java/com/reactnativegrpc/GrpcModule.java +585 -0
- package/android/src/main/java/com/reactnativegrpc/GrpcPackage.java +27 -0
- package/android/src/main/java/com/reactnativegrpc/SpkiPinTrustManager.java +101 -0
- package/android/src/main/java/com/reactnativegrpc/TrustManagers.java +51 -0
- package/ios/Grpc.h +22 -0
- package/ios/Grpc.m +439 -0
- package/ios/SpkiPinning.h +22 -0
- package/ios/SpkiPinning.m +114 -0
- package/lib/commonjs/bidi-streaming.js +37 -0
- package/lib/commonjs/bidi-streaming.js.map +1 -0
- package/lib/commonjs/client-streaming.js +59 -0
- package/lib/commonjs/client-streaming.js.map +1 -0
- package/lib/commonjs/client.js +274 -0
- package/lib/commonjs/client.js.map +1 -0
- package/lib/commonjs/errors.js +16 -0
- package/lib/commonjs/errors.js.map +1 -0
- package/lib/commonjs/index.js +84 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/server-streaming.js +61 -0
- package/lib/commonjs/server-streaming.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/unary.js +38 -0
- package/lib/commonjs/unary.js.map +1 -0
- package/lib/module/bidi-streaming.js +30 -0
- package/lib/module/bidi-streaming.js.map +1 -0
- package/lib/module/client-streaming.js +51 -0
- package/lib/module/client-streaming.js.map +1 -0
- package/lib/module/client.js +268 -0
- package/lib/module/client.js.map +1 -0
- package/lib/module/errors.js +9 -0
- package/lib/module/errors.js.map +1 -0
- package/lib/module/index.js +10 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/server-streaming.js +53 -0
- package/lib/module/server-streaming.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/unary.js +31 -0
- package/lib/module/unary.js.map +1 -0
- package/lib/typescript/bidi-streaming.d.ts +16 -0
- package/lib/typescript/bidi-streaming.d.ts.map +1 -0
- package/lib/typescript/client-streaming.d.ts +29 -0
- package/lib/typescript/client-streaming.d.ts.map +1 -0
- package/lib/typescript/client.d.ts +80 -0
- package/lib/typescript/client.d.ts.map +1 -0
- package/lib/typescript/errors.d.ts +8 -0
- package/lib/typescript/errors.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +10 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/server-streaming.d.ts +23 -0
- package/lib/typescript/server-streaming.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +87 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/unary.d.ts +16 -0
- package/lib/typescript/unary.d.ts.map +1 -0
- package/package.json +169 -0
- package/react-native-grpc.podspec +30 -0
- package/src/bidi-streaming.ts +78 -0
- package/src/client-streaming.ts +106 -0
- package/src/client.ts +521 -0
- package/src/errors.ts +11 -0
- package/src/index.ts +11 -0
- package/src/server-streaming.ts +105 -0
- package/src/types.ts +107 -0
- package/src/unary.ts +71 -0
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
package com.reactnativegrpc;
|
|
2
|
+
|
|
3
|
+
import android.util.Base64;
|
|
4
|
+
import android.util.Log;
|
|
5
|
+
import android.widget.Toast;
|
|
6
|
+
|
|
7
|
+
import androidx.annotation.NonNull;
|
|
8
|
+
import androidx.annotation.UiThread;
|
|
9
|
+
|
|
10
|
+
import com.facebook.react.bridge.Arguments;
|
|
11
|
+
import com.facebook.react.bridge.Promise;
|
|
12
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
13
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
14
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
15
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
16
|
+
import com.facebook.react.bridge.ReadableMap;
|
|
17
|
+
import com.facebook.react.bridge.WritableMap;
|
|
18
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
19
|
+
|
|
20
|
+
import java.io.ByteArrayInputStream;
|
|
21
|
+
import java.io.IOException;
|
|
22
|
+
import java.nio.charset.StandardCharsets;
|
|
23
|
+
import java.util.ArrayList;
|
|
24
|
+
import java.util.HashMap;
|
|
25
|
+
import java.util.List;
|
|
26
|
+
import java.util.Map;
|
|
27
|
+
import java.util.concurrent.TimeUnit;
|
|
28
|
+
|
|
29
|
+
import javax.net.ssl.X509TrustManager;
|
|
30
|
+
|
|
31
|
+
import io.grpc.CallOptions;
|
|
32
|
+
import io.grpc.ClientCall;
|
|
33
|
+
import io.grpc.ConnectivityState;
|
|
34
|
+
import io.grpc.Grpc;
|
|
35
|
+
import io.grpc.ManagedChannel;
|
|
36
|
+
import io.grpc.ManagedChannelBuilder;
|
|
37
|
+
import io.grpc.Metadata;
|
|
38
|
+
import io.grpc.MethodDescriptor;
|
|
39
|
+
import io.grpc.Status;
|
|
40
|
+
import io.grpc.TlsChannelCredentials;
|
|
41
|
+
|
|
42
|
+
public class GrpcModule extends ReactContextBaseJavaModule {
|
|
43
|
+
private final ReactApplicationContext context;
|
|
44
|
+
private final HashMap<Integer, ClientCall> callsMap = new HashMap<>();
|
|
45
|
+
|
|
46
|
+
private String host;
|
|
47
|
+
private boolean isInsecure = false;
|
|
48
|
+
private boolean withCompression = false;
|
|
49
|
+
private String compressorName = "";
|
|
50
|
+
private Integer responseSizeLimit = null;
|
|
51
|
+
private boolean keepAliveEnabled = false;
|
|
52
|
+
private Integer keepAliveTime;
|
|
53
|
+
private Integer keepAliveTimeout;
|
|
54
|
+
private boolean isUiLogEnabled = false;
|
|
55
|
+
/** Per-call deadline in seconds (0 = no deadline). Default 120s. */
|
|
56
|
+
private long callDeadlineSeconds = 120;
|
|
57
|
+
|
|
58
|
+
/** Custom PEM trust roots; null = platform/gRPC defaults. */
|
|
59
|
+
private String rootCertsPem = null;
|
|
60
|
+
/** PEM client certificate chain for mTLS. */
|
|
61
|
+
private String certificateChainPem = null;
|
|
62
|
+
/** PEM client private key for mTLS. */
|
|
63
|
+
private String privateKeyPem = null;
|
|
64
|
+
/** TLS hostname / SNI override (e.g. dial by IP). */
|
|
65
|
+
private String hostNameOverride = null;
|
|
66
|
+
/** SHA-256 SPKI pins (base64, without sha256/ prefix). */
|
|
67
|
+
private List<String> spkiSha256Pins = null;
|
|
68
|
+
|
|
69
|
+
private ManagedChannel managedChannel = null;
|
|
70
|
+
|
|
71
|
+
public GrpcModule(ReactApplicationContext context) {
|
|
72
|
+
this.context = context;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
@NonNull
|
|
76
|
+
@Override
|
|
77
|
+
public String getName() {
|
|
78
|
+
return "Grpc";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@ReactMethod()
|
|
82
|
+
public void getHost(final Promise promise) {
|
|
83
|
+
promise.resolve(this.host);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@ReactMethod()
|
|
87
|
+
public void getIsInsecure(final Promise promise) {
|
|
88
|
+
promise.resolve(this.isInsecure);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
@ReactMethod
|
|
92
|
+
public void setHost(String host) {
|
|
93
|
+
this.host = host;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
@ReactMethod
|
|
97
|
+
public void setInsecure(boolean insecure) {
|
|
98
|
+
this.isInsecure = insecure;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Replace TLS configuration (custom CA / mTLS / hostname override).
|
|
103
|
+
* Omitted or null fields clear the previous value. Apply before initGrpcChannel().
|
|
104
|
+
*/
|
|
105
|
+
@ReactMethod
|
|
106
|
+
public void setTlsOptions(ReadableMap options) {
|
|
107
|
+
this.rootCertsPem = readOptionalString(options, "rootCertsPem");
|
|
108
|
+
this.certificateChainPem = readOptionalString(options, "certificateChainPem");
|
|
109
|
+
this.privateKeyPem = readOptionalString(options, "privateKeyPem");
|
|
110
|
+
this.hostNameOverride = readOptionalString(options, "hostNameOverride");
|
|
111
|
+
this.spkiSha256Pins = SpkiPinTrustManager.normalizePins(
|
|
112
|
+
readOptionalStringList(options, "spkiSha256Pins")
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
boolean hasCert = this.certificateChainPem != null && !this.certificateChainPem.isEmpty();
|
|
116
|
+
boolean hasKey = this.privateKeyPem != null && !this.privateKeyPem.isEmpty();
|
|
117
|
+
if (hasCert != hasKey) {
|
|
118
|
+
throw new IllegalArgumentException(
|
|
119
|
+
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private static String readOptionalString(ReadableMap options, String key) {
|
|
125
|
+
if (options == null || !options.hasKey(key) || options.isNull(key)) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
String value = options.getString(key);
|
|
129
|
+
if (value == null || value.isEmpty()) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
private static List<String> readOptionalStringList(ReadableMap options, String key) {
|
|
136
|
+
if (options == null || !options.hasKey(key) || options.isNull(key)) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
ReadableArray array = options.getArray(key);
|
|
140
|
+
if (array == null || array.size() == 0) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
List<String> values = new ArrayList<>();
|
|
144
|
+
for (int i = 0; i < array.size(); i++) {
|
|
145
|
+
String value = array.getString(i);
|
|
146
|
+
if (value != null && !value.isEmpty()) {
|
|
147
|
+
values.add(value);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return values.isEmpty() ? null : values;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
@ReactMethod
|
|
154
|
+
public void setCompression(Boolean enable, String compressorName) {
|
|
155
|
+
this.withCompression = enable;
|
|
156
|
+
this.compressorName = compressorName;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@ReactMethod
|
|
160
|
+
public void setResponseSizeLimit(int limit) {
|
|
161
|
+
this.responseSizeLimit = limit;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
@ReactMethod
|
|
165
|
+
public void setKeepAlive(boolean enabled, int time, int timeout) {
|
|
166
|
+
this.keepAliveEnabled = enabled;
|
|
167
|
+
this.keepAliveTime = time;
|
|
168
|
+
this.keepAliveTimeout = timeout;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
@ReactMethod
|
|
172
|
+
public void setCallDeadlineSeconds(double seconds) {
|
|
173
|
+
this.callDeadlineSeconds = Math.max(0, (long) seconds);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
@ReactMethod
|
|
177
|
+
public void unaryCall(int id, String path, ReadableMap obj, ReadableMap headers, final Promise promise) {
|
|
178
|
+
ClientCall call;
|
|
179
|
+
|
|
180
|
+
try {
|
|
181
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.UNARY, headers, resolveDeadline(obj));
|
|
182
|
+
} catch (Exception e) {
|
|
183
|
+
promise.reject(e);
|
|
184
|
+
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
189
|
+
|
|
190
|
+
call.sendMessage(data);
|
|
191
|
+
call.request(1);
|
|
192
|
+
call.halfClose();
|
|
193
|
+
|
|
194
|
+
callsMap.put(id, call);
|
|
195
|
+
|
|
196
|
+
promise.resolve(null);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
@ReactMethod
|
|
200
|
+
public void serverStreamingCall(int id, String path, ReadableMap obj, ReadableMap headers, final Promise promise) {
|
|
201
|
+
ClientCall call;
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.SERVER_STREAMING, headers, resolveDeadline(obj));
|
|
205
|
+
} catch (Exception e) {
|
|
206
|
+
promise.reject(e);
|
|
207
|
+
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
212
|
+
|
|
213
|
+
call.sendMessage(data);
|
|
214
|
+
call.request(1);
|
|
215
|
+
call.halfClose();
|
|
216
|
+
|
|
217
|
+
callsMap.put(id, call);
|
|
218
|
+
|
|
219
|
+
promise.resolve(null);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
@ReactMethod
|
|
223
|
+
public void clientStreamingCall(int id, String path, ReadableMap obj, ReadableMap headers, final Promise promise) {
|
|
224
|
+
ClientCall call = callsMap.get(id);
|
|
225
|
+
|
|
226
|
+
if (call == null) {
|
|
227
|
+
try {
|
|
228
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.CLIENT_STREAMING, headers, resolveDeadline(obj));
|
|
229
|
+
} catch (Exception e) {
|
|
230
|
+
promise.reject(e);
|
|
231
|
+
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
callsMap.put(id, call);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
239
|
+
|
|
240
|
+
call.sendMessage(data);
|
|
241
|
+
call.request(1);
|
|
242
|
+
|
|
243
|
+
promise.resolve(null);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
@ReactMethod
|
|
247
|
+
public void bidiStreamingCall(int id, String path, ReadableMap obj, ReadableMap headers, final Promise promise) {
|
|
248
|
+
ClientCall call = callsMap.get(id);
|
|
249
|
+
|
|
250
|
+
if (call == null) {
|
|
251
|
+
try {
|
|
252
|
+
call = this.startGrpcCall(id, path, MethodDescriptor.MethodType.BIDI_STREAMING, headers, resolveDeadline(obj));
|
|
253
|
+
} catch (Exception e) {
|
|
254
|
+
promise.reject(e);
|
|
255
|
+
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
callsMap.put(id, call);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
byte[] data = Base64.decode(obj.getString("data"), Base64.NO_WRAP);
|
|
263
|
+
|
|
264
|
+
call.sendMessage(data);
|
|
265
|
+
call.request(1);
|
|
266
|
+
|
|
267
|
+
promise.resolve(null);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
@ReactMethod
|
|
271
|
+
public void finishClientStreaming(int id, final Promise promise) {
|
|
272
|
+
if (callsMap.containsKey(id)) {
|
|
273
|
+
ClientCall call = callsMap.get(id);
|
|
274
|
+
|
|
275
|
+
call.halfClose();
|
|
276
|
+
|
|
277
|
+
promise.resolve(true);
|
|
278
|
+
} else {
|
|
279
|
+
promise.resolve(false);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
@ReactMethod
|
|
284
|
+
public void cancelGrpcCall(int id, final Promise promise) {
|
|
285
|
+
if (callsMap.containsKey(id)) {
|
|
286
|
+
ClientCall call = callsMap.get(id);
|
|
287
|
+
call.cancel("Cancelled", new Exception("Cancelled by app"));
|
|
288
|
+
|
|
289
|
+
promise.resolve(true);
|
|
290
|
+
} else {
|
|
291
|
+
promise.resolve(false);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Resolve per-call deadline: obj.deadlineSeconds if set, else global default. */
|
|
296
|
+
private long resolveDeadline(ReadableMap obj) {
|
|
297
|
+
if (obj != null && obj.hasKey("deadlineSeconds") && !obj.isNull("deadlineSeconds")) {
|
|
298
|
+
return Math.max(0, (long) obj.getDouble("deadlineSeconds"));
|
|
299
|
+
}
|
|
300
|
+
return this.callDeadlineSeconds;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private ClientCall startGrpcCall(
|
|
304
|
+
int id,
|
|
305
|
+
String path,
|
|
306
|
+
MethodDescriptor.MethodType methodType,
|
|
307
|
+
ReadableMap headers,
|
|
308
|
+
long deadlineSeconds
|
|
309
|
+
) throws Exception {
|
|
310
|
+
if (this.managedChannel == null) {
|
|
311
|
+
throw new Exception("Channel not created");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
path = normalizePath(path);
|
|
315
|
+
|
|
316
|
+
final Metadata headersMetadata = new Metadata();
|
|
317
|
+
|
|
318
|
+
for (Map.Entry<String, Object> headerEntry : headers.toHashMap().entrySet()) {
|
|
319
|
+
headersMetadata.put(Metadata.Key.of(headerEntry.getKey(), Metadata.ASCII_STRING_MARSHALLER), headerEntry.getValue().toString());
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
MethodDescriptor.Marshaller<byte[]> marshaller = new GrpcMarshaller();
|
|
323
|
+
|
|
324
|
+
MethodDescriptor descriptor = MethodDescriptor.<byte[], byte[]>newBuilder()
|
|
325
|
+
.setFullMethodName(path)
|
|
326
|
+
.setType(methodType)
|
|
327
|
+
.setRequestMarshaller(marshaller)
|
|
328
|
+
.setResponseMarshaller(marshaller)
|
|
329
|
+
.build();
|
|
330
|
+
|
|
331
|
+
CallOptions callOptions = CallOptions.DEFAULT;
|
|
332
|
+
|
|
333
|
+
if (!this.compressorName.isEmpty()) {
|
|
334
|
+
callOptions = callOptions.withCompression(this.compressorName);
|
|
335
|
+
}
|
|
336
|
+
if (deadlineSeconds > 0) {
|
|
337
|
+
callOptions = callOptions.withDeadlineAfter(deadlineSeconds, TimeUnit.SECONDS);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
ClientCall call = this.managedChannel.newCall(descriptor, callOptions);
|
|
341
|
+
|
|
342
|
+
call.start(new ClientCall.Listener() {
|
|
343
|
+
@Override
|
|
344
|
+
public void onHeaders(Metadata headers) {
|
|
345
|
+
super.onHeaders(headers);
|
|
346
|
+
|
|
347
|
+
WritableMap event = Arguments.createMap();
|
|
348
|
+
WritableMap payload = Arguments.createMap();
|
|
349
|
+
|
|
350
|
+
for (String key : headers.keys()) {
|
|
351
|
+
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
|
|
352
|
+
byte[] data = headers.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
|
|
353
|
+
|
|
354
|
+
payload.putString(key, new String(Base64.encode(data, Base64.NO_WRAP)));
|
|
355
|
+
} else if (!key.startsWith(":")) {
|
|
356
|
+
String data = headers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
|
|
357
|
+
|
|
358
|
+
payload.putString(key, data);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
event.putInt("id", id);
|
|
363
|
+
event.putString("type", "headers");
|
|
364
|
+
event.putMap("payload", payload);
|
|
365
|
+
|
|
366
|
+
emitEvent("grpc-call", event);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
@Override
|
|
370
|
+
public void onMessage(Object messageObj) {
|
|
371
|
+
super.onMessage(messageObj);
|
|
372
|
+
|
|
373
|
+
byte[] data = (byte[]) messageObj;
|
|
374
|
+
|
|
375
|
+
WritableMap event = Arguments.createMap();
|
|
376
|
+
|
|
377
|
+
event.putInt("id", id);
|
|
378
|
+
event.putString("type", "response");
|
|
379
|
+
event.putString("payload", Base64.encodeToString(data, Base64.NO_WRAP));
|
|
380
|
+
|
|
381
|
+
emitEvent("grpc-call", event);
|
|
382
|
+
|
|
383
|
+
if (methodType == MethodDescriptor.MethodType.SERVER_STREAMING
|
|
384
|
+
|| methodType == MethodDescriptor.MethodType.BIDI_STREAMING) {
|
|
385
|
+
call.request(1);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
@Override
|
|
390
|
+
public void onClose(Status status, Metadata trailers) {
|
|
391
|
+
super.onClose(status, trailers);
|
|
392
|
+
|
|
393
|
+
callsMap.remove(id);
|
|
394
|
+
|
|
395
|
+
WritableMap event = Arguments.createMap();
|
|
396
|
+
event.putInt("id", id);
|
|
397
|
+
|
|
398
|
+
WritableMap trailersMap = Arguments.createMap();
|
|
399
|
+
|
|
400
|
+
for (String key : trailers.keys()) {
|
|
401
|
+
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
|
|
402
|
+
byte[] data = trailers.get(Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER));
|
|
403
|
+
|
|
404
|
+
trailersMap.putString(key, new String(Base64.encode(data, Base64.NO_WRAP)));
|
|
405
|
+
} else if (!key.startsWith(":")) {
|
|
406
|
+
String data = trailers.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
|
|
407
|
+
|
|
408
|
+
trailersMap.putString(key, data);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (!status.isOk()) {
|
|
413
|
+
event.putString("type", "error");
|
|
414
|
+
event.putString("error", status.asException(trailers).getLocalizedMessage());
|
|
415
|
+
event.putInt("code", status.getCode().value());
|
|
416
|
+
event.putMap("trailers", trailersMap);
|
|
417
|
+
} else {
|
|
418
|
+
event.putString("type", "trailers");
|
|
419
|
+
event.putMap("payload", trailersMap);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
emitEvent("grpc-call", event);
|
|
423
|
+
}
|
|
424
|
+
}, headersMetadata);
|
|
425
|
+
|
|
426
|
+
if (this.withCompression) {
|
|
427
|
+
call.setMessageCompression(true);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return call;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
@ReactMethod
|
|
434
|
+
public void addListener(String eventName) {
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
@ReactMethod
|
|
438
|
+
public void removeListeners(Integer count) {
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
private void emitEvent(String eventName, Object params) {
|
|
442
|
+
context
|
|
443
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
444
|
+
.emit(eventName, params);
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
private static String normalizePath(String path) {
|
|
448
|
+
if (path.startsWith("/")) {
|
|
449
|
+
path = path.substring(1);
|
|
450
|
+
}
|
|
451
|
+
return path;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
@ReactMethod
|
|
455
|
+
public void initGrpcChannel() {
|
|
456
|
+
if (this.managedChannel != null) {
|
|
457
|
+
this.managedChannel.shutdown();
|
|
458
|
+
}
|
|
459
|
+
this.managedChannel = createManagedChannel();
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
private ManagedChannel createManagedChannel() {
|
|
463
|
+
ManagedChannelBuilder<?> channelBuilder;
|
|
464
|
+
|
|
465
|
+
if (this.isInsecure) {
|
|
466
|
+
channelBuilder = ManagedChannelBuilder.forTarget(this.host).usePlaintext();
|
|
467
|
+
} else {
|
|
468
|
+
boolean hasRoots = this.rootCertsPem != null && !this.rootCertsPem.isEmpty();
|
|
469
|
+
boolean hasCert = this.certificateChainPem != null && !this.certificateChainPem.isEmpty();
|
|
470
|
+
boolean hasKey = this.privateKeyPem != null && !this.privateKeyPem.isEmpty();
|
|
471
|
+
boolean hasPins = this.spkiSha256Pins != null && !this.spkiSha256Pins.isEmpty();
|
|
472
|
+
|
|
473
|
+
if (hasCert != hasKey) {
|
|
474
|
+
throw new IllegalArgumentException(
|
|
475
|
+
"mTLS requires both certificateChainPem and privateKeyPem"
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
if (hasRoots || hasCert || hasPins) {
|
|
480
|
+
try {
|
|
481
|
+
TlsChannelCredentials.Builder tls = TlsChannelCredentials.newBuilder();
|
|
482
|
+
if (hasRoots || hasPins) {
|
|
483
|
+
X509TrustManager base = hasRoots
|
|
484
|
+
? TrustManagers.fromPemRoots(this.rootCertsPem)
|
|
485
|
+
: TrustManagers.systemTrustManager();
|
|
486
|
+
if (hasPins) {
|
|
487
|
+
tls.trustManager(new SpkiPinTrustManager(base, this.spkiSha256Pins));
|
|
488
|
+
} else {
|
|
489
|
+
tls.trustManager(base);
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (hasCert) {
|
|
493
|
+
tls.keyManager(
|
|
494
|
+
new ByteArrayInputStream(this.certificateChainPem.getBytes(StandardCharsets.UTF_8)),
|
|
495
|
+
new ByteArrayInputStream(this.privateKeyPem.getBytes(StandardCharsets.UTF_8))
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
channelBuilder = Grpc.newChannelBuilder(this.host, tls.build());
|
|
499
|
+
} catch (IOException e) {
|
|
500
|
+
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
501
|
+
} catch (RuntimeException e) {
|
|
502
|
+
throw e;
|
|
503
|
+
} catch (Exception e) {
|
|
504
|
+
throw new RuntimeException("Failed to configure TLS credentials", e);
|
|
505
|
+
}
|
|
506
|
+
} else {
|
|
507
|
+
channelBuilder = ManagedChannelBuilder.forTarget(this.host);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
if (this.hostNameOverride != null && !this.hostNameOverride.isEmpty()) {
|
|
511
|
+
channelBuilder = channelBuilder.overrideAuthority(this.hostNameOverride);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (this.responseSizeLimit != null) {
|
|
516
|
+
channelBuilder = channelBuilder.maxInboundMessageSize(this.responseSizeLimit);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
if (this.keepAliveEnabled) {
|
|
520
|
+
channelBuilder = channelBuilder
|
|
521
|
+
.keepAliveWithoutCalls(true)
|
|
522
|
+
.keepAliveTime(keepAliveTime, TimeUnit.SECONDS)
|
|
523
|
+
.keepAliveTimeout(keepAliveTimeout, TimeUnit.SECONDS);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
managedChannel = channelBuilder.build();
|
|
527
|
+
return managedChannel;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
@ReactMethod
|
|
531
|
+
public void resetConnection(final String message){
|
|
532
|
+
if(null == managedChannel) return;
|
|
533
|
+
|
|
534
|
+
this.managedChannel.resetConnectBackoff();
|
|
535
|
+
|
|
536
|
+
this.initGrpcChannel();
|
|
537
|
+
|
|
538
|
+
showToast("resetConnection "+message);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
@ReactMethod
|
|
542
|
+
public void onConnectionStateChange(){
|
|
543
|
+
if(null == managedChannel) return;
|
|
544
|
+
|
|
545
|
+
final ConnectivityState connectivityState = managedChannel.getState(true);
|
|
546
|
+
if(ConnectivityState.CONNECTING == connectivityState){
|
|
547
|
+
showToast("onConnectionState CONNECTING");
|
|
548
|
+
} else if(ConnectivityState.IDLE == connectivityState){
|
|
549
|
+
showToast("onConnectionState IDLE");
|
|
550
|
+
} else if(ConnectivityState.READY == connectivityState){
|
|
551
|
+
showToast("onConnectionState READY");
|
|
552
|
+
} else if(ConnectivityState.TRANSIENT_FAILURE == connectivityState){
|
|
553
|
+
showToast("onConnectionState TRANSIENT_FAILURE");
|
|
554
|
+
} else if(ConnectivityState.SHUTDOWN == connectivityState){
|
|
555
|
+
showToast("onConnectionState SHUTDOWN");
|
|
556
|
+
} else {
|
|
557
|
+
showToast("onConnectionState UNDEFINED");
|
|
558
|
+
}
|
|
559
|
+
if(ConnectivityState.TRANSIENT_FAILURE == connectivityState && managedChannel.isTerminated() || managedChannel.isShutdown()){
|
|
560
|
+
resetConnection("onConnectionStateChange");
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
@ReactMethod
|
|
565
|
+
public void enterIdle(){
|
|
566
|
+
if(null == managedChannel) return;
|
|
567
|
+
|
|
568
|
+
managedChannel.enterIdle();
|
|
569
|
+
|
|
570
|
+
showToast("enterIdle");
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
@ReactMethod
|
|
574
|
+
public void setUiLogEnabled(boolean isUiLogEnabled){
|
|
575
|
+
this.isUiLogEnabled = isUiLogEnabled;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
@UiThread
|
|
579
|
+
private void showToast(final String message){
|
|
580
|
+
if(!isUiLogEnabled || null == context) return;
|
|
581
|
+
|
|
582
|
+
Toast.makeText(context,message,Toast.LENGTH_SHORT).show();
|
|
583
|
+
Log.d("GRPC_MODULE",message);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
package com.reactnativegrpc;
|
|
2
|
+
|
|
3
|
+
import androidx.annotation.NonNull;
|
|
4
|
+
|
|
5
|
+
import com.facebook.react.ReactPackage;
|
|
6
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
+
import com.facebook.react.uimanager.ViewManager;
|
|
8
|
+
|
|
9
|
+
import java.util.ArrayList;
|
|
10
|
+
import java.util.List;
|
|
11
|
+
|
|
12
|
+
public class GrpcPackage implements ReactPackage {
|
|
13
|
+
@NonNull
|
|
14
|
+
@Override
|
|
15
|
+
public ArrayList createNativeModules(@NonNull ReactApplicationContext reactContext) {
|
|
16
|
+
ArrayList modules = new ArrayList();
|
|
17
|
+
modules.add(new GrpcModule(reactContext));
|
|
18
|
+
|
|
19
|
+
return modules;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@NonNull
|
|
23
|
+
@Override
|
|
24
|
+
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
|
|
25
|
+
return new ArrayList();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
package com.reactnativegrpc;
|
|
2
|
+
|
|
3
|
+
import android.util.Base64;
|
|
4
|
+
|
|
5
|
+
import java.security.MessageDigest;
|
|
6
|
+
import java.security.cert.CertificateException;
|
|
7
|
+
import java.security.cert.X509Certificate;
|
|
8
|
+
import java.util.ArrayList;
|
|
9
|
+
import java.util.Collections;
|
|
10
|
+
import java.util.LinkedHashSet;
|
|
11
|
+
import java.util.List;
|
|
12
|
+
import java.util.Set;
|
|
13
|
+
|
|
14
|
+
import javax.net.ssl.X509TrustManager;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Delegates chain validation to {@code base}, then requires at least one
|
|
18
|
+
* certificate in the chain to match a SHA-256 SPKI pin (OkHttp-compatible).
|
|
19
|
+
*/
|
|
20
|
+
final class SpkiPinTrustManager implements X509TrustManager {
|
|
21
|
+
private final X509TrustManager base;
|
|
22
|
+
private final Set<String> pins;
|
|
23
|
+
|
|
24
|
+
SpkiPinTrustManager(X509TrustManager base, List<String> pins) {
|
|
25
|
+
if (base == null) {
|
|
26
|
+
throw new IllegalArgumentException("base trust manager required");
|
|
27
|
+
}
|
|
28
|
+
if (pins == null || pins.isEmpty()) {
|
|
29
|
+
throw new IllegalArgumentException("SPKI pins required");
|
|
30
|
+
}
|
|
31
|
+
this.base = base;
|
|
32
|
+
this.pins = Collections.unmodifiableSet(new LinkedHashSet<>(pins));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@Override
|
|
36
|
+
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
|
37
|
+
throws CertificateException {
|
|
38
|
+
base.checkClientTrusted(chain, authType);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@Override
|
|
42
|
+
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
|
43
|
+
throws CertificateException {
|
|
44
|
+
base.checkServerTrusted(chain, authType);
|
|
45
|
+
if (!chainMatchesPins(chain, pins)) {
|
|
46
|
+
throw new CertificateException("Certificate SPKI pin mismatch");
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@Override
|
|
51
|
+
public X509Certificate[] getAcceptedIssuers() {
|
|
52
|
+
return base.getAcceptedIssuers();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
static boolean chainMatchesPins(X509Certificate[] chain, Set<String> pins)
|
|
56
|
+
throws CertificateException {
|
|
57
|
+
if (chain == null || chain.length == 0) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
for (X509Certificate cert : chain) {
|
|
61
|
+
if (pins.contains(spkiSha256Base64(cert))) {
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
static String spkiSha256Base64(X509Certificate cert) throws CertificateException {
|
|
69
|
+
try {
|
|
70
|
+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
|
71
|
+
byte[] hash = digest.digest(cert.getPublicKey().getEncoded());
|
|
72
|
+
return Base64.encodeToString(hash, Base64.NO_WRAP);
|
|
73
|
+
} catch (Exception e) {
|
|
74
|
+
throw new CertificateException("Failed to compute SPKI pin", e);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Normalize pin strings: strip optional {@code sha256/} prefix; drop empties. */
|
|
79
|
+
static List<String> normalizePins(List<String> raw) {
|
|
80
|
+
if (raw == null || raw.isEmpty()) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
List<String> out = new ArrayList<>();
|
|
84
|
+
for (String pin : raw) {
|
|
85
|
+
if (pin == null) {
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
String trimmed = pin.trim();
|
|
89
|
+
if (trimmed.isEmpty()) {
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (trimmed.regionMatches(true, 0, "sha256/", 0, 7)) {
|
|
93
|
+
trimmed = trimmed.substring(7).trim();
|
|
94
|
+
}
|
|
95
|
+
if (!trimmed.isEmpty()) {
|
|
96
|
+
out.add(trimmed);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out.isEmpty() ? null : out;
|
|
100
|
+
}
|
|
101
|
+
}
|