Skip to content

Commit 52ca6ce

Browse files
committed
Update readme & quick cleanup
1 parent a41acf4 commit 52ca6ce

6 files changed

+50
-29
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project(nginx_js C)
44
set(CMAKE_C_STANDARD 11)
55

66

7-
add_library(nginx_js SHARED library.c)
7+
add_library(nginx_js SHARED ngx_http_js_challenge.c)
88

99
target_include_directories(
1010
nginx_js

README.md

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
11

2+
## ngx_http_js_challenge_module
3+
4+
![GitHub](https://img.shields.io/github/license/simon987/ngx_http_js_challenge_module.svg)
5+
[![CodeFactor](https://www.codefactor.io/repository/github/simon987/ngx_http_js_challenge_module/badge)](https://www.codefactor.io/repository/github/simon987/ngx_http_js_challenge_module)
6+
[![Development snapshots](https://ci.simon987.net/app/rest/builds/buildType(JsChallenge_Build)/statusIcon)](https://files.simon987.net/artifacts/JsChallenge/Build/)
7+
8+
Simple javascript proof-of-work based access for Nginx with virtually no overhead.
9+
10+
Easy installation: just add `load_module /path/to/ngx_http_js_challenge_module.so;` to your
11+
`nginx.conf` file and follow the [configuration instructions](#configuration).
12+
13+
14+
![throughput.png](throughput.png)
15+
16+
17+
### Configuration
18+
19+
//todo
20+
21+
22+
### Build from source
23+
224
//todo
325

426
```bash

build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ make modules
1414
#cp objs/ "${WD}"
1515
)
1616

17-
#rm /test/*.so
18-
mv /home/simon/Downloads/nginx-1.16.1/objs/ngx_http_hello_world_module.so /test/module.so
17+
rm /test/*.so
18+
mv /home/simon/Downloads/nginx-1.16.1/objs/ngx_http_js_challenge_module.so /test/module.so
1919
chown -R www-data /test/
2020
systemctl restart nginx

config

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
ngx_addon_name=ngx_http_hello_world_module
1+
ngx_addon_name=ngx_http_js_challenge_module
22

33
if test -n "$ngx_module_link"; then
44
ngx_module_type=HTTP
5-
ngx_module_name=ngx_http_hello_world_module
6-
ngx_module_srcs="$ngx_addon_dir/library.c"
5+
ngx_module_name=ngx_http_js_challenge_module
6+
ngx_module_srcs="$ngx_addon_dir/ngx_http_js_challenge.c"
77
. auto/module
88
else
9-
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
10-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/library.c"
9+
HTTP_MODULES="$HTTP_MODULES ngx_http_js_challenge_module"
10+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_js_challenge.c"
1111
fi

library.c ngx_http_js_challenge.c

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,51 @@
11
#include <stdio.h>
22
#include "ngx_http.c"
33

4-
static ngx_int_t ngx_http_hello_world(ngx_conf_t *cf);
4+
static ngx_int_t ngx_http_js_challenge(ngx_conf_t *cf);
55

66
static char *setup1(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
77

8-
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r);
8+
static ngx_int_t ngx_http_js_challenge_handler(ngx_http_request_t *r);
99

1010

11-
static ngx_command_t ngx_http_hello_world_commands[] = {
11+
static ngx_command_t ngx_http_js_challenge_commands[] = {
1212

1313
{ngx_string("hello_world"),
1414

1515
// NGX_CONF_TAKE1, for 1 arg etc
1616
// NGX_CONF_FLAG for boolean
1717
NGX_HTTP_LOC_CONF | NGX_HTTP_SRV_CONF | NGX_CONF_NOARGS,
1818

19-
2019
setup1, /* configuration setup function */
2120
0, /* No offset. Only one context is supported. */
2221
0, /* No offset when storing the module configuration on struct. */
2322
NULL},
24-
25-
ngx_null_command /* command termination */
23+
ngx_null_command
2624
};
2725

28-
/* The hello world string. */
29-
//static u_char ngx_hello_world[] = HELLO_WORLD;
30-
31-
/* The module context. */
32-
static ngx_http_module_t ngx_http_hello_world_module_ctx = {
26+
/**
27+
* Module context
28+
*/
29+
static ngx_http_module_t ngx_http_js_challenge_module_ctx = {
3330
NULL, /* preconfiguration */
34-
ngx_http_hello_world, /* postconfiguration */
31+
ngx_http_js_challenge, /* postconfiguration */
3532

3633
NULL, /* create main configuration */
3734
NULL, /* init main configuration */
3835

3936
NULL, /* create server configuration */
4037
NULL, /* merge server configuration */
4138

39+
//todo
4240
NULL, /* create location configuration */
4341
NULL /* merge location configuration */
4442
};
4543

4644
/* Module definition. */
47-
ngx_module_t ngx_http_hello_world_module = {
45+
ngx_module_t ngx_http_js_challenge_module = {
4846
NGX_MODULE_V1,
49-
&ngx_http_hello_world_module_ctx, /* module context */
50-
ngx_http_hello_world_commands, /* module directives */
47+
&ngx_http_js_challenge_module_ctx, /* module context */
48+
ngx_http_js_challenge_commands, /* module directives */
5149
NGX_HTTP_MODULE, /* module type */
5250
NULL, /* init master */
5351
NULL, /* init module */
@@ -60,7 +58,7 @@ ngx_module_t ngx_http_hello_world_module = {
6058
};
6159

6260
__always_inline
63-
void buf2hex(const unsigned char *buf, size_t buflen, char *hex_string) {
61+
static void buf2hex(const unsigned char *buf, size_t buflen, char *hex_string) {
6462
static const char hexdig[] = "0123456789ABCDEF";
6563

6664
const unsigned char *p;
@@ -90,7 +88,7 @@ static const u_char JS_SOLVER[] =
9088
" document.cookie = 'res=' + c + i + ';';"
9189
" window.location.reload();"
9290
" break;"
93-
" };"
91+
" }"
9492
" i++;"
9593
" }"
9694
"</script>Hello";
@@ -155,6 +153,7 @@ int verify_response(int32_t bucket, ngx_str_t addr, const char *secret, ngx_str_
155153
* ^ offset 24
156154
*/
157155

156+
//todo also check if the response is too large
158157
if (response.len <= SHA1_STR_LEN) {
159158
return -1;
160159
}
@@ -199,7 +198,7 @@ int get_cookie(ngx_http_request_t *r, ngx_str_t *name, ngx_str_t *value) {
199198
return -1;
200199
}
201200

202-
static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) {
201+
static ngx_int_t ngx_http_js_challenge_handler(ngx_http_request_t *r) {
203202

204203
//TODO: If the bucket is less than 5sec away from the next one, accept both current and latest bucket
205204

@@ -245,11 +244,11 @@ static ngx_int_t ngx_http_hello_world_handler(ngx_http_request_t *r) {
245244
static char *setup1(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
246245
// ngx_http_core_loc_conf_t *clcf; /* pointer to core location configuration */
247246
// clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
248-
// clcf->handler = ngx_http_hello_world_handler;
247+
// clcf->handler = ngx_http_js_challenge_handler;
249248
return NGX_CONF_OK;
250249
}
251250

252-
static ngx_int_t ngx_http_hello_world(ngx_conf_t *cf) {
251+
static ngx_int_t ngx_http_js_challenge(ngx_conf_t *cf) {
253252

254253
ngx_http_handler_pt *h;
255254
ngx_http_core_main_conf_t *cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
@@ -260,7 +259,7 @@ static ngx_int_t ngx_http_hello_world(ngx_conf_t *cf) {
260259
return NGX_ERROR;
261260
}
262261

263-
*h = ngx_http_hello_world_handler;
262+
*h = ngx_http_js_challenge_handler;
264263

265264
return NGX_OK;
266265
}

throughput.png

26.9 KB
Loading

0 commit comments

Comments
 (0)