Skip to content

Commit

Permalink
solve CORS problem
Browse files Browse the repository at this point in the history
Signed-off-by: staylightblow8 <[email protected]>
  • Loading branch information
liudf0716 committed Dec 23, 2023
1 parent 7e338ee commit a492cb1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
sudo apt-get install -y libevent-dev
sudo apt-get install -y lua5.1 lua5.1-dev
sudo apt-get install -y nftables
sudo apt-get install -y libnetfilter-queue-dev
- name: install libubox
run: |
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ endif()

add_executable(wdctlx ${src_wdctlx})
if(AW_FW3)
add_executable(wifidogx ${src_apfreewifidog} ${src_fw3})
add_executable(wifidogx ${src_apfreewifidog} ${src_fw3} ${src_dhcp})
target_link_libraries(wifidogx ${libs} ${fw3_libs} ${CURL_LIBRARIES})
else()
add_executable(wifidogx ${src_apfreewifidog} ${src_fw4} ${src_dhcp})
Expand Down
7 changes: 6 additions & 1 deletion src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ http_redir_loop(s_config *config)
http = evhttp_new(base);
if (!http) termination_handler(0);

evhttp_set_allowed_methods(http,
EVHTTP_REQ_GET |
EVHTTP_REQ_POST |
EVHTTP_REQ_OPTIONS);

struct wd_request_context *request_ctx = wd_request_context_new(
base, ssl, get_auth_server()->authserv_use_ssl);
if (!request_ctx) termination_handler(0);
Expand All @@ -465,7 +470,7 @@ http_redir_loop(s_config *config)
//evhttp_set_cb(http, "/wifidog/status", ev_http_callback_status, NULL);
evhttp_set_cb(http, "/wifidog/auth", ev_http_callback_auth, request_ctx);
//evhttp_set_cb(http, "/wifidog/disconnect", ev_http_callback_disconnect, request_ctx);
//evhttp_set_cb(http, "/wifidog/temporary_pass", ev_http_callback_temporary_pass, NULL);
evhttp_set_cb(http, "/wifidog/temporary_pass", ev_http_callback_temporary_pass, NULL);

evhttp_set_gencb(http, ev_http_callback_404, NULL);

Expand Down
16 changes: 15 additions & 1 deletion src/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ void
ev_http_callback_auth(struct evhttp_request *req, void *arg)
{
struct wd_request_context *context = (struct wd_request_context *)arg;

evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Origin", "*");
evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE,PUT");
if (evhttp_request_get_command(req) == EVHTTP_REQ_OPTIONS) {
evhttp_send_error(req, 204, "options success");
return;
}

char *token = ev_http_find_query(req, "token");
if (!token) {
evhttp_send_error(req, 200, "Invalid token");
Expand Down Expand Up @@ -436,6 +444,9 @@ ev_http_callback_disconnect(struct evhttp_request *req, void *arg)
const char *token = ev_http_find_query(req, "token");
const char *mac = ev_http_find_query(req, "mac");

evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Origin", "*");
evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE,PUT");

if (!token || !mac) {
debug(LOG_INFO, "Disconnect called without both token and MAC given");
evhttp_send_error(req, HTTP_OK, "Both the token and MAC need to be specified");
Expand Down Expand Up @@ -464,7 +475,10 @@ void
ev_http_callback_temporary_pass(struct evhttp_request *req, void *arg)
{
const char *mac = ev_http_find_query(req, "mac");


evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Origin", "*");
evhttp_add_header(evhttp_request_get_output_headers(req), "Access-Control-Allow-Methods", "GET,POST,OPTIONS,DELETE,PUT");

if (mac) {
debug(LOG_INFO, "Temporary passed %s", mac);
fw_set_mac_temporary(mac, 0);
Expand Down
6 changes: 5 additions & 1 deletion src/ssl_redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,11 @@ ssl_redirect_loop (char *gw_ip, t_https_server *https_server) {
debug (LOG_ERR, "couldn't create evhttp. Exiting.\n");
exit(EXIT_FAILURE);
}

evhttp_set_allowed_methods(http,
EVHTTP_REQ_GET |
EVHTTP_REQ_POST |
EVHTTP_REQ_OPTIONS);

SSL_CTX *ctx = SSL_CTX_new (SSLv23_server_method ());
SSL_CTX_set_options (ctx,
SSL_OP_SINGLE_DH_USE |
Expand Down

0 comments on commit a492cb1

Please sign in to comment.