Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions tcp-tunnel/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,8 @@ clean:
install: bin/$(TARGET)
@echo "Installing into the iOS image..."
@hdiutil attach -imagekey diskimage-class=CRawDiskImage $(IOS_DIR)/hfs.main
@sudo cp $^ /Volumes/PeaceB16B92.arm64UpdateRamDisk/bin/
@hdiutil detach /Volumes/PeaceB16B92.arm64UpdateRamDisk
@echo "Signing with entitlements..."
@jtool --sign --ent ent.xml --inplace $^
@jtool --sig --ent $^ | grep CDHash | cut -d' ' -f6 | cut -c 1-40 >> $(IOS_DIR)/tchashes
@echo "Updating static trustcache..."
@python $(IOS_DIR)/xnu-qemu-arm64-tools/bootstrap_scripts/create_trustcache.py $(IOS_DIR)/tchashes $(IOS_DIR)/static_tc
@sudo cp $^ /Volumes/PeaceB16B92.arm64UpdateRamDisk/bin/
@echo "Copied to disk"
@hdiutil detach /Volumes/PeaceB16B92.arm64UpdateRamDisk
36 changes: 34 additions & 2 deletions tcp-tunnel/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#define DIR_IN 1
#define DIR_OUT 2

#define SOCKET_SLEEP 10e3

typedef struct {
int fd;
int *error;
Expand All @@ -30,6 +32,7 @@ typedef struct {
ssize_t (*send)(int sckt, const void *buffer, size_t length, int flags);
} socket_t;

static void verbose_log(const char *log);
static void init_qemu_socket(socket_t *sock_struct);
static void init_native_socket(socket_t *sock_struct);
static void terminate(int signum);
Expand All @@ -46,6 +49,7 @@ static int tunnel(socket_t *s_listen, socket_t *in, socket_t *out,

static socket_t s_listen = {.fd = -1}, s_in = {.fd = -1}, s_out = {.fd = -1};
static int daemonize = 0;
static int verbose = 0;

int main(int argc, char *argv[])
{
Expand All @@ -58,11 +62,22 @@ int main(int argc, char *argv[])
case 2:
break;
case 3:
case 4:
if (!strncmp(argv[1], "-d", strlen("-d")) ||
!strncmp(argv[1], "--daemonize", strlen("--daemonize"))) {
daemonize = 1;
break;
}
if (!strncmp(argv[1], "-v", strlen("-v")) ||
!strncmp(argv[1], "--verbose", strlen("--verbose"))) {
verbose = 1;

if(daemonize) {
fprintf(stderr, "Verbose mode available only in foreground mode\n");
return usage(argv[0]);
}
break;
}
default:
return usage(argv[0]);
}
Expand All @@ -81,13 +96,17 @@ int main(int argc, char *argv[])


if (DIR_IN == direction) {
verbose_log("Start tunnel with IN direction mode");

init_qemu_socket(&s_listen);
init_qemu_socket(&s_in);
init_native_socket(&s_out);

return tunnel(&s_listen, &s_in, &s_out, listen_port, target_ip,
target_port);
} else if (DIR_OUT == direction) {
verbose_log("Start tunnel with OUT direction mode");

init_native_socket(&s_listen);
init_native_socket(&s_in);
init_qemu_socket(&s_out);
Expand Down Expand Up @@ -157,12 +176,18 @@ static void terminate(int signum)

static int usage(const char *prog_name)
{
fprintf(stderr, "Usage: %s [-d|--daemonize] [<in|out>:]listen-port:target-ip:target-port\n",
fprintf(stderr, "Usage: %s [-d|--daemonize] [-v|--verbose] [<in|out>:]listen-port:target-ip:target-port\n",
prog_name);

return -1;
}

static void verbose_log(const char *log) {
if (verbose) {
fprintf(stdout, "%s\n", log);
}
}

static int parse_address_spec(char* address_spec, uint32_t *listen_port,
char *target_ip, size_t target_ip_size,
uint32_t *target_port)
Expand Down Expand Up @@ -344,6 +369,11 @@ static int tunnel(socket_t *s_listen, socket_t *in, socket_t *out,
sizeof(local)) < 0)
{
fprintf(stderr, "bind failed: %d\n", *(s_listen->error));

if(*(s_listen->error) == 48) {
fprintf(stderr, "Port %d already in use\n", listen_port);
}

s_listen->close(s_listen->fd);
return -1;
}
Expand Down Expand Up @@ -378,9 +408,11 @@ static int tunnel(socket_t *s_listen, socket_t *in, socket_t *out,
s_listen->close(s_listen->fd);
return -1;
} else {
usleep(10000); // TODO: define!
usleep(SOCKET_SLEEP);
}
} else {
verbose_log("New incoming connection detected");

if (handle_incoming_connection(in, out, target_ip,
target_port) < 0) {
break;
Expand Down