forked from darkk/redsocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
171 lines (147 loc) · 4.19 KB
/
debug.c
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
/* redsocks - transparent TCP-to-proxy redirector
* Copyright (C) 2007-2018 Leonid Evdokimov <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#include "parser.h"
#include "main.h"
#include "log.h"
#include "utils.h"
#include <event2/http.h>
#include <event2/buffer.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct debug_instance_t {
int configured;
char* http_ip;
uint16_t http_port;
struct evhttp* http_server;
} debug_instance;
static debug_instance instance = {
.configured = 0,
};
static parser_entry debug_entries[] =
{
{ .key = "http_ip", .type = pt_pchar },
{ .key = "http_port", .type = pt_uint16 },
{ }
};
static int debug_onenter(parser_section *section)
{
if (instance.configured) {
parser_error(section->context, "only one instance of debug is valid");
return -1;
}
memset(&instance, 0, sizeof(instance));
for (parser_entry *entry = §ion->entries[0]; entry->key; entry++)
entry->addr =
(strcmp(entry->key, "http_ip") == 0) ? (void*)&instance.http_ip:
(strcmp(entry->key, "http_port") == 0) ? (void*)&instance.http_port :
NULL;
return 0;
}
static int debug_onexit(parser_section *section)
{
instance.configured = 1;
if (!instance.http_ip)
instance.http_ip = strdup("localhost");
return 0;
}
static parser_section debug_conf_section =
{
.name = "debug",
.entries = debug_entries,
.onenter = debug_onenter,
.onexit = debug_onexit,
};
static void debug_pipe(struct evhttp_request *req, void *arg)
{
UNUSED(arg);
int fds[2];
int code = (pipe(fds) == 0) ? HTTP_OK : HTTP_SERVUNAVAIL;
evhttp_send_reply(req, code, NULL, NULL);
}
static void debug_meminfo_json(struct evhttp_request *req, void *arg)
{
UNUSED(arg);
struct evbuffer* body = evbuffer_new();
evbuffer_add(body, "{", 1);
FILE* fd = fopen("/proc/vmstat", "r");
while (!feof(fd)) {
char buf[64];
size_t pages;
if (fscanf(fd, "%63s %zu", buf, &pages) == 2 && strncmp(buf, "nr_", 3) == 0) {
evbuffer_add_printf(body, "\"%s\": %zu, ", buf, pages);
}
for (int c = 0; c != EOF && c != '\n'; c = fgetc(fd))
;
}
fclose(fd);
size_t vmsize, rss, share, text, z, data;
fd = fopen("/proc/self/statm", "r");
if (fscanf(fd, "%zu %zu %zu %zu %zu %zu %zu", &vmsize, &rss, &share, &text, &z, &data, &z) == 7) {
evbuffer_add_printf(body,
"\"vmsize\": %zu, \"vmrss\": %zu, \"share\": %zu, \"text\": %zu, \"data\": %zu, ",
vmsize, rss, share, text, data);
}
fclose(fd);
evbuffer_add_printf(body, "\"getpagesize\": %d}", getpagesize());
evhttp_send_reply(req, HTTP_OK, NULL, body);
evbuffer_free(body);
}
static int debug_fini();
static int debug_init(struct event_base* evbase)
{
if (!instance.http_port)
return 0;
instance.http_server = evhttp_new(evbase);
if (!instance.http_server) {
log_errno(LOG_ERR, "evhttp_new()");
goto fail;
}
if (evhttp_bind_socket(instance.http_server, instance.http_ip, instance.http_port) != 0) {
log_errno(LOG_ERR, "evhttp_bind_socket()");
goto fail;
}
if (evhttp_set_cb(instance.http_server, "/debug/pipe", debug_pipe, NULL) != 0) {
log_errno(LOG_ERR, "evhttp_set_cb()");
goto fail;
}
if (evhttp_set_cb(instance.http_server, "/debug/meminfo.json", debug_meminfo_json, NULL) != 0) {
log_errno(LOG_ERR, "evhttp_set_cb()");
goto fail;
}
return 0;
fail:
debug_fini();
return -1;
}
static int debug_fini()
{
if (instance.http_server) {
evhttp_free(instance.http_server);
instance.http_server = 0;
}
free(instance.http_ip);
memset(&instance, 0, sizeof(instance));
return 0;
}
app_subsys debug_subsys =
{
.init = debug_init,
.fini = debug_fini,
.conf_section = &debug_conf_section,
};
/* vim:set tabstop=4 softtabstop=4 shiftwidth=4: */
/* vim:set foldmethod=marker foldlevel=32 foldmarker={,}: */