#include "udp_inbound.h" #include "udp_client.h" #include "conn_map.h" #include "utils.h" #include "bolt/datagram.h" #include "bolt/crypt.h" #include "spdlog/spdlog.h" void udp_on_recvfrom(hio_t *io, void *buf, int readbytes) { if (readbytes > 0) { UNPACK_TUNNEL_DATA(dest, dest_len, ver, res, type, t_ip, t_port, u_ip, u_port, session_id, extend, extend_len, buf, readbytes) if (ver == BOLT_VERSION && res == BOLT_RESERVE) { if (type == BOLT_PAYLOAD_TYPE_CMD) { if (dest_len == 73 && dest[0] == BOLT_CHANNEL_CMD_BIND_REQUEST) { UNPACK_BIND_DATA(command, request_id, signal_id, session_id, data_st, dest, dest_len) UdpServerBoltProxy *serverProxy = new UdpServerBoltProxy(io); serverProxy->getConfig().request_id = request_id; serverProxy->getConfig().session_id = session_id; serverProxy->getConfig().data_st = data_st; serverProxy->getConfig().signal_id = signal_id; if (extend_len >= 5) { if (strcmp(extend, "ept=1") == 0) { serverProxy->getConfig().encrypt = true; } } serverProxy->getConfig().ept_type = serverProxy->getConfig().encrypt ? CRYPT_TYPE::XOR : CRYPT_TYPE::NONE; serverProxy->getConfig().ept_key = serverProxy->getConfig().encrypt ? generateRandomKey() : 0; GENERATE_DECRYPT_KEY(extend_response, extend_response_len, serverProxy->getConfig().ept_type, serverProxy->getConfig().ept_key) uint32_t result = BOLT_BIND_RESPONSE_CODE_SUCESS; PACK_BIND_RESPONSE_DATA(bind_response, bind_response_len, BOLT_CHANNEL_CMD_BIND_RESPONSE, request_id, session_id, result) PACK_TUNNEL_DATA(response, response_len, BOLT_VERSION, BOLT_RESERVE, BOLT_PAYLOAD_TYPE_CMD, 0, 0, 0, 0, session_id, extend_response, extend_response_len, bind_response, bind_response_len) hio_write(io, response, response_len); auto proxy = std::unique_ptr(serverProxy); UdpConnMap::getInstance().add(session_id, proxy); spdlog::info("[UDP]BOLT_CHANNEL_CMD_BIND_REQUEST: requestId:{}, session_id:{}, encrypt:{}", request_id, session_id, serverProxy->getConfig().encrypt); } else if (dest_len == 9 && dest[0] == BOLT_CHANNEL_CMD_UNBIND_REQUEST) { UNPACK_UNBIND_DATA(command, session_id, code, dest, dest_len) auto serverProxy = UdpConnMap::getInstance().get(session_id); serverProxy->recycle(); spdlog::info("[UDP]BOLT_CHANNEL_CMD_UNBIND_REQUEST: requestId:{}, session_id:{}", serverProxy->getConfig().request_id, serverProxy->getConfig().session_id); UdpConnMap::getInstance().remove(session_id); } else { } } else if (type == BOLT_PAYLOAD_TYPE_UDP) { auto serverProxy = UdpConnMap::getInstance().get(session_id); struct sockaddr_in t_addr = {0}, u_addr = {0}; t_addr.sin_family = AF_INET; t_addr.sin_addr.s_addr = t_ip; t_addr.sin_port = t_port; u_addr.sin_family = AF_INET; u_addr.sin_addr.s_addr = u_ip; u_addr.sin_port = u_port; if (serverProxy && serverProxy->analyzeData(t_addr, u_addr, session_id, dest, dest_len, extend, extend_len)) { } } } } } void udp_on_writed(hio_t *io, const void *buf, int writebytes) { } void udp_on_close(hio_t *io) { }