Skip to content

Commit

Permalink
feat: add parameter to auth server offline page
Browse files Browse the repository at this point in the history
gateway ip, gateway port and proto

Signed-off-by: Dengfeng Liu <[email protected]>
  • Loading branch information
liudf0716 committed Sep 9, 2024
1 parent 317ced7 commit 48ab48e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 18 deletions.
98 changes: 87 additions & 11 deletions src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,74 @@ process_apple_wisper(struct evhttp_request *req, const char *mac, const char *re

}

static char *
replace_substrings(const char *str, const char **old_subs, const char **new_subs, int count)
{
char *result;
int i, j, total_new_len = 0, total_old_len = 0, occurrences = 0;

// Calculate the total length of new and old substrings
for (i = 0; i < count; i++) {
total_new_len += strlen(new_subs[i]);
total_old_len += strlen(old_subs[i]);
}

// Count occurrences of each old substring
for (i = 0; str[i] != '\0'; i++) {
for (j = 0; j < count; j++) {
if (strstr(&str[i], old_subs[j]) == &str[i]) {
occurrences++;
i += strlen(old_subs[j]) - 1;
break;
}
}
}

// Allocate memory for the result string
result = (char *)malloc(strlen(str) + occurrences * (total_new_len - total_old_len) + 1);
if (!result) return NULL;
memset(result, 0, strlen(str) + occurrences * (total_new_len - total_old_len) + 1);

i = 0;
while (*str) {
for (j = 0; j < count; j++) {
if (strstr(str, old_subs[j]) == str) {
strcpy(&result[i], new_subs[j]);
i += strlen(new_subs[j]);
str += strlen(old_subs[j]);
break;
}
}
if (j == count) {
result[i++] = *str++;
}
}
result[i] = '\0';
return result;
}

static struct evbuffer *
replace_evbuffer_content(struct evbuffer *evb, const char **old_subs, const char **new_subs, int count)
{
struct evbuffer *new_evb = evbuffer_new();
if (!new_evb) return NULL;

char *content = evbuffer_pullup(evb, -1);
char *new_content = replace_substrings(content, old_subs, new_subs, count);
if (!new_content) {
evbuffer_free(new_evb);
return NULL;
}

evbuffer_add(new_evb, new_content, strlen(new_content));
free(new_content);
return new_evb;
}

// define old_subs and new_subs for replace_substrings
static char *old_subs[] = {"$GATEWAY_IP$", "$GATEWAY_PORT$", "$PROTO$"};
static char *new_subs[] = {NULL, NULL, NULL};

/**
* @brief reply client error of gw internet offline or auth server offline
*
Expand All @@ -162,11 +230,12 @@ process_apple_wisper(struct evhttp_request *req, const char *mac, const char *re
* other: auth server offline
*/
void
ev_http_reply_client_error(struct evhttp_request *req, enum reply_client_error_type type)
ev_http_reply_client_error(struct evhttp_request *req, enum reply_client_error_type type, const char *ip, const char *port, const char *proto)
{
struct evbuffer *evb = evbuffer_new();
struct evbuffer *evb;
switch(type) {
case INTERNET_OFFLINE:
evb = evbuffer_new();
// lock extern pthread_mutex_t g_resource_lock;
pthread_mutex_lock(&g_resource_lock);
// copy evb_internet_offline_page to evb
Expand All @@ -176,7 +245,10 @@ ev_http_reply_client_error(struct evhttp_request *req, enum reply_client_error_t
case AUTHSERVER_OFFLINE:
default:
pthread_mutex_lock(&g_resource_lock);
evbuffer_add(evb, evbuffer_pullup(evb_authserver_offline_page, -1), evbuffer_get_length(evb_authserver_offline_page));
new_subs[0] = ip;
new_subs[1] = port;
new_subs[2] = proto;
evb = replace_evbuffer_content(evb_authserver_offline_page, old_subs, new_subs, 3);
pthread_mutex_unlock(&g_resource_lock);
break;
}
Expand Down Expand Up @@ -281,15 +353,9 @@ ev_http_callback_404(struct evhttp_request *req, void *arg)
{
if (!is_online()) {
debug(LOG_INFO, "Internet is offline");
ev_http_reply_client_error(req, INTERNET_OFFLINE);
return;
}

if (!is_auth_online()) {
debug(LOG_INFO, "Auth server is offline");
ev_http_reply_client_error(req, AUTHSERVER_OFFLINE);
ev_http_reply_client_error(req, INTERNET_OFFLINE, NULL, NULL, NULL);
return;
}
}

char *remote_host = NULL;
uint16_t port;
Expand Down Expand Up @@ -318,6 +384,16 @@ ev_http_callback_404(struct evhttp_request *req, void *arg)
return;
}

if (!is_auth_online()) {
char gw_port[8] = {0};
snprintf(gw_port, sizeof(gw_port), "%d", config_get_config()->gw_port);
debug(LOG_INFO, "Auth server is offline");
ev_http_reply_client_error(req, AUTHSERVER_OFFLINE,
gw_setting->gw_address_v4?gw_setting->gw_address_v4:gw_setting->gw_address_v6,
gw_port, "http");
return;
}

char mac[MAC_LENGTH] = {0};
if (!br_arp_get_mac(gw_setting, remote_host, mac)) {
evhttp_send_error(req, 200, "Cant get client's mac by its ip");
Expand Down
2 changes: 1 addition & 1 deletion src/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void ev_http_send_redirect_to_auth(struct evhttp_request *, const char *, const
/** @brief send the web browser's page which redirect to auth server by js */
void ev_http_send_js_redirect(struct evhttp_request *, const char *);
/** @brief reply client error of gw internet offline or auth server offline */
void ev_http_reply_client_error(struct evhttp_request *, enum reply_client_error_type);
void ev_http_reply_client_error(struct evhttp_request *, enum reply_client_error_type, const char *, const char *, const char *);
/** @brief */
void ev_http_send_apple_redirect(struct evhttp_request *, const char *);
/** @brief send apple wisper detect request again */
Expand Down
17 changes: 11 additions & 6 deletions src/ssl_redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,10 @@ die_most_horribly_from_openssl_error (const char *func) {
static void
process_ssl_request_cb (struct evhttp_request *req, void *arg) {
if (!is_online()){
ev_http_reply_client_error(req, INTERNET_OFFLINE);
ev_http_reply_client_error(req, INTERNET_OFFLINE, NULL, NULL, NULL);
return;
}

if (!is_auth_online()) {
ev_http_reply_client_error(req, AUTHSERVER_OFFLINE);
return;
}

char *remote_host;
uint16_t port;
ev_http_connection_get_peer(evhttp_request_get_connection(req), &remote_host, &port);
Expand Down Expand Up @@ -98,6 +93,16 @@ process_ssl_request_cb (struct evhttp_request *req, void *arg) {
return;
}

if (!is_auth_online()) {
debug(LOG_INFO, "Auth server is offline");
char gw_https_port[8] = {0};
snprintf(gw_https_port, sizeof(gw_https_port), "%d", config_get_config()->gw_https_port);
ev_http_reply_client_error(req, AUTHSERVER_OFFLINE,
gw_setting->gw_address_v4?gw_setting->gw_address_v4:gw_setting->gw_address_v6,
gw_https_port, "https");
return;
}

char mac[MAC_LENGTH] = {0};
if(!br_arp_get_mac(gw_setting, remote_host, mac)) {
evhttp_send_error(req, 200, "Cant get client's mac by its ip");
Expand Down

0 comments on commit 48ab48e

Please sign in to comment.