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;
|
|
|
|
}
|
|
|
|
|
2019-09-04 14:40:02 +00:00
|
|
|
int tor_ctl_add_onion(struct tor_ctl* ctx, struct tor_os_str* tos, uint16_t* port, 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++) {
|
2019-09-04 15:24:51 +00:00
|
|
|
if (flags == TOR_ONION_FLAG_NONE)
|
|
|
|
fprintf(ctx->wsock, "add_onion %s Port=%d,127.13.3.7:%d\n", tos->keys[i].priv, port[i], port[i]);
|
|
|
|
else {
|
|
|
|
fprintf(ctx->wsock, "add_onion %s Port=%d,127.13.3.7:%d Flags=", tos->keys[i].priv, port[i], port[i]);
|
|
|
|
if (flags & TOR_ONION_FLAG_NON_ANONYMOUS)
|
|
|
|
fprintf(ctx->wsock, "NonAnonymous,");
|
|
|
|
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++) {
|
2019-09-04 14:40:02 +00:00
|
|
|
if (flags == TOR_ONION_FLAG_NONE)
|
|
|
|
fprintf(ctx->wsock, "add_onion NEW:ED25519-V3 Port=%d\n", port[i]);
|
|
|
|
else {
|
|
|
|
fprintf(ctx->wsock, "add_onion NEW:ED25519-V3 Port=%d Flags=", port[i]);
|
|
|
|
if (flags & TOR_ONION_FLAG_NON_ANONYMOUS)
|
|
|
|
fprintf(ctx->wsock, "NonAnonymous,");
|
|
|
|
fprintf(ctx->wsock, "\n");
|
|
|
|
}
|
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);
|
|
|
|
}
|