2019-02-08 16:37:02 +00:00
|
|
|
#include "tor_ctl.h"
|
|
|
|
|
|
|
|
int tor_ctl_connect(struct tor_ctl* ctx, char* addr, char* service) {
|
|
|
|
int sock = create_tcp_client (addr, service);
|
|
|
|
int sock2 = dup(sock);
|
|
|
|
ctx->rsock = NULL;
|
|
|
|
ctx->wsock = NULL;
|
|
|
|
|
|
|
|
ctx->rsock = fdopen(sock, "r");
|
|
|
|
if (ctx->rsock == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setbuf(ctx->rsock, NULL);
|
|
|
|
|
|
|
|
ctx->wsock = fdopen(sock2, "w");
|
|
|
|
if (ctx->wsock == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
setbuf(ctx->wsock, NULL);
|
|
|
|
|
|
|
|
fprintf (ctx->wsock, "authenticate \"\"\n");
|
|
|
|
int error_code = 0;
|
|
|
|
fscanf (ctx->rsock, "%d", &error_code);
|
|
|
|
if (error_code != 250) {
|
|
|
|
tor_ctl_close (ctx);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fscanf(ctx->rsock," OK");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-20 22:35:02 +00:00
|
|
|
void tor_ctl_list_onions(struct tor_ctl* ctx) {
|
|
|
|
fprintf(ctx->wsock, "getinfo onions/current\n");
|
|
|
|
|
|
|
|
char delimiter, buffer[1024] = {0};
|
|
|
|
fscanf(ctx->rsock, " 250%c", &delimiter);
|
|
|
|
if (delimiter == '-') {
|
|
|
|
fscanf(ctx->rsock, "onions/current=%s", buffer);
|
|
|
|
if (strlen(buffer) > 0) printf("key: %s\n", buffer);
|
|
|
|
} else if (delimiter == '+') {
|
|
|
|
fscanf(ctx->rsock, "onions/current= ");
|
|
|
|
while (1) {
|
|
|
|
fgets(buffer, 1024, ctx->rsock);
|
|
|
|
if (strcmp(buffer, "250 OK\r\n") == 0) break;
|
|
|
|
if (strcmp(buffer, ".\r\n") == 0) continue;
|
|
|
|
printf("line: %s", buffer);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
printf("deli:%c\n", delimiter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 22:33:45 +00:00
|
|
|
int tor_ctl_add_onion(struct tor_ctl* ctx, struct tor_os_str* tos, uint16_t* port, uint64_t port_per_os, enum TOR_ONION_FLAGS flags) {
|
2019-02-08 16:37:02 +00:00
|
|
|
int err = 0;
|
2019-02-11 08:53:00 +00:00
|
|
|
char buffer1[1024] = {0};
|
|
|
|
char buffer2[1024] = {0};
|
2019-02-08 16:37:02 +00:00
|
|
|
int to_create = tos->size - tos->filled;
|
|
|
|
|
|
|
|
/* Add onion services loaded from file */
|
|
|
|
for (int i = 0; i < tos->filled; i++) {
|
2020-02-01 22:33:45 +00:00
|
|
|
fprintf(ctx->wsock, "add_onion %s ", tos->keys[i].priv);
|
|
|
|
for (int j = 0; j < port_per_os; j++) {
|
|
|
|
fprintf(ctx->wsock, "Port=%d,127.13.3.7:%d ", port[i*port_per_os+j], port[i*port_per_os+j]);
|
|
|
|
}
|
|
|
|
if (flags == TOR_ONION_FLAG_NONE) fprintf(ctx->wsock, "\n");
|
2019-09-04 15:24:51 +00:00
|
|
|
else {
|
2020-02-01 22:33:45 +00:00
|
|
|
fprintf(ctx->wsock, "Flags=");
|
|
|
|
if (flags & TOR_ONION_FLAG_NON_ANONYMOUS) fprintf(ctx->wsock, "NonAnonymous,");
|
2019-09-04 15:24:51 +00:00
|
|
|
fprintf(ctx->wsock, "\n");
|
|
|
|
}
|
|
|
|
|
2019-02-08 16:37:02 +00:00
|
|
|
fscanf(ctx->rsock, "%d", &err);
|
2019-02-08 17:17:42 +00:00
|
|
|
if (err != 250) {
|
|
|
|
printf("err: %d\n", err);
|
|
|
|
return -1;
|
|
|
|
}
|
2019-02-11 09:25:27 +00:00
|
|
|
fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
|
2019-02-11 08:53:00 +00:00
|
|
|
printf("Added onion service %s.onion from file\n", buffer1);
|
2019-02-08 16:37:02 +00:00
|
|
|
fscanf(ctx->rsock, "250 OK");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Complete by creating new onion services */
|
|
|
|
for (int i = tos->filled; i < tos->size; i++) {
|
2020-02-01 22:33:45 +00:00
|
|
|
fprintf(ctx->wsock, "add_onion NEW:ED25519-V3 ");
|
|
|
|
for (int j = 0; j < port_per_os; j++) {
|
|
|
|
fprintf(ctx->wsock, "Port=%d,127.13.3.7:%d ", port[i*port_per_os+j], port[i*port_per_os+j]);
|
|
|
|
}
|
|
|
|
if (flags == TOR_ONION_FLAG_NONE) fprintf(ctx->wsock, "\n");
|
2019-09-04 14:40:02 +00:00
|
|
|
else {
|
2020-02-01 22:33:45 +00:00
|
|
|
fprintf(ctx->wsock, "Flags=");
|
|
|
|
if (flags & TOR_ONION_FLAG_NON_ANONYMOUS) fprintf(ctx->wsock, "NonAnonymous,");
|
2019-09-04 14:40:02 +00:00
|
|
|
fprintf(ctx->wsock, "\n");
|
|
|
|
}
|
2020-02-01 22:33:45 +00:00
|
|
|
|
2019-03-06 16:46:12 +00:00
|
|
|
//fprintf(ctx->wsock, "add_onion NEW:RSA1024 Port=%d\n", port[i]);
|
2019-02-08 16:37:02 +00:00
|
|
|
|
|
|
|
fscanf(ctx->rsock, "%d", &err);
|
|
|
|
|
|
|
|
if (err != 250) return -2;
|
2019-02-11 08:53:00 +00:00
|
|
|
err = fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
|
2019-02-08 16:37:02 +00:00
|
|
|
if (err <= 0) return -3;
|
2019-02-11 08:53:00 +00:00
|
|
|
printf("Created onion service %s.onion\n", buffer1);
|
|
|
|
err = fscanf(ctx->rsock, "250-PrivateKey=%s\n", buffer2);
|
2019-02-08 16:37:02 +00:00
|
|
|
if (err <= 0) return -4;
|
2019-02-11 08:53:00 +00:00
|
|
|
//printf("Onion service private key: %s\n", buffer);
|
|
|
|
if (tor_os_append(tos, buffer1, buffer2) != 0) return -5;
|
2019-02-08 16:37:02 +00:00
|
|
|
err = fscanf(ctx->rsock, "250 OK");
|
|
|
|
if (err < 0) return -6;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to_create > 0) {
|
|
|
|
tor_os_persist(tos);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tor_ctl_close(struct tor_ctl* ctx) {
|
|
|
|
fclose(ctx->rsock);
|
|
|
|
fclose(ctx->wsock);
|
|
|
|
}
|