{"id":48513179,"url":"https://github.com/lloydzhou/pow-captcha","last_synced_at":"2026-04-07T18:01:45.658Z","repository":{"id":293007558,"uuid":"981888713","full_name":"lloydzhou/pow-captcha","owner":"lloydzhou","description":"simple pow-captcha server based on redis-module and openressty","archived":false,"fork":false,"pushed_at":"2025-05-20T08:45:14.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-20T09:47:22.534Z","etag":null,"topics":["capture","openresty","pow","redis-module"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lloydzhou.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-05-12T04:00:10.000Z","updated_at":"2025-05-20T08:45:18.000Z","dependencies_parsed_at":"2025-05-13T11:02:07.054Z","dependency_job_id":null,"html_url":"https://github.com/lloydzhou/pow-captcha","commit_stats":null,"previous_names":["lloydzhou/pow-captcha"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/lloydzhou/pow-captcha","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lloydzhou%2Fpow-captcha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lloydzhou%2Fpow-captcha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lloydzhou%2Fpow-captcha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lloydzhou%2Fpow-captcha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lloydzhou","download_url":"https://codeload.github.com/lloydzhou/pow-captcha/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lloydzhou%2Fpow-captcha/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31522574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-07T16:28:08.000Z","status":"ssl_error","status_checked_at":"2026-04-07T16:28:06.951Z","response_time":105,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["capture","openresty","pow","redis-module"],"created_at":"2026-04-07T18:01:39.085Z","updated_at":"2026-04-07T18:01:45.589Z","avatar_url":"https://github.com/lloydzhou.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PoW Captcha Service\n\n## English\n\nThis project provides a lightweight, efficient, and simple Proof-of-Work (PoW) captcha service. It is built using a custom Redis module, OpenResty, and corresponding JavaScript code.\n\nThe Redis module supports the following three commands:\n1.  `POW.CHALLENGE \u003cdifficulty\u003e`: Creates a new PoW challenge.\n2.  `POW.VERIFY \u003cchallengeId\u003e \u003cnonce\u003e \u003chash\u003e`: Verifies a PoW solution.\n3.  `POW.TOKEN \u003ctoken\u003e`: Checks if a verification token is valid.\n\nOpenResty acts as a wrapper around the Redis module, exposing HTTP endpoints for the captcha functionality, while the JavaScript handles the client-side PoW computation.\n\nThis setup offers a simple yet effective way to protect your services from bots.\n\n---\n\n## Quick Start\n\ndocker run -p 8888:80 lloydzhou/pow-captcha\n\n---\n\n## Usage Example\n\nThere are multiple ways to integrate the PoW Captcha:\n\n**1. Automatic Rendering (Recommended)**\n\nThe easiest way is to declare a `div` element in your HTML with the class `pow-captcha`. The SDK will automatically find these divs, render a hidden `input` field within them, and populate it with the PoW token.\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n  \u003cmeta charset=\"UTF-8\"\u003e\n  \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n  \u003ctitle\u003ePOWCaptcha Auto Render Demo\u003c/title\u003e\n  \u003cscript src=\"/sdk.js\"\u003e\u003c/script\u003e\n  \u003cscript\u003e\n    // Optional callback functions\n    function myCaptchaSuccessCallback(token) {\n      console.log('Captcha success, token:', token);\n      // You can handle the token here, e.g., enable a submit button\n      document.getElementById('submit-button').disabled = false;\n    }\n    function myCaptchaErrorCallback(error) {\n      console.error('Captcha failed:', error);\n      // You can handle the error here\n    }\n  \u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n  \u003cform id=\"myForm\" action=\"/submit-data\" method=\"post\"\u003e\n    \u003c!-- Other form fields --\u003e\n    \u003clabel for=\"username\"\u003eUsername:\u003c/label\u003e\n    \u003cinput type=\"text\" id=\"username\" name=\"username\"\u003e\n    \u003cbr\u003e\n\n    \u003c!-- PoW Captcha container --\u003e\n    \u003c!-- The SDK will automatically create a hidden input named \"pow-captcha-token\" inside this div --\u003e\n    \u003cdiv class=\"pow-captcha\"\n         data-callback=\"myCaptchaSuccessCallback\"\n         data-error-callback=\"myCaptchaErrorCallback\"\n         data-timeout=\"45000\"\u003e\n      \u003c!-- You can place a loading indicator or message here --\u003e\n      Loading captcha...\n    \u003c/div\u003e\n    \u003cbr\u003e\n\n    \u003cbutton type=\"submit\" id=\"submit-button\" disabled\u003eSubmit\u003c/button\u003e\n  \u003c/form\u003e\n\n  \u003cscript\u003e\n    // When the form is submitted, the hidden input (pow-captcha-token) will be submitted with it\n    // document.getElementById('myForm').addEventListener('submit', function(event) {\n    //   const tokenInput = this.querySelector('input[name=\"pow-captcha-token\"]');\n    //   if (!tokenInput || !tokenInput.value) {\n    //     event.preventDefault(); // Prevent submission if no token (optional)\n    //     alert('Please wait for the captcha to complete.');\n    //   }\n    // });\n  \u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nThe `pow-captcha` `div` supports the following `data-*` attributes:\n-   `data-callback`: (Optional) The name of a global JavaScript function to call upon successful PoW. The token will be passed as its first argument.\n-   `data-error-callback`: (Optional) The name of a global JavaScript function to call upon PoW failure. The error object will be passed as its first argument.\n-   `data-timeout`: (Optional) Timeout in milliseconds for the PoW challenge. Defaults to 30000 (30 seconds).\n-   `data-endpoint`: (Optional) The base URL of your PoW Captcha service. Defaults to the origin of `sdk.js`.\n\n**2. Manual Rendering**\n\nIf you need more fine-grained control, you can call the `window.POW.render(containerElement, options)` function.\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n  \u003cmeta charset=\"UTF-8\"\u003e\n  \u003ctitle\u003ePOWCaptcha Manual Render Demo\u003c/title\u003e\n  \u003cscript src=\"/sdk.js\"\u003e\u003c/script\u003e\n  \u003cscript\u003e\n    function myCallback(token) {\n      console.log('Manual render success, token:', token);\n      document.getElementById('token-display').innerText = 'Token obtained: ' + token;\n    }\n    function myErrorCallback(error) {\n      console.error('Manual render failed:', error);\n      document.getElementById('token-display').innerText = 'Failed to obtain token: ' + error.message;\n    }\n\n    // Execute after DOM is loaded\n    document.addEventListener('DOMContentLoaded', function() {\n      const captchaContainer = document.getElementById('my-captcha-container');\n      if (captchaContainer) {\n        window.POW.render(captchaContainer, {\n          // Options object is similar to the POW() function's\n          // timeout: 45000, // milliseconds\n          // endpoint: 'http://your-pow-service.com'\n          // Note: If data-callback, data-error-callback, data-timeout, data-endpoint are present on the container element,\n          // they will override options passed here unless not defined in options.\n          // For clarity, define callbacks either via data-* attributes or solely via options.\n        });\n      }\n    });\n  \u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n  \u003cdiv id=\"my-captcha-container\" data-callback=\"myCallback\" data-error-callback=\"myErrorCallback\"\u003e\n    \u003c!-- Manual rendering will happen here --\u003e\n  \u003c/div\u003e\n  \u003cp id=\"token-display\"\u003e\u003c/p\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\nThe second argument to `window.POW.render`, the `options` object, is similar to the `options` object for the `window.POW()` function described below. Additionally, `data-*` attributes on the container element (like `data-callback`, `data-timeout`) are also considered and will generally take precedence over corresponding settings in the `options` object if present.\n\n**3. Calling `POW()` Directly**\n\nYou can also call the `window.POW()` function directly. It returns a Promise that resolves with the PoW token upon success.\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n  \u003cmeta charset=\"UTF-8\"\u003e\n  \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n  \u003ctitle\u003ePOWCaptcha Demo\u003c/title\u003e\n  \u003c!-- Assuming sdk.js is served from the root of your PoW Captcha service --\u003e\n  \u003cscript src=\"/sdk.js\"\u003e\u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n  \u003cscript\u003e\n    // Call the POW function\n    // You can pass options like endpoint and timeout\n    // e.g., window.POW({ endpoint: 'http://your-pow-captcha-service.com', timeout: 60000 })\n    window.POW().then(function(token) {\n      console.log('POWCaptcha token:', token);\n      // Use the token to verify on your backend\n      // For example, make an AJAX request to your server with this token\n      // fetch('/your-protected-endpoint', {\n      //   method: 'POST',\n      //   headers: {\n      //     'Content-Type': 'application/json',\n      //     'X-POW-TOKEN': token\n      //   },\n      //   body: JSON.stringify({ /* your data */ })\n      // });\n      document.write(`\u003ch3\u003eSuccessfully obtained POWCaptcha token: ${token}\u003c/h3\u003e`);\n      // For demonstration, this opens a new tab to verify the token directly with the PoW service\n      // In a real application, you would send this token to your backend for verification.\n      // Your backend would then call the /token endpoint of the PoW service.\n      // window.open('/token?token=' + token, '_blank');\n    }).catch(function(error) {\n      console.error('POWCaptcha error:', error);\n      document.write(`\u003ch3\u003eFailed to obtain POWCaptcha token: ${error.message}\u003c/h3\u003e`);\n    });\n  \u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\nThe `POW()` function accepts an optional `options` object:\n-   `endpoint`: (Optional) The base URL of your PoW Captcha service. Defaults to the origin of `sdk.js`.\n-   `timeout`: (Optional) Timeout in milliseconds for the PoW challenge. Defaults to 30000 (30 seconds).\n\nOn your server-side, when a user submits a form or performs an action protected by PoW Captcha, you should take the received token (for automatic or manual rendering, it's typically in a hidden input named `pow-captcha-token`) and verify it by making a request to the `/token?token=\u003cTOKEN\u003e` endpoint of your PoW Captcha service. If the service responds with \"VALID\", the token is good.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flloydzhou%2Fpow-captcha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flloydzhou%2Fpow-captcha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flloydzhou%2Fpow-captcha/lists"}