Skip to content

Commit d142ee7

Browse files
committed
Merge branch 'release/v0.1.2'
2 parents c379d19 + 95d2904 commit d142ee7

42 files changed

Lines changed: 3330 additions & 204 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cmd/evse/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ func (h *evse) run() {
8383
h.myService.Start()
8484
// defer h.myService.Shutdown()
8585

86-
remoteService := service.ServiceDetails{
87-
SKI: remoteSki,
88-
}
86+
remoteService := service.NewServiceDetails(remoteSki)
8987
h.myService.PairRemoteService(remoteService)
9088
}
9189

cmd/hems/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ func (h *hems) run() {
8383
h.myService.Start()
8484
// defer h.myService.Shutdown()
8585

86-
remoteService := service.ServiceDetails{
87-
SKI: remoteSki,
88-
}
86+
remoteService := service.NewServiceDetails(remoteSki)
8987
h.myService.PairRemoteService(remoteService)
9088
}
9189

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ require (
1010
github.com/rickb777/plural v1.4.1 // indirect
1111
github.com/stretchr/objx v0.5.0 // indirect
1212
golang.org/x/mod v0.7.0 // indirect
13-
golang.org/x/net v0.2.0 // indirect
14-
golang.org/x/sys v0.2.0 // indirect
15-
golang.org/x/tools v0.3.0 // indirect
13+
golang.org/x/net v0.3.0 // indirect
14+
golang.org/x/sys v0.3.0 // indirect
15+
golang.org/x/tools v0.4.0 // indirect
1616
gopkg.in/yaml.v3 v3.0.1 // indirect
1717
)
1818

go.sum

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
4646
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
4747
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
4848
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
49-
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
50-
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
49+
golang.org/x/net v0.3.0 h1:VWL6FNY2bEEmsGVKabSlHu5Irp34xmMRoqb/9lF9lxk=
50+
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
5151
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5252
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5353
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
@@ -60,18 +60,18 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
6060
golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
6161
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6262
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63-
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
64-
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63+
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
64+
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6565
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
6666
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6767
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
6868
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
69-
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
69+
golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM=
7070
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
7171
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
7272
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
73-
golang.org/x/tools v0.3.0 h1:SrNbZl6ECOS1qFzgTdQfWXZM9XBkiA6tkFrH9YSTPHM=
74-
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
73+
golang.org/x/tools v0.4.0 h1:7mTAgkunk3fr4GAloyyCasadO6h9zSsQZbwvcaIciV4=
74+
golang.org/x/tools v0.4.0/go.mod h1:UE5sM2OK9E/d67R0ANs2xJizIymRP5gJU295PvKXxjQ=
7575
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7676
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7777
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

service/cert_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package service
2+
3+
import (
4+
"crypto/x509"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/suite"
9+
)
10+
11+
func TestCertSuite(t *testing.T) {
12+
suite.Run(t, new(CertSuite))
13+
}
14+
15+
type CertSuite struct {
16+
suite.Suite
17+
}
18+
19+
func (c *CertSuite) Test_CreateCertificate() {
20+
cert, err := CreateCertificate("", "Org", "DE", "CN")
21+
assert.Nil(c.T(), err)
22+
assert.NotNil(c.T(), cert)
23+
}
24+
25+
func (c *CertSuite) Test_SkiFromCertificate() {
26+
cert, err := CreateCertificate("", "Org", "DE", "CN")
27+
assert.Nil(c.T(), err)
28+
29+
leaf, err := x509.ParseCertificate(cert.Certificate[0])
30+
assert.Nil(c.T(), err)
31+
32+
ski, err := skiFromCertificate(leaf)
33+
assert.Nil(c.T(), err)
34+
assert.NotNil(c.T(), ski)
35+
}

service/hub.go

Lines changed: 49 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"github.com/enbility/eebus-go/ship"
1818
"github.com/enbility/eebus-go/spine"
1919
"github.com/enbility/eebus-go/spine/model"
20-
"github.com/enbility/eebus-go/util"
2120
"github.com/gorilla/websocket"
2221
)
2322

