File size: 3,635 Bytes
1bff97b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "udp_inbound.h"
#include "udp_client.h"
#include "conn_map.h"
#include "hv_utils.h"
#include "bolt/datagram.h"
#include "bolt/crypt.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)

                    char result[1] = {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<UdpServerBoltProxy>(serverProxy);
                    UdpConnMap<uint32_t, UdpServerBoltProxy>::getInstance().add(session_id, proxy);
                }
                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<uint32_t, UdpServerBoltProxy>::getInstance().get(session_id);
                    serverProxy->recycle();
                    UdpConnMap<uint32_t, UdpServerBoltProxy>::getInstance().remove(session_id);
                }
                else
                {
                }
            }
            else if (type == BOLT_PAYLOAD_TYPE_UDP)
            {

                auto serverProxy = UdpConnMap<uint32_t, UdpServerBoltProxy>::getInstance().get(session_id);
                struct sockaddr_in t_addr = {0}, u_addr = {0};
                t_addr.sin_addr.s_addr = t_ip;
                t_addr.sin_port = t_port;
                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)
{

}