2024-01-12 00:11:53 +00:00
|
|
|
package protocols
|
2024-01-12 04:13:10 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-16 16:42:50 +00:00
|
|
|
"crypto/ed25519"
|
|
|
|
"fmt"
|
|
|
|
s5config "git.lumeweb.com/LumeWeb/libs5-go/config"
|
|
|
|
s5ed "git.lumeweb.com/LumeWeb/libs5-go/ed25519"
|
|
|
|
s5interfaces "git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
|
|
|
s5node "git.lumeweb.com/LumeWeb/libs5-go/node"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ interfaces.Protocol = (*S5Protocol)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type S5Protocol struct {
|
2024-01-12 13:22:13 +00:00
|
|
|
node s5interfaces.Node
|
|
|
|
portal interfaces.Portal
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewS5Protocol() *S5Protocol {
|
|
|
|
return &S5Protocol{}
|
|
|
|
}
|
|
|
|
|
2024-01-12 13:22:13 +00:00
|
|
|
func (s *S5Protocol) Initialize(portal interfaces.Portal) error {
|
|
|
|
s.portal = portal
|
|
|
|
|
|
|
|
logger := portal.Logger()
|
|
|
|
config := portal.Config()
|
|
|
|
|
2024-01-12 04:13:10 +00:00
|
|
|
cfg := &s5config.NodeConfig{
|
|
|
|
P2P: s5config.P2PConfig{
|
|
|
|
Network: "",
|
|
|
|
Peers: s5config.PeersConfig{Initial: []string{}},
|
|
|
|
},
|
2024-01-12 13:22:40 +00:00
|
|
|
KeyPair: s5ed.New(portal.Identity()),
|
2024-01-12 04:13:10 +00:00
|
|
|
DB: nil,
|
2024-01-12 13:22:51 +00:00
|
|
|
Logger: portal.Logger().Named("s5"),
|
2024-01-12 04:13:10 +00:00
|
|
|
HTTP: s5config.HTTPConfig{},
|
|
|
|
}
|
|
|
|
|
2024-01-12 04:32:31 +00:00
|
|
|
pconfig := config.Sub("protocol.s5")
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-01-12 14:30:23 +00:00
|
|
|
if pconfig == nil {
|
|
|
|
logger.Fatal("Missing protocol.s5 config")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := pconfig.Unmarshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 15:32:19 +00:00
|
|
|
cfg.HTTP.API.Domain = fmt.Sprintf("s5.%s", config.GetString("core.domain"))
|
2024-01-12 14:17:53 +00:00
|
|
|
|
|
|
|
if config.IsSet("core.externalPort") {
|
|
|
|
cfg.HTTP.API.Port = config.GetUint("core.externalPort")
|
|
|
|
} else {
|
|
|
|
cfg.HTTP.API.Port = config.GetUint("core.port")
|
|
|
|
}
|
2024-01-12 14:30:23 +00:00
|
|
|
|
2024-01-12 11:37:01 +00:00
|
|
|
dbPath := pconfig.GetString("dbPath")
|
|
|
|
|
|
|
|
if dbPath == "" {
|
2024-01-12 14:30:55 +00:00
|
|
|
logger.Fatal("protocol.s5.dbPath is required")
|
2024-01-12 11:37:01 +00:00
|
|
|
}
|
2024-01-12 04:33:11 +00:00
|
|
|
|
2024-01-12 04:13:10 +00:00
|
|
|
_, p, err := ed25519.GenerateKey(nil)
|
|
|
|
if err != nil {
|
2024-01-12 11:37:11 +00:00
|
|
|
logger.Fatal("Failed to generate key", zap.Error(err))
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.KeyPair = s5ed.New(p)
|
|
|
|
|
2024-01-12 11:37:01 +00:00
|
|
|
db, err := bolt.Open(dbPath, 0600, nil)
|
2024-01-12 04:13:10 +00:00
|
|
|
if err != nil {
|
2024-01-12 11:37:11 +00:00
|
|
|
logger.Fatal("Failed to open db", zap.Error(err))
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.DB = db
|
|
|
|
|
|
|
|
s.node = s5node.NewNode(cfg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (s *S5Protocol) Start() error {
|
2024-01-12 15:16:04 +00:00
|
|
|
err := s.node.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
identity, err := s.node.Services().P2P().NodeId().ToString()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-12 15:38:57 +00:00
|
|
|
s.portal.Logger().Info("S5 protocol started", zap.String("identity", identity), zap.String("network", s.node.NetworkId()), zap.String("domain", s.node.Config().HTTP.API.Domain))
|
2024-01-12 15:16:04 +00:00
|
|
|
|
|
|
|
return nil
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
func (s *S5Protocol) Node() s5interfaces.Node {
|
|
|
|
return s.node
|
|
|
|
}
|