Repository where nkls researches and experiments with building mixnet stuff using libtorrent
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.6 KiB

5 months ago
5 months ago
5 months ago
5 months ago
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <fstream>
  5. //#include <openssl/rsa.h>
  6. //#include <openssl/pem.h>
  7. //#include <openssl/err.h>
  8. #include <exception>
  9. #include <gpgme.h>
  10. #include <boost/program_options.hpp>
  11. #include <libtorrent/session.hpp>
  12. #include <libtorrent/session_params.hpp>
  13. #include <libtorrent/kademlia/dht_state.hpp>
  14. #include <libtorrent/kademlia/dht_storage.hpp>
  15. //#include <libtorrent/add_torrent_params.hpp>
  16. //#include <libtorrent/torrent_handle.hpp>
  17. //#include <libtorrent/alert_types.hpp>
  18. #include <libtorrent/magnet_uri.hpp>
  19. //#include <thread>
  20. //#include <chrono>
  21. namespace po=boost::program_options;
  22. class mix
  23. {
  24. public:
  25. int id;
  26. std::string name;
  27. gpgme_data_t keydata;
  28. gpgme_ctx_t context;
  29. gpgme_error_t error;
  30. gpgme_key_t key;
  31. void init()
  32. {
  33. std::cout << "Mix starting..." << std::endl;
  34. if(std::ifstream("mix.conf"))
  35. {
  36. std::cout << "Config file existing" << std::endl;
  37. this->read_settings();
  38. std::cout << "name = " << name << std::endl;
  39. std::cout << "id = " << id << std::endl;
  40. // read_keys();
  41. start_peer();
  42. }
  43. else
  44. {
  45. std::cout << "Config file does not exist. Exiting." << std::endl;
  46. std::exit(1);
  47. }
  48. // std::cout << "Looking for keys" << std::endl;
  49. // gpgme_check_version(NULL);
  50. // gpgme_new(&context);
  51. // create_keys();
  52. }
  53. void read_settings()
  54. {
  55. po::options_description desc("Options");
  56. desc.add_options() ("name", po::value<std::string>(&name), "name");
  57. desc.add_options() ("id", po::value<int>(&id), "id");
  58. po::variables_map vm;
  59. std::ifstream settings_file("mix.conf");
  60. vm = po::variables_map();
  61. po::store(po::parse_config_file(settings_file , desc), vm);
  62. po::notify(vm);
  63. }
  64. void read_keys()
  65. {
  66. gpgme_data_new_from_file(&keydata, "keys.gpg", 1);
  67. gpgme_op_import(context, keydata);
  68. gpgme_op_keylist_start (context, "mix", 0);
  69. while(!error)
  70. {
  71. error = gpgme_op_keylist_next (context, &key);
  72. if (error){break;}
  73. std::cout << key->subkeys->keyid << std::endl;
  74. if (key->uids && key->uids->name)
  75. {
  76. std::cout << key->uids->name << std::endl;
  77. }
  78. if (key->uids && key->uids->email)
  79. {
  80. std::cout << key->uids->email << std::endl;
  81. }
  82. }
  83. }
  84. void create_keys()
  85. {
  86. error=gpgme_op_createkey(context,"mix@pep.foundation",NULL,0,0,NULL,0);
  87. if(error)
  88. {
  89. std::cout << "Error generating keys" << error << std::endl;
  90. }
  91. else
  92. {
  93. std::cout << "Keys generated successfully" << std::endl;
  94. }
  95. }
  96. void start_peer()
  97. {
  98. lt::settings_pack p;
  99. p.set_int(lt::settings_pack::alert_mask,lt::alert_category::status | lt::alert_category::error);
  100. lt::session ses(p);
  101. std::cout << "libtorrent version: " << lt::version() << std::endl;
  102. std::cout << "announce ip: " << p.announce_ip << std::endl;
  103. std::cout << "user agent: " << p.user_agent << std::endl;
  104. std::cout << "listen interfaces: " << p.listen_interfaces << std::endl;
  105. std::cout << "peer fingerprint: " << p.peer_fingerprint << std::endl;
  106. p.set_str(lt::settings_pack::dht_bootstrap_nodes, "dht.bootstrap.de:12345");
  107. std::cout << "dht bootstrap nodes: " << p.get_str(lt::settings_pack::dht_bootstrap_nodes) << std::endl;
  108. // lt::dht_default_storage stor;
  109. bool peerexists;
  110. lt::get_peers('276a9c09b87f0695ccc7936da5332d2d35957aff')
  111. std::cout << peerexists << std::endl;
  112. // lt::add_torrent_params atp = lt::parse_magnet_uri("magnet:?xt=urn:btih:D540FC48EB12F2833163EED6421D449DD8F1CE1F&dn=Ubuntu%20desktop%2019.04%20(64bit)");
  113. // atp.save_path=".";
  114. // lt::torrent_handle h = ses.add_torrent(std::move(atp));
  115. // for (;;)
  116. // {
  117. // std::vector<lt::alert*> alerts;
  118. // ses.pop_alerts(&alerts);
  119. // for (lt::alert const* a : alerts)
  120. // {
  121. // std::cout << a->message() << std::endl;
  122. // if (lt::alert_cast<lt::torrent_finished_alert>(a))
  123. // {
  124. // std::cout << "Torrent finished" << std::endl;
  125. // }
  126. // if (lt::alert_cast<lt::torrent_error_alert>(a))
  127. // {
  128. // std::cout << "Torrent error" << std::endl;
  129. // }
  130. // }
  131. // }
  132. // lt::dht::dht_state dst;
  133. // dst=ses.dht_state();
  134. // std::cout << ses.dht_state << std::endl;
  135. // std::vector<lt::alert*> alerts; ses.pop_alerts(&alerts);
  136. // lt::alert a;
  137. }
  138. };
  139. int main()
  140. {
  141. mix mix1;
  142. mix1.init();
  143. }