Skip to content

Update proxy-aware samples with new proxy options #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 25 additions & 23 deletions samples/mqtt/basic_pub_sub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ static void s_printHelp()
stdout,
"signing_region: used for websocket signer it should only be specific if websockets are used. (required for "
"websockets)\n");
fprintf(stdout, "proxy_host: if you want to use a proxy with websockets, specify the host here (optional).\n");
fprintf(
stdout, "proxy_port: defaults to 8080 is proxy_host is set. Set this to any value you'd like (optional).\n");
fprintf(stdout, "proxy_host: host name of the http proxy to use (optional).\n");
fprintf(stdout, "proxy_port: port of the http proxy to use (optional).\n");

fprintf(stdout, " x509: Use the x509 credentials provider while using websockets (optional)\n");
fprintf(stdout, " x509_role_alias: Role alias to use with the x509 credentials provider (required for x509)\n");
Expand Down Expand Up @@ -155,15 +154,19 @@ int main(int argc, char *argv[])
}
useWebSocket = true;
signingRegion = s_getCmdOption(argv, argv + argc, "--signing_region");
}

if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
{
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
}
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
{
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
}

if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
{
int port = atoi(s_getCmdOption(argv, argv + argc, "--proxy_port"));
if (port > 0 && port <= UINT16_MAX)
{
proxyPort = static_cast<uint16_t>(atoi(s_getCmdOption(argv, argv + argc, "--proxy_port")));
proxyPort = static_cast<uint16_t>(port);
}
}

Expand Down Expand Up @@ -265,6 +268,14 @@ int main(int argc, char *argv[])
exit(-1);
}

Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
if (!proxyHost.empty())
{
proxyOptions.HostName = proxyHost;
proxyOptions.Port = proxyPort;
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
}

Aws::Crt::Io::TlsContext x509TlsCtx;
Aws::Iot::MqttClientConnectionConfigBuilder builder;

Expand All @@ -275,15 +286,6 @@ int main(int argc, char *argv[])
else if (useWebSocket)
{
std::shared_ptr<Aws::Crt::Auth::ICredentialsProvider> provider = nullptr;

Aws::Crt::Http::HttpClientConnectionProxyOptions proxyOptions;
if (!proxyHost.empty())
{
proxyOptions.HostName = proxyHost;
proxyOptions.Port = proxyPort;
proxyOptions.AuthType = Aws::Crt::Http::AwsHttpProxyAuthenticationType::None;
}

if (useX509)
{
Aws::Crt::Io::TlsContextOptions tlsCtxOptions =
Expand Down Expand Up @@ -351,18 +353,18 @@ int main(int argc, char *argv[])
}

Aws::Iot::WebsocketConfig config(signingRegion, provider);
if (!proxyHost.empty())
{
config.ProxyOptions = proxyOptions;
}

builder = Aws::Iot::MqttClientConnectionConfigBuilder(config);
}
else
{
s_printHelp();
}

if (!proxyHost.empty())
{
builder.WithHttpProxyOptions(proxyOptions);
}

if (!caFile.empty())
{
builder.WithCertificateAuthority(caFile.c_str());
Expand Down
25 changes: 15 additions & 10 deletions samples/mqtt/raw_pub_sub/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>
#include <aws/crt/UUID.h>
#include <condition_variable>
#include <cstdint>
#include <iostream>
#include <mutex>

Expand All @@ -36,9 +37,8 @@ static void s_printHelp()
" in your trust store, set this.\n");
fprintf(stdout, "\tIt's the path to a CA file in PEM format\n");
fprintf(stdout, "use_websocket: if specified, uses a websocket over https (optional)\n");
fprintf(stdout, "proxy_host: if you want to use a proxy with websockets, specify the host here (optional).\n");
fprintf(
stdout, "proxy_port: defaults to 8080 is proxy_host is set. Set this to any value you'd like (optional).\n");
fprintf(stdout, "proxy_host: host name of the http proxy to use (optional).\n");
fprintf(stdout, "proxy_port: port of the http proxy to use (optional).\n");
fprintf(stdout, "user_name: User name to send with mqtt connect.\n");
fprintf(stdout, "password: Password to send with mqtt connect.\n");
fprintf(stdout, "protocol_name: (optional) defaults to x-amzn-mqtt-ca.\n");
Expand Down Expand Up @@ -124,14 +124,19 @@ int main(int argc, char *argv[])
{
protocolName = "http/1.1";
useWebSocket = true;
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
{
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
}
}

if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
if (s_cmdOptionExists(argv, argv + argc, "--proxy_host"))
{
proxyHost = s_getCmdOption(argv, argv + argc, "--proxy_host");
}

if (s_cmdOptionExists(argv, argv + argc, "--proxy_port"))
{
int port = atoi(s_getCmdOption(argv, argv + argc, "--proxy_port"));
if (port > 0 && port <= UINT16_MAX)
{
proxyPort = static_cast<uint16_t>(atoi(s_getCmdOption(argv, argv + argc, "--proxy_port")));
proxyPort = static_cast<uint16_t>(port);
}
}

Expand Down Expand Up @@ -274,7 +279,7 @@ int main(int argc, char *argv[])
Http::HttpClientConnectionProxyOptions proxyOptions;
proxyOptions.HostName = proxyHost;
proxyOptions.Port = proxyPort;
connection->SetWebsocketProxyOptions(proxyOptions);
connection->SetHttpProxyOptions(proxyOptions);
}

/*
Expand Down