From ce41360604232b82825ae8507904b8a530acc126 Mon Sep 17 00:00:00 2001 From: Laura Date: Sat, 21 Jun 2025 00:09:08 +0200 Subject: [PATCH] combine server and client into one binary --- .github/workflows/release.yml | 6 ++-- client/certificates.go | 2 +- client/{main.go => client.go} | 32 +++--------------- client/hosts.go | 2 +- client/keys.go | 2 +- client/progress.go | 2 +- client/protocol.go | 2 +- main.go | 63 +++++++++++++++++++++++++++++++++++ server/certificate.go | 2 +- server/keys.go | 2 +- server/protocol.go | 2 +- server/rates.go | 2 +- server/{main.go => server.go} | 50 +++++++++------------------ 13 files changed, 94 insertions(+), 75 deletions(-) rename client/{main.go => client.go} (81%) create mode 100644 main.go rename server/{main.go => server.go} (67%) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5b540b6..bba9c8d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -16,7 +16,6 @@ jobs: matrix: goos: [windows, linux, darwin] goarch: [amd64, arm64] - type: [client, server] fail-fast: false steps: @@ -35,7 +34,7 @@ jobs: mkdir -p build [[ "${{ matrix.goos }}" == "windows" ]] && EXT=".exe" || EXT="" - OUT="build/up_${{ matrix.type }}_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}${EXT}" + OUT="build/up_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}${EXT}" GOOS=${{ matrix.goos }} \ GOARCH=${{ matrix.goarch }} \ @@ -44,13 +43,12 @@ jobs: -trimpath \ -buildvcs=false \ -ldflags "-s -w -X 'main.Version=${{ github.ref_name }}'" \ - -tags "${{ matrix.flavour }}" \ -o "$OUT" ./${{ matrix.type }} - name: Upload artifact uses: actions/upload-artifact@v4 with: - name: up_${{ matrix.type }}_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }} + name: up_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }} path: build/* release: diff --git a/client/certificates.go b/client/certificates.go index b9060bf..96c493d 100644 --- a/client/certificates.go +++ b/client/certificates.go @@ -1,4 +1,4 @@ -package main +package client import ( "bytes" diff --git a/client/main.go b/client/client.go similarity index 81% rename from client/main.go rename to client/client.go index 190ac8b..bacf9c4 100644 --- a/client/main.go +++ b/client/client.go @@ -1,4 +1,4 @@ -package main +package client import ( "context" @@ -13,40 +13,18 @@ import ( "github.com/urfave/cli/v3" ) -var ( - Version = "dev" +var log *logger.Logger +func Before(ctx context.Context, _ *cli.Command) (context.Context, error) { log = logger.New().DetectTerminal().WithOptions(logger.Options{ NoTime: true, NoLevel: true, }) -) -func main() { - app := &cli.Command{ - Name: "up", - Usage: "up client", - Version: Version, - ArgsUsage: " ", - UsageText: "up [options] ", - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "identity", - Aliases: []string{"i"}, - Usage: "private key file for authentication", - }, - }, - Action: run, - EnableShellCompletion: true, - UseShortOptionHandling: true, - Suggest: true, - } - - err := app.Run(context.Background(), os.Args) - log.MustPanic(err) + return ctx, nil } -func run(_ context.Context, cmd *cli.Command) error { +func Run(_ context.Context, cmd *cli.Command) error { args := cmd.Args().Slice() if len(args) != 2 { return errors.New("Usage: up [options] ") diff --git a/client/hosts.go b/client/hosts.go index 7a4e6aa..59a9cf2 100644 --- a/client/hosts.go +++ b/client/hosts.go @@ -1,4 +1,4 @@ -package main +package client import ( "os" diff --git a/client/keys.go b/client/keys.go index ed331a5..c91cef6 100644 --- a/client/keys.go +++ b/client/keys.go @@ -1,4 +1,4 @@ -package main +package client import ( "os" diff --git a/client/progress.go b/client/progress.go index acead8a..071fdf7 100644 --- a/client/progress.go +++ b/client/progress.go @@ -1,4 +1,4 @@ -package main +package client import ( "io" diff --git a/client/protocol.go b/client/protocol.go index fb398f9..1057725 100644 --- a/client/protocol.go +++ b/client/protocol.go @@ -1,4 +1,4 @@ -package main +package client import ( "bytes" diff --git a/main.go b/main.go new file mode 100644 index 0000000..aa8e472 --- /dev/null +++ b/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "context" + "fmt" + "os" + + "github.com/coalaura/up/client" + "github.com/coalaura/up/server" + "github.com/urfave/cli/v3" +) + +var Version = "dev" + +func main() { + app := &cli.Command{ + Name: "up", + Description: "fast, drop-in file transfer tool using HTTPS", + UsageText: "up [command options]", + Version: Version, + Commands: []*cli.Command{ + { + Name: "send", + Usage: "send a file to an up server", + Version: Version, + ArgsUsage: " ", + UsageText: "up send [options] ", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "identity", + Aliases: []string{"i"}, + Usage: "private key file for authentication", + }, + }, + Before: client.Before, + Action: client.Run, + }, + { + Name: "receive", + Usage: "receive files from up clients", + Flags: []cli.Flag{ + &cli.UintFlag{ + Name: "port", + Aliases: []string{"p"}, + Usage: "custom port", + Value: 7966, + }, + }, + Before: server.Before, + Action: server.Run, + }, + }, + EnableShellCompletion: true, + UseShortOptionHandling: true, + Suggest: true, + } + + err := app.Run(context.Background(), os.Args) + if err != nil { + fmt.Printf("error: %s", err) + os.Exit(1) + } +} diff --git a/server/certificate.go b/server/certificate.go index 70bd5a4..58a8876 100644 --- a/server/certificate.go +++ b/server/certificate.go @@ -1,4 +1,4 @@ -package main +package server import ( "crypto/rand" diff --git a/server/keys.go b/server/keys.go index 6bba1b2..3ef513d 100644 --- a/server/keys.go +++ b/server/keys.go @@ -1,4 +1,4 @@ -package main +package server import ( "crypto/rand" diff --git a/server/protocol.go b/server/protocol.go index 58f6c15..bc4c5e6 100644 --- a/server/protocol.go +++ b/server/protocol.go @@ -1,4 +1,4 @@ -package main +package server import ( "bytes" diff --git a/server/rates.go b/server/rates.go index 5dd3824..394d414 100644 --- a/server/rates.go +++ b/server/rates.go @@ -1,4 +1,4 @@ -package main +package server import ( "sync" diff --git a/server/main.go b/server/server.go similarity index 67% rename from server/main.go rename to server/server.go index 53b0e0f..21f7d35 100644 --- a/server/main.go +++ b/server/server.go @@ -1,11 +1,10 @@ -package main +package server import ( "context" "crypto/tls" "fmt" "net/http" - "os" "time" "github.com/coalaura/logger" @@ -24,56 +23,37 @@ const ( ) var ( - Version = "dev" - log = logger.New().DetectTerminal().WithOptions(logger.Options{ + log *logger.Logger + rates *RateLimiter + challenges *cache.Cache + sessions *cache.Cache +) + +func Before(ctx context.Context, _ *cli.Command) (context.Context, error) { + log = logger.New().DetectTerminal().WithOptions(logger.Options{ NoLevel: true, }) - challenges = cache.New(10*time.Second, time.Minute) - sessions = cache.New(10*time.Second, time.Minute) - rates = NewRateLimiter() -) + rates = NewRateLimiter() -func init() { + challenges = cache.New(10*time.Second, time.Minute) challenges.OnEvicted(func(_ string, entry interface{}) { challenge := entry.(internal.ChallengeEntry) rates.Dec(challenge.Client) }) + sessions = cache.New(10*time.Second, time.Minute) sessions.OnEvicted(func(_ string, entry interface{}) { session := entry.(internal.SessionEntry) rates.Dec(session.Client) }) + + return ctx, nil } -func main() { - app := &cli.Command{ - Name: "up", - Usage: "up server", - Version: Version, - Flags: []cli.Flag{ - &cli.UintFlag{ - Name: "port", - Aliases: []string{"p"}, - Usage: "custom port", - Value: 7966, - }, - }, - Action: run, - EnableShellCompletion: true, - UseShortOptionHandling: true, - Suggest: true, - } - - err := app.Run(context.Background(), os.Args) - log.MustPanic(err) -} - -func run(_ context.Context, cmd *cli.Command) error { - log.Printf("up server %s\n", Version) - +func Run(_ context.Context, cmd *cli.Command) error { port := cmd.Uint("port") if port <= 0 || port > 65534 { port = 7966