1
0
mirror of https://github.com/coalaura/up.git synced 2025-07-17 21:44:35 +00:00
Files
up/client/main.go

131 lines
2.5 KiB
Go
Raw Normal View History

2025-06-20 03:27:36 +02:00
package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"os"
2025-06-20 16:29:11 +02:00
"path/filepath"
2025-06-20 03:27:36 +02:00
2025-06-20 17:10:03 +02:00
"github.com/coalaura/logger"
2025-06-20 03:27:36 +02:00
"github.com/urfave/cli/v3"
)
2025-06-20 17:10:03 +02:00
var (
Version = "dev"
log = logger.New().DetectTerminal().WithOptions(logger.Options{
NoTime: true,
NoLevel: true,
})
)
2025-06-20 03:27:36 +02:00
func main() {
app := &cli.Command{
Name: "up",
Usage: "UP client",
Version: Version,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "key",
Aliases: []string{"k"},
Usage: "private key file for authentication",
},
&cli.StringFlag{
Name: "file",
Aliases: []string{"f"},
Usage: "file to upload",
},
&cli.StringFlag{
Name: "target",
Aliases: []string{"t"},
Usage: "target to upload to",
},
},
Action: run,
EnableShellCompletion: true,
UseShortOptionHandling: true,
Suggest: true,
}
2025-06-20 17:10:03 +02:00
err := app.Run(context.Background(), os.Args)
log.MustPanic(err)
2025-06-20 03:27:36 +02:00
}
func run(_ context.Context, cmd *cli.Command) error {
2025-06-20 17:10:03 +02:00
log.Println("loading certificate store")
store, err := LoadCertificateStore()
if err != nil {
return fmt.Errorf("failed to load certificate store: %v", err)
}
client := NewPinnedClient(store)
2025-06-20 16:29:11 +02:00
path := cmd.String("key")
if path == "" {
2025-06-20 03:27:36 +02:00
return errors.New("missing private key")
}
2025-06-20 16:29:11 +02:00
kPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to get key path: %v", err)
}
2025-06-20 17:10:03 +02:00
log.Printf("using key %s\n", kPath)
2025-06-20 16:29:11 +02:00
path = cmd.String("file")
if path == "" {
2025-06-20 03:27:36 +02:00
return errors.New("missing file")
}
2025-06-20 16:29:11 +02:00
fPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to get file path: %v", err)
}
2025-06-20 17:10:03 +02:00
log.Printf("using file %s\n", fPath)
2025-06-20 16:29:11 +02:00
2025-06-20 03:27:36 +02:00
file, err := os.OpenFile(fPath, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("failed to open file: %v", err)
}
defer file.Close()
target := cmd.String("target")
if target == "" {
return errors.New("missing target")
}
2025-06-20 16:29:11 +02:00
target = fmt.Sprintf("https://%s", target)
2025-06-20 17:10:03 +02:00
log.Printf("using target %s\n", target)
2025-06-20 16:29:11 +02:00
log.Printf("loading key")
2025-06-20 03:27:36 +02:00
private, err := LoadPrivateKey(kPath)
if err != nil {
return fmt.Errorf("failed to load key: %v", err)
}
public := base64.StdEncoding.EncodeToString(private.PublicKey().Marshal())
2025-06-20 16:29:11 +02:00
log.Println("requesting challenge")
2025-06-20 03:27:36 +02:00
2025-06-20 17:10:03 +02:00
challenge, err := RequestChallenge(client, target, public)
2025-06-20 03:27:36 +02:00
if err != nil {
return err
}
2025-06-20 16:29:11 +02:00
log.Println("completing challenge")
2025-06-20 03:27:36 +02:00
2025-06-20 17:10:03 +02:00
response, err := CompleteChallenge(client, target, public, private, challenge)
2025-06-20 03:27:36 +02:00
if err != nil {
return err
}
2025-06-20 17:10:03 +02:00
return SendFile(client, target, response.Token, file)
2025-06-20 03:27:36 +02:00
}