Add DNS command line option

This commit is contained in:
Mudit Vats
2020-06-26 14:20:39 -07:00
parent c1987ceb38
commit 857c6e3a87
3 changed files with 82 additions and 19 deletions

View File

@@ -297,7 +297,7 @@ std::string getDNSInfo()
} }
string getActivateInfo(string profile) string getActivateInfo(string profile, string dnssuffixcmd)
{ {
utility::string_t tmp; utility::string_t tmp;
@@ -356,7 +356,17 @@ string getActivateInfo(string profile)
// Get DNS Info // Get DNS Info
tmp = utility::conversions::convertstring(""); tmp = utility::conversions::convertstring("");
string dnsSuffix = getDNSInfo(); string dnsSuffix = "";
if (dnssuffixcmd.length() > 0)
{
// use what's passed in
dnsSuffix = dnssuffixcmd;
}
else
{
// get it from AMT or OS
dnsSuffix = getDNSInfo();
}
if (dnsSuffix.length()) if (dnsSuffix.length())
{ {
@@ -401,7 +411,7 @@ string decodeBase64(string str)
return decodedString; return decodedString;
} }
string createActivationRequest(string profile) string createActivationRequest(string profile, string dnssuffixcmd)
{ {
// Activation parameters // Activation parameters
json::value request; json::value request;
@@ -426,7 +436,7 @@ string createActivationRequest(string profile)
request[U("message")] = json::value::string(tmp); request[U("message")] = json::value::string(tmp);
// payload // payload
string activationInfo = getActivateInfo(profile); string activationInfo = getActivateInfo(profile, dnssuffixcmd);
utility::string_t payload = utility::conversions::to_string_t(activationInfo); utility::string_t payload = utility::conversions::to_string_t(activationInfo);
request[U("payload")] = json::value::string(payload); request[U("payload")] = json::value::string(payload);

View File

@@ -37,10 +37,10 @@ using namespace web;
#endif #endif
string getDNSInfo(); string getDNSInfo();
string createActivationRequest(string profile); string createActivationRequest(string profile, string dnssuffixcmd);
json::value getCertificateHashes(); json::value getCertificateHashes();
string createResponse(string payload); string createResponse(string payload);
string getActivateInfo(string profile); string getActivateInfo(string profile, string dnssuffixcmd);
string encodeBase64(string str); string encodeBase64(string str);
string decodeBase64(string str); string decodeBase64(string str);
void dumpMessage(string tmp); void dumpMessage(string tmp);

View File

@@ -20,6 +20,7 @@ limitations under the License.
#include <string> #include <string>
#include <thread> #include <thread>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <cctype>
#include "commands.h" #include "commands.h"
#include "lms.h" #include "lms.h"
#include "version.h" #include "version.h"
@@ -31,10 +32,12 @@ using namespace web::websockets::client;
#include <cpprest/json.h> #include <cpprest/json.h>
void showUsage(); void showUsage();
bool isPrintable(std::string str);
string websocket_address = ""; string websocket_address = "";
string server_profile = ""; string server_profile = "";
string websocket_proxy = ""; string websocket_proxy = "";
string dns_suffix = "";
long long timeoutTimer = 0; long long timeoutTimer = 0;
const int MAX_TIMEOUT = 10; // seconds const int MAX_TIMEOUT = 10; // seconds
@@ -79,13 +82,13 @@ int main(int argc, char *argv[])
if (argc==1) if (argc==1)
{ {
std::cout << "Use --h, --help for help." << std::endl; std::cout << "Use -h, --help for help." << std::endl;
return 0; return 0;
} }
for (int i=1; i<argc; i++) for (int i=1; i<argc; i++)
{ {
if ( (boost::equals(argv[i], "--help")) || (boost::equals(argv[i], "--h")) ) if ( (boost::equals(argv[i], "--help")) || (boost::equals(argv[i], "-h")) )
{ {
showUsage(); showUsage();
return 0; return 0;
@@ -94,34 +97,70 @@ int main(int argc, char *argv[])
for (int i=1; i<argc; i++) for (int i=1; i<argc; i++)
{ {
if ( (boost::equals(argv[i], "--url")) || (boost::equals(argv[i], "--u")) ) if ( (boost::equals(argv[i], "--url")) || (boost::equals(argv[i], "-u")) )
{ {
if (i+1<argc) if (i+1<argc)
{ {
gotURL = true; gotURL = true;
websocket_address = argv[++i]; websocket_address = argv[++i];
if (!isPrintable(websocket_address))
{
std::cout << "Input contains invalid characters." << std::endl;
std::cout << "Use -h, --help for help." << std::endl;
return 0;
}
} }
} }
else if ( (boost::equals(argv[i], "--profile")) || (boost::equals(argv[i], "--p")) ) else if ( (boost::equals(argv[i], "--profile")) || (boost::equals(argv[i], "-p")) )
{ {
if (i+1<argc) if (i+1<argc)
{ {
gotProfile = true; gotProfile = true;
server_profile = argv[++i]; server_profile = argv[++i];
if (!isPrintable(server_profile))
{
std::cout << "Input contains invalid characters." << std::endl;
std::cout << "Use -h, --help for help." << std::endl;
return 0;
}
} }
} }
else if ( (boost::equals(argv[i], "--proxy")) ||(boost::equals(argv[i], "--x")) ) else if ( (boost::equals(argv[i], "--proxy")) ||(boost::equals(argv[i], "-x")) )
{ {
if (i+1<argc) if (i+1<argc)
{ {
gotProxy = true; gotProxy = true;
websocket_proxy = argv[++i]; websocket_proxy = argv[++i];
if (!isPrintable(websocket_proxy))
{
std::cout << "Input contains invalid characters." << std::endl;
std::cout << "Use -h, --help for help." << std::endl;
return 0;
}
}
}
else if ( (boost::equals(argv[i], "--dns")) ||(boost::equals(argv[i], "-d")) )
{
if (i+1<argc)
{
gotDns = true;
dns_suffix = argv[++i];
if (!isPrintable(dns_suffix))
{
std::cout << "Input contains invalid characters." << std::endl;
std::cout << "Use -h, --help for help." << std::endl;
return 0;
}
} }
} }
else else
{ {
std::cout << "Unrecognized command line arguments." << std::endl; std::cout << "Unrecognized command line arguments." << std::endl;
std::cout << "Use --h, --help for help." << std::endl; std::cout << "Use -h, --help for help." << std::endl;
return 0; return 0;
} }
@@ -130,7 +169,7 @@ int main(int argc, char *argv[])
if (!gotURL || !gotProfile) if (!gotURL || !gotProfile)
{ {
std::cout << "Incorrect or missing arguments." << std::endl; std::cout << "Incorrect or missing arguments." << std::endl;
std::cout << "Use --h, --help for help." << std::endl; std::cout << "Use -h, --help for help." << std::endl;
return 0; return 0;
} }
@@ -139,7 +178,7 @@ int main(int argc, char *argv[])
try { try {
// Get activation info // Get activation info
activationInfo = createActivationRequest(server_profile); activationInfo = createActivationRequest(server_profile, dns_suffix);
} }
catch (...) catch (...)
{ {
@@ -463,18 +502,32 @@ int main(int argc, char *argv[])
exit(0); exit(0);
} }
bool isPrintable(std::string str)
{
for (char c : str)
{
if (!std::isprint(c))
{
return false;
}
}
return true;
}
void showUsage() void showUsage()
{ {
cout << "Usage: " << PROJECT_NAME << " --url <url> --profile <name> [--proxy <addr>]" << endl; cout << "Usage: " << PROJECT_NAME << " --url <url> --profile <name> [--proxy <addr>]" << endl;
cout << "Required:" << endl; cout << "Required:" << endl;
cout << " --u, --url <url> websocket server" << endl; cout << " -u, --url <url> websocket server" << endl;
cout << " --p, --profile <name> server profile" << endl; cout << " -p, --profile <name> server profile" << endl;
cout << "Optional:" << endl; cout << "Optional:" << endl;
cout << " --x, --proxy <addr> proxy address and port" << endl; cout << " -x, --proxy <addr> proxy address and port" << endl;
cout << " -d, --dns <dns> dns suffix" << endl;
cout << endl; cout << endl;
cout << "Examples:" << endl; cout << "Examples:" << endl;
cout << " " << PROJECT_NAME << " --url wss://localhost --profile profile1" << endl; cout << " " << PROJECT_NAME << " --url wss://localhost --profile profile1" << endl;
cout << " " << PROJECT_NAME << " --u wss://localhost --profile profile1 --proxy http://proxy.com:1000" << endl; cout << " " << PROJECT_NAME << " -u wss://localhost --profile profile1 --proxy http://proxy.com:1000" << endl;
cout << " " << PROJECT_NAME << " --u wss://localhost --p profile1 --x http://proxy.com:1000" << endl; cout << " " << PROJECT_NAME << " -u wss://localhost -p profile1 -x http://proxy.com:1000" << endl;
cout << endl; cout << endl;
} }