Skip to content

Commit a4c59c6

Browse files
committed
log output : WIP
1 parent a218cdd commit a4c59c6

File tree

13 files changed

+215
-86
lines changed

13 files changed

+215
-86
lines changed

agent.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/Snawoot/rgap/psk"
1213
"github.com/hashicorp/go-multierror"
1314
)
1415

1516
type AgentConfig struct {
1617
Group uint64
1718
Address netip.Addr
18-
Key PSK
19+
Key psk.PSK
1920
Interval time.Duration
2021
Destinations []string
2122
Dialer Dialer

announcement.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"encoding/binary"
88
"fmt"
99
"net"
10+
11+
"github.com/Snawoot/rgap/psk"
1012
)
1113

1214
const (
@@ -42,7 +44,7 @@ func (ad *AnnouncementData) UnmarshalBinary(data []byte) error {
4244
return nil
4345
}
4446

45-
func (ad *AnnouncementData) CalculateSignature(key PSK) ([SignatureSize]byte, error) {
47+
func (ad *AnnouncementData) CalculateSignature(key psk.PSK) ([SignatureSize]byte, error) {
4648
h := hmac.New(sha256.New, key.AsSlice())
4749
h.Write([]byte(SignaturePrefixBytes))
4850
if err := binary.Write(h, binary.BigEndian, ad); err != nil {
@@ -81,7 +83,7 @@ func (a *Announcement) UnmarshalBinary(data []byte) error {
8183
return nil
8284
}
8385

84-
func (a *Announcement) CheckSignature(key PSK) (bool, error) {
86+
func (a *Announcement) CheckSignature(key psk.PSK) (bool, error) {
8587
sig, err := a.Data.CalculateSignature(key)
8688
if err != nil {
8789
return false, fmt.Errorf("signature verification failed: %w", err)

cmd/rgap/agent.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/spf13/cobra"
1010

1111
"github.com/Snawoot/rgap"
12+
"github.com/Snawoot/rgap/psk"
1213
)
1314

1415
const (
@@ -49,26 +50,26 @@ func (a *addressOption) Type() string {
4950
}
5051

5152
type pskOption struct {
52-
psk *rgap.PSK
53+
psk *psk.PSK
5354
}
5455

55-
func (psk *pskOption) String() string {
56-
if psk.psk == nil {
56+
func (pskOpt *pskOption) String() string {
57+
if pskOpt.psk == nil {
5758
return "<nil>"
5859
}
59-
return psk.psk.String()
60+
return pskOpt.psk.String()
6061
}
6162

62-
func (psk *pskOption) Set(s string) error {
63-
newPSK := new(rgap.PSK)
63+
func (pskOpt *pskOption) Set(s string) error {
64+
newPSK := new(psk.PSK)
6465
if err := newPSK.FromHexString(s); err != nil {
6566
return err
6667
}
67-
psk.psk = newPSK
68+
pskOpt.psk = newPSK
6869
return nil
6970
}
7071

71-
func (psk *pskOption) Type() string {
72+
func (_ *pskOption) Type() string {
7273
return "hexstring"
7374
}
7475

cmd/rgap/genpsk.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/Snawoot/rgap/psk"
67
"github.com/spf13/cobra"
7-
8-
"github.com/Snawoot/rgap"
98
)
109

1110
// genpskCmd represents the genpsk command
1211
var genpskCmd = &cobra.Command{
1312
Use: "genpsk",
1413
Short: "Generate and output hex-encoded pre-shared key",
1514
RunE: func(cmd *cobra.Command, args []string) error {
16-
psk, err := rgap.GeneratePSK()
15+
psk, err := psk.GeneratePSK()
1716
if err != nil {
1817
return fmt.Errorf("PSK generation failed: %w", err)
1918
}

cmd/rgap/listener.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"gopkg.in/yaml.v3"
99

1010
"github.com/Snawoot/rgap"
11+
"github.com/Snawoot/rgap/config"
1112
)
1213

1314
var (
@@ -19,7 +20,7 @@ var listenerCmd = &cobra.Command{
1920
Use: "listener",
2021
Short: "Starts listener accepting and processing announcements",
2122
RunE: func(cmd *cobra.Command, args []string) error {
22-
var cfg rgap.ListenerConfig
23+
var cfg config.ListenerConfig
2324
cfgF, err := os.Open(configPath)
2425
if err != nil {
2526
return fmt.Errorf("unable to read configuration file: %w", err)

config/config.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
import (
4+
"time"
5+
6+
"gopkg.in/yaml.v3"
7+
8+
"github.com/Snawoot/rgap/psk"
9+
)
10+
11+
type GroupConfig struct {
12+
ID uint64
13+
PSK *psk.PSK
14+
Expire time.Duration
15+
ClockSkew time.Duration `yaml:"clock_skew"`
16+
ReadinessDelay time.Duration `yaml:"readiness_delay"`
17+
}
18+
19+
type OutputConfig struct {
20+
Kind string
21+
Spec yaml.Node
22+
}
23+
24+
type ListenerConfig struct {
25+
Listen []string
26+
Groups []GroupConfig
27+
Outputs []OutputConfig
28+
}

group.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,36 @@ import (
66
"net/netip"
77
"time"
88

9+
"github.com/Snawoot/rgap/config"
10+
"github.com/Snawoot/rgap/iface"
11+
"github.com/Snawoot/rgap/psk"
912
"github.com/jellydator/ttlcache/v3"
1013
)
1114

1215
type Group struct {
1316
id uint64
14-
psk PSK
17+
psk psk.PSK
1518
expire time.Duration
1619
clockSkew time.Duration
1720
readinessDelay time.Duration
1821
addrSet *ttlcache.Cache[netip.Addr, struct{}]
1922
readyAt time.Time
2023
}
2124

22-
type GroupItem struct {
23-
Address netip.Addr
24-
ExpiresAt time.Time
25+
type groupItem struct {
26+
address netip.Addr
27+
expiresAt time.Time
2528
}
2629

27-
func GroupFromConfig(cfg *GroupConfig) (*Group, error) {
30+
func (gi groupItem) Address() netip.Addr {
31+
return gi.address
32+
}
33+
34+
func (gi groupItem) ExpiresAt() time.Time {
35+
return gi.expiresAt
36+
}
37+
38+
func GroupFromConfig(cfg *config.GroupConfig) (*Group, error) {
2839
if cfg.PSK == nil {
2940
return nil, fmt.Errorf("group %d: PSK is not set", cfg.ID)
3041
}
@@ -96,17 +107,21 @@ func (g *Group) Ingest(a *Announcement) error {
96107
return nil
97108
}
98109

99-
func (g *Group) List() []GroupItem {
110+
func (g *Group) List() []iface.GroupItem {
100111
items := g.addrSet.Items()
101-
res := make([]GroupItem, 0, len(items))
112+
res := make([]iface.GroupItem, 0, len(items))
102113
for _, item := range items {
103114
if item.IsExpired() {
104115
continue
105116
}
106-
res = append(res, GroupItem{
107-
Address: item.Key(),
108-
ExpiresAt: item.ExpiresAt(),
117+
res = append(res, groupItem{
118+
address: item.Key(),
119+
expiresAt: item.ExpiresAt(),
109120
})
110121
}
111122
return res
112123
}
124+
125+
func (g *Group) Ready() bool {
126+
return time.Now().After(g.readyAt)
127+
}

iface/iface.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package iface
2+
3+
import (
4+
"net/netip"
5+
"time"
6+
)
7+
8+
type GroupBridge interface {
9+
Groups() []uint64
10+
ListGroup(uint64) []GroupItem
11+
GroupReady(uint64) bool
12+
}
13+
14+
type GroupItem interface {
15+
Address() netip.Addr
16+
ExpiresAt() time.Time
17+
}
18+
19+
type StartStopper interface {
20+
Start() error
21+
Stop() error
22+
}

listener.go

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,19 @@ import (
44
"context"
55
"fmt"
66
"log"
7-
"time"
87

9-
"gopkg.in/yaml.v3"
8+
"github.com/Snawoot/rgap/config"
9+
"github.com/Snawoot/rgap/iface"
10+
"github.com/Snawoot/rgap/output"
1011
)
1112

12-
type StartStopper interface {
13-
Start() error
14-
Stop() error
15-
}
16-
17-
type GroupConfig struct {
18-
ID uint64
19-
PSK *PSK
20-
Expire time.Duration
21-
ClockSkew time.Duration `yaml:"clock_skew"`
22-
ReadinessDelay time.Duration `yaml:"readiness_delay"`
23-
}
24-
25-
type OutputConfig struct {
26-
Kind string
27-
Spec yaml.Node
28-
}
29-
30-
type ListenerConfig struct {
31-
Listen []string
32-
Groups []GroupConfig
33-
Outputs []OutputConfig
34-
}
35-
3613
type Listener struct {
37-
sources []StartStopper
14+
sources []iface.StartStopper
3815
groups map[uint64]*Group
39-
outputs []StartStopper
16+
outputs []iface.StartStopper
4017
}
4118

42-
func NewListener(cfg *ListenerConfig) (*Listener, error) {
19+
func NewListener(cfg *config.ListenerConfig) (*Listener, error) {
4320
l := &Listener{
4421
groups: make(map[uint64]*Group),
4522
}
@@ -55,7 +32,7 @@ func NewListener(cfg *ListenerConfig) (*Listener, error) {
5532
l.sources = append(l.sources, src)
5633
}
5734
for i, oc := range cfg.Outputs {
58-
out, err := OutputFromConfig(&oc, l)
35+
out, err := output.OutputFromConfig(&oc, l)
5936
if err != nil {
6037
return nil, fmt.Errorf("unable to construct new output with index %d: %w", i, err)
6138
}
@@ -75,7 +52,7 @@ func (l *Listener) announceCallback(label string, ann *Announcement) {
7552
}
7653

7754
func (l *Listener) Run(ctx context.Context) error {
78-
var primeStack []StartStopper
55+
var primeStack []iface.StartStopper
7956
defer func() {
8057
for i := len(primeStack) - 1; i >= 0; i-- {
8158
if err := primeStack[i].Stop(); err != nil {
@@ -115,10 +92,18 @@ func (l *Listener) Groups() []uint64 {
11592
return res
11693
}
11794

118-
func (l *Listener) ListGroup(id uint64) []GroupItem {
95+
func (l *Listener) ListGroup(id uint64) []iface.GroupItem {
11996
g, ok := l.groups[id]
12097
if !ok {
12198
return nil
12299
}
123100
return g.List()
124101
}
102+
103+
func (l *Listener) GroupReady(id uint64) bool {
104+
g, ok := l.groups[id]
105+
if !ok {
106+
return true
107+
}
108+
return g.Ready()
109+
}

output.go

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)