Skip to content

Commit

Permalink
Refactor to add PeerId entity
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 authored and tjjh89017 committed Aug 23, 2024
1 parent 22138ed commit d06b74c
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 2,986 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main

env:
GOVERSION: "1.20"
GOVERSION: "1.22"

jobs:
lint:
Expand Down
33 changes: 32 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/tjjh89017/stunmesh-go

go 1.13
go 1.22

require (
github.com/cloudflare/cloudflare-go v0.17.0
Expand All @@ -10,3 +10,34 @@ require (
golang.org/x/net v0.23.0
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mdlayher/genetlink v1.3.2 // indirect
github.com/mdlayher/netlink v1.7.2 // indirect
github.com/mdlayher/socket v0.4.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2,969 changes: 4 additions & 2,965 deletions go.sum

Large diffs are not rendered by default.

11 changes: 0 additions & 11 deletions helper.go

This file was deleted.

10 changes: 4 additions & 6 deletions internal/entity/peer.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package entity

type Peer struct {
id string
remoteId string
id PeerId
deviceName string
publicKey [32]byte
listenPort int
}

func NewPeer(id, remoteId, deviceName string, listenPort int, publicKey [32]byte) *Peer {
func NewPeer(id PeerId, deviceName string, listenPort int, publicKey [32]byte) *Peer {
return &Peer{
id: id,
remoteId: remoteId,
deviceName: deviceName,
listenPort: listenPort,
publicKey: publicKey,
}
}

func (p *Peer) Id() string {
return p.id
return p.id.EndpointKey()
}

func (p *Peer) RemoteId() string {
return p.remoteId
return p.id.RemoteEndpointKey()
}

func (p *Peer) DeviceName() string {
Expand Down
46 changes: 46 additions & 0 deletions internal/entity/peer_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package entity

import (
"crypto/sha1"
"encoding/hex"
)

var _ = isComparable[PeerId]

const PeerKeyLength = 32

type PeerKey [PeerKeyLength]byte

type PeerId struct {
devicePublicKey PeerKey
peerPublicKey PeerKey
}

func NewPeerId(devicePublicKey, peerPublicKey []byte) PeerId {
id := PeerId{
devicePublicKey: PeerKey{},
peerPublicKey: PeerKey{},
}
copy(id.devicePublicKey[:], devicePublicKey[:])
copy(id.peerPublicKey[:], peerPublicKey[:])

return id
}

func (p *PeerId) EndpointKey() string {
src, dest := make([]byte, PeerKeyLength), make([]byte, PeerKeyLength)
copy(src, p.devicePublicKey[:])
copy(dest, p.peerPublicKey[:])

sum := sha1.Sum(append(src, dest...))
return hex.EncodeToString(sum[:])
}

func (p *PeerId) RemoteEndpointKey() string {
src, dest := make([]byte, PeerKeyLength), make([]byte, PeerKeyLength)
copy(src, p.peerPublicKey[:])
copy(dest, p.devicePublicKey[:])

sum := sha1.Sum(append(src, dest...))
return hex.EncodeToString(sum[:])
}
46 changes: 46 additions & 0 deletions internal/entity/peer_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package entity_test

import (
"testing"

"github.com/tjjh89017/stunmesh-go/internal/entity"
)

func Test_PeerId_EndpointKey(t *testing.T) {
devicePublicKey := []byte{0}
peerPublicKey := []byte{1}

peerId := entity.NewPeerId(devicePublicKey, peerPublicKey)
endpointKey := peerId.EndpointKey()

expected := "37b7dcf21e0e183a9a86170997df242a84a85ff7"
if endpointKey != expected {
t.Errorf("Expected %s, got %s", expected, endpointKey)
}
}

func Test_PeerId_RemoteEndpointKey(t *testing.T) {
devicePublicKey := []byte{0}
peerPublicKey := []byte{1}

peerId := entity.NewPeerId(devicePublicKey, peerPublicKey)
remoteEndpointKey := peerId.RemoteEndpointKey()

expected := "9c8d8e5a31c9802b093c4116dfb0a23a311b8029"
if remoteEndpointKey != expected {
t.Errorf("Expected %s, got %s", expected, remoteEndpointKey)
}
}

func Test_PeerId_Comparable(t *testing.T) {
pid := entity.NewPeerId([]byte{0}, []byte{1})
pid2 := entity.NewPeerId([]byte{0}, []byte{1})

store := make(map[entity.PeerId]int)
store[pid] = 1
store[pid2] = 2

if store[pid] != 2 {
t.Errorf("Expected 2, got %d", store[pid])
}
}
3 changes: 3 additions & 0 deletions internal/entity/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package entity

func isComparable[T comparable]() {}
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ func main() {

for _, p := range device.Peers {
peer := entity.NewPeer(
buildEndpointKey(device.PublicKey[:], p.PublicKey[:]),
buildEndpointKey(p.PublicKey[:], device.PublicKey[:]),
entity.NewPeerId(device.PublicKey[:], p.PublicKey[:]),
device.Name,
device.ListenPort,
p.PublicKey,
Expand Down

0 comments on commit d06b74c

Please sign in to comment.