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

new

parent 418a9636
Pipeline #16633 failed with stages
in 2 minutes and 56 seconds
......@@ -3,13 +3,12 @@ project(tun)
set(CMAKE_CXX_STANDARD 23)
add_executable(tun src/main.cpp)
add_executable(tun src/main.cpp src/Config.cpp src/checksum.cpp)
set(CMAKE_CXX_FLAGS_DEBUG -ggdb)
find_package(Threads REQUIRED)
target_link_libraries(tun Threads::Threads)
#set(Boost_USE_STATIC_LIBS ON)
#find_package(Boost COMPONENTS program_options REQUIRED)
#target_link_libraries(tun Boost::program_options)
find_package(nlohmann_json REQUIRED)
target_link_libraries(tun nlohmann_json::nlohmann_json)
FROM alpine AS builder
RUN apk --no-cache add build-base cmake linux-headers boost-dev boost-static
RUN apk --no-cache add build-base cmake linux-headers boost-dev boost-static nlohmann-json
WORKDIR /usr/src/app
COPY CMakeLists.txt .
......
#include <nlohmann/json.hpp>
using json = nlohmann::json;
struct ConfigRouter {
unsigned char remote_id;
unsigned char proto;
unsigned char mark;
unsigned char family;
std::string remote_secret;
std::string dev;
std::string up;
std::string endpoint;
};
struct Config {
unsigned char local_id;
std::string local_secret;
std::vector<ConfigRouter> routers;
};
void from_json(const json& j, ConfigRouter& p) {
j.at("remote_id").get_to(p.remote_id);
j.at("proto").get_to(p.proto);
j.at("mark").get_to(p.mark);
j.at("family").get_to(p.family);
j.at("remote_secret").get_to(p.remote_secret);
j.at("dev").get_to(p.dev);
j.at("up").get_to(p.up);
j.at("endpoint").get_to(p.endpoint);
}
void from_json(const json& j, Config& p) {
j.at("local_id").get_to(p.local_id);
j.at("local_secret").get_to(p.local_secret);
j.at("routers").get_to(p.routers);
}
#include <cstdint>
uint16_t csum(uint16_t *packet, int packlen) {
unsigned long sum = 0;
while (packlen > 1) {
sum += *(packet++);
packlen -= 2;
}
if (packlen > 0) sum += *(unsigned char *) packet;
while (sum >> 16) sum = (sum & 0xffff) + (sum >> 16);
return (uint16_t) ~sum;
}
\ No newline at end of file
......@@ -22,28 +22,6 @@ unsigned char local_id;
unsigned char remote_id;
sockaddr_storage remote_addr{};
/* Checksum a block of data */
uint16_t csum(uint16_t *packet, int packlen) {
unsigned long sum = 0;
while (packlen > 1) {
sum += *(packet++);
packlen -= 2;
}
if (packlen > 0)
sum += *(unsigned char *) packet;
/* TODO: this depends on byte order */
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return (uint16_t) ~sum;
}
class Secret {
private:
char *key;
......@@ -66,6 +44,7 @@ Secret *localSecret;
Secret *remoteSecret;
#include <linux/ipv6.h>
#include <vector>
// internet -> tun
......@@ -123,88 +102,72 @@ void outbound(int raw, int tun) {
// std::cout << "sendto: " << inet_ntoa(remote_addr.sin_addr) << std::endl;
if (!remote_addr.ss_family) continue;
localSecret->encrypt(inner, packet_length);
if (sendto(raw, buffer, packet_length + sizeof(Meta), 0, (sockaddr *) &remote_addr, sizeof(remote_addr)) < 0) {
if (sendto(raw, buffer, packet_length + sizeof(Meta), 0, (sockaddr *) &remote_addr, sizeof(remote_addr)) <
0) {
perror("outbound write");
}
}
perror("outbound read");
}
auto get_var(const char *varname, bool required = false) {
auto value = getenv(varname);
if (value == nullptr && required) {
std::cerr << "missing required environment variable: " << varname << std::endl;
exit(2);
}
return value;
}
auto get_var_number(const char *varname, bool required = false) {
auto value = get_var(varname, required);
if (value == nullptr) return 0;
return atoi(value);
}
int main(int argc, char *argv[]) {
local_id = get_var_number("LOCAL_ID", true);
remote_id = get_var_number("REMOTE_ID", true);
unsigned char proto = get_var_number("PROTO", true);
localSecret = new Secret(get_var("LOCAL_SECRET", true));
remoteSecret = new Secret(get_var("REMOTE_SECRET", true));
auto dev = get_var("DEV", true);
// optionals
auto up = get_var("UP_SCRIPT");
auto endpoint = get_var("ENDPOINT");
auto mark = get_var_number("MARK");
auto family = get_var_number("FAMILY") == 6 ? AF_INET6 : AF_INET;
if (endpoint != nullptr) {
addrinfo hints = {
.ai_family = family
};
addrinfo *result;
auto ret = getaddrinfo(endpoint, nullptr, &hints, &result);
if (ret != 0) {
puts(gai_strerror(ret));
return -1;
}
remote_addr = *(sockaddr_storage *) result->ai_addr;
freeaddrinfo(result); /* No longer needed */
}
ifreq ifr{};
ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
strncpy(ifr.ifr_name, dev, IFNAMSIZ);
auto raw = socket(family, SOCK_RAW, proto);
if (raw < 0) {
perror("socket init error");
return -1;
}
if (mark) {
if (setsockopt(raw, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
perror("setsockopt error");
return -1;
}
}
auto tun = open("/dev/net/tun", O_RDWR);
if (tun < 0) {
perror("tun init error");
return -1;
}
puts(dev);
if (ioctl(tun, TUNSETIFF, &ifr) < 0) {
perror("ioctl error");
return -1;
}
system(up);
std::thread t1(inbound, raw, tun);
std::thread t2(outbound, raw, tun);
t1.join();
t2.join();
int main(int argc, char *argv[]) {
// json data = json::parse(argv[1]);
// auto config = data.get<Config>();
//
//
// if (endpoint != nullptr) {
// addrinfo hints = {
// .ai_family = family
// };
// addrinfo *result;
//
// auto ret = getaddrinfo(endpoint, nullptr, &hints, &result);
// if (ret != 0) {
// puts(gai_strerror(ret));
// return -1;
// }
// remote_addr = *(sockaddr_storage *) result->ai_addr;
// freeaddrinfo(result); /* No longer needed */
// }
// ifreq ifr{};
// ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
// strncpy(ifr.ifr_name, dev, IFNAMSIZ);
//
// auto raw = socket(family, SOCK_RAW, proto);
// if (raw < 0) {
// perror("socket init error");
// return -1;
// }
// if (mark) {
// if (setsockopt(raw, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0) {
// perror("setsockopt error");
// return -1;
// }
// }
// auto tun = open("/dev/net/tun", O_RDWR);
// if (tun < 0) {
// perror("tun init error");
// return -1;
// }
// puts(dev);
//
// if (ioctl(tun, TUNSETIFF, &ifr) < 0) {
// perror("ioctl error");
// return -1;
// }
//
// system(up);
//
// std::thread t1(inbound, raw, tun);
// std::thread t2(outbound, raw, tun);
// t1.join();
// t2.join();
return 0;
}
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