2014-08-20 07:01:18 +08:00
|
|
|
/* Tox Shell
|
|
|
|
*
|
|
|
|
* Proof of concept ssh like server software using tox.
|
|
|
|
*
|
|
|
|
* Command line arguments are the ip, port and public_key of a node (for bootstrapping).
|
|
|
|
*
|
|
|
|
* EX: ./test 127.0.0.1 33445 CDCFD319CE3460824B33BE58FD86B8941C9585181D8FBD7C79C5721D7C2E9F7C
|
2017-01-14 23:46:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright © 2016-2017 The TokTok team.
|
|
|
|
* Copyright © 2014 Tox project.
|
2014-08-20 07:01:18 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* This file is part of Tox, the free peer to peer instant messenger.
|
2014-08-20 07:01:18 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* Tox is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2014-08-20 07:01:18 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* Tox is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-08-20 07:01:18 +08:00
|
|
|
*
|
2017-01-14 23:46:31 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2014-08-20 07:01:18 +08:00
|
|
|
*/
|
2017-01-06 20:19:12 +08:00
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
|
2014-08-20 07:01:18 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#include "misc_tools.c"
|
|
|
|
|
2014-08-21 00:28:26 +08:00
|
|
|
#if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
|
|
|
|
#include <util.h>
|
2015-08-08 07:45:55 +08:00
|
|
|
#elif defined(__FreeBSD__) || defined(__DragonFly__)
|
2014-08-24 11:17:53 +08:00
|
|
|
#include <libutil.h>
|
2014-08-21 00:28:26 +08:00
|
|
|
#else
|
2014-08-20 07:01:18 +08:00
|
|
|
#include <pty.h>
|
2014-08-21 00:28:26 +08:00
|
|
|
#endif
|
2014-08-20 07:01:18 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void print_online(Tox *tox, uint32_t friendnumber, TOX_CONNECTION status, void *userdata)
|
2014-08-20 07:01:18 +08:00
|
|
|
{
|
2016-09-01 02:12:19 +08:00
|
|
|
if (status) {
|
2014-08-20 07:01:18 +08:00
|
|
|
printf("\nOther went online.\n");
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2014-08-20 07:01:18 +08:00
|
|
|
printf("\nOther went offline.\n");
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-08-20 07:01:18 +08:00
|
|
|
}
|
|
|
|
|
2016-09-05 23:10:48 +08:00
|
|
|
static void print_message(Tox *tox, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length,
|
|
|
|
void *userdata)
|
2014-08-20 07:01:18 +08:00
|
|
|
{
|
|
|
|
int master = *((int *)userdata);
|
|
|
|
write(master, string, length);
|
|
|
|
write(master, "\n", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2015-02-25 09:29:01 +08:00
|
|
|
uint8_t ipv6enabled = 1; /* x */
|
2014-08-20 07:01:18 +08:00
|
|
|
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (argvoffset < 0) {
|
2014-08-20 07:01:18 +08:00
|
|
|
exit(1);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-08-20 07:01:18 +08:00
|
|
|
|
|
|
|
/* with optional --ipvx, now it can be 1-4 arguments... */
|
|
|
|
if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) {
|
|
|
|
printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
int *master = (int *)malloc(sizeof(int));
|
2014-08-20 07:01:18 +08:00
|
|
|
int ret = forkpty(master, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
printf("fork failed\n");
|
2016-08-18 07:37:45 +08:00
|
|
|
free(master);
|
2014-08-20 07:01:18 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0) {
|
|
|
|
execl("/bin/sh", "sh", NULL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int flags = fcntl(*master, F_GETFL, 0);
|
|
|
|
int r = fcntl(*master, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
printf("error setting flags\n");
|
|
|
|
}
|
|
|
|
|
2015-05-23 06:23:56 +08:00
|
|
|
Tox *tox = tox_new(0, 0);
|
2016-09-09 21:30:35 +08:00
|
|
|
tox_callback_friend_connection_status(tox, print_online);
|
2016-08-20 02:01:58 +08:00
|
|
|
tox_callback_friend_message(tox, print_message);
|
2014-08-20 07:01:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
uint16_t port = atoi(argv[argvoffset + 2]);
|
|
|
|
unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]);
|
2015-02-25 09:29:01 +08:00
|
|
|
int res = tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0);
|
2014-08-20 07:01:18 +08:00
|
|
|
free(binary_string);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2015-02-25 09:29:01 +08:00
|
|
|
uint8_t address[TOX_ADDRESS_SIZE];
|
|
|
|
tox_self_get_address(tox, address);
|
2014-08-20 07:01:18 +08:00
|
|
|
uint32_t i;
|
|
|
|
|
2015-02-25 09:29:01 +08:00
|
|
|
for (i = 0; i < TOX_ADDRESS_SIZE; i++) {
|
2014-08-20 07:01:18 +08:00
|
|
|
printf("%02X", address[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
char temp_id[128];
|
|
|
|
printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n");
|
|
|
|
|
|
|
|
if (scanf("%s", temp_id) != 1) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *bin_id = hex_string_to_bin(temp_id);
|
2016-09-05 23:10:48 +08:00
|
|
|
uint32_t num = tox_friend_add(tox, bin_id, (const uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), 0);
|
2014-08-20 07:01:18 +08:00
|
|
|
free(bin_id);
|
|
|
|
|
2015-02-25 09:29:01 +08:00
|
|
|
if (num == UINT32_MAX) {
|
2014-08-20 07:01:18 +08:00
|
|
|
printf("\nSomething went wrong when adding friend.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t notconnected = 1;
|
|
|
|
|
|
|
|
while (1) {
|
2015-03-19 03:18:36 +08:00
|
|
|
if (tox_self_get_connection_status(tox) && notconnected) {
|
2014-08-20 07:01:18 +08:00
|
|
|
printf("\nDHT connected.\n");
|
|
|
|
notconnected = 0;
|
|
|
|
}
|
|
|
|
|
2015-02-25 09:29:01 +08:00
|
|
|
while (tox_friend_get_connection_status(tox, num, 0)) {
|
2014-08-20 07:01:18 +08:00
|
|
|
uint8_t buf[TOX_MAX_MESSAGE_LENGTH];
|
|
|
|
ret = read(*master, buf, sizeof(buf));
|
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (ret <= 0) {
|
2014-08-20 07:01:18 +08:00
|
|
|
break;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2014-08-20 07:01:18 +08:00
|
|
|
|
2015-03-19 05:14:57 +08:00
|
|
|
tox_friend_send_message(tox, num, TOX_MESSAGE_TYPE_NORMAL, buf, ret, 0);
|
2014-08-20 07:01:18 +08:00
|
|
|
}
|
|
|
|
|
2016-08-20 02:01:58 +08:00
|
|
|
tox_iterate(tox, master);
|
2014-08-20 07:01:18 +08:00
|
|
|
c_sleep(1);
|
|
|
|
}
|
|
|
|
}
|