diff --git a/.clangd b/.clangd index 801163e..7a07116 100644 --- a/.clangd +++ b/.clangd @@ -1,6 +1,8 @@ CompileFlags: Add: - "-I../nginx/src/core" + - "-I../nginx/src/http" + - "-I../nginx/src/event" - "-I../nginx/objs" - "-I../nginx/src/os/unix" - "-I/opt/homebrew/Cellar/pcre2/10.47/include" diff --git a/Makefile b/Makefile index fa0296d..081d20f 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,15 @@ -ngx-http-pow: ngx-http-pow.c - cc -o ngx-http-pow \ - -I../nginx/src/core \ - -I../nginx/objs \ - -I../nginx/src/os/unix \ - -I/opt/homebrew/Cellar/pcre2/10.47/include \ - -I/opt/homebrew/Cellar/openssl@3/3.6.0/include \ - -L../nginx/objs/src/core \ - -L/opt/homebrew/Cellar/pcre2/10.47/lib \ - -L/opt/homebrew/Cellar/openssl@3/3.6.0/lib \ - -lssl \ - -lcrypto \ - ngx-http-pow.c +default: build + +build: ngx_http_pow.c + cd ../nginx; make -f Makefile modules; cd ../ngx-http-pow + +.PHONY: configure +configure: + ../nginx/auto/configure \ + --prefix=/Users/jona/repos/ngx-pow/nginx/install \ + --with-debug \ + --add-dynamic-module=/Users/jona/repos/ngx-pow/ngx-http-pow .PHONY: run -run: ngx-http-pow - ./ngx-http-pow +run: build + ../nginx/objs/nginx -c "$(PWD)/ngx_http_pow.conf" -g "daemon off;" diff --git a/config b/config new file mode 100644 index 0000000..3bb9061 --- /dev/null +++ b/config @@ -0,0 +1,7 @@ +ngx_module_type=HTTP +ngx_module_name=ngx_http_pow +ngx_module_srcs="$ngx_addon_dir/ngx_http_pow.c" + +. auto/module + +ngx_addon_name=$ngx_module_name diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..09c6cca --- /dev/null +++ b/html/index.html @@ -0,0 +1,13 @@ + + + +PoW Shield + + + +

+ PoW Shield +

