Commit d6454dd1 authored by 神楽坂玲奈's avatar 神楽坂玲奈

new

parent 6636c1ee
Pipeline #16664 failed with stages
in 48 seconds
...@@ -14,7 +14,6 @@ class Router : private boost::noncopyable { ...@@ -14,7 +14,6 @@ class Router : private boost::noncopyable {
public: public:
static const int secret_length = 32; static const int secret_length = 32;
static std::map<std::pair<unsigned char, unsigned char>, int> raws; // (family,proto) => fd static std::map<std::pair<unsigned char, unsigned char>, int> raws; // (family,proto) => fd
static std::map<int, Router *> tuns; // fd => router
static std::map<int, Router *> all; // id => router static std::map<int, Router *> all; // id => router
static unsigned char local_secret[secret_length]; static unsigned char local_secret[secret_length];
...@@ -32,7 +31,6 @@ public: ...@@ -32,7 +31,6 @@ public:
system(config.up.c_str()); system(config.up.c_str());
all[config.remote_id] = this; all[config.remote_id] = this;
tuns[tun] = this;
}; };
static void create_secret(const std::string &secret, unsigned char *target) { static void create_secret(const std::string &secret, unsigned char *target) {
......
...@@ -59,8 +59,7 @@ void inbound(int raw) { ...@@ -59,8 +59,7 @@ void inbound(int raw) {
} }
// tun -> internet // tun -> internet
void outbound(int tun) { void outbound(Router* router) {
auto router = Router::tuns[tun];
unsigned char buffer[ETH_DATA_LEN]; unsigned char buffer[ETH_DATA_LEN];
auto meta = (Meta *) buffer; auto meta = (Meta *) buffer;
meta->src_id = config.local_id; meta->src_id = config.local_id;
...@@ -68,7 +67,7 @@ void outbound(int tun) { ...@@ -68,7 +67,7 @@ void outbound(int tun) {
meta->reserved = 0; meta->reserved = 0;
auto inner = buffer + sizeof(Meta); auto inner = buffer + sizeof(Meta);
size_t packet_length; size_t packet_length;
while ((packet_length = read(tun, inner, sizeof(buffer) - sizeof(Meta))) >= 0) { while ((packet_length = read(router->tun, inner, sizeof(buffer) - sizeof(Meta))) >= 0) {
if (!router->remote_addr.ss_family) continue; if (!router->remote_addr.ss_family) continue;
router->encrypt(inner, packet_length); router->encrypt(inner, packet_length);
if (setsockopt(router->raw, SOL_SOCKET, SO_MARK, &router->config.mark, sizeof(router->config.mark)) < 0) { if (setsockopt(router->raw, SOL_SOCKET, SO_MARK, &router->config.mark, sizeof(router->config.mark)) < 0) {
...@@ -89,7 +88,7 @@ int main(int argc, char *argv[]) { ...@@ -89,7 +88,7 @@ int main(int argc, char *argv[]) {
for (const auto &item: config.routers) new Router(item); for (const auto &item: config.routers) new Router(item);
std::vector<std::thread> threads; std::vector<std::thread> threads;
for (auto &[_, router]: Router::all)threads.emplace_back(outbound, router->tun); for (auto &[_, router]: Router::all)threads.emplace_back(outbound, router);
for (auto &[_, raw]: Router::raws)threads.emplace_back(inbound, raw); for (auto &[_, raw]: Router::raws)threads.emplace_back(inbound, raw);
for (auto &thread: threads) thread.join(); for (auto &thread: threads) thread.join();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment