summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-08-19 19:01:18 -0400
committerirungentoo <irungentoo@gmail.com>2014-08-19 19:01:18 -0400
commit7330ebdffe75ccdd7d4685e45ee733962dbb3409 (patch)
tree9381e0e5cb8810a32a64b57f041e77a6ba72e835 /testing
parentf368b67914578cfb73cb1a8e5a743bb845c138ab (diff)
Added tox_shell to testing.
tox_shell is a basic secure shell that can be used to control a computer from any Tox client. Just run tox_shell and make it add your Tox id. It's very basic but it works.
Diffstat (limited to 'testing')
-rw-r--r--testing/Makefile.inc18
-rw-r--r--testing/tox_shell.c153
2 files changed, 170 insertions, 1 deletions
diff --git a/testing/Makefile.inc b/testing/Makefile.inc
index 236ab19b..42d89e05 100644
--- a/testing/Makefile.inc
+++ b/testing/Makefile.inc
@@ -87,8 +87,24 @@ tox_sync_LDADD = $(LIBSODIUM_LDFLAGS) \
87 libtoxcore.la \ 87 libtoxcore.la \
88 $(LIBSODIUM_LIBS) \ 88 $(LIBSODIUM_LIBS) \
89 $(NACL_OBJECTS) \ 89 $(NACL_OBJECTS) \
90 $(NACL_LIBS)
91
92
93noinst_PROGRAMS += tox_shell
94
95tox_shell_SOURCES = ../testing/tox_shell.c
96
97tox_shell_CFLAGS = $(LIBSODIUM_CFLAGS) \
98 $(NACL_CFLAGS)
99
100tox_shell_LDADD = $(LIBSODIUM_LDFLAGS) \
101 $(NACL_LDFLAGS) \
102 libtoxcore.la \
103 $(LIBSODIUM_LIBS) \
104 $(NACL_OBJECTS) \
90 $(NACL_LIBS) \ 105 $(NACL_LIBS) \
91 $(WINSOCK2_LIBS) 106 -lutil
107
92endif 108endif
93 109
94EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c 110EXTRA_DIST += $(top_srcdir)/testing/misc_tools.c
diff --git a/testing/tox_shell.c b/testing/tox_shell.c
new file mode 100644
index 00000000..c80c20b8
--- /dev/null
+++ b/testing/tox_shell.c
@@ -0,0 +1,153 @@
1/* Tox Shell
2 *
3 * Proof of concept ssh like server software using tox.
4 *
5 * Command line arguments are the ip, port and public_key of a node (for bootstrapping).
6 *
7 * EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
8 *
9 *
10 * Copyright (C) 2014 Tox project All Rights Reserved.
11 *
12 * This file is part of Tox.
13 *
14 * Tox is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Tox is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
26 *
27 */
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#include "../toxcore/tox.h"
34#include "misc_tools.c"
35
36#include <pty.h>
37#include <unistd.h>
38#include <fcntl.h>
39
40#define c_sleep(x) usleep(1000*x)
41
42void print_online(Tox *tox, int friendnumber, uint8_t status, void *userdata)
43{
44 if (status == 1)
45 printf("\nOther went online.\n");
46 else
47 printf("\nOther went offline.\n");
48}
49
50void print_message(Tox *tox, int friendnumber, const uint8_t *string, uint16_t length, void *userdata)
51{
52 int master = *((int *)userdata);
53 write(master, string, length);
54 write(master, "\n", 1);
55}
56
57int main(int argc, char *argv[])
58{
59 uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
60 int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
61
62 if (argvoffset < 0)
63 exit(1);
64
65 /* with optional --ipvx, now it can be 1-4 arguments... */
66 if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
67 printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
68 exit(0);
69 }
70
71 int *master = malloc(sizeof(int));
72 int ret = forkpty(master, NULL, NULL, NULL);
73
74 if (ret == -1) {
75 printf("fork failed\n");
76 return 1;
77 }
78
79 if (ret == 0) {
80 execl("/bin/sh", "sh", NULL);
81 return 0;
82 }
83
84 int flags = fcntl(*master, F_GETFL, 0);
85 int r = fcntl(*master, F_SETFL, flags | O_NONBLOCK);
86
87 if (r < 0) {
88 printf("error setting flags\n");
89 }
90
91 Tox *tox = tox_new(0);
92 tox_callback_connection_status(tox, print_online, NULL);
93 tox_callback_friend_message(tox, print_message, master);
94
95
96 uint16_t port = atoi(argv[argvoffset + 2]);
97 unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
98 int res = tox_bootstrap_from_address(tox, argv[argvoffset + 1], port, binary_string);
99 free(binary_string);
100
101 if (!res) {
102 printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
103 exit(1);
104 }
105
106 uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
107 tox_get_address(tox, address);
108 uint32_t i;
109
110 for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) {
111 printf("%02X", address[i]);
112 }
113
114 char temp_id[128];
115 printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n");
116
117 if (scanf("%s", temp_id) != 1) {
118 return 1;
119 }
120
121 uint8_t *bin_id = hex_string_to_bin(temp_id);
122 int num = tox_add_friend(tox, bin_id, (uint8_t *)"Install Gentoo", sizeof("Install Gentoo"));
123 free(bin_id);
124
125 if (num < 0) {
126 printf("\nSomething went wrong when adding friend.\n");
127 return 1;
128 }
129
130 uint8_t notconnected = 1;
131
132 while (1) {
133 if (tox_isconnected(tox) && notconnected) {
134 printf("\nDHT connected.\n");
135 notconnected = 0;
136 }
137
138 while (tox_get_friend_connection_status(tox, num) == 1) {
139 uint8_t buf[TOX_MAX_MESSAGE_LENGTH];
140 ret = read(*master, buf, sizeof(buf));
141
142 if (ret <= 0)
143 break;
144
145 tox_send_message(tox, num, buf, ret);
146 }
147
148 tox_do(tox);
149 c_sleep(1);
150 }
151
152 return 0;
153}