Skip to content

Commit

Permalink
added VPN mode
Browse files Browse the repository at this point in the history
  • Loading branch information
csujedihy committed Aug 20, 2015
1 parent e4172bc commit 28c2c05
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 163 deletions.
32 changes: 24 additions & 8 deletions proximac-cli/jconf.c
Expand Up @@ -18,7 +18,10 @@ void read_conf(char* configfile, conf_t* conf)
char* configbuf = NULL;
char localport_buf[6] = { 0 };
char proximac_port_buf[6] = { 0 };
char vpn_mode_buf[6] = { 0 };
int vlen = 0;

/* need to reset these bufs to zero */

FILE* f = fopen(configfile, "rb");
if (f == NULL) {
Expand Down Expand Up @@ -60,25 +63,38 @@ void read_conf(char* configfile, conf_t* conf)
pid_to_insert->name[vlen] = '\0';
pid_to_insert->pid = hash(pid_to_insert->name);
RB_INSERT(pid_tree, &pid_list, pid_to_insert);
LOGI("%d. %s hash %x", index, pid_to_insert->name, pid_to_insert->pid);
LOGI("%d. %s hash %d", index, pid_to_insert->name, pid_to_insert->pid);
}

conf->total_process_num = index;
}

JSONPARSE("proximac_listen_address")
{
conf->proximac_listen_address = (char*)malloc(vlen + 1);
memcpy(conf->proximac_listen_address, val, vlen);
conf->proximac_listen_address[vlen] = '\0';
}

JSONPARSE("proximac_port")
{
memcpy(proximac_port_buf, val, vlen);
conf->proximac_port = atoi(proximac_port_buf);
}

JSONPARSE("VPN_mode")
{
memcpy(vpn_mode_buf, val, vlen);
conf->vpn_mode = atoi(vpn_mode_buf);
}

JSONPARSE("proxyapp_name")
{
struct pid* proxyapp_hash = malloc(sizeof(struct pid));
proxyapp_hash->name = calloc(1, vlen + 1);
memcpy(proxyapp_hash->name, val, vlen);
proxyapp_hash->name[vlen] = '\0';
proxyapp_hash->pid = hash(proxyapp_hash->name);
LOGI("Proxy App name is %s", proxyapp_hash->name);
LOGD("Proxy App name is %s, hash %d", proxyapp_hash->name, proxyapp_hash->pid);

conf->proxyapp_hash = proxyapp_hash->pid;
free(proxyapp_hash);
}

JSONPARSE("local_port")
{
memcpy(localport_buf, val, vlen);
Expand Down
2 changes: 2 additions & 0 deletions proximac-cli/jconf.h
Expand Up @@ -25,6 +25,8 @@ typedef struct {
uint16_t proximac_port;
int pid;
int total_process_num;
int vpn_mode;
int proxyapp_hash;
} conf_t;

extern void read_conf(char* configfile, conf_t* conf);
Expand Down
51 changes: 33 additions & 18 deletions proximac-cli/local.c
Expand Up @@ -118,8 +118,7 @@ static void remote_read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* b
wr->req.data = server_ctx;
unsigned char username_len = strlen(conf.username);
unsigned char password_len = strlen(conf.password);
int len = 1 /* fixed 1 byte */ + 2 /* two bytes indicate the length of username and password */ + username_len + password_len;
int offset = 0;
int len = 1 /* fixed 1 byte */ + 2 /* 2 bytes for username and password */ + username_len + password_len;
char* socks5req = malloc(len);
socks5req[0] = 0x01; /* version of auth */
socks5req[1] = username_len;
Expand Down Expand Up @@ -297,13 +296,12 @@ static void server_read_cb(uv_stream_t* stream, ssize_t nread, const uv_buf_t* b
server_ctx->buf = tmpbuf;
}
else {
server_ctx->buf_len = NULL;
server_ctx->buf_len = 0;
server_ctx->buf = NULL;
}

free(buf->base);

LOGD("server_ctx %x addrlen = %d addr = %s port = %d", server_ctx, server_ctx->addrlen, server_ctx->remote_addr, server_ctx->port);

struct sockaddr_in remote_addr;
memset(&remote_addr, 0, sizeof(remote_addr));
Expand Down Expand Up @@ -353,11 +351,10 @@ int tell_kernel_to_hook()
struct ctl_info ctl_info;
struct sockaddr_ctl sc;
errno_t retval = 0;
LOGI("tell kernel");

gSocket = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
if (gSocket < 0) {
LOGE("socket SYSPROTO_CONTROL");
LOGI("socket() failed.");
exit(EXIT_FAILURE);
}

Expand All @@ -377,16 +374,28 @@ int tell_kernel_to_hook()
sc.sc_unit = 0;

if (connect(gSocket, (struct sockaddr*)&sc, sizeof(struct sockaddr_ctl))) {
LOGE("connect");
LOGI("Connection to kernel failed. The kernel module may not be correctly loaded.");
exit(EXIT_FAILURE);
}

int tmp = 0;
retval = setsockopt(gSocket, SYSPROTO_CONTROL, PROXIMAC_ON, &tmp, sizeof(tmp));

int vpn_mode = 0;
if (conf.vpn_mode == 1)
vpn_mode = 1;

retval = setsockopt(gSocket, SYSPROTO_CONTROL, PROXIMAC_ON, &vpn_mode, sizeof(vpn_mode));
if (retval) {
LOGE("setsockopt failure PROXIMAC_ON");
return retval;
}

if (vpn_mode == 1) {
retval = setsockopt(gSocket, SYSPROTO_CONTROL, NOT_TO_HOOK, &conf.proxyapp_hash, sizeof(conf.proxyapp_hash));
if (retval) {
LOGE("setsockopt failure NOT_TO_HOOK");
return retval;
}
}

struct pid* pid_tmp = NULL;
int pidset_checksum = 0;
Expand Down Expand Up @@ -422,7 +431,10 @@ int tell_kernel_to_hook()
return retval;
}

LOGI("pid_num = %d", pid_num);
if (conf.vpn_mode == 1)
LOGI("All traffic will be redirected to this SOCKS5 proxy");
else
LOGI("The total number of process that will be hooked = %d", pid_num);

return retval;
}
Expand Down Expand Up @@ -451,7 +463,7 @@ int main(int argc, char** argv)
{
int c, option_index = 0, daemon = 0;
char* configfile = NULL;
char* logfile_path = "/tmp/proximac.log";
char* logfile_path = "./proximac.log";
RB_INIT(&pid_list);
opterr = 0;
static struct option long_options[] = {
Expand Down Expand Up @@ -481,6 +493,10 @@ int main(int argc, char** argv)
usage();
exit(EXIT_FAILURE);
}

if (log_to_file) {
USE_LOGFILE(logfile_path);
}

if (configfile) {
read_conf(configfile, &conf);
Expand All @@ -497,8 +513,7 @@ int main(int argc, char** argv)
if (daemon == 1)
init_daemon();

if (log_to_file)
USE_LOGFILE(logfile_path);


struct sockaddr_in bind_addr;
loop = malloc(sizeof *loop);
Expand All @@ -508,16 +523,16 @@ int main(int argc, char** argv)
uv_tcp_init(loop, &listener->handle);
uv_tcp_nodelay(&listener->handle, 1);

r = uv_ip4_addr(conf.proximac_listen_address, conf.proximac_port, &bind_addr);
r = uv_ip4_addr("127.0.0.1", conf.proximac_port, &bind_addr);
if (r)
LOGE("address error");
LOGE("Translate address error");
r = uv_tcp_bind(&listener->handle, (struct sockaddr*)&bind_addr, 0);
if (r)
LOGI("bind error");
LOGI("Bind error");
r = uv_listen((uv_stream_t*)&listener->handle, 128 /*backlog*/, server_accept_cb);
if (r)
LOGI("listen error port");
LOGI("Listening on %s:%d", conf.proximac_listen_address, conf.proximac_port);
LOGI("Listen error");
LOGI("Listening on %d", conf.proximac_port);

signal(SIGPIPE, SIG_IGN);
uv_signal_t sigint, sigstp, sigkil;
Expand Down
3 changes: 3 additions & 0 deletions proximac-cli/local.h
Expand Up @@ -7,6 +7,8 @@
#define CTL_INIT 0x01
#define CTL_NORMAL 0

#define LOCALHOST "127.0.0.1"

// packet related MACROs
#define MAX_PKT_SIZE 8192
#define ID_LEN 4
Expand All @@ -30,6 +32,7 @@
#define HOOK_PID 2
#define PIDLIST_STATUS 3
#define PROXIMAC_OFF 4
#define NOT_TO_HOOK 5

#include "tree.h"

Expand Down
125 changes: 18 additions & 107 deletions proximac-cli/utils.h
Expand Up @@ -7,6 +7,7 @@
#include <unistd.h>
#include <uv.h>
#include <signal.h>

extern FILE * logfile;

#if __GNUC__ >= 3
Expand Down Expand Up @@ -47,135 +48,45 @@ extern FILE * logfile;
assert(0); \
} while(0)

#ifdef XCODE_DEBUG
#define LOGI(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, " %s INFO: " format "\n", timestr, \
## __VA_ARGS__); \
fflush(stderr); \
} \
while (0)
#else
#define LOGI(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, "\x1b[32m %s INFO: \e[0m" format "\n", \
timestr,## __VA_ARGS__); \
fflush(stderr); \
} \
while (0)
#endif

#ifdef XCODE_DEBUG
#define LOGW(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, " %s WARN: " format "\n", \
timestr,## __VA_ARGS__); \
fflush(stderr); \
} \
while (0)
#else
#define LOGW(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
if (logfile != NULL) { \
fprintf(logfile, " %s WARN: " format "\n", \
timestr,## __VA_ARGS__); \
fflush(logfile); \
} \
else { \
fprintf(stderr, "\x1b[33m %s WARN: \e[0m" format "\n", \
timestr,## __VA_ARGS__); \
fflush(stderr); \
} \
} \
while (0)
#endif

#ifdef XCODE_DEBUG
#define LOGD(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, " %s INFO: " format "\n", timestr, \
## __VA_ARGS__); \
fflush(stderr); \
} \
while (0)
#else
#define LOGD(format, ...) \
do { \
if (logfile != NULL) { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(logfile, " %s INFO: " format "\n", timestr, \
## __VA_ARGS__); \
fflush(logfile); \
} \
else { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, "\x1b[32m %s INFO: \e[0m" format "\n", timestr, \
## __VA_ARGS__); \
fflush(stderr); \
} \
} \
while (0)
#endif

#define FATAL(format, ...) \
#define _LOG(color, tag, format, ...) \
do { \
if (logfile != NULL) { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(logfile, " %s FATAL: " format "\n", timestr, \
fprintf(logfile, " %s " tag ": " format "\n", timestr, \
## __VA_ARGS__); \
fflush(logfile); \
} \
else { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, "\x1b[31m %s FATAL: \e[0m" format "\n", timestr, \
fprintf(stderr, color " %s " tag ": \e[0m" format "\n", timestr, \
## __VA_ARGS__); \
fflush(stderr); \
} \
exit(EXIT_FAILURE); \
} \
while (0)

#define LOGE(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
if (logfile != NULL) { \
fprintf(logfile, " %s ERROR: " format "\n", \
timestr,## __VA_ARGS__); \
fflush(logfile); \
} \
else { \
fprintf(stderr, "\x1b[31m %s ERROR: \e[0m" format "\n", \
timestr,## __VA_ARGS__); \
fflush(stderr); \
} \
} \
#define LOGI(format, ...) \
do { \
time_t now = time(NULL); \
char timestr[20]; \
strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \
fprintf(stderr, "\x1b[32m" " %s INFO: \e[0m" format "\n", timestr, \
## __VA_ARGS__); \
fflush(stderr); \
} \
while (0)

#define LOGD(format, ...) _LOG("\x1b[32m", "Debug", format, ##__VA_ARGS__)
#define LOGW(format, ...) _LOG("\x1b[33m", "Warning", format, ##__VA_ARGS__)
#define LOGE(format, ...) _LOG("\x1b[31m", "Error", format, ##__VA_ARGS__)
#define FATAL(format, ...) _LOG("\x1b[31m", "Fatal", format, ##__VA_ARGS__)

#define SHOW_BUFFER(buf, len) \
do { \
for (int i = 0; i<len;i++) \
Expand Down

0 comments on commit 28c2c05

Please sign in to comment.