Add minimal working module

This commit is contained in:
2026-01-19 09:41:39 +01:00
parent 1234688921
commit 3d42ad0c19
7 changed files with 127 additions and 102 deletions
+2
View File
@@ -1,6 +1,8 @@
CompileFlags: CompileFlags:
Add: Add:
- "-I../nginx/src/core" - "-I../nginx/src/core"
- "-I../nginx/src/http"
- "-I../nginx/src/event"
- "-I../nginx/objs" - "-I../nginx/objs"
- "-I../nginx/src/os/unix" - "-I../nginx/src/os/unix"
- "-I/opt/homebrew/Cellar/pcre2/10.47/include" - "-I/opt/homebrew/Cellar/pcre2/10.47/include"
+13 -15
View File
@@ -1,17 +1,15 @@
ngx-http-pow: ngx-http-pow.c default: build
cc -o ngx-http-pow \
-I../nginx/src/core \ build: ngx_http_pow.c
-I../nginx/objs \ cd ../nginx; make -f Makefile modules; cd ../ngx-http-pow
-I../nginx/src/os/unix \
-I/opt/homebrew/Cellar/pcre2/10.47/include \ .PHONY: configure
-I/opt/homebrew/Cellar/openssl@3/3.6.0/include \ configure:
-L../nginx/objs/src/core \ ../nginx/auto/configure \
-L/opt/homebrew/Cellar/pcre2/10.47/lib \ --prefix=/Users/jona/repos/ngx-pow/nginx/install \
-L/opt/homebrew/Cellar/openssl@3/3.6.0/lib \ --with-debug \
-lssl \ --add-dynamic-module=/Users/jona/repos/ngx-pow/ngx-http-pow
-lcrypto \
ngx-http-pow.c
.PHONY: run .PHONY: run
run: ngx-http-pow run: build
./ngx-http-pow ../nginx/objs/nginx -c "$(PWD)/ngx_http_pow.conf" -g "daemon off;"
+7
View File
@@ -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
+13
View File
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>PoW Shield</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="">
<body>
<h1>
PoW Shield
</h1>
This is a test page.
</body>
</html>
-87
View File
@@ -1,87 +0,0 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_string.h>
#include <openssl/hmac.h>
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#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;
}
+66
View File
@@ -0,0 +1,66 @@
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
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;
}
+26
View File
@@ -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;
}
}
}