Skip to content

Commit

Permalink
Add GUI punchr
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis-tra committed Oct 22, 2022
1 parent e1e0de1 commit 453123a
Show file tree
Hide file tree
Showing 15 changed files with 555 additions and 168 deletions.
159 changes: 2 additions & 157 deletions cmd/client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,178 +2,23 @@ package main

import (
"context"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"syscall"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

var (
version = "dev" // set via goreleaser
commit = "" // set via goreleaser
"github.com/dennis-tra/punchr/pkg/client"
)

func main() {
shortCommit := commit
if len(shortCommit) > 7 {
shortCommit = shortCommit[:7]
}

app := &cli.App{
Name: "punchrclient",
Usage: "A libp2p host that is capable of DCUtR.",
UsageText: "punchrclient [global options] command [command options] [arguments...]",
Action: RootAction,
Version: fmt.Sprintf("%s+%s", version, shortCommit),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "telemetry-host",
Usage: "To which network address should the telemetry (prometheus, pprof) server bind",
EnvVars: []string{"PUNCHR_CLIENT_TELEMETRY_HOST"},
Value: "localhost",
DefaultText: "localhost",
},
&cli.StringFlag{
Name: "telemetry-port",
Usage: "On which port should the telemetry (prometheus, pprof) server listen",
EnvVars: []string{"PUNCHR_CLIENT_TELEMETRY_PORT"},
Value: "12001",
DefaultText: "12001",
},
&cli.StringFlag{
Name: "server-host",
Usage: "Where does the the punchr server listen",
EnvVars: []string{"PUNCHR_CLIENT_SERVER_HOST"},
Value: "punchr.dtrautwein.eu",
DefaultText: "punchr.dtrautwein.eu",
},
&cli.StringFlag{
Name: "server-port",
Usage: "On which port listens the punchr server",
EnvVars: []string{"PUNCHR_CLIENT_SERVER_PORT"},
Value: "443",
DefaultText: "443",
},
&cli.BoolFlag{
Name: "server-ssl",
Usage: "Whether or not to use a SSL connection to the server.",
EnvVars: []string{"PUNCHR_CLIENT_SERVER_SSL"},
Value: true,
DefaultText: "true",
},
&cli.BoolFlag{
Name: "server-ssl-skip-verify",
Usage: "Whether or not to skip SSL certificate verification.",
EnvVars: []string{"PUNCHR_CLIENT_SERVER_SSL_SKIP_VERIFY"},
Value: false,
DefaultText: "false",
},
&cli.IntFlag{
Name: "host-count",
Usage: "How many libp2p hosts should be used to hole punch",
EnvVars: []string{"PUNCHR_CLIENT_HOST_COUNT"},
DefaultText: "10",
Value: 10,
},
&cli.StringFlag{
Name: "api-key",
Usage: "The key to authenticate against the API",
EnvVars: []string{"PUNCHR_CLIENT_API_KEY"},
Required: true,
},
&cli.StringFlag{
Name: "key-file",
Usage: "File where punchr saves the host identities.",
TakesFile: true,
EnvVars: []string{"PUNCHR_CLIENT_KEY_FILE"},
DefaultText: "punchrclient.keys",
Value: "punchrclient.keys",
},
&cli.StringSliceFlag{
Name: "bootstrap-peers",
Usage: "Comma separated list of multi addresses of bootstrap peers",
EnvVars: []string{"PUNCHR_BOOTSTRAP_PEERS"},
},
&cli.BoolFlag{
Name: "disable-router-check",
Usage: "Set this flag if you don't want punchr to check your router home page",
Value: false,
},
},
EnableBashCompletion: true,
}

// Create context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
defer stop()

if err := app.RunContext(ctx, os.Args); err != nil {
if err := client.App.RunContext(ctx, os.Args); err != nil {
log.Errorf("error: %v\n", err)
os.Exit(1)
}
}

func RootAction(c *cli.Context) error {
// Start telemetry endpoints
go serveTelemetry(c)

// Create new punchr
punchr, err := NewPunchr(c)
if err != nil {
return errors.Wrap(err, "new punchr")
}

// Initialize its hosts
if err = punchr.InitHosts(c); err != nil {
return errors.Wrap(err, "punchr init hosts")
}

if !c.Bool("disable-router-check") {
if err = punchr.UpdateRouterHTML(); err != nil {
log.WithError(err).Warnln("Could not get router HTML page")
}
}

// Connect punchr hosts to bootstrap nodes
if err = punchr.Bootstrap(c.Context); err != nil {
return errors.Wrap(err, "bootstrap punchr hosts")
}

// Register hosts at the gRPC server
if err = punchr.Register(c); err != nil {
return err
}

// Finally, start hole punching
if err = punchr.StartHolePunching(c.Context); err != nil {
log.Fatalf("failed to hole punch: %v", err)
}

// Waiting for shutdown signal
<-c.Context.Done()
log.Info("Shutting down gracefully, press Ctrl+C again to force")

if err = punchr.Close(); err != nil {
log.WithError(err).Warnln("Closing punchr client")
}

log.Info("Done!")
return nil
}

// serveTelemetry starts an HTTP server for the prometheus and pprof handler.
func serveTelemetry(c *cli.Context) {
addr := fmt.Sprintf("%s:%s", c.String("telemetry-host"), c.String("telemetry-port"))
log.WithField("addr", addr).Debugln("Starting prometheus endpoint")
http.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(addr, nil); err != nil {
log.WithError(err).Warnln("Error serving prometheus")
}
}
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/dennis-tra/punchr
go 1.18

require (
fyne.io/fyne/v2 v2.2.3
github.com/friendsofgo/errors v0.9.2
github.com/golang-migrate/migrate/v4 v4.15.2
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
Expand Down Expand Up @@ -31,6 +32,7 @@ require (
)

require (
fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
Expand All @@ -45,15 +47,23 @@ require (
github.com/ericlagergren/decimal v0.0.0-20211103172832-aca2edc11f73 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fredbi/uri v0.0.0-20181227131451-3dcfdacbaaf3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fyne-io/gl-js v0.0.0-20220119005834-d2da28d9ccfe // indirect
github.com/fyne-io/glfw-js v0.0.0-20220120001248-ee7290d23504 // indirect
github.com/fyne-io/image v0.0.0-20220602074514-4956b0afb3d2 // indirect
github.com/go-gl/gl v0.0.0-20211210172815-726fda9656d6 // indirect
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20211213063430-748e38ca8aec // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gofrs/uuid v4.3.0+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/goki/freetype v0.0.0-20181231101311-fa8a33aabaff // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
Expand All @@ -69,6 +79,7 @@ require (
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/cpuid/v2 v2.1.2 // indirect
github.com/koron/go-ssdp v0.0.3 // indirect
Expand Down Expand Up @@ -126,16 +137,22 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/srwiley/oksvg v0.0.0-20200311192757-870daf9aa564 // indirect
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/tevino/abool v1.2.0 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
go.opencensus.io v0.23.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.23.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/exp v0.0.0-20221019170559-20944726eadf // indirect
golang.org/x/image v0.0.0-20220601225756-64ec528b34cd // indirect
golang.org/x/mobile v0.0.0-20211207041440-4e6c2922fdee // indirect
golang.org/x/mod v0.6.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand All @@ -148,5 +165,6 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/js/dom v0.0.0-20210725211120-f030747120f2 // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)
Loading

0 comments on commit 453123a

Please sign in to comment.