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

combine server and client into one binary

This commit is contained in:
Laura
2025-06-21 00:09:08 +02:00
parent 6633087578
commit ce41360604
13 changed files with 94 additions and 75 deletions

View File

@ -16,7 +16,6 @@ jobs:
matrix: matrix:
goos: [windows, linux, darwin] goos: [windows, linux, darwin]
goarch: [amd64, arm64] goarch: [amd64, arm64]
type: [client, server]
fail-fast: false fail-fast: false
steps: steps:
@ -35,7 +34,7 @@ jobs:
mkdir -p build mkdir -p build
[[ "${{ matrix.goos }}" == "windows" ]] && EXT=".exe" || EXT="" [[ "${{ 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 }} \ GOOS=${{ matrix.goos }} \
GOARCH=${{ matrix.goarch }} \ GOARCH=${{ matrix.goarch }} \
@ -44,13 +43,12 @@ jobs:
-trimpath \ -trimpath \
-buildvcs=false \ -buildvcs=false \
-ldflags "-s -w -X 'main.Version=${{ github.ref_name }}'" \ -ldflags "-s -w -X 'main.Version=${{ github.ref_name }}'" \
-tags "${{ matrix.flavour }}" \
-o "$OUT" ./${{ matrix.type }} -o "$OUT" ./${{ matrix.type }}
- name: Upload artifact - name: Upload artifact
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: up_${{ matrix.type }}_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }} name: up_${{ github.ref_name }}_${{ matrix.goos }}_${{ matrix.goarch }}
path: build/* path: build/*
release: release:

View File

@ -1,4 +1,4 @@
package main package client
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package main package client
import ( import (
"context" "context"
@ -13,40 +13,18 @@ import (
"github.com/urfave/cli/v3" "github.com/urfave/cli/v3"
) )
var ( var log *logger.Logger
Version = "dev"
func Before(ctx context.Context, _ *cli.Command) (context.Context, error) {
log = logger.New().DetectTerminal().WithOptions(logger.Options{ log = logger.New().DetectTerminal().WithOptions(logger.Options{
NoTime: true, NoTime: true,
NoLevel: true, NoLevel: true,
}) })
)
func main() { return ctx, nil
app := &cli.Command{
Name: "up",
Usage: "up client",
Version: Version,
ArgsUsage: "<file> <host>",
UsageText: "up [options] <file> <host>",
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) func Run(_ context.Context, cmd *cli.Command) error {
log.MustPanic(err)
}
func run(_ context.Context, cmd *cli.Command) error {
args := cmd.Args().Slice() args := cmd.Args().Slice()
if len(args) != 2 { if len(args) != 2 {
return errors.New("Usage: up [options] <file> <host>") return errors.New("Usage: up [options] <file> <host>")

View File

@ -1,4 +1,4 @@
package main package client
import ( import (
"os" "os"

View File

@ -1,4 +1,4 @@
package main package client
import ( import (
"os" "os"

View File

@ -1,4 +1,4 @@
package main package client
import ( import (
"io" "io"

View File

@ -1,4 +1,4 @@
package main package client
import ( import (
"bytes" "bytes"

63
main.go Normal file
View File

@ -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> [command options]",
Version: Version,
Commands: []*cli.Command{
{
Name: "send",
Usage: "send a file to an up server",
Version: Version,
ArgsUsage: "<file> <host>",
UsageText: "up send [options] <file> <host>",
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)
}
}

View File

@ -1,4 +1,4 @@
package main package server
import ( import (
"crypto/rand" "crypto/rand"

View File

@ -1,4 +1,4 @@
package main package server
import ( import (
"crypto/rand" "crypto/rand"

View File

@ -1,4 +1,4 @@
package main package server
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package main package server
import ( import (
"sync" "sync"

View File

@ -1,11 +1,10 @@
package main package server
import ( import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"net/http" "net/http"
"os"
"time" "time"
"github.com/coalaura/logger" "github.com/coalaura/logger"
@ -24,56 +23,37 @@ const (
) )
var ( var (
Version = "dev" 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{ log = logger.New().DetectTerminal().WithOptions(logger.Options{
NoLevel: true, 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{}) { challenges.OnEvicted(func(_ string, entry interface{}) {
challenge := entry.(internal.ChallengeEntry) challenge := entry.(internal.ChallengeEntry)
rates.Dec(challenge.Client) rates.Dec(challenge.Client)
}) })
sessions = cache.New(10*time.Second, time.Minute)
sessions.OnEvicted(func(_ string, entry interface{}) { sessions.OnEvicted(func(_ string, entry interface{}) {
session := entry.(internal.SessionEntry) session := entry.(internal.SessionEntry)
rates.Dec(session.Client) rates.Dec(session.Client)
}) })
return ctx, nil
} }
func main() { func Run(_ context.Context, cmd *cli.Command) error {
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)
port := cmd.Uint("port") port := cmd.Uint("port")
if port <= 0 || port > 65534 { if port <= 0 || port > 65534 {
port = 7966 port = 7966