This repository has been archived by the owner on Oct 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
credit-client.cc
126 lines (105 loc) · 3.72 KB
/
credit-client.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "credit-client.hh"
#include <iostream>
#include <boost/program_options.hpp>
#include <boost/program_options/parsers.hpp>
using namespace ten;
namespace po = boost::program_options;
struct options {
po::options_description generic;
po::options_description configuration;
po::options_description hidden;
po::positional_options_description pdesc;
po::options_description cmdline_options;
po::options_description config_file_options;
po::options_description visible;
options() :
generic("Generic options"),
configuration("Configuration"),
hidden("Hidden options"),
visible("Allowed options")
{
generic.add_options()
("help", "Show help message")
;
}
void setup() {
cmdline_options.add(generic).add(configuration).add(hidden);
config_file_options.add(configuration).add(hidden);
visible.add(generic).add(configuration);
}
};
static void showhelp(options &opts, std::ostream &os = std::cerr) {
std::cerr << opts.visible << std::endl;
}
static void parse_args(options &opts, int argc, char *argv[]) {
po::variables_map vm;
try {
opts.setup();
po::store(po::command_line_parser(argc, argv)
.options(opts.cmdline_options).positional(opts.pdesc).run(), vm);
po::notify(vm);
if (vm.count("help")) {
showhelp(opts);
exit(1);
}
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << std::endl << std::endl;
showhelp(opts);
exit(1);
}
}
struct config {
std::string grandfather_file;
std::string blacklist_file;
std::string secret;
std::string server_host;
uint16_t server_port;
std::string ip;
std::string rawkey;
std::string db;
uint64_t key;
uint64_t value;
};
// globals
static config conf;
static key_engine keng{""};
static void startup() {
credit_client cc{conf.server_host, conf.server_port,
conf.blacklist_file, conf.grandfather_file};
if (conf.db.empty()) {
if (conf.secret.empty()) {
std::cerr << "Error: secret key is required\n\n";
kernel::shutdown();
return;
}
keng.secret = conf.secret;
apikey akey = {};
cc.full_query(conf.ip, conf.rawkey, conf.key, akey, conf.db, conf.value, keng, 1);
} else {
if (!cc.query(conf.db, conf.key, conf.value)) {
std::cerr << "timeout\n";
}
}
std::cout << conf.db << "[" << conf.key << "]=" << conf.value << "\n";
cc.close();
}
int main(int argc, char *argv[]) {
return task::main([&] {
options opts;
opts.configuration.add_options()
("credit-server", po::value<std::string>(&conf.server_host)->default_value("localhost"), "credit-server host:port")
("secret", po::value<std::string>(&conf.secret), "hmac secret key")
("grandfather", po::value<std::string>(&conf.grandfather_file), "grandfathered keys file")
("blacklist", po::value<std::string>(&conf.blacklist_file), "blacklisted keys file")
("ip", po::value<std::string>(&conf.ip)->default_value(""), "use ip key and ip database")
("db", po::value<std::string>(&conf.db)->default_value(""), "database name")
("rawkey", po::value<std::string>(&conf.rawkey)->default_value(""), "rawkey")
("key", po::value<uint64_t>(&conf.key)->default_value(0), "64bit numeric key")
("value", po::value<uint64_t>(&conf.value)->default_value(0), "value")
;
parse_args(opts, argc, argv);
conf.server_port = 9876;
parse_host_port(conf.server_host, conf.server_port);
task::spawn(startup);
});
}