radare2/binr/r2agent/r2agent.c

141 lines
3.1 KiB
C
Raw Normal View History

2016-04-25 21:09:39 +00:00
/* radare2 - LGPL - Copyright 2013-2016 - pancake */
2013-05-10 23:58:05 +00:00
#include <getopt.c>
#include <r_core.h>
#include <signal.h>
#if __WINDOWS__
int main() {
eprintf ("r2agent: Not yet implemented for this platform.\n");
return 1;
}
#else
2013-05-10 23:58:05 +00:00
#include "index.h"
static int usage (int v) {
printf ("Usage: r2agent [-adhs] [-p port]\n"
" -a listen for everyone (localhost by default)\n"
" -d run in daemon mode (background)\n"
" -h show this help message\n"
" -s run in sandbox mode\n"
" -p [port] specify listening port (defaults to 8080)\n");
return !v;
2013-05-10 23:58:05 +00:00
}
2016-08-06 23:43:16 +00:00
static int showversion() {
printf (R2_VERSION"\n");
return 0;
}
2013-05-10 23:58:05 +00:00
int main(int argc, char **argv) {
RSocket *s;
RSocketHTTPRequest *rs;
int c, timeout = 3;
int dodaemon = 0;
int dosandbox = 0;
2016-04-25 21:09:39 +00:00
bool listenlocal = true;
2013-05-10 23:58:05 +00:00
const char *port = "8080";
2016-08-06 23:43:16 +00:00
while ((c = getopt (argc, argv, "adhp:sv")) != -1) {
2013-10-04 11:57:49 +00:00
switch (c) {
case 'a':
2016-04-25 21:09:39 +00:00
listenlocal = false;
2013-10-04 11:57:49 +00:00
break;
case 's':
dosandbox = 1;
break;
2013-05-10 23:58:05 +00:00
case 'd':
dodaemon = 1;
break;
case 'h':
return usage (1);
2016-08-06 23:43:16 +00:00
case 'v':
return showversion ();
2013-05-10 23:58:05 +00:00
case 'p':
port = optarg;
break;
default:
return usage (0);
2013-05-10 23:58:05 +00:00
}
}
2013-10-04 11:57:49 +00:00
if (optind != argc)
return usage (0);
2013-05-10 23:58:05 +00:00
if (dodaemon) {
#if LIBC_HAVE_FORK
2013-05-10 23:58:05 +00:00
int pid = fork ();
#else
int pid = -1;
#endif
if (pid > 0) {
2013-05-10 23:58:05 +00:00
printf ("%d\n", pid);
return 0;
}
}
2016-07-12 20:15:19 +00:00
s = r_socket_new (false);
2013-10-04 11:57:49 +00:00
s->local = listenlocal;
2013-05-10 23:58:05 +00:00
if (!r_socket_listen (s, port, NULL)) {
2013-10-04 11:57:49 +00:00
eprintf ("Cannot listen on %d\n", s->port);
r_socket_free (s);
2013-05-10 23:58:05 +00:00
return 1;
}
2013-10-04 11:57:49 +00:00
eprintf ("http://localhost:%d/\n", s->port);
2016-07-12 20:15:19 +00:00
if (dosandbox && !r_sandbox_enable (true)) {
2014-06-22 12:25:54 +00:00
eprintf ("sandbox: Cannot be enabled.\n");
return 1;
}
2013-05-10 23:58:05 +00:00
while (!r_cons_singleton ()->breaked) {
char *result_heap = NULL;
const char *result = page_index;
rs = r_socket_http_accept (s, timeout);
if (!rs) continue;
if (!strcmp (rs->method, "GET")) {
if (!strncmp (rs->path, "/proc/kill/", 11)) {
2013-05-10 23:58:05 +00:00
// TODO: show page here?
int pid = atoi (rs->path + 11);
if (pid > 0) kill (pid, 9);
2013-05-10 23:58:05 +00:00
} else
if (!strncmp (rs->path, "/file/open/", 11)) {
2013-05-10 23:58:05 +00:00
int pid;
int session_port = 3000 + r_num_rand (1024);
char *filename = rs->path + 11;
2013-05-10 23:58:05 +00:00
int filename_len = strlen (filename);
char *cmd;
if (!(cmd = malloc (filename_len + 40))) {
perror ("malloc");
return 1;
}
2016-04-25 21:09:39 +00:00
sprintf (cmd, "r2 -q %s-e http.port=%d -c=h \"%s\"",
listenlocal? "": "-e http.bind=public ",
2013-05-10 23:58:05 +00:00
session_port, filename);
// TODO: use r_sys api to get pid when running in bg
pid = r_sys_cmdbg (cmd);
free (cmd);
result = result_heap = malloc (1024 + filename_len);
if (!result) {
perror ("malloc");
return 1;
}
2013-05-10 23:58:05 +00:00
sprintf (result_heap,
"<html><body>"
"<a href='/'>back</a><hr size=1/>"
" - <a target='_blank' href='http://localhost:%d/'>open</a><br />"
" - <a href='/proc/kill/%d'>kill</a><br />"
"</body></html>", session_port, pid);
eprintf ("\nchild pid %d\n\n", pid);
}
}
r_socket_http_response (rs, 200, result, 0, NULL);
r_socket_http_close (rs);
free (result_heap);
2016-04-25 21:09:39 +00:00
result_heap = NULL;
2013-05-10 23:58:05 +00:00
}
r_socket_free (s);
return 0;
}
#endif