altcha-server/internal/command/solve.go

54 lines
1.2 KiB
Go
Raw Normal View History

2024-09-11 09:54:55 +02:00
package command
import (
"fmt"
2024-09-13 09:12:16 +02:00
"time"
2024-09-11 09:54:55 +02:00
"forge.cadoles.com/cadoles/altcha-server/internal/client"
"forge.cadoles.com/cadoles/altcha-server/internal/config"
"github.com/caarlos0/env/v11"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/logger"
)
func SolveCommand() *cli.Command {
return &cli.Command{
Name: "solve",
Usage: "solve the challenge and return the solution",
Args: true,
ArgsUsage: "[CHALLENGE] [SALT]",
Action: func(ctx *cli.Context) error {
cfg := config.Config{}
if err := env.Parse(&cfg); err != nil {
2024-09-13 09:12:16 +02:00
logger.Error(ctx.Context, err.Error())
return err
2024-09-11 09:54:55 +02:00
}
challenge := ctx.Args().Get(0)
salt := ctx.Args().Get(1)
2024-09-13 09:12:16 +02:00
expirationDuration, err := time.ParseDuration(cfg.Expire+"s")
if err != nil {
logger.Error(ctx.Context, err.Error())
return err
}
2024-09-11 09:54:55 +02:00
2024-09-13 09:12:16 +02:00
client, err := client.New(cfg.HmacKey, cfg.MaxNumber, cfg.Algorithm, salt, expirationDuration, cfg.CheckExpire)
if err != nil {
logger.Error(ctx.Context, err.Error())
return err
}
2024-09-11 09:54:55 +02:00
2024-09-13 09:12:16 +02:00
solution, err := client.Solve(challenge)
2024-09-11 09:54:55 +02:00
if err != nil {
logger.Error(ctx.Context, err.Error())
return nil
}
fmt.Printf("%+v\n", solution)
return nil
},
}
}