From 32b53bb54ee7591fd47dd1ee24e05b73337f40b5 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Wed, 23 Mar 2016 00:43:45 +0300 Subject: [PATCH] EXPERIMENTAL: custom peer collector You can specify a custom peer collector using syntax like -peercollector=. Sample collector can be found in the contriob/seeds/getseeds.py file. --- novacoin-qt.pro | 6 ++- src/init.cpp | 8 ++++- src/ipcollector.cpp | 70 ++++++++++++++++++++++++++++++++++++++++++++++ src/ipcollector.h | 7 ++++ src/makefile.bsd | 3 +- src/makefile.linux-mingw | 3 +- src/makefile.mingw | 3 +- src/makefile.osx | 3 +- src/makefile.unix | 3 +- src/net.h | 1 + 10 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 src/ipcollector.cpp create mode 100644 src/ipcollector.h diff --git a/novacoin-qt.pro b/novacoin-qt.pro index e005090..4bdce2a 100644 --- a/novacoin-qt.pro +++ b/novacoin-qt.pro @@ -271,7 +271,8 @@ HEADERS += src/qt/bitcoingui.h \ src/qt/multisigdialog.h \ src/qt/secondauthdialog.h \ src/qt/qrcodedialog.h \ - src/ies.h + src/ies.h \ + src/ipcollector.h SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \ src/qt/intro.cpp \ @@ -353,7 +354,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \ src/qt/qrcodedialog.cpp \ src/base58.cpp \ src/cryptogram.cpp \ - src/ecies.cpp + src/ecies.cpp \ + src/ipcollector.cpp RESOURCES += \ src/qt/bitcoin.qrc diff --git a/src/init.cpp b/src/init.cpp index 994c784..a636b6e 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -8,6 +8,7 @@ #include "net.h" #include "init.h" #include "util.h" +#include "ipcollector.h" #include "ui_interface.h" #include "checkpoints.h" #include @@ -292,6 +293,7 @@ std::string HelpMessage() " -rpcconnect= " + _("Send commands to node running on (default: 127.0.0.1)") + "\n" + " -blocknotify= " + _("Execute command when the best block changes (%s in cmd is replaced by block hash)") + "\n" + " -walletnotify= " + _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)") + "\n" + + " -peercollector= " + _("Execute command to collect peer addresses") + "\n" + " -confchange " + _("Require a confirmations for change (default: 0)") + "\n" + " -upgradewallet " + _("Upgrade wallet to latest format") + "\n" + " -keypool= " + _("Set key pool size to (default: 100)") + "\n" + @@ -997,7 +999,11 @@ bool AppInit2() if (fServer) NewThread(ThreadRPCServer, NULL); - // ********************************************************* Step 12: finished + // ********************************************************* Step 13: IP collection thread + strCollectorCommand = GetArg("-peercollector", ""); + if (strCollectorCommand != "") + NewThread(ThreadIPCollector, NULL); + // ********************************************************* Step 14: finished uiInterface.InitMessage(_("Done loading")); printf("Done loading\n"); diff --git a/src/ipcollector.cpp b/src/ipcollector.cpp new file mode 100644 index 0000000..f50d098 --- /dev/null +++ b/src/ipcollector.cpp @@ -0,0 +1,70 @@ +#include +#include +#include + +#include "net.h" +#include "ui_interface.h" + +#ifdef WIN32 +#define popen _popen +#define pclose _pclose +#else +#endif + +std::string strCollectorCommand; + +std::string exec(const char* cmd) { + std::string result = ""; + char buffer[128]; + FILE *fp; + + fp = popen(cmd, "r"); + while (fgets(buffer, sizeof(buffer), fp) != NULL) { + result += buffer; + } + pclose(fp); + return result; +} + +bool AddPeer(std::string &strIpAddr) { + LOCK(cs_vAddedNodes); + std::vector::iterator it = vAddedNodes.begin(); + for(; it != vAddedNodes.end(); it++) { + if (strIpAddr == *it) break; + } + if (it != vAddedNodes.end()) + return false; + + vAddedNodes.push_back(strIpAddr); + return true; +} + + +void ThreadIPCollector(void* parg) { + printf("ThreadIPCollector started\n"); + vnThreadsRunning[THREAD_IPCOLLECTOR]++; + + while(!fShutdown) { + if (!fServer) { + // If RPC server is enabled then we don't have to parse anything. + std::string strCollectorOutput = exec(strCollectorCommand.c_str()); + printf("Peer collector output: %s\n", strCollectorOutput.c_str()); + } else { + // Otherwise, there is a work to be done. + std::string strCollectorOutput = exec((strCollectorCommand + " norpc").c_str()); + std::istringstream collectorStream(strCollectorOutput); + + std::string strIpAddr; + while (std::getline(collectorStream, strIpAddr)) { + AddPeer(strIpAddr); + } + } + + int nSleepHours = 1 + GetRandInt(5); // Sleep for 1-6 hours. + for (int i = 0; i < nSleepHours * 3600 && !fShutdown; i++) + Sleep(1000); + } + + printf("ThreadIPCollector stopped\n"); + vnThreadsRunning[THREAD_IPCOLLECTOR]--; +} diff --git a/src/ipcollector.h b/src/ipcollector.h new file mode 100644 index 0000000..e16279f --- /dev/null +++ b/src/ipcollector.h @@ -0,0 +1,7 @@ +#ifndef IPCOLLECTOR_H +#define IPCOLLECTOR_H + +extern std::string strCollectorCommand; +extern void ThreadIPCollector(void* parg); + +#endif \ No newline at end of file diff --git a/src/makefile.bsd b/src/makefile.bsd index b31e457..c7a2f24 100644 --- a/src/makefile.bsd +++ b/src/makefile.bsd @@ -137,7 +137,8 @@ OBJS= \ obj/kernel.o \ obj/kernel_worker.o \ obj/ecies.o \ - obj/cryptogram.o + obj/cryptogram.o \ + obj/ipcollector.o all: novacoind diff --git a/src/makefile.linux-mingw b/src/makefile.linux-mingw index 12ed6d8..2837218 100644 --- a/src/makefile.linux-mingw +++ b/src/makefile.linux-mingw @@ -101,7 +101,8 @@ OBJS= \ obj/kernel.o \ obj/kernel_worker.o \ obj/ecies.o \ - obj/cryptogram.o + obj/cryptogram.o \ + obj/ipcollector.o all: novacoind.exe diff --git a/src/makefile.mingw b/src/makefile.mingw index 368ca62..c83e234 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -91,7 +91,8 @@ OBJS= \ obj/kernel.o \ obj/kernel_worker.o \ obj/ecies.o \ - obj/cryptogram.o + obj/cryptogram.o \ + obj/ipcollector.o all: novacoind.exe diff --git a/src/makefile.osx b/src/makefile.osx index 0fc1052..e0a00c8 100644 --- a/src/makefile.osx +++ b/src/makefile.osx @@ -99,7 +99,8 @@ OBJS= \ obj/kernel.o \ obj/kernel_worker.o \ obj/ecies.o \ - obj/cryptogram.o + obj/cryptogram.o \ + obj/ipcollector.o ifneq (${USE_IPV6}, -) DEFS += -DUSE_IPV6=$(USE_IPV6) diff --git a/src/makefile.unix b/src/makefile.unix index f4da324..69749c2 100644 --- a/src/makefile.unix +++ b/src/makefile.unix @@ -138,7 +138,8 @@ OBJS= \ obj/kernel.o \ obj/kernel_worker.o \ obj/ecies.o \ - obj/cryptogram.o + obj/cryptogram.o \ + obj/ipcollector.o all: novacoind diff --git a/src/net.h b/src/net.h index a98a416..8569d23 100644 --- a/src/net.h +++ b/src/net.h @@ -112,6 +112,7 @@ enum threadId THREAD_MINTER, THREAD_SCRIPTCHECK, THREAD_NTP, + THREAD_IPCOLLECTOR, THREAD_MAX }; -- 1.7.1