Skip to content

Commit 75414e2

Browse files
committed
add ssl/tls support
1 parent 8a6d504 commit 75414e2

File tree

4 files changed

+73
-20
lines changed

4 files changed

+73
-20
lines changed

README.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66

77
## MQTT Features (inherit from native MQTT framework)
88
* Use [MQTT Framework](https://github.com/ckrey/MQTT-Client-Framework) for IOS, [Paho MQTT Client](https://eclipse.org/paho/clients/android/) for Android
9-
* Support both IOS and Android (now only support IOS)
10-
* SSL/TSL with 3 mode
11-
* Native library, support mqtt over tcp and mqtt over websocket
12-
* Auto reconnect
9+
* Support both IOS and Android
10+
* SSL/TLS
11+
* Native library, support mqtt over tcp
1312

1413
## Warning
15-
This library in progress developing, api may change, and not support android now
14+
This library in progress developing, api may change, SSL/TLS non verify
1615

1716
## Getting started
1817
### Mostly automatic install
@@ -76,7 +75,7 @@ var mqtt = require('react-native-mqtt');
7675

7776
/* create mqtt client */
7877
mqtt.createClient({
79-
uri: 'mqtt://test.mosquitto.org:1883',
78+
uri: 'mqtt://test.mosquitto.org:1883',
8079
clientId: 'your_client_id'
8180
}).then(function(client) {
8281

@@ -96,8 +95,8 @@ mqtt.createClient({
9695

9796
client.on('connect', function() {
9897
console.log('connected');
99-
client.subscribe('/device_00059E18/data', 1);
100-
client.publish('/device_00059E18/data', "test", 1, false);
98+
client.subscribe('/data', 0);
99+
client.publish('/data', "test", 0, false);
101100
});
102101

103102
client.connect();
@@ -106,13 +105,27 @@ mqtt.createClient({
106105
```
107106

108107
## API
109-
* `mqtt.Client(options)` with
110-
- `uri`: `protocol://host:port`, protocol is [mqtt | mqtts | ws | wss]
111-
- `host`: ipaddress or host name (overide by uri if set)
112-
- `port`: port number (overide by uri if set)
113-
- `tls`: true/false (overide by uri if set to mqtts or wss)
114-
115-
...
108+
* `mqtt.createClient(options)` create new client instance with `options`, async operation
109+
- `uri`: `protocol://host:port`, protocol is [mqtt | mqtts]
110+
- `host`: ipaddress or host name (override by uri if set)
111+
- `port`: port number (override by uri if set)
112+
- `tls`: true/false (override by uri if set to mqtts or wss)
113+
- `user`: string username
114+
- `pass`: string password
115+
- `auth`: true/false - override = true if `user` or `pass` exist
116+
- `clientId`: string client id
117+
- `keepalive`
118+
119+
* `client`
120+
- `on(event, callback)`: add event listener for
121+
+ event: `connect` - client connected
122+
+ event: `closed` - client disconnected
123+
+ event: `error` - error
124+
+ event: `message` - data received with format {topic, data, retain}
125+
- `connect`: begin connection
126+
- `disconnect`: disconnect
127+
- `subscribe(topic, qos)`
128+
- `publish(topic, payload, qos, retain)`
116129

117130
## Todo
118131

android/src/main/java/com/tuanpm/RCTMqtt/RCTMqtt.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.Map;
2121
import java.util.List;
2222
import javax.net.ssl.*;
23-
import java.security.KeyStore;
2423
import java.security.SecureRandom;
2524

2625
import org.eclipse.paho.client.mqttv3.IMqttActionListener;
@@ -37,6 +36,18 @@
3736

3837
import com.facebook.react.modules.core.DeviceEventManagerModule;
3938

39+
import java.security.KeyManagementException;
40+
import java.security.KeyStore;
41+
import java.security.KeyStoreException;
42+
import java.security.NoSuchAlgorithmException;
43+
import java.security.UnrecoverableKeyException;
44+
import java.security.cert.CertificateException;
45+
import java.security.cert.X509Certificate;
46+
47+
import javax.net.ssl.SSLContext;
48+
import javax.net.ssl.TrustManager;
49+
import javax.net.ssl.X509TrustManager;
50+
4051

4152
public class RCTMqtt implements MqttCallback{
4253
private static final String TAG = "RCTMqttModule";
@@ -128,6 +139,32 @@ private void createClient(final ReadableMap _options) {
128139
String uri = "tcp://";
129140
if(options.getBoolean("tls")) {
130141
uri = "ssl://";
142+
try {
143+
/*
144+
http://stackoverflow.com/questions/3761737/https-get-ssl-with-android-and-self-signed-server-certificate
145+
146+
WARNING: for anybody else arriving at this answer, this is a dirty,
147+
horrible hack and you must not use it for anything that matters.
148+
SSL/TLS without authentication is worse than no encryption at all -
149+
reading and modifying your "encrypted" data is trivial for an attacker and you wouldn't even know it was happening
150+
*/
151+
152+
SSLContext sslContext = SSLContext.getInstance("TLS");
153+
sslContext.init(null, new X509TrustManager[]{new X509TrustManager(){
154+
public void checkClientTrusted(X509Certificate[] chain,
155+
String authType) throws CertificateException {}
156+
public void checkServerTrusted(X509Certificate[] chain,
157+
String authType) throws CertificateException {}
158+
public X509Certificate[] getAcceptedIssuers() {
159+
return new X509Certificate[0];
160+
}}}, new SecureRandom());
161+
162+
mqttoptions.setSocketFactory(sslContext.getSocketFactory());
163+
} catch(Exception e) {
164+
165+
}
166+
167+
131168
}
132169
uri += options.getString("host") + ":";
133170
uri += options.getInt("port");

ios/RCTMqtt/Mqtt.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ - (void) connect {
6868

6969
self.manager = [[MQTTSessionManager alloc] init];
7070
self.manager.delegate = self;
71+
MQTTSSLSecurityPolicy *securityPolicy = nil;
72+
if(self.options[@"tls"]) {
73+
securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeNone];
74+
securityPolicy.allowInvalidCertificates = YES;
75+
}
7176

72-
// MQTTSSLSecurityPolicy *securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeNone];
73-
// securityPolicy.allowInvalidCertificates = YES;
7477

7578
NSData *willMsg = nil;
7679
if(self.options[@"willMsg"] != [NSNull null]) {
@@ -90,7 +93,7 @@ - (void) connect {
9093
willQos:(MQTTQosLevel)[self.options[@"willQos"] intValue]
9194
willRetainFlag:[self.options[@"willRetainFlag"] boolValue]
9295
withClientId:[self.options valueForKey:@"clientId"]
93-
securityPolicy:nil
96+
securityPolicy:securityPolicy
9497
certificates:nil
9598
];
9699

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-mqtt",
3-
"version": "0.1.0-beta2",
3+
"version": "0.1.0-beta3",
44
"description": "MQTT client for react-native",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)