Skip to content

Commit 8526e92

Browse files
authored
Add proxy support for iot connections (#1195)
1 parent 4823774 commit 8526e92

File tree

6 files changed

+512
-3
lines changed

6 files changed

+512
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Release 2.16.3](https://github.com/aws/aws-sdk-android/releases/tag/release_v2.16.3)
44

5+
### New Features
6+
7+
- **AWS IoT**
8+
- Added proxy support for connecting to AWS IoT via Keystore over port 8883
9+
510
### Bug Fixes
611

712
- **AWS Mobile Client**

aws-android-sdk-iot/src/main/java/com/amazonaws/mobileconnectors/iot/AWSIotMqttManager.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
* Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -181,6 +181,12 @@ public class AWSIotMqttManager {
181181
*/
182182
private final String endpoint;
183183

184+
/**
185+
* This is the hostname and port of the proxy, if any, to be used for connecting to AWS IoT
186+
*/
187+
private String proxyHost;
188+
private int proxyPort;
189+
184190
/**
185191
* Turning on/off metrics collection. By default metrics collection is enabled.
186192
* Client must call this to set metrics collection to false before calling connect in order to turn
@@ -735,10 +741,32 @@ public void connectUsingALPN(KeyStore keyStore,
735741
connect(keyStore, 443, statusCallback);
736742
}
737743

744+
/**
745+
* Initializes the MQTT session and connects to the specified MQTT server
746+
* using certificate and private key in keystore on port 8883 via the proxy specified by a
747+
* host and port combination. Keystore should be created using
748+
* {@link com.amazonaws.mobileconnectors.iot.AWSIotKeystoreHelper} to setup the
749+
* certificate and key aliases as expected by the underlying socket helper library.
750+
*
751+
* @param keyStore A keystore containing an keystore with a certificate and
752+
* private key. Use IotKeystoreHelper to get keystore.
753+
* @param proxyHost hostname of the proxy
754+
* @param proxyPort proxy port number
755+
* @param statusCallback When new MQTT session status is received the
756+
* function of callback will be called with new connection
757+
* status.
758+
*/
759+
public void connectWithProxy(KeyStore keyStore, final String proxyHost, final int proxyPort,
760+
final AWSIotMqttClientStatusCallback statusCallback) {
761+
this.proxyHost = proxyHost;
762+
this.proxyPort = proxyPort;
763+
connect(keyStore, 8883, statusCallback);
764+
}
765+
738766
/**
739767
* Initializes the MQTT session and connects to the specified MQTT server
740768
* using certificate and private key in keystore on port 8883. Keystore should be created
741-
* using IotKeystoreHelper to setup the certificate and key aliases as
769+
* using {@link com.amazonaws.mobileconnectors.iot.AWSIotKeystoreHelper} to setup the certificate and key aliases as
742770
* expected by the underlying socket helper library.
743771
*
744772
* @param keyStore A keystore containing an keystore with a certificate and
@@ -800,7 +828,10 @@ private void connect(KeyStore keyStore, int portNumber, final AWSIotMqttClientSt
800828
mqttClient = new MqttAsyncClient(mqttBrokerURL, mqttClientId, new MemoryPersistence());
801829
}
802830

803-
final SocketFactory socketFactory = AWSIotSslUtility.getSocketFactoryWithKeyStore(keyStore, portNumber);
831+
final SocketFactory socketFactory = (proxyHost != null) ?
832+
AWSIotSslUtility.getSocketFactoryWithKeyStoreAndProxy(keyStore, portNumber, proxyHost, proxyPort):
833+
AWSIotSslUtility.getSocketFactoryWithKeyStore(keyStore, portNumber) ;
834+
804835
final MqttConnectOptions options = new MqttConnectOptions();
805836

806837
if (mqttLWT != null) {

0 commit comments

Comments
 (0)