-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpngate-connect.cpp
196 lines (171 loc) · 6.27 KB
/
vpngate-connect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "csv.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <atomic>
#include <cstdlib>
#include <csignal>
#include <boost/beast/core.hpp>
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/scope_exit.hpp>
#include <boost/filesystem.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/process.hpp>
#include <boost/program_options.hpp>
namespace
{
auto on_exit() -> std::vector<std::function<void(void)>>&
{
static std::vector<std::function<void(void)>> on_exit;
return on_exit;
}
template <typename F>
void add_exit_func(F f) {on_exit().emplace_back(f);}
void signal_handler(int)
{
for (auto &&f : on_exit())
std::invoke(f);
}
}
struct vpn
{
std::string ip;
int score;
int speed;
std::string country;
std::string config;
};
auto operator<<(std::ostream& os, vpn const& v) -> std::ostream&
{
return os << "IP: " << v.ip << "\n"
<< "Score: " << v.score << "\n"
<< "Speed: " << v.speed << "\n"
<< "Country: " << v.country << "\n";
}
auto get(std::string host, std::string port, std::string target)
-> boost::beast::http::response<boost::beast::http::dynamic_body>
{
namespace beast = boost::beast; // from <boost/beast.hpp>
namespace http = beast::http; // from <boost/beast/http.hpp>
namespace net = boost::asio; // from <boost/asio.hpp>
using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
net::io_context ioc;
tcp::resolver resolver(ioc);
beast::tcp_stream stream(ioc);
auto const results = resolver.resolve(host, port);
stream.connect(results);
http::request<http::string_body> req{http::verb::get, target, 11};
req.set(http::field::host, host);
req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);
http::write(stream, req);
beast::flat_buffer buffer;
http::response<http::dynamic_body> res;
http::read(stream, buffer, res);
// Gracefully close the socket
beast::error_code ec;
stream.socket().shutdown(tcp::socket::shutdown_both, ec);
if(ec && ec != beast::errc::not_connected)
throw beast::system_error{ec};
return res;
}
auto decode64(const std::string &val) -> std::string
{
using namespace boost::archive::iterators;
using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
return boost::algorithm::trim_right_copy_if(
std::string(It(std::begin(val)), It(std::end(val))),
[](char c) {
return c == '\0';
});
}
int main(int argc, char* argv[])
{
namespace po = boost::program_options;
namespace fs = boost::filesystem;
std::signal(SIGINT, signal_handler);
try
{
po::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("country,c", po::value<std::string>()->default_value("JP"), "Country")
("list,l", "list available country")
("id", po::value<int>()->default_value(0), "the id on the vpn list")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
if (vm.count("help"))
{
std::cout << desc << '\n';
std::exit(EXIT_SUCCESS);
}
std::vector<vpn> configs;
{
auto res = get("www.vpngate.net", "80", "/api/iphone/");
std::string tmpfile =
(fs::temp_directory_path() / fs::unique_path()).string();
std::ofstream f {tmpfile};
BOOST_SCOPE_EXIT(&tmpfile) {
fs::remove(tmpfile);
} BOOST_SCOPE_EXIT_END
f << boost::beast::buffers_to_string(res.body().data()) << std::flush;
csv::CSVReader rows(tmpfile);
for (csv::CSVRow& row: rows)
configs.emplace_back(vpn{
.ip = row["IP"].get<std::string>(),
.score = row["Score"].get<int>(),
.speed = row["Speed"].get<int>(),
.country = row["CountryShort"].get<std::string>(),
.config = row["OpenVPN_ConfigData_Base64"].get<std::string>()
});
}
if (vm.count("list"))
{
std::vector<std::string> list;
std::transform(configs.begin(), configs.end(),
std::back_inserter(list),
[](vpn const & a){ return a.country; });
std::sort(list.begin(), list.end());
auto end = std::unique(list.begin(), list.end());
for (auto it = list.begin(); it != end; ++it)
std::cout << *it << "\n";
std::exit(EXIT_SUCCESS);
}
auto back = std::stable_partition(configs.begin(), configs.end(),
[country = vm["country"].as<std::string>()](vpn const & a) {
return a.country == country;
});
std::sort(configs.begin(), back, [](vpn const & a, vpn const & b) {
return a.score > b.score;
});
std::string configfile =
(fs::temp_directory_path() / fs::unique_path()).string();
add_exit_func([configfile] { fs::remove(configfile); });
BOOST_SCOPE_EXIT(&configfile) { fs::remove(configfile); } BOOST_SCOPE_EXIT_END
{
std::ofstream f {configfile};
std::cout << configs.at(vm["id"].as<int>()) << "\n";
f << decode64(configs.at(vm["id"].as<int>()).config) << std::flush;
}
{
namespace bp = boost::process;
bp::child c(bp::search_path("openvpn"), configfile, bp::std_out > stdout);
c.wait();
}
}
catch(std::exception const& e)
{
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}