Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions .github/workflows/integrationtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,21 @@ jobs:
cd ../plexus-agent
CGO_ENABLED=0 go build -v .

#tests:
# runs-on: ubuntu-latest
# if: ${{ github.actor != 'dependabot[bot]' }}
# steps:
# - name: Checkout
# uses: actions/checkout@v4
# - name: Setup Go
# uses: actions/setup-go@v6
# with:
# go-version-file: ./go.mod
# - name: run tests
# run: |
# go vet ./...
# go test ./... -v
tests:
runs-on: ubuntu-latest
if: ${{ github.actor != 'dependabot[bot]' }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v6
with:
go-version-file: ./go.mod
- name: run tests
run: |
go vet ./...
sudo go test -v ./...



lint:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ time.db
**/launch.json
devops/experiment/*
devops/plexus-update/*
devops/deploy/*
devops/deploy/*
internal/agent/agent.seed
26 changes: 13 additions & 13 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,40 @@ linters:
- noinlineerr
- paralleltest
- testpackage
- thelper
- wrapcheck
- wsl
- wsl_v5
settings:
cyclop:
max-complexity: 20
errcheck:
exclude-functions:
- crypto/rand.Read
funlen:
statements: 60
lines: 80
lines: 82
gosmopolitan:
allow-time-local: true
ireturn:
allow:
- nkeys.KeyPair
- error
- io.Reader
- net/http.Handler
- nkeys.KeyPair
varnamelen:
max-distance: 15
ignore-names:
- i
- wg
ignore-decls:
- c *gin.Context
- c *nftables.Conn
- c nftables.Conn
- ec *nats.Conn
- f embed.FS
- id string
- ns *server.Server
- r *http.Request
- w http.ResponseWriter
exclusions:
warn-unused: true
generated: lax
Expand All @@ -59,23 +64,18 @@ linters:
rules:
- path: _test\.go
linters:
- dupl
- funlen
- gocognit
- goconst
- funlen
- nonamedreturns
- revive
- predeclared
- path: app/plexus-agent/cmd/
linters:
- revive
- path: wireguard.go
- path: app/plexus-agent/cmd/*
linters:
- revive
formatters:
enable:
- gci
- gofmt
- gofumpt
- goimports
- golines
exclusions:
generated: lax
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Agent
Tech Stack
==========
* Language: [Go](https://go.dev)
* HTTP Framework: [Gin](https://github.com/gin-gonic/gin)
* HTTP Router: [devilcove/mux](https://github.com/devilcove/mux)
* FrontEnd Framework: html/[htmx](https://htmx.org)
* CSS Library: [w3schools](https://w3schools.com/w3css)
* Database(key/value): [bbolt](https://github.com/etcd-io/bbolt)
Expand Down
3 changes: 2 additions & 1 deletion app/plexus-agent/cmd/drop.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@ var dropCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(dropCmd)
statusCmd.Flags().BoolVarP(&force, "force", "f", false, "force deletion even when not connected")
statusCmd.Flags().
BoolVarP(&force, "force", "f", false, "force deletion even when not connected")
}
4 changes: 3 additions & 1 deletion app/plexus-agent/cmd/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ var registerCmd = &cobra.Command{
ec, err := agent.ConnectToAgentBroker()
cobra.CheckErr(err)
resp := plexus.MessageResponse{}
cobra.CheckErr(agent.Request(ec, agent.Agent+plexus.Register, request, &resp, agent.NatsTimeout))
cobra.CheckErr(
agent.Request(ec, agent.Agent+plexus.Register, request, &resp, agent.NatsTimeout),
)
fmt.Println(resp.Message)
},
}
Expand Down
4 changes: 3 additions & 1 deletion app/plexus-agent/cmd/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ var resetCmd = &cobra.Command{
ec, err := agent.ConnectToAgentBroker()
cobra.CheckErr(err)
resp := plexus.MessageResponse{}
cobra.CheckErr(agent.Request(ec, agent.Agent+plexus.Reset, request, &resp, agent.NatsTimeout))
cobra.CheckErr(
agent.Request(ec, agent.Agent+plexus.Reset, request, &resp, agent.NatsTimeout),
)
fmt.Println(resp.Message)
},
}
Expand Down
36 changes: 14 additions & 22 deletions app/plexus-agent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@ limitations under the License.
package cmd

import (
"fmt"
"log"
"log/slog"
"os"
"path/filepath"
"runtime/debug"

"github.com/devilcove/plexus"
"github.com/devilcove/plexus/internal/agent"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var config agent.Configuration
// var config agent.Configuration

// rootCmd represents the base command when called without any subcommands.
var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -57,31 +56,24 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringP("verbosity", "v", "INFO", "logging verbosity")
rootCmd.PersistentFlags().IntP("natsport", "p", 4223, "nats port for cli <-> agent comms")

agent.Config.Verbosity = *rootCmd.PersistentFlags().StringP("verbosity", "v", "INFO", "logging verbosity")
agent.Config.NatsPort = *rootCmd.PersistentFlags().IntP("natsport", "p", 4223, "nats port for cli <-> agent comms")
plexus.SetLogging(agent.Config.Verbosity)
// Cobra also supports local flags, which will only run
// when this action is called directly.
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
viper.SetConfigFile("/etc/plexus-agent/config")
viper.SetConfigType("yaml")

if err := viper.BindPFlags(rootCmd.Flags()); err != nil {
log.Println("bindflags", err)
}
viper.SetEnvPrefix("PLEXUS")
viper.AutomaticEnv() // read in environment variables that match
if err := viper.ReadInConfig(); err == nil {
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
if err := viper.UnmarshalExact(&config); err != nil {
log.Println("viper.Unmarshal", err)
plexus.SetLogging("INFO")
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
agent.Config = config
slog.Debug("using configuration", "config", config)
agent.Config.DataDir = home + "/.local/share/" + filepath.Base(os.Args[0]) + "/"

// set defaults
slog.Debug("using configuration", "config", agent.Config)
debug.SetTraceback("single")
}
10 changes: 9 additions & 1 deletion app/plexus-agent/cmd/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ var setCmd = &cobra.Command{
resp := plexus.MessageResponse{}
ec, err := agent.ConnectToAgentBroker()
cobra.CheckErr(err)
cobra.CheckErr(agent.Request(ec, agent.Agent+plexus.SetPrivateEndpoint, request, &resp, agent.NatsTimeout))
cobra.CheckErr(
agent.Request(
ec,
agent.Agent+plexus.SetPrivateEndpoint,
request,
&resp,
agent.NatsTimeout,
),
)
fmt.Println(resp.Message)
},
}
Expand Down
10 changes: 8 additions & 2 deletions app/plexus-agent/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ var statusCmd = &cobra.Command{
cobra.CheckErr(err)
status := agent.StatusResponse{}
// networks := []Network{}
cobra.CheckErr(agent.Request(ec, agent.Agent+plexus.Status, nil, &status, agent.NatsTimeout))
cobra.CheckErr(
agent.Request(ec, agent.Agent+plexus.Status, nil, &status, agent.NatsTimeout),
)
if status.Server == "" {
fmt.Println("agent running... not connected to servers")
return
Expand Down Expand Up @@ -105,7 +107,11 @@ var statusCmd = &cobra.Command{
fmt.Println("\trelay: true")
showRelayedPeers(peer.RelayedPeers, network)
}
fmt.Println("\tprivate-endpoint:", peer.PrivateEndpoint.String()+":", peer.ListenPort)
fmt.Println(
"\tprivate-endpoint:",
peer.PrivateEndpoint.String()+":",
peer.ListenPort,
)
fmt.Println("\tpublic-endpoint:", peer.Endpoint.String()+":", peer.PublicListenPort)
fmt.Println("\twg-endpoint:", wgPeer.Endpoint)
fmt.Print("\tallowed ips:")
Expand Down
20 changes: 10 additions & 10 deletions app/plexus-agent/cmd/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,47 @@ import (
func TestPrintHandshake(t *testing.T) {
bytes := make([]byte, 128)
out, err := os.Open(os.Stdout.Name())
should.BeNil(t, err)
should.NotBeError(t, err)
t.Run("one second", func(t *testing.T) {
printHandshake(time.Now().Add(time.Second * -1))
_, err := out.Read(bytes)
should.BeNil(t, err)
should.NotBeError(t, err)
should.ContainSubstring(t, string(bytes), "1 second ago")
should.BeNil(t, out.Close())
})
t.Run("one minute", func(t *testing.T) {
out, err := os.Open(os.Stdout.Name())
should.BeNil(t, err)
should.NotBeError(t, err)
printHandshake(time.Now().Add(time.Second * -60))
_, err = out.Read(bytes)
should.BeNil(t, err)
should.NotBeError(t, err)
should.ContainSubstring(t, string(bytes), "1 minute 0 seconds ago")
should.BeNil(t, out.Close())
})
t.Run("hours", func(t *testing.T) {
out, err := os.Open(os.Stdout.Name())
should.BeNil(t, err)
should.NotBeError(t, err)
printHandshake(time.Now().Add(time.Second * -3600))
_, err = out.Read(bytes)
should.BeNil(t, err)
should.NotBeError(t, err)
should.ContainSubstring(t, string(bytes), "1 hour 0 minutes 0 seconds ago")
should.BeNil(t, out.Close())
})
t.Run("multi", func(t *testing.T) {
out, err := os.Open(os.Stdout.Name())
should.BeNil(t, err)
should.NotBeError(t, err)
printHandshake(time.Now().Add(time.Second * -7250))
_, err = out.Read(bytes)
should.BeNil(t, err)
should.NotBeError(t, err)
should.ContainSubstring(t, string(bytes), "2 hours 0 minutes 50 seconds ago")
should.BeNil(t, out.Close())
})
t.Run("now", func(t *testing.T) {
out, err := os.Open(os.Stdout.Name())
should.BeNil(t, err)
should.NotBeError(t, err)
printHandshake(time.Now())
_, err = out.Read(bytes)
should.BeNil(t, err)
should.NotBeError(t, err)
should.ContainSubstring(t, string(bytes), "never")
should.BeNil(t, out.Close())
})
Expand Down
13 changes: 10 additions & 3 deletions app/plexus-agent/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/spf13/cobra"
)

const version = "v0.3.0"
const version = "v0.4.0"

// versionCmd represents the version command.
var versionCmd = &cobra.Command{
Expand All @@ -40,7 +40,13 @@ var versionCmd = &cobra.Command{
cobra.CheckErr(err)
response := plexus.VersionResponse{}
// need longer timeout is case of server timeout
err = agent.Request(nc, agent.Agent+plexus.Version, long, &response, agent.NatsLongTimeout)
err = agent.Request(
nc,
agent.Agent+plexus.Version,
long,
&response,
agent.NatsLongTimeout,
)
if err != nil {
fmt.Println("error", err)
}
Expand Down Expand Up @@ -71,5 +77,6 @@ func init() {

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
versionCmd.Flags().BoolVarP(&long, "long", "l", false, "display server(s)/agent version information")
versionCmd.Flags().
BoolVarP(&long, "long", "l", false, "display server(s)/agent version information")
}
Loading