Skip to content

Commit f7a5684

Browse files
committed
add a new option "ip_worker_name_format" (default format will like "192x168x1x23")
1 parent 68fa1bb commit f7a5684

File tree

8 files changed

+180
-170
lines changed

8 files changed

+180
-170
lines changed

src/Server.cc

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,7 @@ StratumSession::~StratumSession() {
501501

502502
void StratumSession::setWorkerName(const string &fullName) {
503503
if (server_->useIpAsWorkerName()) {
504-
// get source IP address
505-
char saddrBuffer[INET_ADDRSTRLEN];
506-
evutil_inet_ntop(AF_INET, &saddr_, saddrBuffer, INET_ADDRSTRLEN);
507-
508-
workerName_ = saddrBuffer;
504+
workerName_ = Strings::FormatIP(saddr_.s_addr, server_->ipWorkerNameFormat());
509505
return;
510506
}
511507

@@ -547,8 +543,8 @@ void StratumSession::recvData(struct evbuffer *buf) {
547543
}
548544

549545
/////////////////////////////////// StratumServer //////////////////////////////
550-
StratumServer::StratumServer(const string &listenIP, const uint16_t listenPort)
551-
: listenIP_(listenIP), listenPort_(listenPort)
546+
StratumServer::StratumServer(const AgentConf &conf)
547+
: conf_(conf)
552548
{
553549
upSessions_ .resize(kUpSessionCount_, NULL);
554550
upSessionCount_.resize(kUpSessionCount_, 0);
@@ -591,14 +587,6 @@ void StratumServer::stop() {
591587
event_base_loopexit(base_, NULL);
592588
}
593589

594-
void StratumServer::addUpPool(const std::vector<PoolConf> &poolConfs) {
595-
upPools_ = poolConfs;
596-
597-
for (const auto &pool : upPools_) {
598-
LOG(INFO) << "add pool: " << pool.host_ << ":" << pool.port_ << ", subaccount name: " << pool.upPoolUserName_ << std::endl;
599-
}
600-
}
601-
602590
UpStratumClient * StratumServer::createUpSession(int8_t idx) {
603591
UpStratumClient *up = createUpClient(idx, this);
604592
if (!up->connect()) {
@@ -607,25 +595,21 @@ UpStratumClient * StratumServer::createUpSession(int8_t idx) {
607595
return up;
608596
}
609597

610-
bool StratumServer::run(bool alwaysKeepDownconn, bool disconnectWhenLostAsicBoost,
611-
bool useIpAsWorkerName, bool submitResponseFromServer,
612-
const string &fixedWorkerName) {
613-
alwaysKeepDownconn_ = alwaysKeepDownconn;
614-
disconnectWhenLostAsicBoost_ = disconnectWhenLostAsicBoost;
615-
useIpAsWorkerName_ = useIpAsWorkerName;
616-
submitResponseFromServer_ = submitResponseFromServer;
617-
fixedWorkerName_ = fixedWorkerName;
598+
bool StratumServer::run() {
599+
for (const auto &pool : conf_.pools_) {
600+
LOG(INFO) << "add pool: " << pool.host_ << ":" << pool.port_ << ", subaccount name: " << pool.upPoolUserName_ << std::endl;
601+
}
618602

619-
if (!fixedWorkerName_.empty()) {
620-
LOG(INFO) << "[OPTION] Fixed worker name enabled, all worker name will be replaced to " << fixedWorkerName_ << " on the server.";
603+
if (!conf_.fixedWorkerName_.empty()) {
604+
LOG(INFO) << "[OPTION] Fixed worker name enabled, all worker name will be replaced to " << conf_.fixedWorkerName_ << " on the server.";
621605
}
622606

623607
if (running_) {
624608
return false;
625609
}
626610
running_ = true;
627611

628-
if (upPools_.size() == 0) {
612+
if (conf_.pools_.size() == 0) {
629613
return false;
630614
}
631615

@@ -694,10 +678,10 @@ bool StratumServer::run(bool alwaysKeepDownconn, bool disconnectWhenLostAsicBoos
694678
struct sockaddr_in sin;
695679
memset(&sin, 0, sizeof(sin));
696680
sin.sin_family = AF_INET;
697-
sin.sin_port = htons(listenPort_);
681+
sin.sin_port = htons(conf_.listenPort_);
698682
sin.sin_addr.s_addr = htonl(INADDR_ANY);
699-
if (evutil_inet_pton(AF_INET, listenIP_.c_str(), &sin.sin_addr) == 0) {
700-
LOG(ERROR) << "invalid ip: " << listenIP_ << std::endl;
683+
if (evutil_inet_pton(AF_INET, conf_.listenIP_.c_str(), &sin.sin_addr) == 0) {
684+
LOG(ERROR) << "invalid ip: " << conf_.listenIP_ << std::endl;
701685
return false;
702686
}
703687

@@ -709,11 +693,11 @@ bool StratumServer::run(bool alwaysKeepDownconn, bool disconnectWhenLostAsicBoos
709693
-1,
710694
(struct sockaddr*)&sin, sizeof(sin));
711695
if(!listener_) {
712-
LOG(ERROR) << "cannot create listener: " << listenIP_ << ":" << listenPort_ << std::endl;
696+
LOG(ERROR) << "cannot create listener: " << conf_.listenIP_ << ":" << conf_.listenPort_ << std::endl;
713697
return false;
714698
}
715699

716-
LOG(INFO) << "startup is successful, listening: " << listenIP_ << ":" << listenPort_ << std::endl;
700+
LOG(INFO) << "startup is successful, listening: " << conf_.listenIP_ << ":" << conf_.listenPort_ << std::endl;
717701

718702
assert(base_ != NULL);
719703
event_base_dispatch(base_);
@@ -776,7 +760,7 @@ void StratumServer::checkUpSessions() {
776760
continue;
777761
}
778762

779-
if (alwaysKeepDownconn_ || upSessions_[i]->reconnectCount_ < upPools_.size()) {
763+
if (conf_.alwaysKeepDownconn_ || upSessions_[i]->reconnectCount_ < conf_.pools_.size()) {
780764
upSessions_[i]->reconnect();
781765
continue;
782766
}
@@ -1008,7 +992,7 @@ void StratumServer::upEventCallback(struct bufferevent *bev,
1008992
LOG(ERROR) << "unhandled events from pool server: " << events << std::endl;
1009993
}
1010994

1011-
if (server->alwaysKeepDownconn_ || up->reconnectCount_ < server->upPools_.size()) {
995+
if (server->conf_.alwaysKeepDownconn_ || up->reconnectCount_ < server->conf_.pools_.size()) {
1012996
up->reconnect();
1013997
return;
1014998
}
@@ -1081,7 +1065,7 @@ UpStratumClient *StratumServer::findUpSession() {
10811065
}
10821066

10831067
// Provide an unavailable connection if no available connection can be found
1084-
if (upSession == nullptr && alwaysKeepDownconn_) {
1068+
if (upSession == nullptr && conf_.alwaysKeepDownconn_) {
10851069
for (size_t i = 0; i < upSessions_.size(); i++) {
10861070
if (upSessions_[i] == nullptr) {
10871071
continue;
@@ -1108,7 +1092,7 @@ void StratumServer::registerWorker(StratumSession *downSession) {
11081092
//
11091093
// | magic_number(1) | cmd(1) | len (2) | session_id(2) | clientAgent | worker_name |
11101094
//
1111-
string workerName = fixedWorkerName_.empty() ? downSession->workerName() : fixedWorkerName_;
1095+
string workerName = conf_.fixedWorkerName_.empty() ? downSession->workerName() : conf_.fixedWorkerName_;
11121096

11131097
uint16_t len = 0;
11141098
len += (1+1+2+2); // magic_num, cmd, len, session_id

src/Server.h

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,6 @@ class StratumServer {
211211
static const int8_t kUpSessionCount_ = 5; // MAX is 127
212212
bool running_ = false;
213213

214-
string listenIP_;
215-
uint16_t listenPort_ = 0;
216-
vector<PoolConf> upPools_;
217-
218214
struct event *upEvTimer_ = nullptr;
219215

220216
// libevent2
@@ -240,29 +236,25 @@ class StratumServer {
240236

241237
// down stream connections
242238
vector<StratumSession *> downSessions_;
243-
bool alwaysKeepDownconn_ = false;
244-
bool disconnectWhenLostAsicBoost_ = false;
245-
bool useIpAsWorkerName_ = false;
246-
bool submitResponseFromServer_ = false;
247-
string fixedWorkerName_;
239+
240+
AgentConf conf_;
248241

249242
public:
250243
SessionIDManager sessionIDManager_;
251244

252245
public:
253-
StratumServer(const string &listenIP, const uint16_t listenPort);
246+
StratumServer(const AgentConf &conf);
254247
virtual ~StratumServer();
255248

256249
UpStratumClient *createUpSession(const int8_t idx);
257250

258-
void addUpPool(const std::vector<PoolConf> &poolConfs);
259-
260-
inline const vector<PoolConf>& getUpPools() { return upPools_; }
251+
inline const vector<PoolConf>& getUpPools() { return conf_.pools_; }
261252
inline struct event_base* getEventBase() { return base_; }
262-
inline bool disconnectWhenLostAsicBoost() { return disconnectWhenLostAsicBoost_; }
263-
inline bool useIpAsWorkerName() { return useIpAsWorkerName_; }
264-
inline bool submitResponseFromServer() { return submitResponseFromServer_; }
265-
inline const string &fixedWorkerName() { return fixedWorkerName_; }
253+
inline bool disconnectWhenLostAsicBoost() { return conf_.disconnectWhenLostAsicBoost_; }
254+
inline bool useIpAsWorkerName() { return conf_.useIpAsWorkerName_; }
255+
inline string ipWorkerNameFormat() { return conf_.ipWorkerNameFormat_; }
256+
inline bool submitResponseFromServer() { return conf_.submitResponseFromServer_; }
257+
inline const string &fixedWorkerName() { return conf_.fixedWorkerName_; }
266258

267259
void addDownConnection (StratumSession *conn);
268260
void removeDownConnection(StratumSession *conn);
@@ -296,10 +288,8 @@ class StratumServer {
296288
void registerWorker (UpStratumClient *upSession);
297289
void registerWorker (StratumSession *downSession);
298290
void unRegisterWorker(StratumSession *downSession);
299-
300-
bool run(bool alwaysKeepDownconn, bool disconnectWhenLostAsicBoost,
301-
bool useIpAsWorkerName, bool submitResponseFromServer,
302-
const string &fixedWorkerName);
291+
292+
bool run();
303293
void stop();
304294
};
305295

src/Utils.cc

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <iostream>
2525
#include <iomanip>
2626

27+
#include <event2/util.h>
28+
2729
#if defined(SUPPORT_GLOG) && defined(GLOG_TO_STDOUT)
2830
void GLogToStdout::send(google::LogSeverity severity, const char* full_filename,
2931
const char* base_filename, int line, const struct ::tm* tm_time,
@@ -89,6 +91,31 @@ void Strings::Append(string & dest, const char * fmt, ...) {
8991
}
9092
}
9193

94+
string Strings::ReplaceAll(std::string str, const std::string& from, const std::string& to) {
95+
size_t start_pos = 0;
96+
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
97+
str.replace(start_pos, from.length(), to);
98+
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
99+
}
100+
return str;
101+
}
102+
103+
string Strings::FormatIP(uint32_t ipv4Int, string format) {
104+
union IPv4Arr {
105+
uint32_t ipv4Int;
106+
uint8_t parts[4];
107+
} ipv4Arr;
108+
109+
ipv4Arr.ipv4Int = std::move(ipv4Int);
110+
111+
format = Strings::ReplaceAll(format, string("{1}"), std::to_string(ipv4Arr.parts[0]));
112+
format = Strings::ReplaceAll(format, string("{2}"), std::to_string(ipv4Arr.parts[1]));
113+
format = Strings::ReplaceAll(format, string("{3}"), std::to_string(ipv4Arr.parts[2]));
114+
format = Strings::ReplaceAll(format, string("{4}"), std::to_string(ipv4Arr.parts[3]));
115+
116+
return format;
117+
}
118+
92119
string getJsonStr(const char *c,const jsmntok_t *t) {
93120
if (t == NULL || t->end <= t->start)
94121
return "";
@@ -105,12 +132,7 @@ int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
105132
return -1;
106133
}
107134

108-
bool parseConfJson(const string &jsonStr,
109-
string &agentType, string &listenIP, string &listenPort,
110-
std::vector<PoolConf> &poolConfs,
111-
bool &alwaysKeepDownconn, bool &disconnectWhenLostAsicBoost,
112-
bool &useIpAsWorkerName, bool &submitResponseFromServer,
113-
string &fixedWorkerName) {
135+
bool parseConfJson(const string &jsonStr, AgentConf &conf) {
114136
jsmn_parser p;
115137
jsmn_init(&p);
116138
jsmntok_t t[64]; // we expect no more than 64 tokens
@@ -128,15 +150,15 @@ bool parseConfJson(const string &jsonStr,
128150

129151
for (int i = 1; i < r; i++) {
130152
if (jsoneq(c, &t[i], "agent_type") == 0) {
131-
agentType = getJsonStr(c, &t[i+1]);
153+
conf.agentType_ = getJsonStr(c, &t[i+1]);
132154
i++;
133155
}
134156
if (jsoneq(c, &t[i], "agent_listen_ip") == 0) {
135-
listenIP = getJsonStr(c, &t[i+1]);
157+
conf.listenIP_ = getJsonStr(c, &t[i+1]);
136158
i++;
137159
}
138160
else if (jsoneq(c, &t[i], "agent_listen_port") == 0) {
139-
listenPort = getJsonStr(c, &t[i+1]);
161+
conf.listenPort_ = (uint16_t)strtoul(getJsonStr(c, &t[i+1]).c_str(), NULL, 10);
140162
i++;
141163
}
142164
else if (jsoneq(c, &t[i], "pools") == 0) {
@@ -159,51 +181,79 @@ bool parseConfJson(const string &jsonStr,
159181
return false;
160182
}
161183

162-
PoolConf conf;
163-
conf.host_ = getJsonStr(c, &t[idx + 1]);
164-
conf.port_ = (uint16_t)strtoul(getJsonStr(c, &t[idx + 2]).c_str(), NULL, 10);
165-
conf.upPoolUserName_= getJsonStr(c, &t[idx + 3]);
184+
PoolConf pool;
185+
pool.host_ = getJsonStr(c, &t[idx + 1]);
186+
pool.port_ = (uint16_t)strtoul(getJsonStr(c, &t[idx + 2]).c_str(), NULL, 10);
187+
pool.upPoolUserName_ = getJsonStr(c, &t[idx + 3]);
166188

167-
poolConfs.push_back(conf);
189+
conf.pools_.push_back(pool);
168190
}
169191
i += poolCount * 4;
170192
}
171193
else if (jsoneq(c, &t[i], "always_keep_downconn") == 0) {
172194
string opt = getJsonStr(c, &t[i+1]);
173195
std::transform(opt.begin(), opt.end(), opt.begin(), ::tolower);
174-
alwaysKeepDownconn = (opt == "true");
196+
conf.alwaysKeepDownconn_ = (opt == "true");
175197
i++;
176198
}
177199
else if (jsoneq(c, &t[i], "disconnect_when_lost_asicboost") == 0) {
178200
string opt = getJsonStr(c, &t[i + 1]);
179201
std::transform(opt.begin(), opt.end(), opt.begin(), ::tolower);
180-
disconnectWhenLostAsicBoost = (opt == "true");
202+
conf.disconnectWhenLostAsicBoost_ = (opt == "true");
181203
i++;
182204
}
183205
else if (jsoneq(c, &t[i], "use_ip_as_worker_name") == 0) {
184206
string opt = getJsonStr(c, &t[i + 1]);
185207
std::transform(opt.begin(), opt.end(), opt.begin(), ::tolower);
186-
useIpAsWorkerName = (opt == "true");
208+
conf.useIpAsWorkerName_ = (opt == "true");
209+
i++;
210+
}
211+
else if (jsoneq(c, &t[i], "ip_worker_name_format") == 0) {
212+
conf.ipWorkerNameFormat_ = getJsonStr(c, &t[i+1]);
187213
i++;
188214
}
189215
else if (jsoneq(c, &t[i], "submit_response_from_server") == 0) {
190216
string opt = getJsonStr(c, &t[i + 1]);
191217
std::transform(opt.begin(), opt.end(), opt.begin(), ::tolower);
192-
submitResponseFromServer = (opt == "true");
218+
conf.submitResponseFromServer_ = (opt == "true");
193219
i++;
194220
}
195221
else if (jsoneq(c, &t[i], "fixed_worker_name") == 0) {
196-
fixedWorkerName = getJsonStr(c, &t[i+1]);
222+
conf.fixedWorkerName_ = getJsonStr(c, &t[i+1]);
197223
i++;
198224
}
199225
}
200226

201227
// check parametes
202-
if (listenIP.length() && listenPort.length() && poolConfs.size()) {
203-
return true;
228+
if (conf.listenIP_.empty()) {
229+
LOG(ERROR) << "[conf] agent_listen_ip cannot be empty.";
230+
return false;
231+
}
232+
233+
if (conf.listenPort_ == 0) {
234+
LOG(ERROR) << "[conf] agent_listen_port must be between 1 and 65535.";
235+
return false;
236+
}
237+
238+
if (conf.pools_.empty()) {
239+
LOG(ERROR) << "[conf] pools cannot be empty.";
240+
return false;
241+
}
242+
243+
for (size_t i=0; i<conf.pools_.size(); i++) {
244+
const auto &pool = conf.pools_[i];
245+
if (pool.host_.empty()) {
246+
LOG(ERROR) << "[conf] the host of pool " << i << " cannot be empty.";
247+
return false;
248+
}
249+
if (pool.port_ == 0) {
250+
LOG(ERROR) << "[conf] the port of pool " << i << " (" << pool.host_
251+
<< ") must be between 1 and 65535.";
252+
return false;
253+
}
204254
}
205255

206-
return false;
256+
return true;
207257
}
208258

209259
const char *splitNotify(const string &line, int number) {

0 commit comments

Comments
 (0)