2024-01-12 00:11:53 +00:00
|
|
|
package protocols
|
2024-01-12 04:13:10 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-28 07:20:59 +00:00
|
|
|
"context"
|
2024-01-24 08:28:47 +00:00
|
|
|
"crypto/ed25519"
|
|
|
|
"fmt"
|
|
|
|
s5config "git.lumeweb.com/LumeWeb/libs5-go/config"
|
|
|
|
s5ed "git.lumeweb.com/LumeWeb/libs5-go/ed25519"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
|
|
|
s5interfaces "git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
|
|
|
s5node "git.lumeweb.com/LumeWeb/libs5-go/node"
|
|
|
|
s5storage "git.lumeweb.com/LumeWeb/libs5-go/storage"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-28 07:20:59 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/protocols/registry"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/storage"
|
|
|
|
"github.com/spf13/viper"
|
2024-01-24 08:28:47 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
2024-01-28 07:20:59 +00:00
|
|
|
"go.uber.org/fx"
|
2024-01-24 08:28:47 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"time"
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-24 08:28:47 +00:00
|
|
|
_ s5interfaces.ProviderStore = (*S5ProviderStore)(nil)
|
2024-01-28 07:20:59 +00:00
|
|
|
_ registry.Protocol = (*S5Protocol)(nil)
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type S5Protocol struct {
|
2024-01-28 07:20:59 +00:00
|
|
|
node s5interfaces.Node
|
|
|
|
config *viper.Viper
|
|
|
|
logger *zap.Logger
|
|
|
|
storage *storage.StorageServiceImpl
|
|
|
|
identity ed25519.PrivateKey
|
|
|
|
providerStore *S5ProviderStore
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type S5ProtocolParams struct {
|
|
|
|
fx.In
|
|
|
|
Config *viper.Viper
|
|
|
|
Logger *zap.Logger
|
|
|
|
Storage *storage.StorageServiceImpl
|
|
|
|
Identity ed25519.PrivateKey
|
|
|
|
ProviderStore *S5ProviderStore
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type S5ProtocolResult struct {
|
|
|
|
fx.Out
|
2024-01-28 09:35:43 +00:00
|
|
|
Protocol registry.Protocol `group:"protocol"`
|
2024-01-28 08:58:49 +00:00
|
|
|
S5Protocol *S5Protocol
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 09:17:56 +00:00
|
|
|
var S5ProtocolModule = fx.Module("s5_api",
|
2024-01-28 07:20:59 +00:00
|
|
|
fx.Provide(NewS5Protocol),
|
2024-01-28 08:57:59 +00:00
|
|
|
fx.Provide(NewS5ProviderStore),
|
2024-01-28 07:20:59 +00:00
|
|
|
)
|
2024-01-12 13:22:13 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func NewS5Protocol(
|
|
|
|
params S5ProtocolParams,
|
|
|
|
) (S5ProtocolResult, error) {
|
2024-01-28 08:58:49 +00:00
|
|
|
proto := &S5Protocol{
|
|
|
|
config: params.Config,
|
|
|
|
logger: params.Logger,
|
|
|
|
storage: params.Storage,
|
|
|
|
identity: params.Identity,
|
|
|
|
providerStore: params.ProviderStore,
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
return S5ProtocolResult{
|
2024-01-28 09:35:43 +00:00
|
|
|
Protocol: proto,
|
2024-01-28 08:58:49 +00:00
|
|
|
S5Protocol: proto,
|
2024-01-28 07:20:59 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2024-01-12 13:22:13 +00:00
|
|
|
|
2024-01-28 08:57:59 +00:00
|
|
|
func NewS5ProviderStore(config *viper.Viper, logger *zap.Logger, storage *storage.StorageServiceImpl) *S5ProviderStore {
|
|
|
|
return &S5ProviderStore{
|
|
|
|
config: config,
|
|
|
|
logger: logger,
|
|
|
|
storage: storage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func InitS5Protocol(s5 *S5Protocol) error {
|
|
|
|
return s5.Init()
|
|
|
|
}
|
|
|
|
func (s *S5Protocol) Init() error {
|
2024-01-12 04:13:10 +00:00
|
|
|
cfg := &s5config.NodeConfig{
|
|
|
|
P2P: s5config.P2PConfig{
|
|
|
|
Network: "",
|
|
|
|
Peers: s5config.PeersConfig{Initial: []string{}},
|
|
|
|
},
|
2024-01-28 07:20:59 +00:00
|
|
|
KeyPair: s5ed.New(s.identity),
|
2024-01-12 04:13:10 +00:00
|
|
|
DB: nil,
|
2024-01-28 07:20:59 +00:00
|
|
|
Logger: s.logger.Named("s5"),
|
2024-01-12 04:13:10 +00:00
|
|
|
HTTP: s5config.HTTPConfig{},
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
pconfig := s.config.Sub("protocol.s5")
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-01-12 14:30:23 +00:00
|
|
|
if pconfig == nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
s.logger.Fatal("Missing protocol.s5 Config")
|
2024-01-12 14:30:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err := pconfig.Unmarshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
cfg.HTTP.API.Domain = fmt.Sprintf("s5.%s", s.config.GetString("core.domain"))
|
2024-01-12 14:17:53 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
if s.config.IsSet("core.externalPort") {
|
|
|
|
cfg.HTTP.API.Port = s.config.GetUint("core.externalPort")
|
2024-01-12 14:17:53 +00:00
|
|
|
} else {
|
2024-01-28 07:20:59 +00:00
|
|
|
cfg.HTTP.API.Port = s.config.GetUint("core.port")
|
2024-01-12 14:17:53 +00:00
|
|
|
}
|
2024-01-12 14:30:23 +00:00
|
|
|
|
2024-01-12 11:37:01 +00:00
|
|
|
dbPath := pconfig.GetString("dbPath")
|
|
|
|
|
|
|
|
if dbPath == "" {
|
2024-01-28 07:20:59 +00:00
|
|
|
s.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-28 07:20:59 +00:00
|
|
|
s.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-28 07:20:59 +00:00
|
|
|
s.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)
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
s.node.SetProviderStore(s.providerStore)
|
2024-01-24 08:28:47 +00:00
|
|
|
|
2024-01-12 04:13:10 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-28 07:20:59 +00:00
|
|
|
func (s *S5Protocol) Start(ctx context.Context) 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-28 07:20:59 +00:00
|
|
|
s.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
|
|
|
}
|
2024-01-28 07:20:59 +00:00
|
|
|
|
|
|
|
func (s *S5Protocol) Name() string {
|
|
|
|
return "s5"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S5Protocol) Stop(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-12 04:13:10 +00:00
|
|
|
func (s *S5Protocol) Node() s5interfaces.Node {
|
|
|
|
return s.node
|
|
|
|
}
|
2024-01-24 08:28:47 +00:00
|
|
|
|
|
|
|
type S5ProviderStore struct {
|
2024-01-28 08:57:59 +00:00
|
|
|
config *viper.Viper
|
|
|
|
logger *zap.Logger
|
|
|
|
storage *storage.StorageServiceImpl
|
2024-01-24 08:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s S5ProviderStore) CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool {
|
|
|
|
for _, t := range kind {
|
|
|
|
switch t {
|
|
|
|
case types.StorageLocationTypeArchive, types.StorageLocationTypeFile, types.StorageLocationTypeFull:
|
|
|
|
rawHash := hash.HashBytes()
|
2024-01-25 14:51:55 +00:00
|
|
|
|
2024-01-28 08:57:59 +00:00
|
|
|
if exists, upload := s.storage.TusUploadExists(rawHash); exists {
|
2024-01-25 14:51:55 +00:00
|
|
|
if upload.Completed {
|
|
|
|
return true
|
|
|
|
}
|
2024-01-25 14:58:53 +00:00
|
|
|
|
2024-01-25 14:51:55 +00:00
|
|
|
}
|
2024-01-28 08:57:59 +00:00
|
|
|
if exists, _ := s.storage.FileExists(rawHash); exists {
|
2024-01-24 08:28:47 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s S5ProviderStore) Provide(hash *encoding.Multihash, kind []types.StorageLocationType) (s5interfaces.StorageLocation, error) {
|
|
|
|
for _, t := range kind {
|
|
|
|
if !s.CanProvide(hash, []types.StorageLocationType{t}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
switch t {
|
|
|
|
case types.StorageLocationTypeArchive:
|
|
|
|
return s5storage.NewStorageLocation(int(types.StorageLocationTypeArchive), []string{}, calculateExpiry(24*time.Hour)), nil
|
|
|
|
case types.StorageLocationTypeFile, types.StorageLocationTypeFull:
|
2024-01-28 08:57:59 +00:00
|
|
|
return s5storage.NewStorageLocation(int(types.StorageLocationTypeFull), []string{generateDownloadUrl(hash, s.config, s.logger)}, calculateExpiry(24*time.Hour)), nil
|
2024-01-24 08:28:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hashStr, err := hash.ToString()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("could not provide hash %s for types %v", hashStr, kind)
|
|
|
|
}
|
|
|
|
func calculateExpiry(duration time.Duration) int64 {
|
|
|
|
return time.Now().Add(duration).Unix()
|
|
|
|
}
|
2024-01-24 08:36:03 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func generateDownloadUrl(hash *encoding.Multihash, config *viper.Viper, logger *zap.Logger) string {
|
|
|
|
domain := config.GetString("core.domain")
|
2024-01-24 08:36:03 +00:00
|
|
|
|
|
|
|
hashStr, err := hash.ToBase64Url()
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
logger.Error("error encoding hash", zap.Error(err))
|
2024-01-24 08:36:03 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 15:32:44 +00:00
|
|
|
return fmt.Sprintf("https://s5.%s/s5/download/%s", domain, hashStr)
|
2024-01-24 08:36:03 +00:00
|
|
|
}
|