// Package uberbringer wires the Überbringer daemon together and provides its // command-line interface. package uberbringer import ( "context" "crypto/rand" "encoding/base64" "fmt" "io" "time" "github.com/golang-jwt/jwt/v5" "github.com/liamg/tml" "github.com/spf13/cobra" "uberbringer/internal/pkg/config" ) // Project information shown by the banner, the version command and the API // index route. const ( Project = "Überbringer" Description = "EPSON Receipt Printer Server" Authors = "https://git.anomalous.dev/57_Wolve/uberbringer" copyright = "William Gill" ) // secretBytes is the size of generated API secrets: 256 bits, the HS256 hash // size. const secretBytes = 32 // BuildInfo is injected at build time through -ldflags. type BuildInfo struct { Version string Commit string // SentryDSN is the default DSN for release builds; sentry.dsn in the // config and the SENTRY_DSN environment variable take precedence. SentryDSN string } // Execute runs the command line given by args. func Execute(ctx context.Context, build BuildInfo, args []string) error { root := newRootCommand(build) root.SetArgs(args) return root.ExecuteContext(ctx) } func newRootCommand(build BuildInfo) *cobra.Command { var configPath string root := &cobra.Command{ Use: "uberbringer", Short: "Runs the Überbringer receipt printer daemon.", SilenceUsage: true, SilenceErrors: true, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { return serve(cmd.Context(), configPath, build, cmd.OutOrStdout()) }, } root.PersistentFlags().StringVarP(&configPath, "config", "c", "config.toml", "path to the daemon config file") root.AddCommand( newVersionCommand(build), newSecretCommand(), newTokenCommand(&configPath), ) return root } func newVersionCommand(build BuildInfo) *cobra.Command { return &cobra.Command{ Use: "version", Short: "Prints the version and exits.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { _, err := fmt.Fprintln(cmd.OutOrStdout(), versionLine(build)) return err }, } } func newSecretCommand() *cobra.Command { return &cobra.Command{ Use: "secret", Short: "Prints a new random API secret for api.secret in the config.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { _, err := fmt.Fprintln(cmd.OutOrStdout(), newSecret()) return err }, } } func newTokenCommand(configPath *string) *cobra.Command { var ( subject string ttl time.Duration ) cmd := &cobra.Command{ Use: "token", Short: "Prints a bearer token signed with the config's api.secret.", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { cfg, err := config.Load(*configPath) if err != nil { return err } token, err := signToken([]byte(cfg.API.Secret), subject, ttl, time.Now()) if err != nil { return err } _, err = fmt.Fprintln(cmd.OutOrStdout(), token) return err }, } cmd.Flags().StringVar(&subject, "subject", "uberbringer", "token subject, e.g. the client's name") cmd.Flags().DurationVar(&ttl, "ttl", 0, "token lifetime, e.g. 720h (0 means it never expires)") return cmd } // newSecret returns a random base64 API secret. crypto/rand.Read never fails // on supported platforms; it crashes the program rather than return weak bytes. func newSecret() string { b := make([]byte, secretBytes) _, _ = rand.Read(b) // documented to never return an error return base64.StdEncoding.EncodeToString(b) } // signToken returns an HS256 JWT for subject. A zero ttl means no expiry. func signToken(secret []byte, subject string, ttl time.Duration, now time.Time) (string, error) { if ttl < 0 { return "", fmt.Errorf("ttl must not be negative") } claims := jwt.RegisteredClaims{ Subject: subject, IssuedAt: jwt.NewNumericDate(now), } if ttl > 0 { claims.ExpiresAt = jwt.NewNumericDate(now.Add(ttl)) } signed, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(secret) if err != nil { return "", fmt.Errorf("signing token: %w", err) } return signed, nil } func versionLine(build BuildInfo) string { return fmt.Sprintf("%s | Version: %s Build: %s | Copyright © %d %s", Project, build.Version, build.Commit, time.Now().Year(), copyright) } // printBanner writes the startup banner. func printBanner(w io.Writer, build BuildInfo) { const rule = "-----------------------------------------------------------------------------------------------" // The banner is cosmetic; a failed write to the terminal is not worth // stopping the daemon for. _ = tml.Fprintf(w, "%s\n\n", rule) _ = tml.Fprintf(w, " %s\n\n", versionLine(build)) _ = tml.Fprintf(w, " Project: %s\n", Project) _ = tml.Fprintf(w, " Description: %s\n", Description) _ = tml.Fprintf(w, " Authors: %s\n\n", Authors) _ = tml.Fprintf(w, "%s\n", rule) }