+ This is a test page. + + diff --git a/ngx-http-pow.c b/ngx-http-pow.c deleted file mode 100644 index e247744..0000000 --- a/ngx-http-pow.c +++ /dev/null @@ -1,87 +0,0 @@ -#include -#include -#include - -#include - -#include -#include -#include -#include - -#define NGX_HTTP_POW_RAND_LEN 64 - -typedef struct { - ngx_uint_t version; - ngx_uint_t created; - ngx_uint_t validity; - ngx_uint_t hardness; - u_char random[NGX_HTTP_POW_RAND_LEN]; - u_char hmac[EVP_MAX_MD_SIZE]; -} ngx_http_pow_challenge; - -/* -const u_char *ngx_http_pow_challenge_to_string(ngx_http_pow_challenge *c, - ngx_str_t *str) -{ - u_char random_hex[NGX_HTTP_POW_RAND_LEN * 2 + 1]; - u_char hmac_hex[EVP_MAX_MD_SIZE * 2 + 1]; - - ngx_hex_dump(random_hex, c->random, NGX_HTTP_POW_RAND_LEN); - random_hex[NGX_HTTP_POW_RAND_LEN * 2] = 0; - ngx_hex_dump(hmac_hex, c->hmac, EVP_MAX_MD_SIZE); - hmac_hex[EVP_MAX_MD_SIZE * 2] = 0; - - char out[1024] = { 0 }; - - sprintf(out, - "version=%lu, created=%lu, validity=%lu, hardness=%lu, random=%s, hmac=%s", - c->version, c->created, c->validity, c->hardness, random_hex, - hmac_hex); - - printf("%s\n", out); - - return NULL; -} -*/ - -const bool ngx_http_pow_sign_challenge(ngx_http_pow_challenge *c, ngx_str_t *k) -{ - size_t clen; - const EVP_MD *digest; - - clen = sizeof(ngx_http_pow_challenge) - EVP_MAX_MD_SIZE; - digest = EVP_sha256(); - - u_char *hmac = HMAC(digest, k->data, k->len, (const unsigned char *) c, - clen, c->hmac, NULL); - - return hmac == c->hmac; -} - -int main(int argc, char** argv) -{ - ngx_http_pow_challenge c = { - .version = 1, - .created = 1234567, - .validity = 300, - .hardness = 22, - .random = { 'a', 'b', 'c' }, - .hmac = { 0 } - }; - - ngx_str_t key = ngx_string("asdf"); - bool success = ngx_http_pow_sign_challenge(&c, &key); - printf("Success: %d\n", success); - printf("challenge:\n"); - printf(" version: %lu\n", c.version); - printf(" created: %lu\n", c.created); - printf(" validity: %lu\n", c.validity); - printf(" hardness: %lu\n", c.hardness); - printf(" random: %.*s\n", NGX_HTTP_POW_RAND_LEN, (char *) c.random); - printf(" random: %.*s\n", EVP_MAX_MD_SIZE, (char *) c.hmac); - - // ngx_http_pow_challenge_to_string(&c, NULL); - - return 0; -} diff --git a/ngx_http_pow.c b/ngx_http_pow.c new file mode 100644 index 0000000..243cd55 --- /dev/null +++ b/ngx_http_pow.c @@ -0,0 +1,66 @@ +#include +#include +#include + +static ngx_int_t ngx_http_pow_init(ngx_conf_t *cf); +static ngx_int_t ngx_http_pow_handler(ngx_http_request_t *r); + +static ngx_http_module_t ngx_http_pow_module_ctx = { + NULL, /* preconfiguration */ + ngx_http_pow_init, /* postconfiguration */ + + NULL, /* create main configuration */ + NULL, /* init main configuration */ + + NULL, /* create server configuration */ + NULL, /* merge server configuration */ + + NULL, /* create location configuration */ + NULL /* merge location configuration */ +}; + +ngx_module_t ngx_http_pow = { + NGX_MODULE_V1, + &ngx_http_pow_module_ctx, /* module context */ + NULL, /* module directives */ + NGX_HTTP_MODULE, /* module type */ + NULL, /* init master */ + NULL, /* init module */ + NULL, /* init process */ + NULL, /* init thread */ + NULL, /* exit thread */ + NULL, /* exit process */ + NULL, /* exit master */ + NGX_MODULE_V1_PADDING +}; + +static ngx_int_t +ngx_http_pow_handler(ngx_http_request_t *r) +{ + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, + "ngx_http_pow_handler was invoked"); + + return NGX_DECLINED; +} + + +static ngx_int_t +ngx_http_pow_init(ngx_conf_t *cf) +{ + ngx_http_handler_pt *h; + ngx_http_core_main_conf_t *cmcf; + + cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); + + h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); + if (h == NULL) { + return NGX_ERROR; + } + + *h = ngx_http_pow_handler; + + ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0, + "ngx_http_pow_init was invoked"); + + return NGX_OK; +} diff --git a/ngx_http_pow.conf b/ngx_http_pow.conf new file mode 100644 index 0000000..c8100a4 --- /dev/null +++ b/ngx_http_pow.conf @@ -0,0 +1,26 @@ +worker_processes 1; + +error_log stderr debug; +pid /tmp/nginx.pid; + +load_module "/Users/jona/repos/ngx-pow/nginx/objs/ngx_http_pow.so"; + +events { + worker_connections 1024; +} + +http { + access_log /dev/stdout; + + server { + listen 80; + server_name localhost; + + root /Users/jona/repos/ngx-pow/ngx-http-pow/html; + index index.html; + + location / { + try_files $uri $uri/ =404; + } + } +}