diff --git a/cmd/puzzledbg/README.md b/cmd/puzzledbg/README.md index 82ce713a..9429ee97 100644 --- a/cmd/puzzledbg/README.md +++ b/cmd/puzzledbg/README.md @@ -3,3 +3,9 @@ Usage: ```bash curl -s -H "Origin: portal.privatecaptcha.com" https://api.privatecaptcha.com/puzzle?sitekey=abcdef | go run cmd/puzzledbg/main.go ``` + +For stub puzzle: + +```bash +curl -s https://api.privatecaptcha.com/puzzle?sitekey=aaaaaaaabbbbccccddddeeeeeeeeeeee | go run cmd/puzzledbg/main.go -solve +``` diff --git a/cmd/puzzledbg/main.go b/cmd/puzzledbg/main.go index fc265ced..b41e3f50 100644 --- a/cmd/puzzledbg/main.go +++ b/cmd/puzzledbg/main.go @@ -3,15 +3,26 @@ package main import ( "encoding/base64" "encoding/json" + "flag" "fmt" "io" "os" "strings" + "github.com/PrivateCaptcha/PrivateCaptcha/pkg/common" "github.com/PrivateCaptcha/PrivateCaptcha/pkg/puzzle" + "github.com/jackc/pgx/v5/pgtype" +) + +var ( + solveFlag = flag.Bool("solve", false, "Solve puzzle instead of printing") ) func main() { + flag.Parse() + + common.SetupTraceLogs() + data, err := io.ReadAll(os.Stdin) if err != nil { fmt.Fprintf(os.Stderr, "Error reading stdin: %v\n", err) @@ -33,11 +44,36 @@ func main() { os.Exit(3) } - out, err := json.MarshalIndent(p, "", " ") - if err != nil { - fmt.Fprintf(os.Stderr, "Error marshalling puzzle: %v\n", err) - os.Exit(4) - } + if *solveFlag { + solver := &puzzle.ComputeSolver{} + solutions, err := solver.Solve(p) + if err != nil { + fmt.Fprintf(os.Stderr, "Error solving puzzle: %v\n", err) + os.Exit(5) + } - fmt.Print(string(out)) + fmt.Printf("%s.%s", solutions.String(), responseStr) + } else { + m := make(map[string]interface{}) + propertyID := p.PropertyID() + var propertyUUID pgtype.UUID + propertyUUID.Valid = true + copy(propertyUUID.Bytes[:], propertyID[:]) + + m["PuzzleID"] = p.PuzzleID() + m["PropertyID"] = propertyUUID.String() + m["Difficulty"] = p.Difficulty() + m["SolutionsCount"] = p.SolutionsCount() + m["Expiration"] = p.Expiration() + m["IsStub"] = p.IsStub() + m["IsZero"] = p.IsZero() + + out, err := json.MarshalIndent(m, "", " ") + if err != nil { + fmt.Fprintf(os.Stderr, "Error marshalling puzzle: %v\n", err) + os.Exit(4) + } + + fmt.Print(string(out)) + } } diff --git a/pkg/api/server.go b/pkg/api/server.go index 2d76c4ae..5d23dd5a 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -320,6 +320,7 @@ func (s *Server) pcVerifyHandler(w http.ResponseWriter, r *http.Request) { payload, err := s.Verifier.ParseSolutionPayload(ctx, data) if err != nil { + slog.Log(ctx, common.LevelTrace, "Failed to parse solution payload", common.ErrAttr(err)) http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) return }