add main project files

This commit is contained in:
2024-09-11 09:54:55 +02:00
parent 5f82ac25cb
commit 8fc677a17f
18 changed files with 735 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package common
import "github.com/urfave/cli/v2"
func Flags() []cli.Flag {
return []cli.Flag{}
}

View File

@ -0,0 +1,40 @@
package command
import (
"fmt"
"forge.cadoles.com/cadoles/altcha-server/internal/client"
"forge.cadoles.com/cadoles/altcha-server/internal/command/common"
"forge.cadoles.com/cadoles/altcha-server/internal/config"
"github.com/caarlos0/env/v11"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/logger"
)
func GenerateCommand() *cli.Command {
flags := common.Flags()
return &cli.Command{
Name: "generate",
Usage: "generate a challenge",
Flags: flags,
Action: func(ctx *cli.Context) error {
cfg := config.Config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
c := client.NewClient(cfg.HmacKey, cfg.MaxNumber, cfg.Algorithm, cfg.Salt, cfg.Expire, cfg.CheckExpire)
challenge, err := c.Generate()
if err != nil {
logger.Error(ctx.Context, err.Error())
return err
}
fmt.Printf("%+v\n", challenge)
return nil
},
}
}

49
internal/command/main.go Normal file
View File

@ -0,0 +1,49 @@
package command
import (
"context"
"fmt"
"os"
"sort"
"github.com/urfave/cli/v2"
)
func Main(commands ...*cli.Command) {
ctx := context.Background()
app := &cli.App {
Version: "1",
Name: "altcha-server",
Usage: "create challenges and validate solutions for atlcha captcha",
Commands: commands,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
EnvVars: []string{"ALTCHA_DEBUG"},
Value: false,
},
},
}
app.ExitErrHandler = func (ctx *cli.Context, err error) {
if err == nil {
return
}
debug := ctx.Bool("debug")
if !debug {
fmt.Printf("[ERROR] %v\n", err)
} else {
fmt.Printf("%+v", err)
}
}
sort.Sort(cli.FlagsByName(app.Flags))
sort.Sort(cli.CommandsByName(app.Commands))
if err := app.RunContext(ctx, os.Args); err != nil {
os.Exit(1)
}
}

30
internal/command/run.go Normal file
View File

@ -0,0 +1,30 @@
package command
import (
"fmt"
"forge.cadoles.com/cadoles/altcha-server/internal/api"
"forge.cadoles.com/cadoles/altcha-server/internal/command/common"
"forge.cadoles.com/cadoles/altcha-server/internal/config"
"github.com/caarlos0/env/v11"
"github.com/urfave/cli/v2"
)
func RunCommand() *cli.Command {
flags := common.Flags()
return &cli.Command{
Name: "run",
Usage: "run the atlcha api server",
Flags: flags,
Action: func(ctx *cli.Context) error {
cfg := config.Config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
api.NewServer(cfg).Run(ctx.Context)
return nil
},
}
}

47
internal/command/solve.go Normal file
View File

@ -0,0 +1,47 @@
package command
import (
"fmt"
"forge.cadoles.com/cadoles/altcha-server/internal/client"
"forge.cadoles.com/cadoles/altcha-server/internal/command/common"
"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 {
flags := common.Flags()
return &cli.Command{
Name: "solve",
Usage: "solve the challenge and return the solution",
Flags: flags,
Args: true,
ArgsUsage: "[CHALLENGE] [SALT]",
Action: func(ctx *cli.Context) error {
cfg := config.Config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
challenge := ctx.Args().Get(0)
salt := ctx.Args().Get(1)
c := client.NewClient(cfg.HmacKey, cfg.MaxNumber, cfg.Algorithm, salt, cfg.Expire, cfg.CheckExpire)
solution, err := c.Solve(challenge)
if err != nil {
logger.Error(ctx.Context, err.Error())
return nil
}
fmt.Printf("%+v\n", solution)
return nil
},
}
}

View File

@ -0,0 +1,56 @@
package command
import (
"fmt"
"strconv"
"forge.cadoles.com/cadoles/altcha-server/internal/client"
"forge.cadoles.com/cadoles/altcha-server/internal/command/common"
"forge.cadoles.com/cadoles/altcha-server/internal/config"
"github.com/altcha-org/altcha-lib-go"
"github.com/caarlos0/env/v11"
"github.com/urfave/cli/v2"
)
func VerifyCommand() *cli.Command {
flags := common.Flags()
return &cli.Command{
Name: "verify",
Usage: "verify the solution",
Flags: flags,
Args: true,
ArgsUsage: "[challenge] [salt] [signature] [solution]",
Action: func(ctx *cli.Context) error {
cfg := config.Config{}
if err := env.Parse(&cfg); err != nil {
fmt.Printf("%+v\n", err)
}
challenge := ctx.Args().Get(0)
salt := ctx.Args().Get(1)
signature := ctx.Args().Get(2)
solution, _ := strconv.ParseInt(ctx.Args().Get(3), 10, 64)
c := client.NewClient(cfg.HmacKey, cfg.MaxNumber, cfg.Algorithm, cfg.Salt, cfg.Expire, cfg.CheckExpire)
payload := altcha.Payload{
Algorithm: cfg.Algorithm,
Challenge: challenge,
Number: solution,
Salt: salt,
Signature: signature,
}
verified, err := c.VerifySolution(payload)
if err != nil {
return err
}
fmt.Print(verified)
return nil
},
}
}