Jake was worried about using the same keycode for multiple servers. To combat this I look at ifconfig for
an ip to appear on the list that matches what is optionally encoded in the keycode
This commit is contained in:
1 parent
af365177d0
commit
95a5791867
4 files changed
+136
-10
No files matched your search
@@ -29,6 +29,11 @@
|
||||
#include <cstdlib>
|
||||
#include <vector>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
namespace bts {
|
||||
namespace cmd {
|
||||
|
||||
@@ -42,13 +47,48 @@ std::string KeyCode::helpstr ()
|
||||
{
|
||||
std::ostringstream oss;
|
||||
oss << "\n"
|
||||
<< "ENCODE -keyfile:[keyfile] -pass:password -users:<nusers> -expiration:<2020-05-18>\n "
|
||||
<< "ENCODE -keyfile:[keyfile] -pass:password -users:<nusers> -expiration:<2020-05-18> [-ip:<ip_address>] [-port:<spark_port>]\n "
|
||||
<< "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n "
|
||||
<< "KEYGEN -keyfile:[keyfile_prefix]\n ";
|
||||
<< "KEYGEN -keyfile:[keyfile_prefix]\n "
|
||||
<< "CHECKIPS \n ";
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
int checkips()
|
||||
{
|
||||
struct ifaddrs * ifAddrStruct=NULL;
|
||||
struct ifaddrs * ifa=NULL;
|
||||
void * tmpAddrPtr=NULL;
|
||||
|
||||
getifaddrs(&ifAddrStruct);
|
||||
|
||||
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next)
|
||||
{
|
||||
if (!ifa->ifa_addr)
|
||||
continue;
|
||||
if (ifa->ifa_addr->sa_family == AF_INET) // check it is IP4
|
||||
{
|
||||
// is a valid IP4 Address
|
||||
tmpAddrPtr=&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
|
||||
char addressBuffer[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
|
||||
printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer);
|
||||
}
|
||||
else if (ifa->ifa_addr->sa_family == AF_INET6) // check it is IP6
|
||||
{
|
||||
// is a valid IP6 Address
|
||||
tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
|
||||
char addressBuffer[INET6_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
|
||||
printf("%s IP Address => %s\n", ifa->ifa_name, addressBuffer);
|
||||
}
|
||||
}
|
||||
if (ifAddrStruct!=NULL)
|
||||
freeifaddrs(ifAddrStruct);
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string parse_date (std::string const &date)
|
||||
{
|
||||
auto the_date = bts::datetime::parse_iso_date_time_string(date);
|
||||
@@ -57,7 +97,8 @@ std::string parse_date (std::string const &date)
|
||||
|
||||
|
||||
void get_cmd(const std::vector<std::string>& args, std::string& keyfile, std::string& pass,
|
||||
std::string& users, std::string& expiration, std::string* source = nullptr)
|
||||
std::string& users, std::string& expiration, std::string* source = nullptr,
|
||||
std::string* ip = nullptr, std::string* port = nullptr)
|
||||
{
|
||||
for(size_t i = 1; i < args.size(); i++)
|
||||
{
|
||||
@@ -80,6 +121,14 @@ void get_cmd(const std::vector<std::string>& args, std::string& keyfile, std::st
|
||||
size_t pay_pos = args[i].find("-source:");
|
||||
if(source && pay_pos != args[i].npos)
|
||||
*source = args[i].substr(pay_pos+8);
|
||||
|
||||
size_t i_pos = args[i].find("-ip:");
|
||||
if(ip && i_pos != args[i].npos)
|
||||
*ip = args[i].substr(i_pos+4);
|
||||
|
||||
size_t p_pos = args[i].find("-port:");
|
||||
if(port && p_pos != args[i].npos)
|
||||
*port = args[i].substr(p_pos+6);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +183,8 @@ int keygen(const std::vector<std::string>& args)
|
||||
// }
|
||||
// }
|
||||
|
||||
void write_keycode(const std::string& users, const std::string& expiration, bts::config::KeyCode& kc)
|
||||
void write_keycode(const std::string& users, const std::string& expiration,
|
||||
const std::string& ip, const std::string& port, bts::config::KeyCode& kc)
|
||||
{
|
||||
std::ostringstream out_stream;
|
||||
|
||||
@@ -146,6 +196,9 @@ void write_keycode(const std::string& users, const std::string& expiration, bts:
|
||||
kc.set_nusers(nusers);
|
||||
kc.set_expiration(expir_ll);
|
||||
kc.set_generation_time(now);
|
||||
bts_assert_msg(ip != "127.0.0.1", "you can't generate a keycode for loopback, just leave the parameter blank");
|
||||
kc.set_ip(ip);
|
||||
kc.set_port(port);
|
||||
}
|
||||
|
||||
void eraseAllSubStr(std::string& mainStr, const std::string& toErase)
|
||||
@@ -160,7 +213,8 @@ void eraseAllSubStr(std::string& mainStr, const std::string& toErase)
|
||||
}
|
||||
}
|
||||
|
||||
int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users, const std::string& expiration)
|
||||
int encrypt(const std::string& keyfile, const std::string& pass, const std::string& users,
|
||||
const std::string& expiration, const std::string& ip, const std::string& port)
|
||||
{
|
||||
std::string protobuf;
|
||||
|
||||
@@ -177,7 +231,7 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri
|
||||
PrivateKeyPtr key = PrivateKey::read_file(keyfile, &cache);
|
||||
|
||||
bts::config::KeyCode kc;
|
||||
write_keycode(users, expiration, kc);
|
||||
write_keycode(users, expiration, ip, port, kc);
|
||||
if(google::protobuf::TextFormat::PrintToString(kc, &kc_string))
|
||||
{
|
||||
oss << kc_string;
|
||||
@@ -213,10 +267,10 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri
|
||||
|
||||
int encode(const std::vector<std::string>& args)
|
||||
{
|
||||
std::string keyfile, pass, users, expiration;
|
||||
get_cmd(args, keyfile, pass, users, expiration);
|
||||
std::string keyfile, pass, users, expiration, ip, port;
|
||||
get_cmd(args, keyfile, pass, users, expiration, nullptr, &ip, &port);
|
||||
|
||||
return encrypt(keyfile, pass, users, expiration);
|
||||
return encrypt(keyfile, pass, users, expiration, ip, port);
|
||||
}
|
||||
|
||||
int decode(const std::vector<std::string>& args)
|
||||
@@ -273,6 +327,10 @@ int KeyCode::execute(const std::vector<std::string>& args)
|
||||
{
|
||||
return decode(args);
|
||||
}
|
||||
if(boost::to_lower_copy(args[0]) == "checkips")
|
||||
{
|
||||
return checkips();
|
||||
}
|
||||
}
|
||||
std::cout << help() << std::endl;
|
||||
return -1;
|
||||
|
||||
Reference in new issue
Block a user