forked from Rapsssito/react-native-tcp-socket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpSocketClient.java
283 lines (253 loc) · 10.6 KB
/
TcpSocketClient.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.asterinet.react.tcpsocket;
import android.content.Context;
import android.net.Network;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableArray;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
class TcpSocketClient extends TcpSocket {
private final ExecutorService listenExecutor;
private final ExecutorService writeExecutor;
private final TcpEventListener receiverListener;
private TcpReceiverTask receiverTask;
private Socket socket;
private boolean closed = true;
TcpSocketClient(TcpEventListener receiverListener, Integer id, Socket socket) {
super(id);
listenExecutor = Executors.newSingleThreadExecutor();
writeExecutor = Executors.newSingleThreadExecutor();
this.socket = socket;
this.receiverListener = receiverListener;
}
public Socket getSocket() {
return socket;
}
public void connect(Context context, String address, final Integer port, ReadableMap options, Network network, ReadableMap tlsOptions) throws IOException, GeneralSecurityException {
if (socket != null) throw new IOException("Already connected");
if (tlsOptions != null) {
SSLSocketFactory ssf = getSSLSocketFactory(context, tlsOptions);
socket = ssf.createSocket();
((SSLSocket) socket).setUseClientMode(true);
} else {
socket = new Socket();
}
// Get the addresses
final String localAddress = options.hasKey("localAddress") ? options.getString("localAddress") : "0.0.0.0";
final InetAddress localInetAddress = InetAddress.getByName(localAddress);
final InetAddress remoteInetAddress = InetAddress.getByName(address);
if (network != null)
network.bindSocket(socket);
// setReuseAddress
if (options.hasKey("reuseAddress")) {
boolean reuseAddress = options.getBoolean("reuseAddress");
socket.setReuseAddress(reuseAddress);
} else {
// Default to true
socket.setReuseAddress(true);
}
final int localPort = options.hasKey("localPort") ? options.getInt("localPort") : 0;
// bind
socket.bind(new InetSocketAddress(localInetAddress, localPort));
socket.connect(new InetSocketAddress(remoteInetAddress, port));
if (socket instanceof SSLSocket) ((SSLSocket) socket).startHandshake();
startListening();
}
public void startTLS(Context context, ReadableMap tlsOptions) throws IOException, GeneralSecurityException {
if (socket instanceof SSLSocket) return;
SSLSocketFactory ssf = getSSLSocketFactory(context, tlsOptions);
SSLSocket sslSocket = (SSLSocket) ssf.createSocket(socket, socket.getInetAddress().getHostAddress(), socket.getPort(), true);
sslSocket.setUseClientMode(true);
sslSocket.startHandshake();
socket = sslSocket;
}
private boolean containsKey(ReadableArray array, String key) {
for (int i = 0; i < array.size(); i++) {
if (array.getString(i).equals(key)) {
return true;
}
}
return false;
}
private ResolvableOption getResolvableOption(ReadableMap tlsOptions, String key) {
if (tlsOptions.hasKey(key)) {
String value = tlsOptions.getString(key);
if (value == null || value.isEmpty()) {
return null;
}
ReadableArray resolvedKeys = tlsOptions.hasKey("resolvedKeys") ? tlsOptions.getArray("resolvedKeys") : null;
boolean needsResolution = resolvedKeys != null && containsKey(resolvedKeys, key);
return new ResolvableOption(value, needsResolution);
}
return null;
}
private SSLSocketFactory getSSLSocketFactory(Context context, ReadableMap tlsOptions) throws GeneralSecurityException, IOException {
SSLSocketFactory ssf = null;
final ResolvableOption customTlsCa = getResolvableOption(tlsOptions, "ca");
final ResolvableOption customTlsKey = getResolvableOption(tlsOptions, "key");
final ResolvableOption customTlsCert = getResolvableOption(tlsOptions, "cert");
final String keystoreName = tlsOptions.hasKey("androidKeyStore") ? tlsOptions.getString("androidKeyStore") : "";
final String caAlias = tlsOptions.hasKey("caAlias") ? tlsOptions.getString("caAlias") : "";
final String keyAlias = tlsOptions.hasKey("keyAlias") ? tlsOptions.getString("keyAlias") : "";
final String certAlias = tlsOptions.hasKey("certAlias") ? tlsOptions.getString("certAlias") : "";
final KeystoreInfo keystoreInfo = new KeystoreInfo(keystoreName, caAlias, certAlias, keyAlias);
if (tlsOptions.hasKey("rejectUnauthorized") && !tlsOptions.getBoolean("rejectUnauthorized")) {
if ((customTlsKey != null && customTlsCert != null) ||
(keyAlias != null && !keyAlias.isEmpty() && customTlsKey == null) ) {
ssf = SSLCertificateHelper.createCustomTrustedSocketFactory(
context,
customTlsCa,
customTlsKey,
customTlsCert,
keystoreInfo
);
} else {
ssf = SSLCertificateHelper.createBlindSocketFactory();
}
} else {
ssf = (customTlsCa != null)
? SSLCertificateHelper.createCustomTrustedSocketFactory(
context,
customTlsCa,
customTlsKey,
customTlsCert,
keystoreInfo
)
: (SSLSocketFactory) SSLSocketFactory.getDefault();
}
return ssf;
}
public void startListening() {
receiverTask = new TcpReceiverTask(this, receiverListener);
listenExecutor.execute(receiverTask);
}
/**
* Sends data from the socket
*
* @param data data to be sent
*/
public void write(final int msgId, final byte[] data) {
writeExecutor.execute(new Runnable() {
@Override
public void run() {
if (socket == null) {
receiverListener.onError(getId(), new IOException("Attempted to write to closed socket"));
return;
}
try {
socket.getOutputStream().write(data);
receiverListener.onWritten(getId(), msgId, null);
} catch (IOException e) {
receiverListener.onWritten(getId(), msgId, e);
receiverListener.onError(getId(), e);
}
}
});
}
public ReadableMap getPeerCertificate() {
return SSLCertificateHelper.getCertificateInfo(socket, true);
}
public ReadableMap getCertificate() {
return SSLCertificateHelper.getCertificateInfo(socket, false);
}
/**
* Shuts down the receiver task, closing the socket.
*/
public void destroy() {
try {
// close the socket
if (socket != null && !socket.isClosed()) {
closed = true;
socket.close();
receiverListener.onClose(getId(), null);
socket = null;
}
} catch (IOException e) {
receiverListener.onClose(getId(), e);
}
}
/**
* @param noDelay `true` will disable Nagle's algorithm for the socket (enable TCP_NODELAY)
*/
public void setNoDelay(final boolean noDelay) throws IOException {
if (socket == null) {
throw new IOException("Socket is not connected.");
}
socket.setTcpNoDelay(noDelay);
}
/**
* @param enable `true` to enable keep-alive functionality
*/
public void setKeepAlive(final boolean enable, final int initialDelay) throws IOException {
if (socket == null) {
throw new IOException("Socket is not connected.");
}
// `initialDelay` is ignored
socket.setKeepAlive(enable);
}
public void pause() {
receiverTask.pause();
}
public void resume() {
receiverTask.resume();
}
/**
* This is a specialized Runnable that receives data from a socket in the background, and
* notifies it's listener when data is received. This is not threadsafe, the listener
* should handle synchronicity.
*/
private static class TcpReceiverTask implements Runnable {
private final TcpSocketClient clientSocket;
private final TcpEventListener receiverListener;
private boolean paused = false;
public TcpReceiverTask(TcpSocketClient clientSocket, TcpEventListener receiverListener) {
this.clientSocket = clientSocket;
this.receiverListener = receiverListener;
}
/**
* An infinite loop to block and read data from the socket.
*/
@Override
public void run() {
int socketId = clientSocket.getId();
Socket socket = clientSocket.getSocket();
byte[] buffer = new byte[16384];
try {
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
while (!socket.isClosed()) {
int bufferCount = in.read(buffer);
waitIfPaused();
if (bufferCount > 0) {
receiverListener.onData(socketId, Arrays.copyOfRange(buffer, 0, bufferCount));
} else if (bufferCount == -1) {
clientSocket.destroy();
}
}
} catch (IOException | InterruptedException ioe) {
if (receiverListener != null && !socket.isClosed() && !clientSocket.closed) {
receiverListener.onError(socketId, ioe);
}
}
}
public synchronized void pause() {
paused = true;
}
public synchronized void resume() {
paused = false;
notify();
}
private synchronized void waitIfPaused() throws InterruptedException {
while (paused) {
wait();
}
}
}
}