2024-01-29 20:11:57 +00:00
|
|
|
package s5
|
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"
|
2024-02-27 14:11:17 +00:00
|
|
|
"crypto/sha256"
|
2024-02-17 03:00:53 +00:00
|
|
|
"errors"
|
2024-01-24 08:28:47 +00:00
|
|
|
"fmt"
|
2024-02-17 03:00:53 +00:00
|
|
|
"time"
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/config"
|
2024-02-27 14:11:17 +00:00
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2024-02-22 07:09:31 +00:00
|
|
|
|
2024-02-17 03:00:53 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/metadata"
|
|
|
|
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/storage"
|
|
|
|
|
2024-01-24 08:28:47 +00:00
|
|
|
s5config "git.lumeweb.com/LumeWeb/libs5-go/config"
|
|
|
|
s5ed "git.lumeweb.com/LumeWeb/libs5-go/ed25519"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-29 20:11:57 +00:00
|
|
|
s5fx "git.lumeweb.com/LumeWeb/libs5-go/fx"
|
2024-01-24 08:28:47 +00:00
|
|
|
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"
|
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"
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-02-01 03:51:51 +00:00
|
|
|
_ s5storage.ProviderStore = (*S5ProviderStore)(nil)
|
|
|
|
_ registry.Protocol = (*S5Protocol)(nil)
|
2024-02-17 10:20:42 +00:00
|
|
|
_ storage.StorageProtocol = (*S5Protocol)(nil)
|
2024-01-12 04:13:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type S5Protocol struct {
|
2024-02-22 07:09:31 +00:00
|
|
|
portalConfig *config.Manager
|
|
|
|
config *Config
|
|
|
|
logger *zap.Logger
|
|
|
|
storage storage.StorageService
|
|
|
|
identity ed25519.PrivateKey
|
|
|
|
node *s5node.Node
|
|
|
|
tusHandler *TusHandler
|
|
|
|
store *S5ProviderStore
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type S5ProtocolParams struct {
|
|
|
|
fx.In
|
2024-02-22 07:09:31 +00:00
|
|
|
PortalConfig *config.Manager
|
2024-01-28 07:20:59 +00:00
|
|
|
Logger *zap.Logger
|
2024-02-17 03:00:53 +00:00
|
|
|
Storage storage.StorageService
|
2024-01-28 07:20:59 +00:00
|
|
|
Identity ed25519.PrivateKey
|
|
|
|
ProviderStore *S5ProviderStore
|
2024-02-17 03:00:53 +00:00
|
|
|
TusHandler *TusHandler
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type S5ProtocolResult struct {
|
|
|
|
fx.Out
|
2024-02-17 09:41:12 +00:00
|
|
|
Protocol registry.Protocol `group:"protocol"`
|
|
|
|
S5Protocol *S5Protocol
|
|
|
|
S5NodeConfig *s5config.NodeConfig
|
|
|
|
Db *bolt.DB
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-02-17 03:00:53 +00:00
|
|
|
type S5ProviderStoreParams struct {
|
|
|
|
fx.In
|
2024-02-22 08:25:48 +00:00
|
|
|
Config *config.Manager
|
2024-02-17 03:00:53 +00:00
|
|
|
Metadata metadata.MetadataService
|
|
|
|
Logger *zap.Logger
|
|
|
|
Tus *TusHandler
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
var ProtocolModule = fx.Module("s5_api",
|
2024-01-28 07:20:59 +00:00
|
|
|
fx.Provide(NewS5Protocol),
|
2024-02-17 03:00:53 +00:00
|
|
|
fx.Provide(NewTusHandler),
|
2024-01-28 08:57:59 +00:00
|
|
|
fx.Provide(NewS5ProviderStore),
|
2024-02-01 03:51:51 +00:00
|
|
|
fx.Replace(func(cfg *s5config.NodeConfig) *zap.Logger {
|
2024-01-30 19:32:46 +00:00
|
|
|
return cfg.Logger
|
|
|
|
}),
|
2024-01-29 20:11:57 +00:00
|
|
|
s5fx.Module,
|
2024-01-28 07:20:59 +00:00
|
|
|
)
|
2024-01-12 13:22:13 +00:00
|
|
|
|
2024-02-17 10:16:52 +00:00
|
|
|
var PreInit = func(protocol *S5Protocol, node *s5node.Node) {
|
|
|
|
protocol.SetNode(node)
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
func NewS5Protocol(
|
|
|
|
params S5ProtocolParams,
|
|
|
|
) (S5ProtocolResult, error) {
|
2024-01-28 08:58:49 +00:00
|
|
|
proto := &S5Protocol{
|
2024-02-22 07:09:31 +00:00
|
|
|
portalConfig: params.PortalConfig,
|
|
|
|
logger: params.Logger,
|
|
|
|
storage: params.Storage,
|
|
|
|
identity: params.Identity,
|
|
|
|
tusHandler: params.TusHandler,
|
|
|
|
store: params.ProviderStore,
|
2024-01-29 20:11:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
cfg, err := configureS5Protocol(proto)
|
2024-01-29 20:11:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return S5ProtocolResult{}, err
|
2024-01-28 08:58:49 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 03:51:51 +00:00
|
|
|
return S5ProtocolResult{
|
2024-02-17 09:41:12 +00:00
|
|
|
Protocol: proto,
|
|
|
|
S5Protocol: proto,
|
|
|
|
S5NodeConfig: cfg,
|
|
|
|
Db: cfg.DB,
|
2024-02-01 03:51:51 +00:00
|
|
|
}, nil
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
2024-01-12 13:22:13 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
func configureS5Protocol(proto *S5Protocol) (*s5config.NodeConfig, error) {
|
|
|
|
cfg := proto.Config().(*Config)
|
|
|
|
cm := proto.portalConfig
|
|
|
|
portalCfg := cm.Config()
|
|
|
|
vpr := cm.Viper()
|
2024-01-12 14:30:23 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
err := cm.ConfigureProtocol(proto.Name(), cfg)
|
2024-01-12 14:30:23 +00:00
|
|
|
if err != nil {
|
2024-01-29 20:11:57 +00:00
|
|
|
return nil, err
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
cfg.HTTP.API.Domain = fmt.Sprintf("s5.%s", vpr.GetString("core.domain"))
|
2024-01-12 14:17:53 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
if portalCfg.Core.ExternalPort != 0 {
|
|
|
|
cfg.HTTP.API.Port = portalCfg.Core.ExternalPort
|
2024-01-12 14:17:53 +00:00
|
|
|
} else {
|
2024-02-22 07:09:31 +00:00
|
|
|
cfg.HTTP.API.Port = portalCfg.Core.Port
|
2024-01-12 14:17:53 +00:00
|
|
|
}
|
2024-01-12 14:30:23 +00:00
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
if cfg.DbPath == "" {
|
2024-02-22 08:44:27 +00:00
|
|
|
proto.logger.Fatal("protocol.s5.db_path is required")
|
2024-01-12 11:37:01 +00:00
|
|
|
}
|
2024-01-12 04:33:11 +00:00
|
|
|
|
2024-02-27 14:54:11 +00:00
|
|
|
derivedSeed := pbkdf2.Key(proto.identity, []byte("s5"), 600000, 32, sha256.New)
|
2024-01-12 04:13:10 +00:00
|
|
|
|
2024-02-27 14:11:17 +00:00
|
|
|
p := ed25519.NewKeyFromSeed(derivedSeed)
|
2024-01-12 04:13:10 +00:00
|
|
|
cfg.KeyPair = s5ed.New(p)
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
db, err := bolt.Open(cfg.DbPath, 0600, nil)
|
2024-01-12 04:13:10 +00:00
|
|
|
if err != nil {
|
2024-02-22 07:09:31 +00:00
|
|
|
proto.logger.Fatal("Failed to open db", zap.Error(err))
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.DB = db
|
|
|
|
|
2024-02-27 09:14:18 +00:00
|
|
|
cfg.Logger = proto.logger.Named("s5")
|
|
|
|
|
2024-02-23 13:18:05 +00:00
|
|
|
return cfg.NodeConfig, nil
|
2024-01-29 20:11:57 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 07:09:31 +00:00
|
|
|
func (s *S5Protocol) Config() config.ProtocolConfig {
|
|
|
|
if s.config == nil {
|
2024-02-23 13:18:05 +00:00
|
|
|
s.config = &Config{
|
|
|
|
NodeConfig: &s5config.NodeConfig{},
|
|
|
|
}
|
2024-02-22 07:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.config
|
|
|
|
}
|
2024-02-17 03:00:53 +00:00
|
|
|
func NewS5ProviderStore(params S5ProviderStoreParams) *S5ProviderStore {
|
2024-01-29 20:11:57 +00:00
|
|
|
return &S5ProviderStore{
|
2024-02-17 03:00:53 +00:00
|
|
|
config: params.Config,
|
|
|
|
logger: params.Logger,
|
|
|
|
tus: params.Tus,
|
|
|
|
metadata: params.Metadata,
|
2024-01-29 20:11:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 08:35:43 +00:00
|
|
|
func (s *S5Protocol) Init(ctx context.Context) error {
|
2024-02-17 10:16:52 +00:00
|
|
|
s.node.Services().Storage().SetProviderStore(s.store)
|
2024-01-24 08:28:47 +00:00
|
|
|
|
2024-02-27 08:35:43 +00:00
|
|
|
err := s.node.Init(ctx)
|
2024-01-30 20:48:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-17 09:41:12 +00:00
|
|
|
s.tusHandler.SetStorageProtocol(GetStorageProtocol(s))
|
|
|
|
|
2024-02-17 03:00:53 +00:00
|
|
|
err = s.tusHandler.Init()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-02-27 08:36:09 +00:00
|
|
|
err := s.node.Start(ctx)
|
2024-01-12 15:16:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
identity, err := s.node.NodeId().ToString()
|
2024-01-12 15:16:04 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
func (s *S5Protocol) Node() *s5node.Node {
|
|
|
|
return s.node
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
func (s *S5Protocol) Stop(ctx context.Context) error {
|
|
|
|
return nil
|
2024-01-12 04:13:10 +00:00
|
|
|
}
|
2024-01-24 08:28:47 +00:00
|
|
|
|
2024-02-17 10:16:52 +00:00
|
|
|
func (s *S5Protocol) SetNode(node *s5node.Node) {
|
|
|
|
s.node = node
|
|
|
|
}
|
|
|
|
|
2024-02-17 10:20:42 +00:00
|
|
|
func (s *S5Protocol) EncodeFileName(bytes []byte) string {
|
|
|
|
bytes = append([]byte{byte(types.HashTypeBlake3)}, bytes...)
|
|
|
|
|
|
|
|
hash, err := encoding.NewMultihash(bytes).ToBase64Url()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("error encoding hash", zap.Error(err))
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
2024-01-24 08:28:47 +00:00
|
|
|
type S5ProviderStore struct {
|
2024-02-22 08:25:48 +00:00
|
|
|
config *config.Manager
|
2024-02-17 03:00:53 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
tus *TusHandler
|
|
|
|
metadata metadata.MetadataService
|
2024-01-24 08:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s S5ProviderStore) CanProvide(hash *encoding.Multihash, kind []types.StorageLocationType) bool {
|
2024-02-17 07:39:42 +00:00
|
|
|
ctx := context.Background()
|
2024-01-24 08:28:47 +00:00
|
|
|
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-02-17 07:39:42 +00:00
|
|
|
if exists, upload := s.tus.UploadExists(ctx, 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-02-17 07:39:42 +00:00
|
|
|
if _, err := s.metadata.GetUpload(ctx, rawHash); errors.Is(err, metadata.ErrNotFound) {
|
2024-01-24 08:28:47 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
func (s S5ProviderStore) Provide(hash *encoding.Multihash, kind []types.StorageLocationType) (s5storage.StorageLocation, error) {
|
2024-01-24 08:28:47 +00:00
|
|
|
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-02-09 20:55:04 +00:00
|
|
|
return s5storage.NewStorageLocation(int(types.StorageLocationTypeFull), []string{generateDownloadUrl(hash, s.config, s.logger), generateProofUrl(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-02-22 08:25:48 +00:00
|
|
|
func generateDownloadUrl(hash *encoding.Multihash, config *config.Manager, logger *zap.Logger) string {
|
|
|
|
domain := config.Config().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
|
|
|
}
|
2024-02-09 20:55:04 +00:00
|
|
|
|
2024-02-22 08:25:48 +00:00
|
|
|
func generateProofUrl(hash *encoding.Multihash, config *config.Manager, logger *zap.Logger) string {
|
|
|
|
domain := config.Config().Core.Domain
|
2024-02-09 20:55:04 +00:00
|
|
|
|
|
|
|
hashStr, err := hash.ToBase64Url()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error encoding hash", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("https://s5.%s/s5/download/%s.obao", domain, hashStr)
|
|
|
|
}
|