aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRunxi Yu <me@runxiyu.org>2025-03-23 12:47:10 +0800
committerRunxi Yu <me@runxiyu.org>2025-03-23 12:47:10 +0800
commit911c932ec426ee45c9380bd26714c4bb505a3a88 (patch)
tree5224b91b5622997ccfc882c2e4e0b22e97bc450b
parentRevert "Remove the trivial habitual makefile" (diff)
downloadpowxy-911c932ec426ee45c9380bd26714c4bb505a3a88.tar.gz
powxy-911c932ec426ee45c9380bd26714c4bb505a3a88.tar.zst
powxy-911c932ec426ee45c9380bd26714c4bb505a3a88.zip
Use a global struct with version and source info
-rw-r--r--.gitignore1
-rw-r--r--Makefile7
-rw-r--r--README.md14
-rw-r--r--flags.go4
-rw-r--r--global.go10
-rw-r--r--main.go6
-rw-r--r--tmpl.go13
7 files changed, 36 insertions, 19 deletions
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 <https://runxiyu.org>
+
+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.
</script>
</head>
<body>
+<header>
<h1>Proof of Work Challenge</h1>
-<p>This site is protected by <a href="https://forge.lindenii.runxiyu.org/powxy/:/repos/powxy/">Powxy</a>.</p>
+</header>
+<p>This site is protected by Powxy{{ if .Global.Version }} {{ .Global.Version }}{{ end }}.</p>
<p>You must complete this proof of work challenge before you could access this site.</p>
{{- if .Message }}
<p><strong>{{ .Message }}</strong></p>
{{- end }}
-<p>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.</p>
+<p>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.</p>
<p>{{ .UnsignedTokenBase64 }}</p>
<form method="POST">
<p>
@@ -69,11 +71,14 @@ Please note that if your submission is successful, you will be given a cookie th
<summary>Offline solver program</summary>
<pre>` + html.EscapeString(solverProgram) + `</pre>
</details>
+<p>
+Powxy is free software: you can redistribute it and/or modify it under the terms of the <a href="https://www.gnu.org/licenses/agpl-3.0.html">GNU Affero General Public License, version 3</a>, as published by the Free Software Foundation. Powxy is distributed in the hope that it will be useful, but <strong>without any warranty</strong>; without even the implied warranty of <strong>merchantability</strong> or <strong>fitness for a particular purpose</strong>. See the GNU Affero General Public License for more details. The source code is available at {{ .Global.SourceURL }}
+</p>
</body>
<script>
document.addEventListener("DOMContentLoaded", function() {
- let challenge_b64 = "{{.UnsignedTokenBase64}}";
- let difficulty = {{.NeedBits}};
+ let challenge_b64 = "{{ .UnsignedTokenBase64 }}";
+ let difficulty = {{ .Global.NeedBits }};
let form = document.querySelector("form");
let field = form.querySelector("input[name='powxy']");
let status_el = document.getElementById("solver_status");