RC-6
release-tag / release-image (push) Successful in 2m10s

This commit is contained in:
2026-08-10 19:44:17 +02:00
parent 551ed007e6
commit 185ccf1101
20 changed files with 875 additions and 124 deletions
+38 -7
View File
@@ -117,10 +117,38 @@ func (c *apiClient) do(ctx context.Context, method, path string, body, out any)
return nil
}
func leadingZeroBitsClient(b []byte) int {
n := 0
for _, x := range b {
if x == 0 {
n += 8
continue
}
for m := byte(0x80); m != 0 && x&m == 0; m >>= 1 {
n++
}
break
}
return n
}
func solveProofClient(challenge, cid string, bits int) string {
if bits <= 0 {
return ""
}
for i := uint64(0); ; i++ {
counter := strconv.FormatUint(i, 10)
h := sha256.Sum256([]byte("nh-pow-v1|" + challenge + "|" + cid + "|" + counter))
if leadingZeroBitsClient(h[:]) >= bits {
return counter
}
}
}
func (c *apiClient) login(ctx context.Context) error {
var ch struct {
ClientID string `json:"client_id"`
Challenge string `json:"challenge"`
ClientID string `json:"client_id"`
Challenge string `json:"challenge"`
ProofOfWorkBits int `json:"proof_of_work_bits"`
}
if err := c.do(ctx, http.MethodPost, "/api/auth/challenge", map[string]any{"public_jwk": c.id.PublicJWK}, &ch); err != nil {
return fmt.Errorf("challenge: %w", err)
@@ -133,10 +161,12 @@ func (c *apiClient) login(ctx context.Context) error {
Token string `json:"token"`
ClientID string `json:"client_id"`
}
pow := solveProofClient(ch.Challenge, ch.ClientID, ch.ProofOfWorkBits)
if err := c.do(ctx, http.MethodPost, "/api/auth/login", map[string]any{
"public_jwk": c.id.PublicJWK,
"challenge": ch.Challenge,
"signature": sig,
"public_jwk": c.id.PublicJWK,
"challenge": ch.Challenge,
"signature": sig,
"proof_of_work_counter": pow,
}, &lg); err != nil {
return fmt.Errorf("login: %w", err)
}
@@ -276,10 +306,11 @@ func (c *apiClient) dialWS(ctx context.Context, maxNodes int) (*websocket.Conn,
scheme = "wss"
}
q := url.Values{}
q.Set("token", c.token)
q.Set("max_nodes", strconv.Itoa(maxNodes))
wu := scheme + "://" + u.Host + "/api/ws?" + q.Encode()
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, wu, nil)
h := http.Header{}
h.Set("Authorization", "Bearer "+c.token)
conn, resp, err := websocket.DefaultDialer.DialContext(ctx, wu, h)
if err != nil && resp != nil {
b, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
_ = resp.Body.Close()
+36 -5
View File
@@ -83,6 +83,34 @@ func (c *apiClient) do(method, path string, body any, out any) error {
}
return nil
}
func leadingZeroBitsLoad(b []byte) int {
n := 0
for _, x := range b {
if x == 0 {
n += 8
continue
}
for m := byte(0x80); m != 0 && x&m == 0; m >>= 1 {
n++
}
break
}
return n
}
func solveProofLoad(challenge, cid string, bits int) string {
if bits <= 0 {
return ""
}
for i := uint64(0); ; i++ {
counter := fmt.Sprint(i)
h := sha256.Sum256([]byte("nh-pow-v1|" + challenge + "|" + cid + "|" + counter))
if leadingZeroBitsLoad(h[:]) >= bits {
return counter
}
}
}
func (c *apiClient) authn() error {
k, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
@@ -91,8 +119,9 @@ func (c *apiClient) authn() error {
c.key = k
j := auth.PublicJWK{Kty: "EC", Crv: "P-256", X: b64.EncodeToString(pad32(k.X)), Y: b64.EncodeToString(pad32(k.Y)), Ext: true}
var ch struct {
ClientID string `json:"client_id"`
Challenge string `json:"challenge"`
ClientID string `json:"client_id"`
Challenge string `json:"challenge"`
ProofOfWorkBits int `json:"proof_of_work_bits"`
}
if err = c.do("POST", "/api/auth/challenge", map[string]any{"public_jwk": j}, &ch); err != nil {
return err
@@ -102,7 +131,8 @@ func (c *apiClient) authn() error {
var lg struct {
Token string `json:"token"`
}
if err = c.do("POST", "/api/auth/login", map[string]any{"public_jwk": j, "challenge": ch.Challenge, "signature": sig}, &lg); err != nil {
pow := solveProofLoad(ch.Challenge, c.cid, ch.ProofOfWorkBits)
if err = c.do("POST", "/api/auth/login", map[string]any{"public_jwk": j, "challenge": ch.Challenge, "signature": sig, "proof_of_work_counter": pow}, &lg); err != nil {
return err
}
c.token = lg.Token
@@ -123,10 +153,11 @@ func (c *apiClient) ws(ctx context.Context, maxNodes int) (*websocket.Conn, erro
scheme = "wss"
}
q := url.Values{}
q.Set("token", c.token)
q.Set("max_nodes", fmt.Sprint(maxNodes))
wu := scheme + "://" + u.Host + "/api/ws?" + q.Encode()
conn, _, err := websocket.DefaultDialer.DialContext(ctx, wu, nil)
h := http.Header{}
h.Set("Authorization", "Bearer "+c.token)
conn, _, err := websocket.DefaultDialer.DialContext(ctx, wu, h)
return conn, err
}
+31 -4
View File
@@ -3,6 +3,7 @@ package main
import (
"bufio"
"context"
"fmt"
"log"
"net/http"
"os"
@@ -61,8 +62,26 @@ func loadDotEnv(path string) {
}
}
func validateSecurityConfig() error {
if strings.EqualFold(strings.TrimSpace(os.Getenv("ALLOW_INSECURE_DEV_DEFAULTS")), "1") {
return nil
}
jwt := strings.TrimSpace(os.Getenv("JWT_SECRET"))
if len(jwt) < 32 || jwt == "dev-secret-change-me" || jwt == "change-me-to-a-long-random-secret" {
return fmt.Errorf("JWT_SECRET must be set to a unique random value of at least 32 characters (or set ALLOW_INSECURE_DEV_DEFAULTS=1 for local development only)")
}
pass := strings.TrimSpace(os.Getenv("ADMIN_PASSWORD"))
if len(pass) < 16 || pass == "change-me" {
return fmt.Errorf("ADMIN_PASSWORD must be set to a unique value of at least 16 characters")
}
return nil
}
func main() {
loadDotEnv(".env")
if err := validateSecurityConfig(); err != nil {
log.Fatal(err)
}
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer cancel()
@@ -94,10 +113,17 @@ func main() {
srv := server.New(store, a, sm, hub, runtimeState, artifactDir, aw)
go srv.Scheduler(ctx)
httpSrv := &http.Server{Addr: env("HTTP_ADDR", ":8080"), Handler: srv.Routes(), ReadHeaderTimeout: 5 * time.Second}
publicSrv := &http.Server{Addr: env("HTTP_ADDR", ":8080"), Handler: srv.PublicRoutes(), ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 60 * time.Second}
adminSrv := &http.Server{Addr: env("ADMIN_HTTP_ADDR", ":8081"), Handler: srv.AdminRoutes(), ReadHeaderTimeout: 5 * time.Second, IdleTimeout: 60 * time.Second}
go func() {
log.Printf("listening on %s", httpSrv.Addr)
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("public listener on %s", publicSrv.Addr)
if err := publicSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
go func() {
log.Printf("admin listener on %s (do not expose publicly)", adminSrv.Addr)
if err := adminSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
@@ -105,5 +131,6 @@ func main() {
<-ctx.Done()
shutdown, done := context.WithTimeout(context.Background(), 10*time.Second)
defer done()
_ = httpSrv.Shutdown(shutdown)
_ = publicSrv.Shutdown(shutdown)
_ = adminSrv.Shutdown(shutdown)
}