| 1129 | } |
| 1130 | |
| 1131 | static RPCMethod exportasmap() |
| 1132 | { |
| 1133 | return RPCMethod{ |
| 1134 | "exportasmap", |
| 1135 | "Export the embedded ASMap data to a file. Any existing file at the path will be overwritten.\n", |
| 1136 | { |
| 1137 | {"path", RPCArg::Type::STR, RPCArg::Optional::NO, "Path to the output file. If relative, will be prefixed by datadir."}, |
| 1138 | }, |
| 1139 | RPCResult{ |
| 1140 | RPCResult::Type::OBJ, "", "", |
| 1141 | { |
| 1142 | {RPCResult::Type::STR, "path", "the absolute path that the ASMap data was written to"}, |
| 1143 | {RPCResult::Type::NUM, "bytes_written", "the number of bytes written to the file"}, |
| 1144 | {RPCResult::Type::STR_HEX, "file_hash", "the SHA256 hash of the exported ASMap data"}, |
| 1145 | } |
| 1146 | }, |
| 1147 | RPCExamples{ |
| 1148 | HelpExampleCli("exportasmap", "\"asmap.dat\"") + HelpExampleRpc("exportasmap", "\"asmap.dat\"")}, |
| 1149 | [&](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue { |
| 1150 | #ifndef ENABLE_EMBEDDED_ASMAP |
| 1151 | throw JSONRPCError(RPC_MISC_ERROR, "No embedded ASMap data available"); |
| 1152 | #else |
| 1153 | if (node::data::ip_asn.empty() || !CheckStandardAsmap(node::data::ip_asn)) { |
| 1154 | throw JSONRPCError(RPC_MISC_ERROR, "Embedded ASMap data appears to be corrupted"); |
| 1155 | } |
| 1156 | |
| 1157 | const ArgsManager& args{EnsureAnyArgsman(request.context)}; |
| 1158 | const fs::path export_path{fsbridge::AbsPathJoin(args.GetDataDirNet(), fs::u8path(self.Arg<std::string_view>("path")))}; |
| 1159 | |
| 1160 | AutoFile file{fsbridge::fopen(export_path, "wb")}; |
| 1161 | if (file.IsNull()) { |
| 1162 | throw JSONRPCError(RPC_MISC_ERROR, strprintf("Failed to open asmap file: %s", fs::PathToString(export_path))); |
| 1163 | } |
| 1164 | |
| 1165 | file << node::data::ip_asn; |
| 1166 | |
| 1167 | if (file.fclose() != 0) { |
| 1168 | throw JSONRPCError(RPC_MISC_ERROR, strprintf("Failed to close asmap file: %s", fs::PathToString(export_path))); |
| 1169 | } |
| 1170 | |
| 1171 | HashWriter hasher; |
| 1172 | hasher.write(node::data::ip_asn); |
| 1173 | |
| 1174 | UniValue result(UniValue::VOBJ); |
| 1175 | result.pushKV("path", export_path.utf8string()); |
| 1176 | result.pushKV("bytes_written", node::data::ip_asn.size()); |
| 1177 | result.pushKV("file_hash", HexStr(hasher.GetSHA256())); |
| 1178 | return result; |
| 1179 | #endif |
| 1180 | }, |
| 1181 | }; |
| 1182 | } |
| 1183 | |
| 1184 | UniValue AddrmanEntryToJSON(const AddrInfo& info, const CConnman& connman) |
| 1185 | { |
nothing calls this directly
no test coverage detected