|
| 1 | +/* |
| 2 | + This file is part of TON Blockchain source code. |
| 3 | +
|
| 4 | + TON Blockchain is free software; you can redistribute it and/or |
| 5 | + modify it under the terms of the GNU General Public License |
| 6 | + as published by the Free Software Foundation; either version 2 |
| 7 | + of the License, or (at your option) any later version. |
| 8 | +
|
| 9 | + TON Blockchain is distributed in the hope that it will be useful, |
| 10 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + GNU General Public License for more details. |
| 13 | +
|
| 14 | + You should have received a copy of the GNU General Public License |
| 15 | + along with TON Blockchain. If not, see <http://www.gnu.org/licenses/>. |
| 16 | +
|
| 17 | + In addition, as a special exception, the copyright holders give permission |
| 18 | + to link the code of portions of this program with the OpenSSL library. |
| 19 | + You must obey the GNU General Public License in all respects for all |
| 20 | + of the code used other than OpenSSL. If you modify file(s) with this |
| 21 | + exception, you may extend this exception to your version of the file(s), |
| 22 | + but you are not obligated to do so. If you do not wish to do so, delete this |
| 23 | + exception statement from your version. If you delete this exception statement |
| 24 | + from all source files in the program, then also delete it here. |
| 25 | +
|
| 26 | + Copyright 2017-2019 Telegram Systems LLP |
| 27 | +*/ |
| 28 | +#include "td/actor/actor.h" |
| 29 | +#include "td/utils/buffer.h" |
| 30 | +#include "td/utils/port/IPAddress.h" |
| 31 | +#include "td/net/UdpServer.h" |
| 32 | +#include "td/utils/port/signals.h" |
| 33 | +#include "td/utils/OptionsParser.h" |
| 34 | +#include "td/utils/FileLog.h" |
| 35 | +#include "td/utils/port/path.h" |
| 36 | +#include "td/utils/port/user.h" |
| 37 | +#include "td/utils/filesystem.h" |
| 38 | +#include "common/checksum.h" |
| 39 | +#include "common/errorcode.h" |
| 40 | +#include "tl-utils/tl-utils.hpp" |
| 41 | +#include "auto/tl/ton_api_json.h" |
| 42 | +#include "adnl/adnl.h" |
| 43 | +#include <map> |
| 44 | + |
| 45 | +#if TD_DARWIN || TD_LINUX |
| 46 | +#include <unistd.h> |
| 47 | +#endif |
| 48 | + |
| 49 | +namespace ton { |
| 50 | + |
| 51 | +namespace adnl { |
| 52 | + |
| 53 | +class Callback : public adnl::Adnl::Callback { |
| 54 | + public: |
| 55 | + void receive_message(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data) override { |
| 56 | + } |
| 57 | + void receive_query(AdnlNodeIdShort src, AdnlNodeIdShort dst, td::BufferSlice data, |
| 58 | + td::Promise<td::BufferSlice> promise) override { |
| 59 | + TRY_RESULT_PROMISE_PREFIX(promise, f, fetch_tl_object<ton_api::adnl_ping>(std::move(data), true), |
| 60 | + "adnl.ping expected"); |
| 61 | + promise.set_value(create_serialize_tl_object<ton_api::adnl_pong>(f->value_)); |
| 62 | + } |
| 63 | + |
| 64 | + Callback() { |
| 65 | + } |
| 66 | + |
| 67 | + private: |
| 68 | +}; |
| 69 | + |
| 70 | +} // namespace adnl |
| 71 | + |
| 72 | +} // namespace ton |
| 73 | + |
| 74 | +std::atomic<bool> rotate_logs_flags{false}; |
| 75 | +void force_rotate_logs(int sig) { |
| 76 | + rotate_logs_flags.store(true); |
| 77 | +} |
| 78 | + |
| 79 | +int main(int argc, char *argv[]) { |
| 80 | + SET_VERBOSITY_LEVEL(verbosity_INFO); |
| 81 | + |
| 82 | + ton::PrivateKey pk; |
| 83 | + td::IPAddress addr; |
| 84 | + |
| 85 | + td::set_default_failure_signal_handler().ensure(); |
| 86 | + |
| 87 | + std::unique_ptr<td::LogInterface> logger_; |
| 88 | + SCOPE_EXIT { |
| 89 | + td::log_interface = td::default_log_interface; |
| 90 | + }; |
| 91 | + |
| 92 | + std::string config = "/var/ton-work/etc/adnl-proxy.conf.json"; |
| 93 | + |
| 94 | + td::OptionsParser p; |
| 95 | + p.set_description("adnl pinger"); |
| 96 | + p.add_option('v', "verbosity", "set verbosity level", [&](td::Slice arg) { |
| 97 | + int v = VERBOSITY_NAME(FATAL) + (td::to_integer<int>(arg)); |
| 98 | + SET_VERBOSITY_LEVEL(v); |
| 99 | + return td::Status::OK(); |
| 100 | + }); |
| 101 | + p.add_option('h', "help", "prints_help", [&]() { |
| 102 | + char b[10240]; |
| 103 | + td::StringBuilder sb(td::MutableSlice{b, 10000}); |
| 104 | + sb << p; |
| 105 | + std::cout << sb.as_cslice().c_str(); |
| 106 | + std::exit(2); |
| 107 | + return td::Status::OK(); |
| 108 | + }); |
| 109 | + p.add_option('d', "daemonize", "set SIGHUP", [&]() { |
| 110 | +#if TD_DARWIN || TD_LINUX |
| 111 | + close(0); |
| 112 | + setsid(); |
| 113 | +#endif |
| 114 | + td::set_signal_handler(td::SignalType::HangUp, force_rotate_logs).ensure(); |
| 115 | + return td::Status::OK(); |
| 116 | + }); |
| 117 | + p.add_option('l', "logname", "log to file", [&](td::Slice fname) { |
| 118 | + auto F = std::make_unique<td::FileLog>(); |
| 119 | + TRY_STATUS(F->init(fname.str(), std::numeric_limits<td::uint64>::max(), true)); |
| 120 | + logger_ = std::move(F); |
| 121 | + td::log_interface = logger_.get(); |
| 122 | + return td::Status::OK(); |
| 123 | + }); |
| 124 | + td::uint32 threads = 7; |
| 125 | + p.add_option('t', "threads", PSTRING() << "number of threads (default=" << threads << ")", [&](td::Slice fname) { |
| 126 | + td::int32 v; |
| 127 | + try { |
| 128 | + v = std::stoi(fname.str()); |
| 129 | + } catch (...) { |
| 130 | + return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: not a number"); |
| 131 | + } |
| 132 | + if (v < 1 || v > 256) { |
| 133 | + return td::Status::Error(ton::ErrorCode::error, "bad value for --threads: should be in range [1..256]"); |
| 134 | + } |
| 135 | + threads = v; |
| 136 | + return td::Status::OK(); |
| 137 | + }); |
| 138 | + p.add_option('u', "user", "change user", [&](td::Slice user) { return td::change_user(user); }); |
| 139 | + p.add_option('k', "key", "private key", [&](td::Slice key) { |
| 140 | + TRY_RESULT_ASSIGN(pk, ton::PrivateKey::import(key)); |
| 141 | + return td::Status::OK(); |
| 142 | + }); |
| 143 | + p.add_option('a', "addr", "ip:port of instance", [&](td::Slice key) { |
| 144 | + TRY_STATUS(addr.init_host_port(key.str())); |
| 145 | + return td::Status::OK(); |
| 146 | + }); |
| 147 | + |
| 148 | + p.run(argc, argv).ensure(); |
| 149 | + |
| 150 | + if (pk.empty()) { |
| 151 | + LOG(FATAL) << "no --key given"; |
| 152 | + } |
| 153 | + if (!addr.is_valid()) { |
| 154 | + LOG(FATAL) << "no --addr given"; |
| 155 | + } |
| 156 | + |
| 157 | + td::actor::Scheduler scheduler({threads}); |
| 158 | + |
| 159 | + td::actor::ActorOwn<ton::keyring::Keyring> keyring; |
| 160 | + td::actor::ActorOwn<ton::adnl::Adnl> adnl; |
| 161 | + td::actor::ActorOwn<ton::adnl::AdnlNetworkManager> network_manager; |
| 162 | + |
| 163 | + auto pub = pk.compute_public_key(); |
| 164 | + |
| 165 | + scheduler.run_in_context([&]() { |
| 166 | + keyring = ton::keyring::Keyring::create(""); |
| 167 | + td::actor::send_closure(keyring, &ton::keyring::Keyring::add_key, std::move(pk), true, [](td::Unit) {}); |
| 168 | + |
| 169 | + adnl = ton::adnl::Adnl::create("", keyring.get()); |
| 170 | + |
| 171 | + network_manager = ton::adnl::AdnlNetworkManager::create(static_cast<td::uint16>(addr.get_port())); |
| 172 | + |
| 173 | + td::actor::send_closure(network_manager, &ton::adnl::AdnlNetworkManager::add_self_addr, addr, 0); |
| 174 | + |
| 175 | + auto tladdr = ton::create_tl_object<ton::ton_api::adnl_address_udp>(addr.get_ipv4(), addr.get_port()); |
| 176 | + auto addr_vec = std::vector<ton::tl_object_ptr<ton::ton_api::adnl_Address>>(); |
| 177 | + addr_vec.push_back(std::move(tladdr)); |
| 178 | + auto tladdrlist = ton::create_tl_object<ton::ton_api::adnl_addressList>( |
| 179 | + std::move(addr_vec), ton::adnl::Adnl::adnl_start_time(), ton::adnl::Adnl::adnl_start_time(), 0, 2000000000); |
| 180 | + auto addrlist = ton::adnl::AdnlAddressList::create(tladdrlist).move_as_ok(); |
| 181 | + |
| 182 | + td::actor::send_closure(adnl, &ton::adnl::Adnl::add_id, ton::adnl::AdnlNodeIdFull{pub}, std::move(addrlist)); |
| 183 | + td::actor::send_closure(adnl, &ton::adnl::Adnl::subscribe, ton::adnl::AdnlNodeIdShort{pub.compute_short_id()}, |
| 184 | + ton::adnl::Adnl::int_to_bytestring(ton::ton_api::adnl_ping::ID), |
| 185 | + std::make_unique<ton::adnl::Callback>()); |
| 186 | + }); |
| 187 | + |
| 188 | + while (scheduler.run(1)) { |
| 189 | + if (rotate_logs_flags.exchange(false)) { |
| 190 | + if (td::log_interface) { |
| 191 | + td::log_interface->rotate(); |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments