Skip to content
This repository has been archived by the owner on Jun 24, 2023. It is now read-only.

Commit

Permalink
[control-server] finish config integration
Browse files Browse the repository at this point in the history
  • Loading branch information
hugsy committed May 30, 2015
1 parent 2b60229 commit 490fbb6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 36 deletions.
1 change: 1 addition & 0 deletions config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define CFG_RESPONSE_PLUGIN_FUNCTION "proxenet_response_hook" // default name for hooking response function
#define CFG_DEFAULT_SSL_CLIENT_DOMAIN "*" // default domain to use the SSL client certificate (* means any)
#define CFG_DEFAULT_INTERCEPT_PATTERN "*" // default pattern to intercept (all)
#define CFG_CONTROL_SOCK_PATH "/tmp/proxenet-control-socket"


/********************************************************************************
Expand Down
15 changes: 12 additions & 3 deletions control-client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# -*- coding: utf-8 -*-
# -*- mode: python -*-

import argparse, socket, datetime, os, json, rlcompleter, readline
import argparse, socket, datetime, os, json, rlcompleter, readline, pprint


__author__ = "hugsy"
Expand All @@ -15,7 +15,7 @@
syntax: {3} [options] args
""".format(__version__, __licence__, __author__, __file__)

# the socket path can be modified in control-server.h
# the socket path can be modified in config.h.in
PROXENET_SOCKET_PATH = "/tmp/proxenet-control-socket"

def now():
Expand Down Expand Up @@ -81,7 +81,16 @@ def recv_until(sock, pattern=">>> "):

res = recv_until(cli)
data, prompt = res[:-4], res[-4:]
print data
try:
js = json.loads( data )
for k,v in js.iteritems():
if v.__class__.__name__ == "dict":
print("{}".format(k))
for a,b in v.iteritems(): print("\t{} -> {}".format(a,b))
else:
print("{} -> {}".format(k,v))
except:
print(data)
cmd = raw_input( prompt )
cli.send(cmd.strip()+"\n")
if cmd.strip() == "quit":
Expand Down
61 changes: 34 additions & 27 deletions control-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,18 +151,20 @@ static void help_cmd(sock_t fd, char *options, unsigned int nb_options)
struct command_t *cmd;
char msg[BUFSIZE];
int n;
bool first_iter = true;

(void) options;
(void) nb_options;

proxenet_write(fd, "{\"Command list\":{", 17);
for (cmd=known_commands; cmd && cmd->name; cmd++) {
if(first_iter) first_iter=false;
else proxenet_write(fd, ",", 1);
proxenet_xzero(msg, sizeof(msg));
n = snprintf(msg, sizeof(msg), "\"%s\": \"%s\",", cmd->name, cmd->desc);
n = snprintf(msg, sizeof(msg), "\"%s\": \"%s\"", cmd->name, cmd->desc);
proxenet_write(fd, msg, n);
}
proxenet_write(fd, "\"\":\"\"}", 6);

proxenet_write(fd, "}}", 2);
return;
}

Expand Down Expand Up @@ -579,11 +581,11 @@ static void plugin_config_cmd_list(sock_t fd)
n = snprintf(msg, BUFSIZE,
"{\"Configuration parameters\":"
"{"
" \"verbose\": %d,"
" \"state\": \"%s\","
" \"logfile\": \"%s\","
" \"intercept_pattern\": \"%s\","
" \"ssl_intercept\": \"%s\""
" \"verbose\": {\"value\": %d, \"type\": \"int\" },"
" \"state\": {\"value\": \"%s\", \"type\": \"int\" },"
" \"logfile\": {\"value\": \"%s\", \"type\": \"None\" },"
" \"intercept_pattern\": {\"value\": \"%s\", \"type\": \"str\" },"
" \"ssl_intercept\": {\"value\": \"%s\", \"type\": \"bool\" }"
"}"
"}"
,
Expand Down Expand Up @@ -631,14 +633,11 @@ static void config_cmd(sock_t fd, char *options, unsigned int nb_options)
return;
}

ptr = strtok(options, " \n");
if (!ptr){
ERR_MISSING_ARGUMENT_JSON(fd);
return;
}
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }

if (!strcmp(ptr, "intercept_pattern")){
ptr = strtok(options, " \n");
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
proxenet_xfree( cfg->intercept_pattern );
cfg->intercept_pattern = proxenet_xstrdup2(ptr);
Expand All @@ -648,7 +647,7 @@ static void config_cmd(sock_t fd, char *options, unsigned int nb_options)
}

if (!strcmp(ptr, "verbose")){
ptr = strtok(options, " \n");
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
cfg->verbose = atoi(ptr);
n = snprintf(msg, sizeof(msg), "{\"success\": \"Verbose is now '%d'\" }", cfg->verbose);
Expand All @@ -657,28 +656,36 @@ static void config_cmd(sock_t fd, char *options, unsigned int nb_options)
}

if (!strcmp(ptr, "ssl_intercept")){
ptr = strtok(options, " \n");
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
cfg->ssl_intercept = strcasecmp(ptr,"true")==0?true:false;
n = snprintf(msg, sizeof(msg), "{\"success\": \"SSL Intercept is set to %d\" }", cfg->ssl_intercept);
proxenet_write(fd, msg, n);
return;
}

if (!strcmp(ptr, "pause")){
proxy_state = SLEEPING;
n = snprintf(msg, sizeof(msg), "{\"success\": \""PROGNAME" is paused\" }");
proxenet_write(fd, msg, n);
return;
}
if (!strcmp(ptr, "state")){
ptr = strtok(NULL, " \n");
if (!ptr){ ERR_MISSING_ARGUMENT_JSON(fd); return; }
if (strcasecmp(ptr, "sleeping")==0 && proxy_state==ACTIVE){
proxy_state = SLEEPING;
n = snprintf(msg, sizeof(msg), "{\"success\": \""PROGNAME" is paused\" }");
proxenet_write(fd, msg, n);
return;
} else if (strcasecmp(ptr, "active")==0 && proxy_state==SLEEPING){
proxy_state = ACTIVE;
n = snprintf(msg, sizeof(msg), "{\"success\": \""PROGNAME" is unpaused\" }");
proxenet_write(fd, msg, n);
return;
} else {
n = snprintf(msg, sizeof(msg), "{\"success\": \"nothing to do\" }");
proxenet_write(fd, msg, n);
return;
}

if (!strcmp(ptr, "unpause")){
proxy_state = ACTIVE;
n = snprintf(msg, sizeof(msg), "{\"success\": \""PROGNAME" is unpaused\" }");
proxenet_write(fd, msg, n);
return;
}


}


Expand Down
3 changes: 1 addition & 2 deletions control-server.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef _CONTROL_SERVER_H
#define _CONTROL_SERVER_H

#include "config.h"
#include "core.h"
#include "socket.h"

#define MAX_CMD_LEN 1024

#define CONTROL_SOCK_PATH "/tmp/proxenet-control-socket"
#define CONTROL_MOTD "Welcome on "PROGNAME" control interface\nType `help` to list available commands\n"
#define CONTROL_PROMPT ">>> "
#define CONTROL_INVALID "{\"error\": \"Invalid command\"}"
Expand Down
31 changes: 29 additions & 2 deletions control-web.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,43 @@ def config():
js = json.loads( res )
title = js.keys()[0]
values = js[title]

# view config
html = """<div class="panel panel-default">"""
html += """<div class="panel-heading"><h3 class="panel-title">{}</h3></div>""".format( title )
html += """<div class="panel-body">"""
html += """<table class="table table-hover table-condensed">"""
html += """<tr><th>Setting</th><th>Value</th></tr>"""
for k,v in values.iteritems(): html += "<tr><td><code>{}</code></td><td>{}</td></tr>".format(k,v)
html += """<tr><th>Setting</th><th>Value</th><th>Type</th></tr>"""
for k,v in values.iteritems():
_val, _type = v["value"], v["type"]
html += "<tr><td><code>{}</code></td><td>{}</td><td>{}</td></tr>".format(k,_val,_type)
html += "</table></div></div>"""

# edit config
html += """<div class="panel panel-default">"""
html += """<div class="panel-heading"><h3 class="panel-title">Change Value</h3></div>"""
html += """<div class="panel-body"><form class="form-inline" action="/config/set" method="get">"""
html += """<div class="form-group"><select class="form-control" name="setting">"""
for k,v in values.iteritems():
html += """<option>{}</option>""".format( k )
html += """</select></div><div class="form-group"><label class="sr-only">Value</label><input class="form-control" name="value" placeholder="Value"></div><button type="submit" class="btn btn-default">Change Value</button>"""
html += """</form></div></div>"""

return build_html(body=html, title="proxenet Configuration", page="config")

@route('/config/set')
def config_set():
if not is_proxenet_running(): return build_html(body=not_running_html())
param = request.params.get("setting")
value = request.params.get("value")
print param, value
res = sr("config set {} {}".format(param, value))
js = json.loads( res )
retcode = js.keys()[0]
if retcode == "error":
return """<div class="alert alert-danger" role="alert">{}</div>""".format(res[retcode])
redirect("/config")
return

if __name__ == "__main__":
parser = argparse.ArgumentParser(usage = __usage__, description = __desc__)
Expand Down
2 changes: 1 addition & 1 deletion core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,6 @@ int proxenet_start()
close_socket(listening_socket);
close_socket(control_socket);

unlink(CONTROL_SOCK_PATH);
unlink(CFG_CONTROL_SOCK_PATH);
return 0;
}
3 changes: 2 additions & 1 deletion socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "errno.h"
#include "main.h"
#include "control-server.h"
#include "config.h"


/**
Expand All @@ -31,7 +32,7 @@ sock_t create_control_socket()
}

sun_local.sun_family = AF_UNIX;
strcpy(sun_local.sun_path, CONTROL_SOCK_PATH);
strcpy(sun_local.sun_path, CFG_CONTROL_SOCK_PATH);
unlink(sun_local.sun_path);

/* and bind+listen */
Expand Down

0 comments on commit 490fbb6

Please sign in to comment.