returns a port on success and -1 on failure
| 734 | |
| 735 | // returns a port on success and -1 on failure |
| 736 | int start_proxy(int proxy_type) |
| 737 | { |
| 738 | using namespace lt; |
| 739 | |
| 740 | std::map<int, proxy_t> :: iterator i = running_proxies.begin(); |
| 741 | for (; i != running_proxies.end(); ++i) |
| 742 | { |
| 743 | if (i->second.type == proxy_type) { return i->first; } |
| 744 | } |
| 745 | |
| 746 | int const port = find_available_port(); |
| 747 | |
| 748 | char const* type = ""; |
| 749 | char const* auth = ""; |
| 750 | char const* cmd = ""; |
| 751 | |
| 752 | switch (proxy_type) |
| 753 | { |
| 754 | case settings_pack::socks4: |
| 755 | type = "socks4"; |
| 756 | auth = " --allow-v4"; |
| 757 | cmd = ".." SEPARATOR "socks.py"; |
| 758 | break; |
| 759 | case settings_pack::socks5: |
| 760 | type = "socks5"; |
| 761 | cmd = ".." SEPARATOR "socks.py"; |
| 762 | break; |
| 763 | case settings_pack::socks5_pw: |
| 764 | type = "socks5"; |
| 765 | auth = " --username testuser --password testpass"; |
| 766 | cmd = ".." SEPARATOR "socks.py"; |
| 767 | break; |
| 768 | case settings_pack::http: |
| 769 | type = "http"; |
| 770 | cmd = ".." SEPARATOR "http_proxy.py"; |
| 771 | break; |
| 772 | case settings_pack::http_pw: |
| 773 | type = "http"; |
| 774 | auth = " --basic-auth testuser:testpass"; |
| 775 | cmd = ".." SEPARATOR "http_proxy.py"; |
| 776 | break; |
| 777 | } |
| 778 | std::vector<std::string> python_exes = get_python(); |
| 779 | for (auto const& python_exe : python_exes) |
| 780 | { |
| 781 | char buf[1024]; |
| 782 | std::snprintf(buf, sizeof(buf), "%s %s --port %d%s", python_exe.c_str(), cmd, port, auth); |
| 783 | |
| 784 | std::printf("%s starting proxy on port %d (%s %s)...\n", time_now_string().c_str(), port, type, auth); |
| 785 | std::printf("%s\n", buf); |
| 786 | pid_type r = async_run(buf); |
| 787 | if (r == 0) continue; |
| 788 | proxy_t t = { r, proxy_type }; |
| 789 | running_proxies.insert(std::make_pair(port, t)); |
| 790 | std::printf("%s launched\n", time_now_string().c_str()); |
| 791 | std::this_thread::sleep_for(lt::milliseconds(500)); |
| 792 | wait_for_port(port); |
| 793 | return port; |