From 911c932ec426ee45c9380bd26714c4bb505a3a88 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sun, 23 Mar 2025 12:47:10 +0800 Subject: Use a global struct with version and source info --- .gitignore | 1 + Makefile | 7 ++++++- README.md | 14 +++++--------- flags.go | 4 ++-- global.go | 10 ++++++++++ main.go | 6 +++--- tmpl.go | 13 +++++++++---- 7 files changed, 36 insertions(+), 19 deletions(-) create mode 100644 global.go diff --git a/.gitignore b/.gitignore index 4345043..f595ae8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +/version.go /powxy diff --git a/Makefile b/Makefile index 1810daf..a75b758 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,7 @@ -powxy: *.go +powxy: *.go version.go go build -o powxy + +version.go: + printf 'package main\n\nfunc init() {\n\tglobal.Version = "%s"\n}\n' $(shell git describe --tags --always --dirty) > $@ + +.PHONY: version.go diff --git a/README.md b/README.md index 43f0feb..13492dc 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,14 @@ a bit unnecessary. - Currently we round times to the nearest week for persistence, but this could cause issues if a user completes the challenge at the end of a rounded week. -## Installation +## Build -You need a working Go installation. Then either - -``` -go install go.lindenii.runxiyu.org/powxy@latest -``` - -or +You need a working Go installation. ``` git clone ssh://forge.lindenii.runxiyu.org/powxy/:/repos/powxy/ cd powxy -go install +make ``` ## Usage @@ -49,6 +43,8 @@ Usage of ./powxy: address to listen on (default ":8081") -secondary trust X-Forwarded-For headers + -source string + url to the source code (default "https://https://forge.lindenii.runxiyu.org/powxy/:/repos/powxy/") -upstream string destination url base to proxy to (default "http://127.0.0.1:8080") ``` diff --git a/flags.go b/flags.go index abb1ace..8b16708 100644 --- a/flags.go +++ b/flags.go @@ -6,14 +6,14 @@ package main import "flag" var ( - difficulty uint listenAddr string destHost string secondary bool ) func init() { - flag.UintVar(&difficulty, "difficulty", 17, "leading zero bits required for the challenge") + flag.UintVar(&global.NeedBits, "difficulty", 17, "leading zero bits required for the challenge") + flag.StringVar(&global.SourceURL, "source", "https://https://forge.lindenii.runxiyu.org/powxy/:/repos/powxy/", "url to the source code") flag.StringVar(&listenAddr, "listen", ":8081", "address to listen on") flag.StringVar(&destHost, "upstream", "http://127.0.0.1:8080", "destination url base to proxy to") flag.BoolVar(&secondary, "secondary", false, "trust X-Forwarded-For headers") diff --git a/global.go b/global.go new file mode 100644 index 0000000..0e2bf81 --- /dev/null +++ b/global.go @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: AGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu + +package main + +var global = struct { + NeedBits uint + SourceURL string + Version string +}{} diff --git a/main.go b/main.go index a2113ec..4844f48 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,8 @@ import ( type tparams struct { UnsignedTokenBase64 string - NeedBits uint Message string + Global any } func main() { @@ -42,7 +42,7 @@ func main() { _ = tmpl.Execute(writer, tparams{ UnsignedTokenBase64: base64.StdEncoding.EncodeToString(expectedToken[:sha256.Size]), Message: message, - NeedBits: difficulty, + Global: global, }) } @@ -75,7 +75,7 @@ func main() { h.Write(expectedToken[:sha256.Size]) h.Write(nonce) ck := h.Sum(nil) - if !validateBitZeros(ck, difficulty) { + if !validateBitZeros(ck, global.NeedBits) { authPage("Your submission was incorrect, or your session has expired while submitting.") return } diff --git a/tmpl.go b/tmpl.go index 81659ea..a2e2c5b 100644 --- a/tmpl.go +++ b/tmpl.go @@ -46,13 +46,15 @@ for the JavaScript code in this page. +

Proof of Work Challenge

-

This site is protected by Powxy.

+
+

This site is protected by Powxy{{ if .Global.Version }} {{ .Global.Version }}{{ end }}.

You must complete this proof of work challenge before you could access this site.

{{- if .Message }}

{{ .Message }}

{{- end }} -

Select an nonce shorter than or equal to 32 bytes, such that when it is appended to the decoded form of the following base64 string, and a SHA-256 hash is taken as a whole, the first {{ .NeedBits }} bits of the SHA-256 hash are zeros. Within one octet, higher bits are considered to be in front of lower bits.

+

Select an nonce shorter than or equal to 32 bytes, such that when it is appended to the decoded form of the following base64 string, and a SHA-256 hash is taken as a whole, the first {{ .Global.NeedBits }} bits of the SHA-256 hash are zeros. Within one octet, higher bits are considered to be in front of lower bits.

{{ .UnsignedTokenBase64 }}

@@ -69,11 +71,14 @@ Please note that if your submission is successful, you will be given a cookie th

Offline solver program
` + html.EscapeString(solverProgram) + `
+

+Powxy is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. Powxy is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU Affero General Public License for more details. The source code is available at {{ .Global.SourceURL }} +