@@ -71,13 +70,13 @@ type connectionsHub struct {
7170
serviceProvider serviceProvider
7271

7372
// The list of paired devices
74-
pairedServices []ServiceDetails
73+
pairedServices []*ServiceDetails
7574

7675
// The web server for handling incoming websocket connections
7776
httpServer *http.Server
7877

7978
// Handling mDNS related tasks
80-
mdns *mdns
79+
mdns MdnsService
8180

8281
// the SPINE local device
8382
spineLocalDevice *spine.DeviceLocalImpl
@@ -88,40 +87,38 @@ type connectionsHub struct {
8887
muxMdns sync.Mutex
8988
}
9089

91-
func newConnectionsHub(serviceProvider serviceProvider, spineLocalDevice *spine.DeviceLocalImpl, configuration *Configuration, localService *ServiceDetails) (*connectionsHub, error) {
90+
func newConnectionsHub(serviceProvider serviceProvider, mdns MdnsService, spineLocalDevice *spine.DeviceLocalImpl, configuration *Configuration, localService *ServiceDetails) *connectionsHub {
9291
hub := &connectionsHub{
9392
connections: make(map[string]*ship.ShipConnection),
9493
connectionAttemptCounter: make(map[string]int),
9594
connectionAttemptRunning: make(map[string]bool),
96-
pairedServices: make([]ServiceDetails, 0),
95+
pairedServices: make([]*ServiceDetails, 0),
9796
serviceProvider: serviceProvider,
9897
spineLocalDevice: spineLocalDevice,
9998
configuration: configuration,
10099
localService: localService,
100+
mdns: mdns,
101101
}
102102

103-
localService.SKI = util.NormalizeSKI(localService.SKI)
104-
105-
mdns, err := newMDNS(localService.SKI, configuration)
106-
if err != nil {
107-
return nil, err
108-
}
109-
110-
hub.mdns = mdns
111-
112-
return hub, nil
103+
return hub
113104
}
114105

115106
// start the ConnectionsHub with all its services
116107
func (h *connectionsHub) start() {
108+
// start mDNS
109+
err := h.mdns.SetupMdnsService()
110+
if err != nil {
111+
logging.Log.Error("error during mdns setup:", err)
112+
}
113+
117114
// start the websocket server
118115
go func() {
119116
if err := h.startWebsocketServer(); err != nil {
120117
logging.Log.Error("error during websocket server starting:", err)
121118
}
122119
}()
123120

124-
if err := h.mdns.Announce(); err != nil {
121+
if err := h.mdns.AnnounceMdnsEntry(); err != nil {
125122
logging.Log.Error("error registering mDNS Service:", err)
126123
}
127124
}
@@ -171,7 +168,7 @@ func (h *connectionsHub) checkRestartMdnsSearch() {
171168
if countPairedServices > countConnections {
172169
// if this is not a CEM also start the mDNS announcement
173170
if h.localService.deviceType != model.DeviceTypeTypeEnergyManagementSystem {
174-
_ = h.mdns.Announce()
171+
_ = h.mdns.AnnounceMdnsEntry()
175172
}
176173

177174
logging.Log.Debug("restarting mdns search")
@@ -209,7 +206,7 @@ func (h *connectionsHub) registerConnection(connection *ship.ShipConnection) {
209206

210207
// shutdown mDNS if this is not a CEM
211208
if h.localService.deviceType != model.DeviceTypeTypeEnergyManagementSystem {
212-
h.mdns.Unannounce()
209+
h.mdns.UnannounceMdnsEntry()
213210
h.mdns.UnregisterMdnsSearch(h)
214211
}
215212
}
@@ -231,7 +228,7 @@ func (h *connectionsHub) shutdown() {
231228
h.muxCon.Lock()
232229
defer h.muxCon.Unlock()
233230

234-
h.mdns.shutdown()
231+
h.mdns.ShutdownMdnsService()
235232
for _, c := range h.connections {
236233
c.CloseConnection(false, "")
237234
}
@@ -328,31 +325,29 @@ func (h *connectionsHub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
328325
return
329326
}
330327

331-
ski = util.NormalizeSKI(ski)
332-
logging.Log.Debug("incoming connection request from", ski)
328+
// normalize the incoming SKI
329+
remoteService := NewServiceDetails(ski)
330+
logging.Log.Debug("incoming connection request from", remoteService.SKI())
333331

334332
// Check if the remote service is paired
335-
_, err = h.PairedServiceForSKI(ski)
333+
_, err = h.PairedServiceForSKI(remoteService.SKI())
336334
if err != nil {
337335
logging.Log.Debug("ski", ski, "is not paired, closing the connection")
338336
return
339337
}
340338

341-
remoteService := &ServiceDetails{
342-
SKI: ski,
343-
}
344339
// check if we already know this remote service
345-
if remoteS, err := h.PairedServiceForSKI(ski); err == nil {
340+
if remoteS, err := h.PairedServiceForSKI(remoteService.SKI()); err == nil {
346341
remoteService = remoteS
347342
}
348343

349344
// don't allow a second connection
350-
if !h.keepThisConnection(conn, true, remoteService.SKI) {
345+
if !h.keepThisConnection(conn, true, remoteService) {
351346
return
352347
}
353348

354-
dataHandler := ship.NewWebsocketConnection(conn, remoteService.SKI)
355-
shipConnection := ship.NewConnectionHandler(h, dataHandler, h.spineLocalDevice, ship.ShipRoleServer, h.localService.ShipID, remoteService.SKI, remoteService.ShipID)
349+
dataHandler := ship.NewWebsocketConnection(conn, remoteService.SKI())
350+
shipConnection := ship.NewConnectionHandler(h, dataHandler, h.spineLocalDevice, ship.ShipRoleServer, h.localService.ShipID(), remoteService.SKI(), remoteService.ShipID())
356351
shipConnection.Run()
357352

358353
h.registerConnection(shipConnection)
@@ -362,7 +357,7 @@ func (h *connectionsHub) ServeHTTP(w http.ResponseWriter, r *http.Request) {
362357
//
363358
// returns error contains a reason for failing the connection or nil if no further tries should be processed
364359
func (h *connectionsHub) connectFoundService(remoteService *ServiceDetails, host, port string) error {
365-
if h.isSkiConnected(remoteService.SKI) {
360+
if h.isSkiConnected(remoteService.SKI()) {
366361
return nil
367362
}
368363

@@ -391,33 +386,33 @@ func (h *connectionsHub) connectFoundService(remoteService *ServiceDetails, host
391386

392387
if len(remoteCerts) == 0 || remoteCerts[0].SubjectKeyId == nil {
393388
// Close connection as we couldn't get the remote SKI
394-
errorString := fmt.Sprintf("closing connection to %s: could not get remote SKI from certificate", remoteService.SKI)
389+
errorString := fmt.Sprintf("closing connection to %s: could not get remote SKI from certificate", remoteService.SKI())
395390
conn.Close()
396391
return errors.New(errorString)
397392
}
398393

399394
if _, err := skiFromCertificate(remoteCerts[0]); err != nil {
400395
// Close connection as the remote SKI can't be correct
401-
errorString := fmt.Sprintf("closing connection to %s: %s", remoteService.SKI, err)
396+
errorString := fmt.Sprintf("closing connection to %s: %s", remoteService.SKI(), err)
402397
conn.Close()
403398
return errors.New(errorString)
404399
}
405400

406401
remoteSKI := fmt.Sprintf("%0x", remoteCerts[0].SubjectKeyId)
407402

408-
if remoteSKI != remoteService.SKI {
409-
errorString := fmt.Sprintf("closing connection to %s: SKI does not match %s", remoteService.SKI, remoteSKI)
403+
if remoteSKI != remoteService.SKI() {
404+
errorString := fmt.Sprintf("closing connection to %s: SKI does not match %s", remoteService.SKI(), remoteSKI)
410405
conn.Close()
411406
return errors.New(errorString)
412407
}
413408

414-
if !h.keepThisConnection(conn, false, remoteService.SKI) {
415-
errorString := fmt.Sprintf("closing connection to %s: ignoring this connection", remoteService.SKI)
409+
if !h.keepThisConnection(conn, false, remoteService) {
410+
errorString := fmt.Sprintf("closing connection to %s: ignoring this connection", remoteService.SKI())
416411
return errors.New(errorString)
417412
}
418413

419-
dataHandler := ship.NewWebsocketConnection(conn, remoteService.SKI)
420-
shipConnection := ship.NewConnectionHandler(h, dataHandler, h.spineLocalDevice, ship.ShipRoleClient, h.localService.ShipID, remoteService.SKI, remoteService.ShipID)
414+
dataHandler := ship.NewWebsocketConnection(conn, remoteService.SKI())
415+
shipConnection := ship.NewConnectionHandler(h, dataHandler, h.spineLocalDevice, ship.ShipRoleClient, h.localService.ShipID(), remoteService.SKI(), remoteService.ShipID())
421416
shipConnection.Run()
422417

423418
h.registerConnection(shipConnection)
@@ -430,7 +425,7 @@ func (h *connectionsHub) connectFoundService(remoteService *ServiceDetails, host
430425
//
431426
// returns true if this connection is fine to be continue
432427
// returns false if this connection should not be established or kept
433-
func (h *connectionsHub) keepThisConnection(conn *websocket.Conn, incomingRequest bool, remoteSKI string) bool {
428+
func (h *connectionsHub) keepThisConnection(conn *websocket.Conn, incomingRequest bool, remoteService *ServiceDetails) bool {
434429
// SHIP 12.2.2 defines:
435430
// prevent double connections with SKI Comparison
436431
// the node with the hight SKI value kees the most recent connection and
@@ -439,16 +434,17 @@ func (h *connectionsHub) keepThisConnection(conn *websocket.Conn, incomingReques
439434
// This is hard to implement without any flaws. Therefor I chose a
440435
// different approach: The connection initiated by the higher SKI will be kept
441436

437+
remoteSKI := remoteService.SKI()
442438
existingC := h.connectionForSKI(remoteSKI)
443439
if existingC == nil {
444440
return true
445441
}
446442

447443
keep := false
448444
if incomingRequest {
449-
keep = remoteSKI > h.localService.SKI
445+
keep = remoteSKI > h.localService.SKI()
450446
} else {
451-
keep = h.localService.SKI > remoteSKI
447+
keep = h.localService.SKI() > remoteSKI
452448
}
453449

454450
if keep {
@@ -475,29 +471,26 @@ func (h *connectionsHub) keepThisConnection(conn *websocket.Conn, incomingReques
475471
func (h *connectionsHub) PairedServiceForSKI(ski string) (*ServiceDetails, error) {
476472
h.muxReg.Lock()
477473
defer h.muxReg.Unlock()
474+
478475
for _, service := range h.pairedServices {
479-
if service.SKI == ski {
480-
return &service, nil
476+
if service.SKI() == ski {
477+
return service, nil
481478
}
482479
}
483-
return &ServiceDetails{}, fmt.Errorf("no registered service found for SKI %s", ski)
480+
return nil, fmt.Errorf("no registered service found for SKI %s", ski)
484481
}
485482

486483
// Adds a new device to the list of known devices which can be connected to
487484
// and connect it if it is currently not connected
488-
func (h *connectionsHub) PairRemoteService(service ServiceDetails) {
489-
490-
// standardize the provided SKI strings
491-
service.SKI = util.NormalizeSKI(service.SKI)
492-
485+
func (h *connectionsHub) PairRemoteService(service *ServiceDetails) {
493486
// check if it is already registered
494-
if _, err := h.PairedServiceForSKI(service.SKI); err != nil {
487+
if _, err := h.PairedServiceForSKI(service.SKI()); err != nil {
495488
h.muxReg.Lock()
496489
h.pairedServices = append(h.pairedServices, service)
497490
h.muxReg.Unlock()
498491
}
499492

500-
if !h.isSkiConnected(service.SKI) {
493+
if !h.isSkiConnected(service.SKI()) {
501494
h.mdns.RegisterMdnsSearch(h)
502495
}
503496
}
@@ -507,11 +500,11 @@ func (h *connectionsHub) PairRemoteService(service ServiceDetails) {
507500
func (h *connectionsHub) UnpairRemoteService(ski string) error {
508501
h.removeConnectionAttemptCounter(ski)
509502

510-
newRegisteredDevice := make([]ServiceDetails, 0)
503+
newRegisteredDevice := make([]*ServiceDetails, 0)
511504

512505
h.muxReg.Lock()
513506
for _, device := range h.pairedServices {
514-
if device.SKI != ski {
507+
if device.SKI() != ski {
515508
newRegisteredDevice = append(newRegisteredDevice, device)
516509
}
517510
}
@@ -543,8 +536,8 @@ func (h *connectionsHub) ReportMdnsEntries(entries map[string]MdnsEntry) {
543536
}
544537

545538
// patch the addresses list if an IPv4 address was provided
546-
if remoteService.IPv4 != "" {
547-
if ip := net.ParseIP(remoteService.IPv4); ip != nil {
539+
if remoteService.IPv4() != "" {
540+
if ip := net.ParseIP(remoteService.IPv4()); ip != nil {
548541
entry.Addresses = []net.IP{ip}
549542
}
550543
}
@@ -682,7 +675,7 @@ func (h *connectionsHub) getConnectionInitiationDelayTime(ski string) (int, time
682675

683676
// seed with the local SKI for initializing rand
684677
i := new(big.Int)
685-
hex := fmt.Sprintf("0x%s", h.localService.SKI)
678+
hex := fmt.Sprintf("0x%s", h.localService.SKI())
686679
if _, err := fmt.Sscan(hex, i); err == nil {
687680
rand.Seed(i.Int64() + time.Now().UnixNano())
688681
} else {

0 commit comments

Comments
 (0)