Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
heyvito committed Dec 7, 2023
0 parents commit 0c52285
Show file tree
Hide file tree
Showing 76 changed files with 7,536 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Vito Sartori

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
43 changes: 43 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"github.com/heyvito/eswim"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"
"os/signal"
"syscall"
)

func main() {
var logger *zap.Logger
var err error
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
config.DisableCaller = true
logger, err = config.Build()
if err != nil {
panic(err)
}

opts := eswim.Options{
IPv4MulticastAddress: "224.0.0.244",
MulticastPort: 1337,
SWIMPort: 1338,
InsecureDisableCrypto: true,
UseAdaptivePingTimeout: false,
LogHandler: logger,
}
srv, err := eswim.NewServer(&opts)
if err != nil {
panic(err)
}
srv.Start()
sigChan := make(chan os.Signal, 3)
go func() {
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
}()

<-sigChan
srv.Shutdown()
}
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module github.com/heyvito/eswim

go 1.21.1

require (
github.com/cloudflare/circl v1.3.6
github.com/heyvito/gateway v0.1.1
github.com/influxdata/tdigest v0.0.1
github.com/stretchr/testify v1.8.4
go.uber.org/zap v1.26.0
golang.org/x/sys v0.15.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
34 changes: 34 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
github.com/cloudflare/circl v1.3.6 h1:/xbKIqSHbZXHwkhbrhrt2YOHIwYJlXH94E3tI/gDlUg=
github.com/cloudflare/circl v1.3.6/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/heyvito/gateway v0.1.1 h1:F2DOQOBRKlH1Y1u0lOQ2BdovEVSf+qH2iWUO4bx2j2M=
github.com/heyvito/gateway v0.1.1/go.mod h1:g7FIBBk+dKr9dsm+VXJM/4PUon0RdVc99sjePjKuFFw=
github.com/influxdata/tdigest v0.0.1 h1:XpFptwYmnEKUqmkcDjrzffswZ3nvNeevbUSLPP/ZzIY=
github.com/influxdata/tdigest v0.0.1/go.mod h1:Z0kXnxzbTC2qrx4NaIzYkE1k66+6oEDQTvL95hQFh5Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de h1:xSjD6HQTqT0H/k60N5yYBtnN1OEkVy7WIo/DYyxKRO0=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.0.0-20180525024113-a5b4c53f6e8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca h1:PupagGYwj8+I4ubCxcmcBRk3VlUWtTg5huQpZR9flmE=
gonum.org/v1/gonum v0.0.0-20181121035319-3f7ecaa7e8ca/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
gonum.org/v1/netlib v0.0.0-20181029234149-ec6d1f5cefe6/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
32 changes: 32 additions & 0 deletions internal/containers/containers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package containers

// Triple represents a generic container with three values of types T, U, and V.
type Triple[T, U, V any] struct {
First T
Second U
Third V
}

// Decompose returns T, U, and V objects in order. Useful for obtaining all
// values in a single call.
func (t Triple[T, U, V]) Decompose() (T, U, V) {
return t.First, t.Second, t.Third
}

// Tri returns a new Triple with provided arguments.
func Tri[T, U, V any](first T, second U, third V) Triple[T, U, V] {
return Triple[T, U, V]{first, second, third}
}

type Tuple[T, U any] struct {
First T
Second U
}

func (t Tuple[T, U]) Decompose() (T, U) {
return t.First, t.Second
}

func Tup[T, U any](first T, second U) Tuple[T, U] {
return Tuple[T, U]{first, second}
}
64 changes: 64 additions & 0 deletions internal/containers/sync_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package containers

import "sync"

type SyncMap[K comparable, V any] struct {
inner sync.Map
}

func (s *SyncMap[K, V]) Load(key K) (value V, ok bool) {
var v any
if v, ok = s.inner.Load(key); !ok {
return
}
return v.(V), ok
}

func (s *SyncMap[K, V]) Store(key K, value V) {
s.inner.Store(key, value)
}

func (s *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
var v any
v, loaded = s.inner.LoadOrStore(key, value)
if loaded {
actual = v.(V)
}
return
}

func (s *SyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
var v any
v, loaded = s.inner.LoadAndDelete(key)
if loaded {
value = v.(V)
}
return
}

func (s *SyncMap[K, V]) Delete(key K) {
s.inner.Delete(key)
}

func (s *SyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) {
var prev any
prev, loaded = s.inner.Swap(key, value)
if loaded {
previous = prev.(V)
}
return
}

func (s *SyncMap[K, V]) CompareAndSwap(key K, old, new V) bool {
return s.inner.CompareAndSwap(key, old, new)
}

func (s *SyncMap[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
return s.inner.CompareAndDelete(key, old)
}

func (s *SyncMap[K, V]) Range(f func(key K, value V) bool) {
s.inner.Range(func(key, value any) bool {
return f(key.(K), value.(V))
})
}
42 changes: 42 additions & 0 deletions internal/containers/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package containers

import "fmt"

func InsertAt[S interface{ ~[]E }, E any](base S, item E, pos int) S {
return append(base[:pos], append(S{item}, base[pos:]...)...)
}

func AllMatch[S interface{ ~[]E }, E any](base S, matcher func(item E) bool) bool {
for _, v := range base {
if !matcher(v) {
return false
}
}

return true
}

func DrainChan[T any](ch chan T) {
drain:
for {
select {
case <-ch:
default:
break drain
}
}
}

func MapFn[S interface{ ~[]E }, E any, T any](set S, fn func(E) T) []T {
res := make([]T, 0, len(set))
for _, item := range set {
res = append(res, fn(item))
}
return res
}

func StringerStr[T fmt.Stringer](i T) string { return i.String() }

func StrMapper[S interface{ ~[]E }, E fmt.Stringer](set S) []string {
return MapFn(set, StringerStr[E])
}
14 changes: 14 additions & 0 deletions internal/containers/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package containers

import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)

func TestInsertAt(t *testing.T) {
arr := []int{1, 2, 3}
idxToInsert := rand.Intn(len(arr))
arr = InsertAt(arr, 4, idxToInsert)
assert.ElementsMatch(t, []int{1, 2, 3, 4}, arr)
}
92 changes: 92 additions & 0 deletions internal/core/ascon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package core

import (
"crypto/rand"
"github.com/cloudflare/circl/cipher/ascon"
"sync"
)

// ASCON represents an instance capable of sealing and opening data encrypted
// by the ASCON cipher suite, using 128-bit security and 320-bit permutation
// with different round numbers, thus being enough to implement AEAD with low
// overhead.
type ASCON interface {
// Seal seals the provided buffer and returns its ciphered data, except
// when ASCON is initialized without a key. On the latter, the input is
// immediately returned to the caller as-is.
Seal(data []byte) ([]byte, error)

// Open opens a provided sealed buffer and returns its plain data, except
// when ASCON is initialized without a key. On the latter, the input is
// immediately returned to the caller as-is. In case decoding fails, nil
// is returned.
Open(data []byte) []byte
}

// NewASCON returns a new ASCON handler. When key is empty or nil, the returned
// handler performs NOOPs on both Seal and Open operations.
func NewASCON(key []byte) (ASCON, error) {
if len(key) == 0 {
return &asconWrapper{enabled: false}, nil
}

encCipher, err := ascon.New(key, ascon.Ascon128a)
if err != nil {
return nil, err
}
decCipher, err := ascon.New(key, ascon.Ascon128a)
if err != nil {
return nil, err
}

return &asconWrapper{
enabled: true,
encodeCipher: encCipher,
decodeCipher: decCipher,
}, nil
}

type asconWrapper struct {
encodeMu sync.Mutex
encodeCipher *ascon.Cipher

decodeMu sync.Mutex
decodeCipher *ascon.Cipher

enabled bool
}

func (a *asconWrapper) Seal(data []byte) ([]byte, error) {
if !a.enabled {
return data, nil
}

a.encodeMu.Lock()
defer a.encodeMu.Unlock()
nonce := make([]byte, 16)
_, err := rand.Read(nonce)
if err != nil {
return nil, err
}

cipherText := a.encodeCipher.Seal(nil, nonce, data, nil)
return append(nonce, cipherText...), nil
}

func (a *asconWrapper) Open(data []byte) []byte {
if !a.enabled {
return data
}

a.decodeMu.Lock()
defer a.decodeMu.Unlock()

nonce := data[0:16]
data = data[16:]
result, err := a.decodeCipher.Open(nil, nonce, data, nil)
if err != nil {
return nil
}

return result
}
Loading

0 comments on commit 0c52285

Please sign in to comment.