2015-01-23 00:18:13 +00:00
|
|
|
/* radare - Copyright 2009-2015 - pancake, nibble */
|
2010-08-24 09:58:09 +00:00
|
|
|
|
|
|
|
#include "r_core.h"
|
|
|
|
#include "r_socket.h"
|
|
|
|
|
2010-09-08 17:49:34 +00:00
|
|
|
#define endian core->assembler->big_endian
|
2010-08-24 09:58:09 +00:00
|
|
|
#define rtr_n core->rtr_n
|
|
|
|
#define rtr_host core->rtr_host
|
|
|
|
|
2012-09-06 01:12:54 +00:00
|
|
|
static RSocket *s = NULL;
|
2012-11-05 01:00:34 +00:00
|
|
|
static const char *listenport = NULL;
|
2012-09-06 01:12:54 +00:00
|
|
|
|
2014-08-28 02:41:33 +00:00
|
|
|
typedef struct {
|
|
|
|
const char *host;
|
|
|
|
const char *port;
|
|
|
|
const char *file;
|
|
|
|
} TextLog;
|
2014-09-02 00:20:03 +00:00
|
|
|
static int rtr_textlog_chat (RCore *core, TextLog T);
|
2014-08-28 02:41:33 +00:00
|
|
|
|
|
|
|
static char *rtrcmd (TextLog T, const char *str) {
|
|
|
|
int len;
|
|
|
|
char uri[1024];
|
|
|
|
char *res, *ptr2, *ptr = r_str_uri_encode (str);
|
|
|
|
if (ptr) str = ptr;
|
|
|
|
snprintf (uri, sizeof (uri), "http://%s:%s/%s%s",
|
|
|
|
T.host, T.port, T.file, str);
|
|
|
|
if (ptr == str) free (ptr);
|
|
|
|
ptr2 = r_socket_http_get (uri, NULL, &len);
|
|
|
|
if (ptr2) {
|
|
|
|
ptr2[len] = 0;
|
|
|
|
res = strstr (ptr2, "\n\n");
|
|
|
|
if (res) res = strstr (res+1, "\n\n");
|
|
|
|
if (res) res += 2; else res = ptr2;
|
|
|
|
return ptr2;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-09-02 00:20:03 +00:00
|
|
|
static void showcursor(RCore *core, int x) {
|
|
|
|
if (core && core->vmode) {
|
|
|
|
r_cons_show_cursor (x);
|
2014-09-03 21:55:09 +00:00
|
|
|
if (x) {
|
|
|
|
// TODO: cache this
|
|
|
|
int wheel = r_config_get_i (core->config, "scr.wheel");
|
|
|
|
if (wheel)
|
2015-09-14 10:35:38 +00:00
|
|
|
r_cons_enable_mouse (true);
|
2014-09-03 21:55:09 +00:00
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
r_cons_enable_mouse (false);
|
2014-09-03 21:55:09 +00:00
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
} else r_cons_enable_mouse (false);
|
2014-09-02 00:20:03 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
|
2014-08-28 09:38:11 +00:00
|
|
|
static int rtr_visual (RCore *core, TextLog T, const char *cmd) {
|
2015-09-14 10:35:38 +00:00
|
|
|
int autorefresh = false;
|
2014-08-28 09:38:11 +00:00
|
|
|
if (cmd) {
|
|
|
|
r_cons_break (NULL, NULL);
|
|
|
|
for (;;) {
|
2014-10-06 23:58:42 +00:00
|
|
|
char *ret;
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_clear00 ();
|
2014-10-06 23:58:42 +00:00
|
|
|
ret = rtrcmd (T, cmd);
|
|
|
|
r_cons_printf ("%s\n", ret);
|
|
|
|
free (ret);
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_flush ();
|
|
|
|
if (r_cons_singleton ()->breaked)
|
|
|
|
break;
|
|
|
|
r_sys_sleep (1);
|
|
|
|
}
|
|
|
|
r_cons_break_end ();
|
|
|
|
} else {
|
2015-11-23 15:31:31 +00:00
|
|
|
const char *cmds[] = { "px", "pd", "pxa", "dr", "sr SP;pxa", NULL };
|
2014-08-28 09:38:11 +00:00
|
|
|
int cmdidx = 0;
|
|
|
|
char *ret, ch;
|
2014-10-06 22:56:39 +00:00
|
|
|
free (rtrcmd (T, "e scr.color=true"));
|
|
|
|
free (rtrcmd (T, "e scr.html=false"));
|
2014-08-28 09:38:11 +00:00
|
|
|
for (;;) {
|
|
|
|
r_cons_clear00 ();
|
|
|
|
ret = rtrcmd (T, cmds[cmdidx]);
|
|
|
|
if (ret) {
|
|
|
|
r_cons_printf ("%s\n", ret);
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
r_cons_flush ();
|
2014-09-02 00:20:03 +00:00
|
|
|
if (autorefresh) {
|
|
|
|
r_cons_printf ("(auto-refresh)\n");
|
|
|
|
r_cons_flush ();
|
|
|
|
r_cons_break (NULL, NULL);
|
|
|
|
r_sys_sleep (1);
|
|
|
|
if (r_cons_singleton()->breaked) {
|
2015-09-14 10:35:38 +00:00
|
|
|
autorefresh = false;
|
2014-09-02 00:20:03 +00:00
|
|
|
ch = r_cons_readchar ();
|
|
|
|
} else {
|
|
|
|
r_cons_break_end ();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r_cons_break_end ();
|
|
|
|
} else {
|
|
|
|
ch = r_cons_readchar ();
|
|
|
|
}
|
2014-08-28 09:38:11 +00:00
|
|
|
#if 0
|
2015-04-11 09:45:10 +00:00
|
|
|
TODO:
|
2014-08-28 09:38:11 +00:00
|
|
|
i insert hex/string/asm
|
|
|
|
0-9 follow jumps
|
|
|
|
#endif
|
|
|
|
switch (ch) {
|
2015-04-11 09:45:10 +00:00
|
|
|
case '?':
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_clear00();
|
2014-09-02 21:53:36 +00:00
|
|
|
r_cons_printf ("Remote Visual keys:\n"
|
|
|
|
" hjkl : move\n"
|
2014-10-06 22:56:39 +00:00
|
|
|
" HJKL : move faster\n"
|
2014-09-02 21:53:36 +00:00
|
|
|
" +-*/ : change block size\n"
|
|
|
|
" pP : rotate print modes\n"
|
|
|
|
" T : enter TextLog chat console\n"
|
|
|
|
" @ : enter auto-refresh mode\n"
|
2014-09-02 22:18:33 +00:00
|
|
|
" i : insert hexpair\n"
|
2014-09-02 21:53:36 +00:00
|
|
|
" q : quit this mode and go back to the shell\n"
|
2014-09-02 22:18:33 +00:00
|
|
|
" sS : step / step over\n"
|
2014-09-02 21:53:36 +00:00
|
|
|
" . : seek entry or pc\n");
|
2014-08-28 09:38:11 +00:00
|
|
|
r_cons_flush ();
|
2015-03-05 22:33:28 +00:00
|
|
|
r_cons_any_key (NULL);
|
2014-08-28 09:38:11 +00:00
|
|
|
break;
|
2014-09-02 22:18:33 +00:00
|
|
|
case 'i':
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
#if __UNIX__
|
|
|
|
if (core->print->flags & R_PRINT_FLAGS_COLOR) {
|
|
|
|
#else
|
|
|
|
if (0) {
|
|
|
|
#endif
|
|
|
|
r_line_set_prompt (Color_RESET":> ");
|
|
|
|
} else {
|
|
|
|
r_line_set_prompt (":> ");
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
showcursor (core, true);
|
2014-09-02 22:18:33 +00:00
|
|
|
r_cons_fgets (buf+3, sizeof (buf)-4, 0, NULL);
|
|
|
|
memcpy (buf, "wx ", 3);
|
|
|
|
if (buf[3]) {
|
|
|
|
char *res = rtrcmd (T, buf);
|
|
|
|
if (res) {
|
|
|
|
r_cons_printf ("%s\n", res);
|
|
|
|
free (res);
|
|
|
|
}
|
|
|
|
r_cons_flush ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
free (rtrcmd (T, "ds;.dr*"));
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
free (rtrcmd (T, "dso;.dr*"));
|
|
|
|
break;
|
2014-09-02 21:53:36 +00:00
|
|
|
case '.':
|
2015-10-31 00:57:52 +00:00
|
|
|
free (rtrcmd (T, "s entry0;dr?rip;?? sr PC"));
|
2014-09-02 21:53:36 +00:00
|
|
|
break;
|
2014-09-02 00:20:03 +00:00
|
|
|
case ':':
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
eprintf ("Press <enter> to return to Visual mode.\n");
|
|
|
|
do {
|
|
|
|
char buf[1024];
|
|
|
|
#if __UNIX__
|
|
|
|
r_line_set_prompt (Color_RESET":> ");
|
|
|
|
#else
|
|
|
|
r_line_set_prompt (":> ");
|
|
|
|
#endif
|
2015-09-14 10:35:38 +00:00
|
|
|
showcursor (core, true);
|
2014-09-02 00:20:03 +00:00
|
|
|
r_cons_fgets (buf, sizeof (buf), 0, NULL);
|
|
|
|
if (*buf) {
|
|
|
|
r_line_hist_add (buf);
|
|
|
|
char *res = rtrcmd (T, buf);
|
|
|
|
if (res) {
|
|
|
|
r_cons_printf ("%s\n", res);
|
|
|
|
free (res);
|
|
|
|
}
|
|
|
|
r_cons_flush ();
|
2015-09-14 10:35:38 +00:00
|
|
|
ret = true;
|
2014-09-02 00:20:03 +00:00
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
ret = false;
|
2014-09-02 00:20:03 +00:00
|
|
|
//r_cons_any_key ();
|
|
|
|
r_cons_clear00 ();
|
2015-09-14 10:35:38 +00:00
|
|
|
showcursor (core, false);
|
2014-09-02 00:20:03 +00:00
|
|
|
}
|
|
|
|
} while (ret);
|
|
|
|
}
|
|
|
|
break;
|
2015-09-14 10:35:38 +00:00
|
|
|
case '@': autorefresh = true; break;
|
2015-04-11 09:45:10 +00:00
|
|
|
case 'j':
|
2014-10-06 22:56:39 +00:00
|
|
|
if (cmdidx==1) {
|
|
|
|
free (rtrcmd (T, "so")); break;
|
|
|
|
} else {
|
|
|
|
free (rtrcmd (T, "s+16")); break;
|
|
|
|
}
|
|
|
|
break;
|
2014-08-28 09:38:11 +00:00
|
|
|
case 'k': free (rtrcmd (T, "s-16")); break;
|
|
|
|
case 'h': free (rtrcmd (T, "s-1")); break;
|
|
|
|
case 'l': free (rtrcmd (T, "s+1")); break;
|
2014-10-06 22:56:39 +00:00
|
|
|
case 'J':
|
|
|
|
if (cmdidx==1) {
|
|
|
|
free (rtrcmd (T, "4so"));
|
|
|
|
} else {
|
|
|
|
free (rtrcmd (T, "s+32"));
|
|
|
|
} break;
|
|
|
|
case 'K': free (rtrcmd (T, "s-32")); break;
|
|
|
|
case 'H': free (rtrcmd (T, "s-2")); break;
|
|
|
|
case 'L': free (rtrcmd (T, "s+2")); break;
|
2014-09-02 00:20:03 +00:00
|
|
|
case 'T': rtr_textlog_chat (core, T); break;
|
|
|
|
case '+': free (rtrcmd (T, "b+1")); break;
|
|
|
|
case '*': free (rtrcmd (T, "b+16")); break;
|
|
|
|
case '-': free (rtrcmd (T, "b-1")); break;
|
|
|
|
case '/': free (rtrcmd (T, "b-16")); break;
|
2014-08-28 09:38:11 +00:00
|
|
|
case 'p': cmdidx++; if (!cmds[cmdidx]) cmdidx = 0; break;
|
|
|
|
case 'P': cmdidx--; if (cmdidx<0) cmdidx = 2; break;
|
|
|
|
case 'q':
|
2014-09-02 00:20:03 +00:00
|
|
|
//restore prompt
|
2014-08-28 09:38:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-28 02:41:33 +00:00
|
|
|
// TODO: rename /name to /nick or /so?
|
|
|
|
// clone of textlog_chat () using rtrcmd()
|
|
|
|
static int rtr_textlog_chat (RCore *core, TextLog T) {
|
|
|
|
char prompt[64];
|
|
|
|
char buf[1024];
|
|
|
|
int lastmsg = 0;
|
|
|
|
const char *me = r_config_get (core->config, "cfg.user");
|
|
|
|
char *ret, msg[1024];
|
|
|
|
|
2014-09-02 00:20:03 +00:00
|
|
|
eprintf ("Type '/help' for commands and ^D to quit:\n");
|
2014-08-28 02:41:33 +00:00
|
|
|
char *oldprompt = strdup (r_line_singleton ()->prompt);
|
|
|
|
snprintf (prompt, sizeof (prompt)-1, "[%s]> ", me);
|
|
|
|
r_line_set_prompt (prompt);
|
|
|
|
ret = rtrcmd (T, msg);
|
|
|
|
for (;;) {
|
2014-09-02 00:20:03 +00:00
|
|
|
if (lastmsg>=0) {
|
2014-08-28 02:41:33 +00:00
|
|
|
snprintf (msg, sizeof (msg)-1, "T %d", lastmsg);
|
|
|
|
} else {
|
|
|
|
strcpy (msg, "T");
|
|
|
|
}
|
|
|
|
ret = rtrcmd (T, msg);
|
|
|
|
r_cons_printf ("%s\n", ret);
|
|
|
|
free (ret);
|
|
|
|
ret = rtrcmd (T, "Tl");
|
|
|
|
lastmsg = atoi (ret)-1;
|
|
|
|
free (ret);
|
|
|
|
if (r_cons_fgets (buf, sizeof (buf)-1, 0, NULL)<0)
|
|
|
|
goto beach;
|
|
|
|
if (!*buf) continue;
|
|
|
|
if (!strcmp (buf, "/help")) {
|
|
|
|
eprintf ("/quit quit the chat (same as ^D)\n");
|
2014-09-02 00:20:03 +00:00
|
|
|
eprintf ("/nick <nick> set cfg.user nick name\n");
|
2014-08-28 02:41:33 +00:00
|
|
|
eprintf ("/log show full log\n");
|
|
|
|
eprintf ("/clear clear text log messages\n");
|
2014-09-02 00:20:03 +00:00
|
|
|
} else if (!strncmp (buf, "/nick ", 6)) {
|
2014-08-28 02:41:33 +00:00
|
|
|
snprintf (msg, sizeof (msg)-1, "* '%s' is now known as '%s'", me, buf+6);
|
2014-09-02 00:20:03 +00:00
|
|
|
r_cons_printf ("%s\n", msg);
|
2014-08-28 02:41:33 +00:00
|
|
|
r_core_log_add (core, msg);
|
|
|
|
r_config_set (core->config, "cfg.user", buf+6);
|
|
|
|
me = r_config_get (core->config, "cfg.user");
|
|
|
|
snprintf (prompt, sizeof (prompt)-1, "[%s]> ", me);
|
|
|
|
r_line_set_prompt (prompt);
|
|
|
|
} else if (!strcmp (buf, "/log")) {
|
|
|
|
char *ret = rtrcmd (T, "T");
|
|
|
|
if (ret) {
|
|
|
|
r_cons_printf ("%s\n", ret);
|
|
|
|
free (ret);
|
|
|
|
}
|
|
|
|
} else if (!strcmp (buf, "/clear")) {
|
|
|
|
//r_core_log_del (core, 0);
|
|
|
|
free (rtrcmd (T, "T-"));
|
|
|
|
} else if (!strcmp (buf, "/quit")) {
|
|
|
|
goto beach;
|
|
|
|
} else if (*buf=='/') {
|
|
|
|
eprintf ("Unknown command: %s\n", buf);
|
|
|
|
} else {
|
|
|
|
snprintf (msg, sizeof (msg)-1, "T [%s] %s", me, buf);
|
|
|
|
free (rtrcmd (T, msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
beach:
|
|
|
|
r_line_set_prompt (oldprompt);
|
|
|
|
free (oldprompt);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-04-11 00:22:41 +00:00
|
|
|
#define http_break r_core_rtr_http_stop
|
|
|
|
R_API int r_core_rtr_http_stop(RCore *u) {
|
2012-10-25 19:40:11 +00:00
|
|
|
RSocket* sock;
|
|
|
|
const char *port;
|
|
|
|
const int timeout = 1; // 1 second
|
2012-09-06 01:12:54 +00:00
|
|
|
RCore *core = (RCore*)u;
|
2015-06-03 22:13:13 +00:00
|
|
|
#if __WINDOWS__
|
2015-09-14 17:21:57 +00:00
|
|
|
r_socket_http_server_set_breaked (&r_cons_singleton()->breaked);
|
2015-06-03 22:13:13 +00:00
|
|
|
#endif
|
2012-10-25 19:40:11 +00:00
|
|
|
if (((size_t)u)>0xff) {
|
2014-04-16 04:26:47 +00:00
|
|
|
port = listenport? listenport: r_config_get (
|
|
|
|
core->config, "http.port");
|
2012-10-25 19:40:11 +00:00
|
|
|
sock = r_socket_new (0);
|
2015-04-11 09:45:10 +00:00
|
|
|
(void)r_socket_connect (sock, "localhost",
|
2015-03-23 23:47:10 +00:00
|
|
|
//r_config_get (core->config, "http.bind"),
|
2014-04-16 04:26:47 +00:00
|
|
|
port, R_SOCKET_PROTO_TCP, timeout);
|
2012-11-05 01:00:34 +00:00
|
|
|
r_socket_free (sock);
|
2012-10-25 19:40:11 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
r_socket_free (s);
|
2012-09-06 10:47:32 +00:00
|
|
|
s = NULL;
|
2014-04-11 00:22:41 +00:00
|
|
|
return 0;
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2012-09-06 23:48:09 +00:00
|
|
|
SECURITY IMPLICATIONS
|
2012-09-06 01:12:54 +00:00
|
|
|
- no ssl
|
|
|
|
- no auth
|
|
|
|
- commands can be executed by anyone
|
|
|
|
- default is to listen on localhost
|
|
|
|
- can access full filesystem
|
|
|
|
- follow symlinks
|
|
|
|
#endif
|
|
|
|
|
2014-06-22 12:57:54 +00:00
|
|
|
static char *rtr_dir_files (const char *path) {
|
|
|
|
char *ptr = strdup ("<html><body>\n");
|
|
|
|
const char *file;
|
|
|
|
RListIter *iter;
|
|
|
|
// list files
|
|
|
|
RList *files = r_sys_dir (path);
|
|
|
|
eprintf ("Listing directory %s\n", path);
|
|
|
|
r_list_foreach (files, iter, file) {
|
|
|
|
if (file[0] == '.') continue;
|
|
|
|
ptr = r_str_concatf (ptr, "<a href=\"%s%s\">%s</a><br />\n",
|
|
|
|
path, file, file);
|
|
|
|
}
|
|
|
|
r_list_free (files);
|
|
|
|
return r_str_concat (ptr, "</body></html>\n");
|
|
|
|
}
|
|
|
|
|
2014-08-28 10:08:46 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RCore *core;
|
|
|
|
int launch;
|
|
|
|
const char *path;
|
|
|
|
} HttpThread;
|
|
|
|
|
2015-01-12 01:52:12 +00:00
|
|
|
#if __UNIX__
|
2014-11-26 15:59:58 +00:00
|
|
|
static void dietime (int sig) {
|
|
|
|
eprintf ("It's Die Time!\n");
|
2015-01-12 01:52:12 +00:00
|
|
|
exit (0);
|
2014-11-26 15:59:58 +00:00
|
|
|
}
|
2015-01-12 01:52:12 +00:00
|
|
|
#endif
|
2014-11-26 15:59:58 +00:00
|
|
|
|
2014-11-26 21:19:55 +00:00
|
|
|
static void activateDieTime (RCore *core) {
|
|
|
|
int dt = r_config_get_i (core->config, "http.dietime");
|
|
|
|
if (dt>0) {
|
|
|
|
#if __UNIX__
|
|
|
|
signal (SIGALRM, dietime);
|
|
|
|
alarm (dt);
|
|
|
|
#else
|
|
|
|
eprintf ("http.dietime only works on *nix systems\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 22:56:39 +00:00
|
|
|
// return 1 on error
|
2014-08-28 10:08:46 +00:00
|
|
|
static int r_core_rtr_http_run (RCore *core, int launch, const char *path) {
|
2013-01-25 02:47:09 +00:00
|
|
|
char buf[32];
|
2014-09-01 22:56:28 +00:00
|
|
|
RSocket *s;
|
2012-09-07 10:49:36 +00:00
|
|
|
RSocketHTTPRequest *rs;
|
2014-09-01 22:56:28 +00:00
|
|
|
RConfig *newcfg = NULL, *origcfg = NULL;
|
2014-09-02 21:53:36 +00:00
|
|
|
int iport, timeout = r_config_get_i (core->config, "http.timeout");
|
2015-03-01 21:07:28 +00:00
|
|
|
const char *host = r_config_get (core->config, "http.bind");
|
2016-02-10 23:19:05 +00:00
|
|
|
const char *root = r_config_get (core->config, "http.root");
|
2012-09-06 01:12:54 +00:00
|
|
|
const char *port = r_config_get (core->config, "http.port");
|
2015-01-13 01:22:20 +00:00
|
|
|
const char *allow = r_config_get (core->config, "http.allow");
|
|
|
|
const char *httpui = r_config_get (core->config, "http.ui");
|
2015-01-24 02:08:03 +00:00
|
|
|
char *dir;
|
2015-04-13 00:02:07 +00:00
|
|
|
int ret = 0;
|
2015-04-11 22:06:38 +00:00
|
|
|
char headers[128] = {0};
|
2014-08-29 16:45:14 +00:00
|
|
|
|
2016-02-10 23:19:05 +00:00
|
|
|
if (!r_file_is_directory (root)) {
|
|
|
|
eprintf ("Cannot find http.root '%s'\n", root);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-29 16:45:14 +00:00
|
|
|
if (path && atoi (path)) {
|
|
|
|
port = path;
|
|
|
|
path = NULL;
|
2015-01-13 01:22:20 +00:00
|
|
|
} else {
|
2015-05-27 22:59:37 +00:00
|
|
|
if (core->file) {
|
|
|
|
if (!strcmp (httpui, "p")
|
2015-08-13 21:58:01 +00:00
|
|
|
|| !strcmp (httpui, "m")
|
|
|
|
|| !strcmp (httpui, "enyo")
|
|
|
|
|| !strcmp (httpui, "t")) {
|
2015-05-27 22:59:37 +00:00
|
|
|
path = httpui;
|
|
|
|
}
|
2015-01-13 01:22:20 +00:00
|
|
|
}
|
2014-08-29 16:45:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 02:47:09 +00:00
|
|
|
if (!strcmp (port, "0")) {
|
|
|
|
r_num_irand ();
|
|
|
|
iport = 1024+r_num_rand (45256);
|
|
|
|
snprintf (buf, sizeof (buf), "%d", iport);
|
|
|
|
port = buf;
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
s = r_socket_new (false);
|
2015-03-23 23:47:10 +00:00
|
|
|
{
|
2015-04-13 00:02:07 +00:00
|
|
|
if (host && *host) {
|
|
|
|
if (!strcmp (host, "::1")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (!strcmp (host, "localhost")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (!strcmp (host, "127.0.0.1")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (!strcmp (host, "local")) {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-03-23 23:47:10 +00:00
|
|
|
r_config_set (core->config, "http.bind", "localhost");
|
2015-04-13 00:02:07 +00:00
|
|
|
} else if (host[0]=='0' || !strcmp (host, "public")) {
|
2015-03-23 23:47:10 +00:00
|
|
|
// public
|
|
|
|
r_config_set (core->config, "http.bind", "0.0.0.0");
|
2015-04-17 22:36:24 +00:00
|
|
|
host = "127.0.0.1";
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = false;
|
2015-03-23 23:47:10 +00:00
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-03-23 23:47:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-09-14 10:35:38 +00:00
|
|
|
s->local = true;
|
2015-03-23 23:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
if (!r_socket_listen (s, port, NULL)) {
|
2015-01-24 14:26:17 +00:00
|
|
|
r_socket_free (s);
|
2012-09-06 01:12:54 +00:00
|
|
|
eprintf ("Cannot listen on http.port\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-01 22:01:56 +00:00
|
|
|
if (launch=='H') {
|
2012-09-06 01:12:54 +00:00
|
|
|
const char *browser = r_config_get (core->config, "http.browser");
|
2015-12-24 14:13:17 +00:00
|
|
|
r_sys_cmdf ("%s http://%s:%d/%s &",
|
2015-03-01 21:07:28 +00:00
|
|
|
browser, host, atoi (port), path? path:"");
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2014-09-01 22:56:28 +00:00
|
|
|
|
|
|
|
origcfg = core->config;
|
|
|
|
newcfg = r_config_clone (core->config);
|
|
|
|
core->config = newcfg;
|
|
|
|
|
2012-10-01 07:32:04 +00:00
|
|
|
r_config_set (core->config, "asm.cmtright", "false");
|
2012-09-06 01:12:54 +00:00
|
|
|
r_config_set (core->config, "scr.html", "true");
|
|
|
|
r_config_set (core->config, "scr.color", "false");
|
2012-09-06 23:48:09 +00:00
|
|
|
r_config_set (core->config, "asm.bytes", "false");
|
2012-09-25 12:06:09 +00:00
|
|
|
r_config_set (core->config, "scr.interactive", "false");
|
2012-12-03 03:01:55 +00:00
|
|
|
if (r_config_get_i (core->config, "http.sandbox")) {
|
2014-09-02 21:53:36 +00:00
|
|
|
//(void)r_config_get_i (core->config, "cfg.sandbox");
|
2012-12-03 03:01:55 +00:00
|
|
|
r_config_set (core->config, "cfg.sandbox", "true");
|
|
|
|
}
|
2012-09-06 23:48:09 +00:00
|
|
|
eprintf ("Starting http server...\n");
|
2015-03-01 21:07:28 +00:00
|
|
|
eprintf ("open http://%s:%d/\n", host, atoi (port));
|
|
|
|
eprintf ("r2 -C http://%s:%d/cmd/\n", host, atoi (port));
|
2015-09-14 10:35:38 +00:00
|
|
|
core->http_up = true;
|
2014-09-01 22:56:28 +00:00
|
|
|
|
2014-09-02 21:53:36 +00:00
|
|
|
ut64 newoff, origoff = core->offset;
|
2014-11-26 21:19:55 +00:00
|
|
|
int newblksz, origblksz = core->blocksize;
|
2014-09-02 21:53:36 +00:00
|
|
|
ut8 *newblk, *origblk = core->block;
|
|
|
|
|
|
|
|
newblk = malloc (core->blocksize);
|
|
|
|
memcpy (newblk, core->block, core->blocksize);
|
|
|
|
|
|
|
|
core->block = newblk;
|
2014-09-01 22:56:28 +00:00
|
|
|
// TODO: handle mutex lock/unlock here
|
2012-09-06 01:12:54 +00:00
|
|
|
while (!r_cons_singleton ()->breaked) {
|
2014-06-22 12:57:54 +00:00
|
|
|
r_cons_break ((RConsBreak)http_break, core);
|
2014-09-01 22:56:28 +00:00
|
|
|
|
2014-09-02 21:53:36 +00:00
|
|
|
/* restore environment */
|
2014-09-01 22:56:28 +00:00
|
|
|
core->config = origcfg;
|
|
|
|
r_config_set (origcfg, "scr.html", r_config_get (origcfg, "scr.html"));
|
|
|
|
r_config_set (origcfg, "scr.color", r_config_get (origcfg, "scr.color"));
|
|
|
|
r_config_set (origcfg, "scr.interactive", r_config_get (origcfg, "scr.interactive"));
|
2014-09-02 21:53:36 +00:00
|
|
|
core->http_up = 0; // DAT IS NOT TRUE AT ALL.. but its the way to enable visual
|
|
|
|
|
|
|
|
newoff = core->offset;
|
|
|
|
newblk = core->block;
|
|
|
|
newblksz = core->blocksize;
|
|
|
|
|
|
|
|
core->offset = origoff;
|
|
|
|
core->block = origblk;
|
|
|
|
core->blocksize = origblksz;
|
|
|
|
|
|
|
|
// backup and restore offset and blocksize
|
2015-04-11 09:45:10 +00:00
|
|
|
|
2014-09-02 21:53:36 +00:00
|
|
|
/* this is blocking */
|
2014-11-26 21:19:55 +00:00
|
|
|
activateDieTime (core);
|
2012-12-03 03:01:55 +00:00
|
|
|
rs = r_socket_http_accept (s, timeout);
|
2014-09-02 21:53:36 +00:00
|
|
|
|
|
|
|
origoff = core->offset;
|
|
|
|
origblk = core->block;
|
|
|
|
origblksz = core->blocksize;
|
|
|
|
core->offset = newoff;
|
|
|
|
core->block = newblk;
|
|
|
|
core->blocksize = newblksz;
|
|
|
|
/* set environment */
|
|
|
|
// backup and restore offset and blocksize
|
|
|
|
core->http_up = 1;
|
2014-09-01 22:56:28 +00:00
|
|
|
core->config = newcfg;
|
|
|
|
r_config_set (newcfg, "scr.html", r_config_get (newcfg, "scr.html"));
|
|
|
|
r_config_set (newcfg, "scr.color", r_config_get (newcfg, "scr.color"));
|
|
|
|
r_config_set (newcfg, "scr.interactive", r_config_get (newcfg, "scr.interactive"));
|
|
|
|
|
2012-09-06 23:48:09 +00:00
|
|
|
if (!rs) {
|
2013-02-25 00:47:55 +00:00
|
|
|
r_sys_usleep (100);
|
2012-09-06 23:48:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-02-25 00:47:55 +00:00
|
|
|
if (allow && *allow) {
|
2015-09-14 10:35:38 +00:00
|
|
|
int accepted = false;
|
2015-03-01 21:07:28 +00:00
|
|
|
const char *allows_host;
|
2013-02-25 00:47:55 +00:00
|
|
|
char *p, *peer = r_socket_to_string (rs->s);
|
|
|
|
char *allows = strdup (allow);
|
|
|
|
//eprintf ("Firewall (%s)\n", allows);
|
|
|
|
int i, count = r_str_split (allows, ',');
|
|
|
|
p = strchr (peer, ':');
|
|
|
|
if (p) *p = 0;
|
|
|
|
for (i=0; i<count; i++) {
|
2015-03-01 21:07:28 +00:00
|
|
|
allows_host = r_str_word_get0 (allows, i);
|
2013-02-25 00:47:55 +00:00
|
|
|
//eprintf ("--- (%s) (%s)\n", host, peer);
|
2015-03-01 21:07:28 +00:00
|
|
|
if (!strcmp (allows_host, peer)) {
|
2015-09-14 10:35:38 +00:00
|
|
|
accepted = true;
|
2013-02-25 00:47:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (peer);
|
|
|
|
free (allows);
|
|
|
|
if (!accepted) {
|
|
|
|
r_socket_http_close (rs);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-12-03 03:36:00 +00:00
|
|
|
if (!rs->method || !rs->path) {
|
2014-04-21 10:55:34 +00:00
|
|
|
eprintf ("Invalid http headers received from client\n");
|
2012-12-03 03:36:00 +00:00
|
|
|
r_socket_http_close (rs);
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-24 02:08:03 +00:00
|
|
|
dir = NULL;
|
2014-06-22 12:57:54 +00:00
|
|
|
|
2015-01-24 02:08:03 +00:00
|
|
|
if (r_config_get_i (core->config, "http.verbose")) {
|
|
|
|
char *peer = r_socket_to_string (rs->s);
|
|
|
|
eprintf ("[HTTP] %s %s\n", peer, rs->path);
|
|
|
|
free (peer);
|
|
|
|
}
|
2014-06-22 12:57:54 +00:00
|
|
|
if (r_config_get_i (core->config, "http.dirlist"))
|
|
|
|
if (r_file_is_directory (rs->path))
|
|
|
|
dir = strdup (rs->path);
|
2015-04-11 16:54:35 +00:00
|
|
|
|
|
|
|
if (r_config_get_i (core->config, "http.cors")) {
|
|
|
|
sprintf (headers, "Access-Control-Allow-Origin: *\nAccess-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept\n");
|
|
|
|
}
|
|
|
|
|
2015-04-11 09:45:10 +00:00
|
|
|
if (!strcmp (rs->method, "OPTIONS")) {
|
2015-07-09 17:23:59 +00:00
|
|
|
r_socket_http_response (rs, 200, "", 0, headers);
|
2015-04-11 09:45:10 +00:00
|
|
|
} else
|
2012-09-06 01:12:54 +00:00
|
|
|
if (!strcmp (rs->method, "GET")) {
|
2015-01-23 00:18:13 +00:00
|
|
|
if (!strncmp (rs->path, "/up/", 4)) {
|
2013-01-03 00:43:23 +00:00
|
|
|
if (r_config_get_i (core->config, "http.upget")) {
|
2013-01-04 09:16:41 +00:00
|
|
|
const char *uproot = r_config_get (core->config, "http.uproot");
|
|
|
|
if (!rs->path[3] || (rs->path[3]=='/'&&!rs->path[4])) {
|
2014-06-22 12:57:54 +00:00
|
|
|
char *ptr = rtr_dir_files (uproot);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 200, ptr, 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (ptr);
|
2013-01-04 09:16:41 +00:00
|
|
|
} else {
|
|
|
|
char *path = r_file_root (uproot, rs->path + 4);
|
|
|
|
if (r_file_exists (path)) {
|
2014-09-20 12:43:18 +00:00
|
|
|
int sz = 0;
|
2013-01-04 09:16:41 +00:00
|
|
|
char *f = r_file_slurp (path, &sz);
|
|
|
|
if (f) {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 200, f, sz, headers);
|
2013-01-04 09:16:41 +00:00
|
|
|
free (f);
|
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "Permission denied", 0, headers);
|
2013-01-04 09:16:41 +00:00
|
|
|
eprintf ("http: Cannot open '%s'\n", path);
|
|
|
|
}
|
|
|
|
} else {
|
2014-06-22 12:57:54 +00:00
|
|
|
if (dir) {
|
|
|
|
char *resp = rtr_dir_files (dir);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, resp, 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (resp);
|
|
|
|
} else {
|
|
|
|
eprintf ("File '%s' not found\n", path);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, "File not found\n", 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
}
|
2013-01-04 09:16:41 +00:00
|
|
|
}
|
|
|
|
free (path);
|
|
|
|
}
|
2013-01-03 00:43:23 +00:00
|
|
|
} else {
|
|
|
|
r_socket_http_response (rs, 403,
|
2013-01-04 09:16:41 +00:00
|
|
|
"Permission denied\n", 0, NULL);
|
2013-01-03 00:43:23 +00:00
|
|
|
}
|
2014-09-18 14:54:21 +00:00
|
|
|
} else if (!strncmp (rs->path, "/cmd/", 5)) {
|
2013-02-06 09:35:23 +00:00
|
|
|
char *cmd = rs->path +5;
|
|
|
|
char foo[32];
|
|
|
|
const char *httpcmd = r_config_get (core->config, "http.uri");
|
|
|
|
while (*cmd=='/') cmd++;
|
|
|
|
if (httpcmd && *httpcmd) {
|
|
|
|
int len;
|
|
|
|
char *res;
|
|
|
|
// do remote http query and proxy response
|
|
|
|
snprintf (foo, sizeof (foo), "%s/%s", httpcmd, cmd);
|
|
|
|
res = r_socket_http_get (foo, NULL, &len);
|
|
|
|
if (res) {
|
|
|
|
res[len]=0;
|
|
|
|
r_cons_printf ("%s\n", res);
|
|
|
|
}
|
|
|
|
} else {
|
2013-01-04 09:16:41 +00:00
|
|
|
char *out, *cmd = rs->path+5;
|
2013-02-06 09:35:23 +00:00
|
|
|
r_str_uri_decode (cmd);
|
2015-01-23 00:18:13 +00:00
|
|
|
r_config_set (core->config, "scr.interactive", "false");
|
2015-04-13 00:02:07 +00:00
|
|
|
if (!strcmp (cmd, "=h*")) {
|
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
out = NULL;
|
|
|
|
} else {
|
|
|
|
/* do stuff */
|
|
|
|
out = NULL;
|
|
|
|
r_socket_http_close (rs);
|
|
|
|
free (dir);
|
|
|
|
ret = -2;
|
|
|
|
goto the_end;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 01:28:33 +00:00
|
|
|
if (*cmd == ':') {
|
|
|
|
/* commands in /cmd/: starting with : do not show any output */
|
|
|
|
r_core_cmd0 (core, cmd+1);
|
|
|
|
out = NULL;
|
|
|
|
} else {
|
|
|
|
// eprintf ("CMD (%s)\n", cmd);
|
|
|
|
out = r_core_cmd_str_pipe (core, cmd);
|
|
|
|
}
|
2013-02-11 09:51:45 +00:00
|
|
|
// eprintf ("\nOUT LEN = %d\n", strlen (out));
|
2013-02-06 09:35:23 +00:00
|
|
|
if (out) {
|
|
|
|
char *res = r_str_uri_encode (out);
|
2015-04-11 16:54:35 +00:00
|
|
|
char newheaders [128];
|
|
|
|
snprintf(newheaders, sizeof(newheaders),
|
|
|
|
"Content-Type: text/plain\n%s", headers);
|
|
|
|
r_socket_http_response (rs, 200, out, 0, newheaders);
|
2013-02-06 09:35:23 +00:00
|
|
|
free (out);
|
|
|
|
free (res);
|
2015-04-11 16:54:35 +00:00
|
|
|
} else r_socket_http_response (rs, 200, "", 0, headers);
|
2013-02-06 09:35:23 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
} else {
|
|
|
|
const char *root = r_config_get (core->config, "http.root");
|
2014-06-22 12:57:54 +00:00
|
|
|
char *path = r_file_root (root, rs->path);
|
2014-04-21 10:55:34 +00:00
|
|
|
// FD IS OK HERE
|
2012-12-01 01:35:12 +00:00
|
|
|
if (rs->path [strlen (rs->path)-1] == '/') {
|
2013-01-04 09:16:41 +00:00
|
|
|
path = r_str_concat (path, "index.html");
|
|
|
|
//rs->path = r_str_concat (rs->path, "index.html");
|
2012-12-01 01:35:12 +00:00
|
|
|
} else {
|
2013-01-04 09:16:41 +00:00
|
|
|
//snprintf (path, sizeof (path), "%s/%s", root, rs->path);
|
2012-12-01 01:35:12 +00:00
|
|
|
if (r_file_is_directory (path)) {
|
2013-01-04 09:16:41 +00:00
|
|
|
char res[128];
|
|
|
|
snprintf (res, sizeof (res),
|
2015-04-11 16:54:35 +00:00
|
|
|
"Location: %s/\n%s", rs->path, headers);
|
2013-01-03 00:43:23 +00:00
|
|
|
r_socket_http_response (rs, 302,
|
2013-01-04 09:16:41 +00:00
|
|
|
NULL, 0, res);
|
2012-12-01 01:35:12 +00:00
|
|
|
r_socket_http_close (rs);
|
2013-01-04 09:16:41 +00:00
|
|
|
free (path);
|
2014-06-23 23:01:27 +00:00
|
|
|
free (dir);
|
|
|
|
dir = NULL;
|
2012-12-01 01:35:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-09-06 06:59:13 +00:00
|
|
|
if (r_file_exists (path)) {
|
2014-09-20 12:43:18 +00:00
|
|
|
int sz = 0;
|
2012-09-06 01:12:54 +00:00
|
|
|
char *f = r_file_slurp (path, &sz);
|
|
|
|
if (f) {
|
2013-01-24 02:48:24 +00:00
|
|
|
const char *contenttype = NULL;
|
2015-04-11 16:54:35 +00:00
|
|
|
char newheaders [128];
|
2013-01-24 02:48:24 +00:00
|
|
|
if (strstr (path, ".js")) contenttype = "Content-Type: application/javascript\n";
|
|
|
|
if (strstr (path, ".css")) contenttype = "Content-Type: text/css\n";
|
|
|
|
if (strstr (path, ".html")) contenttype = "Content-Type: text/html\n";
|
2015-04-11 16:54:35 +00:00
|
|
|
snprintf(newheaders, sizeof(newheaders),
|
|
|
|
"%s%s", contenttype, headers);
|
|
|
|
r_socket_http_response (rs, 200, f, sz, newheaders);
|
2012-09-06 01:12:54 +00:00
|
|
|
free (f);
|
2012-12-03 03:01:55 +00:00
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "Permission denied", 0, headers);
|
2012-12-03 03:01:55 +00:00
|
|
|
eprintf ("http: Cannot open '%s'\n", path);
|
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
} else {
|
2014-06-22 12:57:54 +00:00
|
|
|
if (dir) {
|
|
|
|
char *resp = rtr_dir_files (dir);
|
|
|
|
eprintf ("Dirlisting %s\n", dir);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, resp, 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (resp);
|
|
|
|
} else {
|
|
|
|
eprintf ("File '%s' not found\n", path);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, "File not found\n", 0, headers);
|
2014-06-22 12:57:54 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2013-01-04 09:16:41 +00:00
|
|
|
free (path);
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2015-04-11 09:45:10 +00:00
|
|
|
} else
|
2012-09-06 01:12:54 +00:00
|
|
|
if (!strcmp (rs->method, "POST")) {
|
2013-01-03 00:43:23 +00:00
|
|
|
ut8 *ret;
|
2013-01-02 17:37:48 +00:00
|
|
|
int retlen;
|
2013-01-02 23:47:58 +00:00
|
|
|
char buf[128];
|
|
|
|
if (r_config_get_i (core->config, "http.upload")) {
|
2013-01-03 00:43:23 +00:00
|
|
|
ret = r_socket_http_handle_upload (
|
|
|
|
rs->data, rs->data_length, &retlen);
|
|
|
|
if (ret) {
|
|
|
|
ut64 size = r_config_get_i (core->config, "http.maxsize");
|
|
|
|
if (size && retlen > size) {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "403 File too big\n", 0, headers);
|
2013-01-03 00:43:23 +00:00
|
|
|
} else {
|
|
|
|
char *filename = r_file_root (
|
|
|
|
r_config_get (core->config, "http.uproot"),
|
|
|
|
rs->path + 4);
|
|
|
|
eprintf ("UPLOADED '%s'\n", filename);
|
2015-04-03 02:04:46 +00:00
|
|
|
r_file_dump (filename, ret, retlen, 0);
|
2013-01-03 00:43:23 +00:00
|
|
|
free (filename);
|
|
|
|
snprintf (buf, sizeof (buf),
|
|
|
|
"<html><body><h2>uploaded %d bytes. Thanks</h2>\n", retlen);
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 200, buf, 0, headers);
|
2013-01-03 00:43:23 +00:00
|
|
|
}
|
|
|
|
free (ret);
|
|
|
|
}
|
2013-01-02 23:47:58 +00:00
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 403, "403 Forbidden\n", 0, headers);
|
2013-01-02 23:47:58 +00:00
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
} else {
|
2015-04-11 16:54:35 +00:00
|
|
|
r_socket_http_response (rs, 404, "Invalid protocol", 0, headers);
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2012-12-12 03:10:44 +00:00
|
|
|
r_socket_http_close (rs);
|
2014-06-22 12:57:54 +00:00
|
|
|
free (dir);
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
2015-04-13 00:02:07 +00:00
|
|
|
the_end:
|
|
|
|
{
|
|
|
|
int timeout = r_config_get_i (core->config, "http.timeout");
|
|
|
|
const char *host = r_config_get (core->config, "http.bind");
|
|
|
|
const char *port = r_config_get (core->config, "http.port");
|
|
|
|
const char *cors = r_config_get (core->config, "http.cors");
|
|
|
|
const char *allow = r_config_get (core->config, "http.allow");
|
|
|
|
const char *httpui = r_config_get (core->config, "http.ui");
|
|
|
|
core->config = origcfg;
|
|
|
|
r_config_set_i (core->config, "http.timeout", timeout);
|
|
|
|
r_config_set (core->config, "http.bind", host);
|
|
|
|
r_config_set (core->config, "http.port", port);
|
|
|
|
r_config_set (core->config, "http.cors", cors);
|
|
|
|
r_config_set (core->config, "http.allow", allow);
|
|
|
|
r_config_set (core->config, "http.ui", httpui);
|
|
|
|
}
|
|
|
|
r_cons_break_end ();
|
2015-09-14 10:35:38 +00:00
|
|
|
core->http_up = false;
|
2012-09-06 01:12:54 +00:00
|
|
|
r_socket_free (s);
|
2014-09-01 22:56:28 +00:00
|
|
|
r_config_free (newcfg);
|
|
|
|
r_config_set (origcfg, "scr.html", r_config_get (origcfg, "scr.html"));
|
|
|
|
r_config_set (origcfg, "scr.color", r_config_get (origcfg, "scr.color"));
|
|
|
|
r_config_set (origcfg, "scr.interactive", r_config_get (origcfg, "scr.interactive"));
|
2015-04-13 00:02:07 +00:00
|
|
|
return ret;
|
2012-09-06 01:12:54 +00:00
|
|
|
}
|
|
|
|
|
2014-08-28 10:08:46 +00:00
|
|
|
static int r_core_rtr_http_thread (RThread *th) {
|
2014-10-06 22:56:39 +00:00
|
|
|
HttpThread *ht;
|
2015-09-14 10:35:38 +00:00
|
|
|
if (!th) return false;
|
2014-10-06 22:56:39 +00:00
|
|
|
ht = th->user;
|
2015-09-14 10:35:38 +00:00
|
|
|
if (!ht || !ht->core) return false;
|
2014-10-06 23:58:42 +00:00
|
|
|
return r_core_rtr_http_run (ht->core, ht->launch, ht->path);
|
2014-08-28 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static RThread *httpthread = NULL;
|
|
|
|
|
|
|
|
#define USE_THREADS 1
|
|
|
|
|
|
|
|
R_API int r_core_rtr_http(RCore *core, int launch, const char *path) {
|
2015-04-13 00:02:07 +00:00
|
|
|
int ret;
|
2014-08-28 10:08:46 +00:00
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("sandbox: connect disabled\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-01 22:01:56 +00:00
|
|
|
|
|
|
|
if (launch=='-') {
|
|
|
|
if (httpthread) {
|
|
|
|
eprintf ("Press ^C to stop the webserver\n");
|
|
|
|
r_th_free (httpthread);
|
|
|
|
httpthread = NULL;
|
|
|
|
} else {
|
|
|
|
eprintf ("No webserver running\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-08-28 10:08:46 +00:00
|
|
|
if (core->http_up) {
|
|
|
|
eprintf ("http server is already running\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-01 22:01:56 +00:00
|
|
|
if (launch=='&') {
|
|
|
|
if (httpthread) {
|
|
|
|
eprintf ("HTTP Thread is already running\n");
|
|
|
|
eprintf ("This is experimental and probably buggy. Use at your own risk\n");
|
|
|
|
eprintf ("TODO: Use different eval environ for scr. for the web\n");
|
|
|
|
eprintf ("TODO: Visual mode should be enabled on local\n");
|
|
|
|
} else {
|
|
|
|
HttpThread ht = { core, launch, path };
|
|
|
|
httpthread = r_th_new (r_core_rtr_http_thread, &ht, 0);
|
|
|
|
r_th_start (httpthread, 1);
|
|
|
|
eprintf ("Background http server started.\n");
|
|
|
|
}
|
|
|
|
return 0;
|
2014-08-28 10:08:46 +00:00
|
|
|
}
|
2015-04-13 00:02:07 +00:00
|
|
|
do {
|
|
|
|
ret = r_core_rtr_http_run (core, launch, path);
|
|
|
|
} while (ret == -2);
|
|
|
|
return ret;
|
2014-08-28 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
R_API void r_core_rtr_help(RCore *core) {
|
2014-06-20 11:25:17 +00:00
|
|
|
const char* help_msg[] = {
|
|
|
|
"Usage:", " =[:!+-=hH] [...]", " # radare remote command execution protocol",
|
|
|
|
"\nrap commands:", "", "",
|
|
|
|
"=", "", "list all open connections",
|
|
|
|
"=<", "[fd] cmd", "send output of local command to remote fd",
|
|
|
|
"=", "[fd] cmd", "exec cmd at remote 'fd' (last open is default one)",
|
|
|
|
"=!", " cmd", "run command via r_io_system",
|
|
|
|
"=+", " [proto://]host", "add host (default=rap://, tcp://, udp://)",
|
|
|
|
"=-", "[fd]", "remove all hosts or host 'fd'",
|
|
|
|
"==", "[fd]", "open remote session with host 'fd', 'q' to quit",
|
2015-07-09 21:44:45 +00:00
|
|
|
"=!=", "", "disable remote cmd mode",
|
|
|
|
"!=!", "", "enable remote cmd mode",
|
2014-08-28 01:41:23 +00:00
|
|
|
"\nrap server:","","",
|
|
|
|
"=", ":port", "listen on given port using rap protocol (o rap://9999)",
|
|
|
|
"=", ":host:port cmd", "run 'cmd' command on remote server",
|
2014-06-20 11:25:17 +00:00
|
|
|
"\nhttp server:", "", "",
|
2014-09-01 22:01:56 +00:00
|
|
|
"=h", " port", "listen for http connections (r2 -qc=H /bin/ls)",
|
|
|
|
"=h-", "", "stop background webserver",
|
2015-04-13 00:02:07 +00:00
|
|
|
"=h*", "", "restart current webserver",
|
2014-09-01 22:01:56 +00:00
|
|
|
"=h&", " port", "start http server in background)",
|
|
|
|
"=H", " port", "launch browser and listen for http",
|
|
|
|
"=H&", " port", "launch browser and listen for http in background",
|
|
|
|
NULL };
|
2014-06-20 11:25:17 +00:00
|
|
|
r_core_cmd_help (core, help_msg);
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_pushout(RCore *core, const char *input) {
|
2011-09-03 03:09:31 +00:00
|
|
|
int fd = atoi (input);
|
2010-08-24 09:58:09 +00:00
|
|
|
const char *cmd = NULL;
|
|
|
|
char *str = NULL;
|
|
|
|
if (fd) {
|
2011-03-21 00:47:17 +00:00
|
|
|
for (rtr_n = 0; rtr_host[rtr_n].fd->fd != fd \
|
2014-08-03 22:48:23 +00:00
|
|
|
&& rtr_n < RTR_MAX_HOSTS - 1; rtr_n++);
|
2010-08-24 09:58:09 +00:00
|
|
|
if (!(cmd = strchr (input, ' '))) {
|
|
|
|
eprintf ("Error\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else cmd = input;
|
|
|
|
|
2011-03-21 00:47:17 +00:00
|
|
|
if (!rtr_host[rtr_n].fd->fd) {
|
2010-08-24 09:58:09 +00:00
|
|
|
eprintf("Error: Unknown host\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(str = r_core_cmd_str (core, cmd))) {
|
|
|
|
eprintf ("Error: radare_cmd_str returned NULL\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
switch (rtr_host[rtr_n].proto) {
|
|
|
|
case RTR_PROT_RAP:
|
|
|
|
eprintf ("Error: Cannot use '=<' to a rap connection.\n");
|
|
|
|
break;
|
|
|
|
case RTR_PROT_TCP:
|
|
|
|
case RTR_PROT_UDP:
|
|
|
|
default:
|
|
|
|
r_socket_write (rtr_host[rtr_n].fd, str, strlen (str));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
free (str);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_list(RCore *core) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++)
|
|
|
|
if (rtr_host[i].fd) {
|
2011-03-21 00:47:17 +00:00
|
|
|
r_cons_printf("%i - ", rtr_host[i].fd->fd);
|
2013-02-06 09:35:23 +00:00
|
|
|
if (rtr_host[i].proto == RTR_PROT_HTTP)
|
|
|
|
r_cons_printf( "http://");
|
2010-08-24 09:58:09 +00:00
|
|
|
if (rtr_host[i].proto == RTR_PROT_TCP)
|
2013-02-06 09:35:23 +00:00
|
|
|
r_cons_printf ("tcp://");
|
2010-08-24 09:58:09 +00:00
|
|
|
else if (rtr_host[i].proto == RTR_PROT_UDP)
|
|
|
|
r_cons_printf("udp://");
|
|
|
|
else r_cons_printf("rap://");
|
|
|
|
r_cons_printf ("%s:%i/%s\n", rtr_host[i].host,
|
|
|
|
rtr_host[i].port, rtr_host[i].file);
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 22:12:03 +00:00
|
|
|
|
2010-08-24 09:58:09 +00:00
|
|
|
R_API void r_core_rtr_add(RCore *core, const char *_input) {
|
2011-03-27 15:44:27 +00:00
|
|
|
char *port, input[1024], *host = NULL, *file = NULL, *ptr = NULL, buf[1024];
|
2013-11-30 01:47:29 +00:00
|
|
|
int proto, i, timeout, ret;
|
2011-03-21 00:47:17 +00:00
|
|
|
RSocket *fd;
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2013-11-29 16:27:46 +00:00
|
|
|
timeout = r_config_get_i (core->config, "http.timeout");
|
2010-09-08 17:49:34 +00:00
|
|
|
strncpy (input, _input, sizeof (input)-4);
|
2014-11-15 20:48:13 +00:00
|
|
|
input[sizeof(input)-4] = '\0';
|
2010-08-24 09:58:09 +00:00
|
|
|
/* Parse uri */
|
2014-03-03 01:01:40 +00:00
|
|
|
if ((ptr = strstr (input, "tcp://"))) {
|
2010-08-24 09:58:09 +00:00
|
|
|
proto = RTR_PROT_TCP;
|
|
|
|
host = ptr+6;
|
2013-02-06 09:35:23 +00:00
|
|
|
} else if ((ptr = strstr(input, "http://"))) {
|
|
|
|
proto = RTR_PROT_HTTP;
|
|
|
|
host = ptr+7;
|
2010-08-24 09:58:09 +00:00
|
|
|
} else if ((ptr = strstr(input, "udp://"))) {
|
|
|
|
proto = RTR_PROT_UDP;
|
|
|
|
host = ptr+6;
|
|
|
|
} else if ((ptr = strstr(input, "rap://"))) {
|
|
|
|
proto = RTR_PROT_RAP;
|
|
|
|
host = ptr+6;
|
|
|
|
} else {
|
|
|
|
proto = RTR_PROT_RAP;
|
|
|
|
host = input;
|
|
|
|
}
|
2012-12-22 02:37:50 +00:00
|
|
|
while (*host && iswhitechar (*host))
|
2011-09-03 03:09:31 +00:00
|
|
|
host++;
|
2010-08-24 09:58:09 +00:00
|
|
|
|
|
|
|
if (!(ptr = strchr (host, ':'))) {
|
2013-03-17 23:38:04 +00:00
|
|
|
ptr = host;
|
|
|
|
port = "80";
|
|
|
|
} else {
|
|
|
|
*ptr++ = '\0';
|
|
|
|
port = ptr;
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(file = strchr (ptr, '/'))) {
|
|
|
|
eprintf("Error: Missing '/'\n");
|
|
|
|
return;
|
|
|
|
}
|
2012-09-06 01:12:54 +00:00
|
|
|
*file++ = 0;
|
2013-11-29 16:27:46 +00:00
|
|
|
port = r_str_chop (port);
|
|
|
|
while (*file==' ') file++;
|
|
|
|
if (r_sandbox_enable (0)) {
|
|
|
|
eprintf ("sandbox: connect disabled\n");
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
|
2015-09-14 10:35:38 +00:00
|
|
|
fd = r_socket_new (false);
|
2011-05-05 01:41:57 +00:00
|
|
|
if (!fd) {
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error: Cannot create new socket\n");
|
2011-05-05 01:41:57 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
switch (proto) {
|
2013-02-06 09:35:23 +00:00
|
|
|
case RTR_PROT_HTTP:
|
|
|
|
{
|
2015-01-27 16:03:06 +00:00
|
|
|
char uri[1024], prompt[64], prompt2[64];
|
2013-02-06 09:35:23 +00:00
|
|
|
int len;
|
2015-01-30 23:50:05 +00:00
|
|
|
const char* res;
|
|
|
|
char *str, *ptr;
|
2014-10-06 23:58:42 +00:00
|
|
|
int flen = strlen (file);
|
|
|
|
int is_visual = (file[flen-1]== 'V')?1:0;
|
|
|
|
int is_valid = (file[flen-(is_visual?2:1)] == '/')?1:0;
|
|
|
|
if (is_valid) {
|
2014-08-28 09:38:11 +00:00
|
|
|
TextLog T = { host, port, file };
|
2014-10-06 23:58:42 +00:00
|
|
|
if (is_visual) {
|
|
|
|
file[flen-1] = 0; // remove V from url
|
|
|
|
rtr_visual (core, T, NULL);
|
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
snprintf (prompt, sizeof (prompt), "[http://%s:%s/%s]> ",
|
2014-10-06 23:58:42 +00:00
|
|
|
host, port, file);
|
2015-01-27 16:03:06 +00:00
|
|
|
snprintf (prompt2, sizeof(prompt2), "[%s:%s]$ ", host, port);
|
2014-10-06 23:58:42 +00:00
|
|
|
for (;;) {
|
|
|
|
r_line_set_prompt (prompt);
|
2015-01-30 23:50:05 +00:00
|
|
|
res = r_line_readline ();
|
|
|
|
if (!res || !*res) break;
|
|
|
|
if (*res == 'q') break;
|
|
|
|
if (!strcmp (res, "!sh")) {
|
2015-01-27 16:03:06 +00:00
|
|
|
for (;;) {
|
|
|
|
r_line_set_prompt (prompt2);
|
|
|
|
res = r_line_readline ();
|
2015-01-29 11:16:37 +00:00
|
|
|
if (!res || !*res || !strcmp (res, "exit")) {
|
2015-01-27 16:06:01 +00:00
|
|
|
break;
|
2015-01-29 11:16:37 +00:00
|
|
|
}
|
2015-01-27 16:03:06 +00:00
|
|
|
ptr = r_str_uri_encode (res);
|
|
|
|
|
|
|
|
snprintf (uri, sizeof (uri), "http://%s:%s/%s!%s",
|
|
|
|
host, port, file, res);
|
|
|
|
str = r_socket_http_get (uri, NULL, &len);
|
|
|
|
if (str) {
|
|
|
|
str[len] = 0;
|
|
|
|
res = strstr (str, "\n\n");
|
|
|
|
if (res) res = strstr (res+1, "\n\n");
|
|
|
|
if (res) res += 2; else res = str;
|
|
|
|
printf ("%s%s", res, (res[strlen (res)-1]=='\n')?"":"\n");
|
|
|
|
r_line_hist_add (str);
|
|
|
|
free (str);
|
|
|
|
}
|
2015-01-29 11:16:37 +00:00
|
|
|
free (ptr);
|
2015-01-27 16:03:06 +00:00
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
} else if (res[0]=='V') {
|
|
|
|
if (res[1]==' ') {
|
|
|
|
rtr_visual (core, T, res+1);
|
2014-08-28 09:38:11 +00:00
|
|
|
} else {
|
|
|
|
rtr_visual (core, T, NULL);
|
|
|
|
}
|
2015-01-30 23:50:05 +00:00
|
|
|
} else if (!strcmp (res, "TT")) {
|
2014-08-28 02:41:33 +00:00
|
|
|
rtr_textlog_chat (core, T);
|
|
|
|
} else {
|
2015-01-30 23:50:05 +00:00
|
|
|
ptr = r_str_uri_encode (res);
|
|
|
|
if (ptr) res = ptr;
|
2014-10-06 23:58:42 +00:00
|
|
|
snprintf (uri, sizeof (uri), "http://%s:%s/%s%s",
|
2015-01-30 23:50:05 +00:00
|
|
|
host, port, file, res);
|
|
|
|
if (ptr == res) free (ptr);
|
2014-10-06 23:58:42 +00:00
|
|
|
str = r_socket_http_get (uri, NULL, &len);
|
|
|
|
if (str) {
|
|
|
|
str[len] = 0;
|
|
|
|
res = strstr (str, "\n\n");
|
|
|
|
if (res) res = strstr (res+1, "\n\n");
|
|
|
|
if (res) res += 2; else res = str;
|
|
|
|
printf ("%s%s", res, (res[strlen (res)-1]=='\n')?"":"\n");
|
|
|
|
r_line_hist_add (str);
|
|
|
|
free (str);
|
|
|
|
}
|
2013-03-17 23:38:04 +00:00
|
|
|
}
|
2013-02-06 09:35:23 +00:00
|
|
|
}
|
2014-08-17 18:30:28 +00:00
|
|
|
r_socket_free (fd);
|
2013-02-06 09:35:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-29 16:27:46 +00:00
|
|
|
snprintf (uri, sizeof (uri), "http://%s:%s/%s",
|
|
|
|
host, port, file);
|
2013-02-06 09:35:23 +00:00
|
|
|
str = r_socket_http_get (uri, NULL, &len);
|
|
|
|
if (str) {
|
|
|
|
str[len] = 0;
|
|
|
|
res = strstr (str, "\n\n");
|
|
|
|
if (res) res = strstr (res+1, "\n\n");
|
|
|
|
if (res) res += 2; else res = str;
|
|
|
|
printf ("%s", res);
|
|
|
|
free (str);
|
2013-03-03 16:28:06 +00:00
|
|
|
} else eprintf ("HTTP connection has failed\n");
|
2013-02-06 09:35:23 +00:00
|
|
|
// do not add connection. wtf
|
2015-02-03 14:53:21 +00:00
|
|
|
r_socket_free(fd);
|
2013-02-06 09:35:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
2010-08-24 09:58:09 +00:00
|
|
|
case RTR_PROT_RAP:
|
2013-11-29 16:27:46 +00:00
|
|
|
if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
|
2015-01-15 22:52:56 +00:00
|
|
|
r_socket_free(fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-30 01:47:29 +00:00
|
|
|
eprintf ("Connected to %s at port %s\n", host, port);
|
2010-08-24 09:58:09 +00:00
|
|
|
/* send */
|
|
|
|
buf[0] = RTR_RAP_OPEN;
|
|
|
|
buf[1] = 0;
|
|
|
|
buf[2] = (ut8)(strlen (file)+1);
|
|
|
|
memcpy (buf+3, file, buf[2]);
|
|
|
|
r_socket_write(fd, buf, 3+buf[2]);
|
|
|
|
/* read */
|
|
|
|
eprintf ("waiting... "); fflush(stdout);
|
|
|
|
r_socket_read (fd, (ut8*)buf, 5);
|
2010-09-08 17:49:34 +00:00
|
|
|
r_mem_copyendian ((ut8 *)&i, (ut8*)buf+1, 4, core->assembler->big_endian);
|
2010-08-24 09:58:09 +00:00
|
|
|
if (buf[0] != (char)(RTR_RAP_OPEN|RTR_RAP_REPLY) || i<= 0) {
|
|
|
|
eprintf ("Error: Wrong reply\n");
|
2015-01-15 22:52:56 +00:00
|
|
|
r_socket_free(fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
eprintf ("ok\n");
|
|
|
|
break;
|
|
|
|
case RTR_PROT_TCP:
|
2013-11-29 16:27:46 +00:00
|
|
|
if (!r_socket_connect_tcp (fd, host, port, timeout)) { //TODO: Use rap.ssl
|
|
|
|
core->num->value = 1;
|
2011-05-05 01:41:57 +00:00
|
|
|
eprintf("Error: Cannot connect to '%s' (%s)\n", host, port);
|
2015-01-15 22:52:56 +00:00
|
|
|
r_socket_free(fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 0;
|
2011-03-27 15:44:27 +00:00
|
|
|
eprintf ("Connected to: %s at port %s\n", host, port);
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
|
|
|
case RTR_PROT_UDP:
|
2013-11-29 16:27:46 +00:00
|
|
|
if (!r_socket_connect_udp (fd, host, port, timeout)) { //TODO: Use rap.ssl
|
|
|
|
core->num->value = 1;
|
|
|
|
eprintf ("Error: Cannot connect to '%s' (%s)\n", host, port);
|
2015-01-15 22:52:56 +00:00
|
|
|
r_socket_free(fd);
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 0;
|
2011-03-27 15:44:27 +00:00
|
|
|
eprintf("Connected to: %s at port %s\n", host, port);
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-30 01:47:29 +00:00
|
|
|
ret = core->num->value;
|
2010-08-24 09:58:09 +00:00
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++)
|
|
|
|
if (!rtr_host[i].fd) {
|
|
|
|
rtr_host[i].proto = proto;
|
2015-05-12 21:47:26 +00:00
|
|
|
strncpy (rtr_host[i].host, host, sizeof (rtr_host[i].proto)-1);
|
2013-11-29 16:27:46 +00:00
|
|
|
rtr_host[i].port = r_num_get (core->num, port);
|
2015-05-12 21:47:26 +00:00
|
|
|
strncpy (rtr_host[i].file, file, sizeof (rtr_host[i].file)-1);
|
2010-08-24 09:58:09 +00:00
|
|
|
rtr_host[i].fd = fd;
|
|
|
|
rtr_n = i;
|
|
|
|
break;
|
|
|
|
}
|
2013-11-30 01:47:29 +00:00
|
|
|
core->num->value = ret;
|
2015-05-12 21:47:26 +00:00
|
|
|
// double free wtf is freed this here? r_socket_free(fd);
|
2013-02-06 09:35:23 +00:00
|
|
|
//r_core_rtr_list (core);
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_remove(RCore *core, const char *input) {
|
|
|
|
int fd, i;
|
|
|
|
|
|
|
|
if (input[0] >= '0' && input[0] <= '9') {
|
|
|
|
fd = r_num_math (core->num, input);
|
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++)
|
2015-04-01 00:05:43 +00:00
|
|
|
if (rtr_host[i].fd && rtr_host[i].fd->fd == fd) {
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_free (rtr_host[i].fd);
|
|
|
|
rtr_host[i].fd = NULL;
|
2010-08-24 09:58:09 +00:00
|
|
|
if (rtr_n == i)
|
2014-08-03 22:48:23 +00:00
|
|
|
for (rtr_n = 0; !rtr_host[rtr_n].fd \
|
|
|
|
&& rtr_n < RTR_MAX_HOSTS - 1; rtr_n++);
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-12 21:47:26 +00:00
|
|
|
for (i = 0; i < RTR_MAX_HOSTS; i++) {
|
|
|
|
if (rtr_host[i].fd) {
|
2011-03-21 00:47:17 +00:00
|
|
|
r_socket_free (rtr_host[i].fd);
|
2015-05-12 21:47:26 +00:00
|
|
|
rtr_host[i].fd = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
memset (rtr_host, '\0', RTR_MAX_HOSTS * sizeof(RCoreRtrHost));
|
|
|
|
rtr_n = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_session(RCore *core, const char *input) {
|
2014-01-18 22:02:53 +00:00
|
|
|
char prompt[64], buf[4096];
|
2010-08-24 09:58:09 +00:00
|
|
|
int fd;
|
|
|
|
|
2014-01-18 22:02:53 +00:00
|
|
|
prompt[0] = 0;
|
2010-08-24 09:58:09 +00:00
|
|
|
if (input[0] >= '0' && input[0] <= '9') {
|
|
|
|
fd = r_num_math (core->num, input);
|
2015-03-22 22:24:13 +00:00
|
|
|
for (rtr_n = 0; rtr_host[rtr_n].fd \
|
|
|
|
&& rtr_host[rtr_n].fd->fd != fd \
|
2014-08-03 22:48:23 +00:00
|
|
|
&& rtr_n < RTR_MAX_HOSTS - 1; rtr_n++);
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2011-09-03 03:09:31 +00:00
|
|
|
if (rtr_host[rtr_n].fd)
|
|
|
|
snprintf (prompt, sizeof (prompt),
|
|
|
|
"fd:%d> ", rtr_host[rtr_n].fd->fd);
|
2012-08-19 01:28:17 +00:00
|
|
|
free (r_line_singleton ()->prompt);
|
|
|
|
r_line_singleton ()->prompt = strdup (prompt);
|
2010-08-24 09:58:09 +00:00
|
|
|
if ((r_cons_fgets (buf, sizeof (buf), 0, NULL))) {
|
2014-04-28 08:17:42 +00:00
|
|
|
if (!*buf || *buf == 'q')
|
2010-08-24 09:58:09 +00:00
|
|
|
break;
|
2014-04-28 08:17:42 +00:00
|
|
|
if (*buf == 'V') {
|
2010-08-24 09:58:09 +00:00
|
|
|
eprintf ("Visual mode not supported\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
r_core_rtr_cmd (core, buf);
|
|
|
|
r_cons_flush ();
|
2014-04-28 08:17:42 +00:00
|
|
|
} else break;
|
2010-08-24 09:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_core_rtr_cmd(RCore *core, const char *input) {
|
|
|
|
char bufw[1024], bufr[8];
|
|
|
|
const char *cmd = NULL, *cmd_output = NULL;
|
|
|
|
int i, cmd_len, fd = atoi (input);
|
|
|
|
|
2012-12-30 22:40:17 +00:00
|
|
|
if (*input==':' && !strchr (input+1, ':')) {
|
|
|
|
r_core_cmdf (core, "o rap://%s", input);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
if (fd != 0) {
|
2011-09-03 03:09:31 +00:00
|
|
|
if (rtr_host[rtr_n].fd)
|
|
|
|
for (rtr_n = 0; rtr_host[rtr_n].fd->fd != fd
|
2014-08-03 22:48:23 +00:00
|
|
|
&& rtr_n < RTR_MAX_HOSTS - 1; rtr_n++);
|
2010-08-24 09:58:09 +00:00
|
|
|
if (!(cmd = strchr (input, ' '))) {
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error\n");
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else cmd = input;
|
|
|
|
|
|
|
|
if (!rtr_host[rtr_n].fd){
|
|
|
|
eprintf ("Error: Unknown host\n");
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 1; // fail
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!rtr_host[rtr_n].proto == RTR_PROT_RAP){
|
|
|
|
eprintf ("Error: Not a rap:// host\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-29 16:27:46 +00:00
|
|
|
core->num->value = 0; // that's fine
|
|
|
|
if (!strlen (cmd)) {
|
|
|
|
// just check if we can connect
|
|
|
|
r_socket_close (rtr_host[rtr_n].fd);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-24 09:58:09 +00:00
|
|
|
/* send */
|
|
|
|
bufw[0] = RTR_RAP_CMD;
|
|
|
|
i = strlen (cmd) + 1;
|
|
|
|
r_mem_copyendian ((ut8*)bufw+1, (ut8*)&i, 4, endian);
|
|
|
|
memcpy (bufw+5, cmd, i);
|
|
|
|
r_socket_write (rtr_host[rtr_n].fd, bufw, 5+i);
|
|
|
|
/* read */
|
|
|
|
r_socket_read (rtr_host[rtr_n].fd, (ut8*)bufr, 5);
|
|
|
|
if (bufr[0] != (char)(RTR_RAP_CMD|RTR_RAP_REPLY)) {
|
|
|
|
eprintf ("Error: Wrong reply\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
r_mem_copyendian ((ut8*)&cmd_len, (ut8*)bufr+1, 4, endian);
|
|
|
|
cmd_output = malloc (cmd_len);
|
|
|
|
if (!cmd_output) {
|
2011-09-03 03:09:31 +00:00
|
|
|
eprintf ("Error: Allocating cmd output\n");
|
2010-08-24 09:58:09 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
r_socket_read (rtr_host[rtr_n].fd, (ut8*)cmd_output, cmd_len);
|
|
|
|
r_cons_printf ("%s\n", cmd_output);
|
|
|
|
free ((void *)cmd_output);
|
|
|
|
}
|
2012-11-05 01:00:34 +00:00
|
|
|
|
|
|
|
// TODO: support len for binary data?
|
|
|
|
R_API char *r_core_rtr_cmds_query (RCore *core, const char *host, const char *port, const char *cmd) {
|
|
|
|
int retries = 6;
|
|
|
|
unsigned char buf[1024];
|
|
|
|
char *rbuf = NULL;
|
|
|
|
const int timeout = 0;
|
|
|
|
RSocket *s = r_socket_new (0);
|
2013-01-23 13:50:50 +00:00
|
|
|
for (;retries>0; r_sys_usleep (10*1000)) {
|
2012-11-05 01:00:34 +00:00
|
|
|
if (r_socket_connect (s, host, port, R_SOCKET_PROTO_TCP, timeout))
|
|
|
|
break;
|
|
|
|
retries--;
|
|
|
|
}
|
|
|
|
if (retries>0) {
|
|
|
|
rbuf = strdup ("");
|
|
|
|
r_socket_write (s, (void*)cmd, strlen (cmd));
|
|
|
|
//r_socket_write (s, "px\n", 3);
|
|
|
|
for (;;) {
|
|
|
|
int ret = r_socket_read (s, buf, sizeof (buf));
|
|
|
|
if (ret<1) break;
|
|
|
|
buf[ret] = 0;
|
|
|
|
rbuf = r_str_concat (rbuf, (const char *)buf);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("Cannot connect\n");
|
|
|
|
}
|
|
|
|
r_socket_free (s);
|
|
|
|
return rbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API int r_core_rtr_cmds (RCore *core, const char *port) {
|
2014-04-22 22:12:03 +00:00
|
|
|
unsigned char buf[4097];
|
2015-02-17 23:28:53 +00:00
|
|
|
RSocket *ch = NULL;
|
|
|
|
RSocket *s;
|
2012-12-22 02:37:50 +00:00
|
|
|
int i, ret;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (!port || port[0]=='?') {
|
|
|
|
r_cons_printf ("Usage: .:[tcp-port] run r2 commands for clients\n");
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2012-12-22 02:37:50 +00:00
|
|
|
}
|
2012-12-03 03:01:55 +00:00
|
|
|
|
2012-12-22 02:37:50 +00:00
|
|
|
s = r_socket_new (0);
|
2012-11-05 01:00:34 +00:00
|
|
|
if (!r_socket_listen (s, port, NULL)) {
|
|
|
|
eprintf ("Error listening on port %s\n", port);
|
2012-12-22 02:37:50 +00:00
|
|
|
r_socket_free (s);
|
2015-09-14 10:35:38 +00:00
|
|
|
return false;
|
2012-11-05 01:00:34 +00:00
|
|
|
}
|
2014-04-22 22:12:03 +00:00
|
|
|
|
2012-11-05 01:00:34 +00:00
|
|
|
eprintf ("Listening for commands on port %s\n", port);
|
|
|
|
listenport = port;
|
|
|
|
for (;;) {
|
2014-06-22 12:57:54 +00:00
|
|
|
r_cons_break ((RConsBreak)http_break, core);
|
2012-11-05 01:00:34 +00:00
|
|
|
ch = r_socket_accept (s);
|
|
|
|
buf[0] = 0;
|
2014-04-22 22:12:03 +00:00
|
|
|
ret = r_socket_read (ch, buf, sizeof (buf) - 1);
|
2012-11-05 01:00:34 +00:00
|
|
|
if (ret>0) {
|
|
|
|
buf[ret] = 0;
|
2012-12-03 03:01:55 +00:00
|
|
|
for (i=0; buf[i]; i++)
|
2012-11-05 01:00:34 +00:00
|
|
|
if (buf[i] == '\n')
|
2012-12-12 22:58:18 +00:00
|
|
|
buf[i] = buf[i+1]? ';': '\0';
|
2012-11-05 01:00:34 +00:00
|
|
|
if (!r_config_get_i (core->config, "scr.prompt") \
|
|
|
|
&& !strcmp ((char*)buf, "q!"))
|
2012-12-12 22:58:18 +00:00
|
|
|
break;
|
2012-11-05 01:00:34 +00:00
|
|
|
str = r_core_cmd_str (core, (const char *)buf);
|
2012-12-12 22:58:18 +00:00
|
|
|
if (str &&*str) {
|
2012-11-05 01:00:34 +00:00
|
|
|
r_socket_write (ch, str, strlen (str));
|
2014-04-22 22:12:03 +00:00
|
|
|
} else
|
2012-12-12 22:58:18 +00:00
|
|
|
r_socket_write (ch, "\n", 1);
|
|
|
|
free (str);
|
2012-11-05 01:00:34 +00:00
|
|
|
}
|
2014-06-22 12:57:54 +00:00
|
|
|
if (r_cons_singleton()->breaked)
|
|
|
|
break;
|
2012-11-05 01:00:34 +00:00
|
|
|
r_socket_close (ch);
|
2015-02-17 23:28:53 +00:00
|
|
|
r_socket_free(ch);
|
2012-11-05 01:00:34 +00:00
|
|
|
r_cons_break_end ();
|
|
|
|
}
|
2014-08-03 22:48:23 +00:00
|
|
|
r_socket_free(s);
|
|
|
|
r_socket_free(ch);
|
2012-11-05 01:00:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|