Skip to content

Commit

Permalink
add ws client thread
Browse files Browse the repository at this point in the history
Signed-off-by: staylightblow8 <[email protected]>
  • Loading branch information
liudf0716 committed Feb 21, 2024
1 parent ca80159 commit 69b04e6
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(src_apfreewifidog
wd_client.c
wd_util.c
mqtt_thread.c
ws_thread.c
)


Expand Down
2 changes: 1 addition & 1 deletion src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
#define DEFAULT_AUTHSERVMSGPATHFRAGMENT "gw_message?"
#define DEFAULT_AUTHSERVPINGPATHFRAGMENT "ping/?"
#define DEFAULT_AUTHSERVAUTHPATHFRAGMENT "auth/?"
#define DEFAULT_AUTHSERVWSPATHFRAGMENT "ws/"
#define DEFAULT_AUTHSERVWSPATHFRAGMENT "apfree-ws"
/** Note that DEFAULT_AUTHSERVSSLNOPEERVER must be 0 or 1, even if the config file syntax is yes or no */
#define DEFAULT_AUTHSERVSSLPEERVER 1 /* 0 means: Enable peer verification */
#define DEFAULT_DELTATRAFFIC 0 /* 0 means: Enable peer verification */
Expand Down
7 changes: 1 addition & 6 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@
#include "wd_util.h"
#include "wd_client.h"
#include "dhcp_cpi.h"
#include "ws_thread.h"

#ifdef APFREE_WIFIDOG_WEBSSH
#include "ws_thread.h"
#endif

struct evbuffer *evb_internet_offline_page, *evb_authserver_offline_page;
struct redir_file_buffer *wifidog_redir_html;
Expand Down Expand Up @@ -410,15 +408,12 @@ threads_init(s_config *config)
}
pthread_detach(tid_wdctl);

#ifdef APFREE_WIFIDOG_WEBSSH
/* Start control thread */
result = pthread_create(&tid_ws, NULL, (void *)start_ws_thread, NULL);
if (result != 0) {
debug(LOG_INFO, "Failed to create a new thread (ws)");
}
pthread_detach(tid_ws);
#endif

}

static void
Expand Down
25 changes: 3 additions & 22 deletions src/ws_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "ws_thread.h"
#include "debug.h"
#include "conf.h"
#include "ssh_client.h"

#define MAX_OUTPUT (512*1024)
#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
Expand All @@ -35,7 +34,6 @@ static char *fixed_key = "dGhlIHNhbXBsZSBub25jZQ==";
static char *fixed_accept = "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=";
static bool upgraded = false;

struct libssh_client *ssh_client;

static void
ws_send(struct evbuffer *buf, const char *msg, const size_t len)
Expand Down Expand Up @@ -119,20 +117,14 @@ ws_receive(struct evbuffer *buf, struct evbuffer *output){


const unsigned char* mask_key = data + header_len - 4;

debug(LOG_DEBUG, "ws receive data_len %d mask %d head_len %d payload_len\n",
data_len, mask, header_len, payload_len);
for(int i = 0; mask && i < payload_len; i++)
data[header_len + i] ^= mask_key[i%4];


if(opcode == 0x01) {
// redirect data to ssh client
ssh_client_channel_write(ssh_client, data, payload_len);
char *ssh_resp = ssh_client_channel_read(ssh_client, CHANNEL_READ_TIMTOUT);
if(ssh_resp) {
// send ssh response to ws server
ws_send(output, ssh_resp, strlen(ssh_resp));
free(ssh_resp);
}
// TODO:
}

evbuffer_drain(buf, header_len + payload_len);
Expand Down Expand Up @@ -183,16 +175,6 @@ ws_read_cb(struct bufferevent *b_ws, void *ctx)
return;
}

// first connect ssh server
if (ssh_client_connect(ssh_client)) {
char *ssh_resp = ssh_client_create_channel(ssh_client, NULL);
if (ssh_resp) {
struct evbuffer *output = bufferevent_get_output(b_ws);
ws_send(output, ssh_resp, strlen(ssh_resp));
free(ssh_resp);
}
}

upgraded = true;
} else {
ws_receive(input, bufferevent_get_output(b_ws));
Expand All @@ -217,7 +199,6 @@ start_ws_thread(void *arg)
t_auth_serv *auth_server = get_auth_server();
ws_base = event_base_new();
ws_dnsbase = evdns_base_new(ws_base, 1);
ssh_client = new_libssh_client(NULL, 0, '#', NULL, "password");

struct bufferevent *ws_bev = bufferevent_socket_new(ws_base, -1, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS);

Expand Down

0 comments on commit 69b04e6

Please sign in to comment.