2024-01-15 04:54:43 +00:00
|
|
|
package s5
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-01-17 21:46:13 +00:00
|
|
|
"context"
|
2024-01-16 18:32:47 +00:00
|
|
|
"crypto/ed25519"
|
2024-01-16 16:31:33 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base64"
|
2024-01-16 07:01:18 +00:00
|
|
|
"encoding/hex"
|
2024-01-15 04:54:43 +00:00
|
|
|
"errors"
|
2024-01-16 21:16:37 +00:00
|
|
|
"fmt"
|
2024-01-15 04:54:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-17 19:46:37 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/metadata"
|
2024-01-29 20:11:57 +00:00
|
|
|
libs5node "git.lumeweb.com/LumeWeb/libs5-go/node"
|
|
|
|
libs5protocol "git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
|
|
|
libs5service "git.lumeweb.com/LumeWeb/libs5-go/service"
|
|
|
|
libs5storage "git.lumeweb.com/LumeWeb/libs5-go/storage"
|
2024-01-30 05:33:57 +00:00
|
|
|
libs5storageProvider "git.lumeweb.com/LumeWeb/libs5-go/storage/provider"
|
2024-01-15 19:41:24 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-28 07:20:59 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/account"
|
2024-01-19 22:04:25 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/api/middleware"
|
2024-01-16 07:01:18 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/db/models"
|
2024-01-29 20:11:57 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/protocols/s5"
|
2024-01-28 07:20:59 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/portal/storage"
|
2024-01-16 18:32:47 +00:00
|
|
|
emailverifier "github.com/AfterShip/email-verifier"
|
2024-01-17 20:36:21 +00:00
|
|
|
"github.com/samber/lo"
|
2024-01-28 07:20:59 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-01-17 17:33:05 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-01-15 04:54:43 +00:00
|
|
|
"go.sia.tech/jape"
|
2024-01-28 07:20:59 +00:00
|
|
|
"go.uber.org/fx"
|
2024-01-15 04:54:43 +00:00
|
|
|
"go.uber.org/zap"
|
2024-01-28 07:20:59 +00:00
|
|
|
"gorm.io/gorm"
|
2024-01-15 04:54:43 +00:00
|
|
|
"io"
|
2024-01-17 17:38:52 +00:00
|
|
|
"math"
|
2024-01-15 04:54:43 +00:00
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
2024-01-17 21:46:13 +00:00
|
|
|
"nhooyr.io/websocket"
|
2024-01-18 02:59:27 +00:00
|
|
|
"strconv"
|
2024-01-15 13:29:09 +00:00
|
|
|
"strings"
|
2024-01-16 18:32:47 +00:00
|
|
|
"time"
|
2024-01-15 04:54:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-01-16 16:31:33 +00:00
|
|
|
errMultiformParse = "Error parsing multipart form"
|
|
|
|
errRetrievingFile = "Error retrieving the file"
|
|
|
|
errReadFile = "Error reading the file"
|
|
|
|
errClosingStream = "Error closing the stream"
|
|
|
|
errUploadingFile = "Error uploading the file"
|
|
|
|
errAccountGenerateChallenge = "Error generating challenge"
|
2024-01-16 18:32:47 +00:00
|
|
|
errAccountRegister = "Error registering account"
|
2024-01-16 18:51:03 +00:00
|
|
|
errAccountLogin = "Error logging in account"
|
2024-01-17 17:33:05 +00:00
|
|
|
errFailedToGetPins = "Failed to get pins"
|
2024-01-17 18:04:32 +00:00
|
|
|
errFailedToDelPin = "Failed to delete pin"
|
2024-01-17 18:13:37 +00:00
|
|
|
errFailedToAddPin = "Failed to add pin"
|
2024-01-17 19:46:37 +00:00
|
|
|
errorNotMultiform = "Not a multipart form"
|
2024-01-17 20:36:21 +00:00
|
|
|
errFetchingUrls = "Error fetching urls"
|
2024-01-24 06:27:05 +00:00
|
|
|
errDownloadingFile = "Error downloading file"
|
2024-01-15 04:54:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-16 16:31:33 +00:00
|
|
|
errUploadingFileErr = errors.New(errUploadingFile)
|
|
|
|
errAccountGenerateChallengeErr = errors.New(errAccountGenerateChallenge)
|
2024-01-16 18:32:47 +00:00
|
|
|
errAccountRegisterErr = errors.New(errAccountRegister)
|
|
|
|
errInvalidChallengeErr = errors.New("Invalid challenge")
|
|
|
|
errInvalidSignatureErr = errors.New("Invalid signature")
|
|
|
|
errPubkeyNotSupported = errors.New("Only ed25519 keys are supported")
|
|
|
|
errInvalidEmail = errors.New("Invalid email")
|
|
|
|
errEmailAlreadyExists = errors.New("Email already exists")
|
|
|
|
errGeneratingPassword = errors.New("Error generating password")
|
|
|
|
errPubkeyAlreadyExists = errors.New("Pubkey already exists")
|
2024-01-16 21:05:28 +00:00
|
|
|
errPubkeyNotExist = errors.New("Pubkey does not exist")
|
2024-01-16 18:51:03 +00:00
|
|
|
errAccountLoginErr = errors.New(errAccountLogin)
|
2024-01-17 17:33:05 +00:00
|
|
|
errFailedToGetPinsErr = errors.New(errFailedToGetPins)
|
2024-01-17 18:04:32 +00:00
|
|
|
errFailedToDelPinErr = errors.New(errFailedToDelPin)
|
2024-01-17 18:13:37 +00:00
|
|
|
errFailedToAddPinErr = errors.New(errFailedToAddPin)
|
2024-01-17 19:46:37 +00:00
|
|
|
errNotMultiformErr = errors.New(errorNotMultiform)
|
2024-01-17 20:36:21 +00:00
|
|
|
errFetchingUrlsErr = errors.New(errFetchingUrls)
|
2024-01-24 06:27:05 +00:00
|
|
|
errDownloadingFileErr = errors.New(errDownloadingFile)
|
2024-01-15 04:54:43 +00:00
|
|
|
)
|
|
|
|
|
2024-01-16 16:42:50 +00:00
|
|
|
type HttpHandler struct {
|
2024-01-16 18:32:47 +00:00
|
|
|
verifier *emailverifier.Verifier
|
2024-01-28 07:20:59 +00:00
|
|
|
config *viper.Viper
|
|
|
|
logger *zap.Logger
|
2024-02-01 01:29:27 +00:00
|
|
|
storage *storage.StorageServiceDefault
|
2024-01-28 07:20:59 +00:00
|
|
|
db *gorm.DB
|
2024-02-01 01:29:27 +00:00
|
|
|
accounts *account.AccountServiceDefault
|
2024-01-29 20:11:57 +00:00
|
|
|
protocol *s5.S5Protocol
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
type HttpHandlerParams struct {
|
|
|
|
fx.In
|
2024-01-16 18:32:47 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
Config *viper.Viper
|
|
|
|
Logger *zap.Logger
|
2024-02-01 01:29:27 +00:00
|
|
|
Storage *storage.StorageServiceDefault
|
2024-01-28 07:20:59 +00:00
|
|
|
Db *gorm.DB
|
2024-02-01 01:29:27 +00:00
|
|
|
Accounts *account.AccountServiceDefault
|
2024-01-29 20:11:57 +00:00
|
|
|
Protocol *s5.S5Protocol
|
2024-01-28 07:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 09:18:32 +00:00
|
|
|
type HttpHandlerResult struct {
|
|
|
|
fx.Out
|
|
|
|
|
2024-01-28 09:22:49 +00:00
|
|
|
HttpHandler HttpHandler
|
2024-01-28 09:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHttpHandler(params HttpHandlerParams) (HttpHandlerResult, error) {
|
2024-01-16 18:32:47 +00:00
|
|
|
verifier := emailverifier.NewVerifier()
|
|
|
|
|
|
|
|
verifier.DisableSMTPCheck()
|
|
|
|
verifier.DisableGravatarCheck()
|
|
|
|
verifier.DisableDomainSuggest()
|
|
|
|
verifier.DisableAutoUpdateDisposable()
|
|
|
|
|
2024-01-28 09:18:32 +00:00
|
|
|
return HttpHandlerResult{
|
2024-01-28 09:22:49 +00:00
|
|
|
HttpHandler: HttpHandler{
|
2024-01-28 09:18:32 +00:00
|
|
|
verifier: verifier,
|
|
|
|
config: params.Config,
|
|
|
|
logger: params.Logger,
|
|
|
|
storage: params.Storage,
|
|
|
|
db: params.Db,
|
|
|
|
accounts: params.Accounts,
|
|
|
|
protocol: params.Protocol,
|
|
|
|
},
|
|
|
|
}, nil
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) smallFileUpload(jc jape.Context) {
|
2024-01-15 20:16:48 +00:00
|
|
|
var rs io.ReadSeeker
|
|
|
|
var bufferSize int64
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
r := jc.Request
|
2024-01-15 13:29:09 +00:00
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
if strings.HasPrefix(contentType, "multipart/form-data") {
|
2024-01-15 04:54:43 +00:00
|
|
|
// Parse the multipart form
|
2024-01-28 07:20:59 +00:00
|
|
|
err := r.ParseMultipartForm(h.config.GetInt64("core.post-upload-limit"))
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
if jc.Check(errMultiformParse, err) != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errMultiformParse, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the file from the form data
|
|
|
|
file, _, err := r.FormFile("file")
|
|
|
|
if jc.Check(errRetrievingFile, err) != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errRetrievingFile, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func(file multipart.File) {
|
|
|
|
err := file.Close()
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errClosingStream, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
|
|
|
}(file)
|
|
|
|
|
2024-01-15 20:16:48 +00:00
|
|
|
rs = file
|
2024-01-15 04:54:43 +00:00
|
|
|
} else {
|
2024-01-15 20:16:48 +00:00
|
|
|
data, err := io.ReadAll(r.Body)
|
2024-01-15 04:54:43 +00:00
|
|
|
if jc.Check(errReadFile, err) != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errReadFile, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-15 20:16:48 +00:00
|
|
|
buffer := bytes.NewReader(data)
|
|
|
|
bufferSize = int64(buffer.Len())
|
|
|
|
rs = buffer
|
|
|
|
|
2024-01-15 04:54:43 +00:00
|
|
|
defer func(Body io.ReadCloser) {
|
|
|
|
err := Body.Close()
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errClosingStream, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
|
|
|
}(r.Body)
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
hash, err := h.storage.GetHashSmall(rs)
|
2024-01-17 16:04:24 +00:00
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 16:04:24 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-16 06:05:09 +00:00
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
if exists, upload := h.storage.FileExists(hash.Hash); exists {
|
2024-01-17 16:31:48 +00:00
|
|
|
cid, err := encoding.CIDFromHash(hash, upload.Size, types.CIDTypeRaw, types.HashTypeBlake3)
|
2024-01-16 06:05:09 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-16 06:05:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-16 06:05:09 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-17 22:21:15 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
err = h.accounts.PinByID(upload.ID, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)))
|
2024-01-17 22:21:15 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 22:21:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-18 18:53:04 +00:00
|
|
|
jc.Encode(&SmallUploadResponse{
|
|
|
|
CID: cidStr,
|
|
|
|
})
|
2024-01-16 06:05:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
hash, err = h.storage.PutFileSmall(rs, "s5")
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
h.logger.Info("Hash", zap.String("hash", hex.EncodeToString(hash.Hash)))
|
2024-01-18 19:28:32 +00:00
|
|
|
|
2024-01-17 16:31:48 +00:00
|
|
|
cid, err := encoding.CIDFromHash(hash, uint64(bufferSize), types.CIDTypeRaw, types.HashTypeBlake3)
|
2024-01-15 04:54:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-15 04:54:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Info("CID", zap.String("cidStr", cidStr))
|
2024-01-18 19:28:32 +00:00
|
|
|
|
2024-01-26 00:15:01 +00:00
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-26 00:15:01 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var mimeBytes [512]byte
|
|
|
|
|
|
|
|
_, err = rs.Read(mimeBytes[:])
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-26 00:15:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mimeType := http.DetectContentType(mimeBytes[:])
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
upload, err := h.storage.CreateUpload(hash.Hash, mimeType, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(bufferSize), "s5")
|
2024-01-17 22:16:12 +00:00
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 22:16:12 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
err = h.accounts.PinByID(upload.ID, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)))
|
2024-01-17 19:49:35 +00:00
|
|
|
if err != nil {
|
2024-01-16 07:01:18 +00:00
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-16 07:01:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
jc.Encode(&SmallUploadResponse{
|
|
|
|
CID: cidStr,
|
|
|
|
})
|
2024-01-15 04:54:43 +00:00
|
|
|
}
|
2024-01-16 16:31:33 +00:00
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountRegisterChallenge(jc jape.Context) {
|
2024-01-16 16:31:33 +00:00
|
|
|
var pubkey string
|
|
|
|
if jc.DecodeForm("pubKey", &pubkey) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
challenge := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err := rand.Read(challenge)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountGenerateChallenge, zap.Error(err))
|
2024-01-16 16:31:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountGenerateChallenge, zap.Error(err))
|
2024-01-16 16:31:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:22:48 +00:00
|
|
|
if len(decodedKey) != 33 && int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
2024-01-16 16:31:33 +00:00
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountGenerateChallenge, zap.Error(err))
|
2024-01-16 16:31:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := h.db.Create(&models.S5Challenge{
|
2024-01-16 19:22:16 +00:00
|
|
|
Pubkey: pubkey,
|
2024-01-16 20:12:05 +00:00
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
2024-01-16 18:51:03 +00:00
|
|
|
Type: "register",
|
2024-01-16 16:31:33 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountGenerateChallenge, zap.Error(err))
|
2024-01-16 16:31:33 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
jc.Encode(&AccountRegisterChallengeResponse{
|
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
|
|
|
})
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountRegister(jc jape.Context) {
|
2024-01-16 18:32:47 +00:00
|
|
|
var request AccountRegisterRequest
|
|
|
|
|
|
|
|
if jc.Decode(&request) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errAccountRegisterErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountRegister, zap.Error(err))
|
2024-01-16 18:32:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(request.Pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:26:36 +00:00
|
|
|
if len(decodedKey) != 33 {
|
2024-01-16 18:32:47 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var challenge models.S5Challenge
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := h.db.Model(&models.S5Challenge{}).Where(&models.S5Challenge{Pubkey: request.Pubkey, Type: "register"}).First(&challenge)
|
2024-01-16 18:32:47 +00:00
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedResponse, err := base64.RawURLEncoding.DecodeString(request.Response)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-16 20:26:57 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
2024-01-16 18:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:32:53 +00:00
|
|
|
if len(decodedResponse) != 65 {
|
2024-01-16 20:26:57 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
2024-01-16 18:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedChallenge, err := base64.RawURLEncoding.DecodeString(challenge.Challenge)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-16 20:26:57 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
2024-01-16 18:32:47 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:45:07 +00:00
|
|
|
if !bytes.Equal(decodedResponse[1:33], decodedChallenge) {
|
2024-01-16 18:32:47 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
|
|
|
errored(errPubkeyNotSupported)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedSignature, err := base64.RawURLEncoding.DecodeString(request.Signature)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:37:08 +00:00
|
|
|
if !ed25519.Verify(decodedKey[1:], decodedResponse, decodedSignature) {
|
2024-01-16 18:32:47 +00:00
|
|
|
errored(errInvalidSignatureErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:16:37 +00:00
|
|
|
if request.Email == "" {
|
|
|
|
request.Email = fmt.Sprintf("%s@%s", hex.EncodeToString(decodedKey[1:]), "example.com")
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:32:47 +00:00
|
|
|
verify, _ := h.verifier.Verify(request.Email)
|
|
|
|
|
|
|
|
if !verify.Syntax.Valid {
|
|
|
|
errored(errInvalidEmail)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:29:37 +00:00
|
|
|
accountExists, _, _ := h.accounts.EmailExists(request.Email)
|
2024-01-16 18:32:47 +00:00
|
|
|
|
|
|
|
if accountExists {
|
|
|
|
errored(errEmailAlreadyExists)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:29:37 +00:00
|
|
|
pubkeyExists, _, _ := h.accounts.PubkeyExists(hex.EncodeToString(decodedKey[1:]))
|
2024-01-16 18:32:47 +00:00
|
|
|
|
|
|
|
if pubkeyExists {
|
|
|
|
errored(errPubkeyAlreadyExists)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
passwd := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err = rand.Read(passwd)
|
|
|
|
|
|
|
|
if accountExists {
|
|
|
|
errored(errGeneratingPassword)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
newAccount, err := h.accounts.CreateAccount(request.Email, string(passwd))
|
2024-01-16 18:32:47 +00:00
|
|
|
if err != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:30:00 +00:00
|
|
|
rawPubkey := hex.EncodeToString(decodedKey[1:])
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
err = h.accounts.AddPubkeyToAccount(*newAccount, rawPubkey)
|
2024-01-16 18:32:47 +00:00
|
|
|
if err != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
jwt, err := h.accounts.LoginPubkey(rawPubkey)
|
2024-01-16 18:32:47 +00:00
|
|
|
if err != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result = h.db.Delete(&challenge)
|
2024-01-16 20:48:45 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
errored(errAccountRegisterErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:38:10 +00:00
|
|
|
setAuthCookie(jwt, jc)
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountLoginChallenge(jc jape.Context) {
|
2024-01-16 18:51:03 +00:00
|
|
|
var pubkey string
|
|
|
|
if jc.DecodeForm("pubKey", &pubkey) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errAccountLoginErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountLogin, zap.Error(err))
|
2024-01-16 18:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
challenge := make([]byte, 32)
|
|
|
|
|
|
|
|
_, err := rand.Read(challenge)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errAccountGenerateChallengeErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountGenerateChallenge, zap.Error(err))
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(pubkey)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-16 20:23:00 +00:00
|
|
|
errored(errAccountGenerateChallengeErr)
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:22:48 +00:00
|
|
|
if len(decodedKey) != 33 && int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
|
|
|
errored(errPubkeyNotSupported)
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:29:37 +00:00
|
|
|
pubkeyExists, _, _ := h.accounts.PubkeyExists(hex.EncodeToString(decodedKey[1:]))
|
2024-01-16 21:05:28 +00:00
|
|
|
|
|
|
|
if pubkeyExists {
|
|
|
|
errored(errPubkeyNotExist)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := h.db.Create(&models.S5Challenge{
|
2024-01-16 20:12:05 +00:00
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
2024-01-16 18:51:03 +00:00
|
|
|
Type: "login",
|
|
|
|
})
|
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-01-16 20:23:00 +00:00
|
|
|
errored(result.Error)
|
2024-01-16 18:51:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&AccountLoginChallengeResponse{
|
|
|
|
Challenge: base64.RawURLEncoding.EncodeToString(challenge),
|
|
|
|
})
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountLogin(jc jape.Context) {
|
2024-01-16 18:56:25 +00:00
|
|
|
var request AccountLoginRequest
|
|
|
|
|
|
|
|
if jc.Decode(&request) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errAccountLoginErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errAccountLogin, zap.Error(err))
|
2024-01-16 18:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decodedKey, err := base64.RawURLEncoding.DecodeString(request.Pubkey)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(decodedKey) != 32 {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var challenge models.S5Challenge
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result := h.db.Model(&models.S5Challenge{}).Where(&models.S5Challenge{Pubkey: request.Pubkey, Type: "login"}).First(&challenge)
|
2024-01-16 18:56:25 +00:00
|
|
|
|
|
|
|
if result.RowsAffected == 0 || result.Error != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedResponse, err := base64.RawURLEncoding.DecodeString(request.Response)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:32:53 +00:00
|
|
|
if len(decodedResponse) != 65 {
|
2024-01-16 18:56:25 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedChallenge, err := base64.RawURLEncoding.DecodeString(challenge.Challenge)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 19:45:07 +00:00
|
|
|
if !bytes.Equal(decodedResponse[1:33], decodedChallenge) {
|
2024-01-16 18:56:25 +00:00
|
|
|
errored(errInvalidChallengeErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if int(decodedKey[0]) != int(types.HashTypeEd25519) {
|
|
|
|
errored(errPubkeyNotSupported)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedSignature, err := base64.RawURLEncoding.DecodeString(request.Signature)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 20:37:08 +00:00
|
|
|
if !ed25519.Verify(decodedKey[1:], decodedResponse, decodedSignature) {
|
2024-01-16 18:56:25 +00:00
|
|
|
errored(errInvalidSignatureErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
jwt, err := h.accounts.LoginPubkey(request.Pubkey)
|
2024-01-16 18:56:25 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(errAccountLoginErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
result = h.db.Delete(&challenge)
|
2024-01-16 20:48:45 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
errored(errAccountLoginErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:56:25 +00:00
|
|
|
setAuthCookie(jwt, jc)
|
2024-01-16 16:31:33 +00:00
|
|
|
}
|
2024-01-16 18:38:10 +00:00
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountInfo(jc jape.Context) {
|
2024-02-14 00:29:37 +00:00
|
|
|
_, user, _ := h.accounts.AccountExists(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint))
|
2024-01-17 16:52:54 +00:00
|
|
|
|
|
|
|
info := &AccountInfoResponse{
|
|
|
|
Email: user.Email,
|
|
|
|
QuotaExceeded: false,
|
|
|
|
EmailConfirmed: false,
|
|
|
|
IsRestricted: false,
|
2024-01-17 17:38:52 +00:00
|
|
|
Tier: AccountTier{
|
|
|
|
Id: 1,
|
|
|
|
Name: "default",
|
2024-01-24 17:51:19 +00:00
|
|
|
UploadBandwidth: math.MaxUint32,
|
|
|
|
StorageLimit: math.MaxUint32,
|
2024-01-17 17:38:52 +00:00
|
|
|
Scopes: []interface{}{},
|
|
|
|
},
|
2024-01-17 16:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(info)
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountStats(jc jape.Context) {
|
2024-02-14 00:29:37 +00:00
|
|
|
_, user, _ := h.accounts.AccountExists(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint))
|
2024-01-17 17:03:08 +00:00
|
|
|
|
|
|
|
info := &AccountStatsResponse{
|
|
|
|
AccountInfoResponse: AccountInfoResponse{
|
|
|
|
Email: user.Email,
|
|
|
|
QuotaExceeded: false,
|
|
|
|
EmailConfirmed: false,
|
|
|
|
IsRestricted: false,
|
2024-01-17 17:38:52 +00:00
|
|
|
Tier: AccountTier{
|
|
|
|
Id: 1,
|
|
|
|
Name: "default",
|
2024-01-24 17:51:19 +00:00
|
|
|
UploadBandwidth: math.MaxUint32,
|
|
|
|
StorageLimit: math.MaxUint32,
|
2024-01-17 17:38:52 +00:00
|
|
|
Scopes: []interface{}{},
|
|
|
|
},
|
2024-01-17 17:03:08 +00:00
|
|
|
},
|
|
|
|
Stats: AccountStats{
|
|
|
|
Total: AccountStatsTotal{
|
|
|
|
UsedStorage: 0,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(info)
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountPins(jc jape.Context) {
|
2024-01-17 17:33:05 +00:00
|
|
|
var cursor uint64
|
|
|
|
|
|
|
|
if jc.DecodeForm("cursor", &cursor) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errFailedToGetPinsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFailedToGetPins, zap.Error(err))
|
2024-01-17 17:33:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
pins, err := h.accounts.AccountPins(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64), cursor)
|
2024-01-17 17:33:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-24 17:29:25 +00:00
|
|
|
pinResponse := &AccountPinResponse{Cursor: cursor, Pins: pins}
|
2024-01-17 17:33:05 +00:00
|
|
|
|
2024-01-24 17:29:25 +00:00
|
|
|
result, err := msgpack.Marshal(pinResponse)
|
2024-01-17 17:33:05 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Custom(jc.Request, result)
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = jc.ResponseWriter.Write(result)
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountPinDelete(jc jape.Context) {
|
2024-01-17 18:04:32 +00:00
|
|
|
var cid string
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errFailedToDelPinErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFailedToDelPin, zap.Error(err))
|
2024-01-17 18:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decodedCid, err := encoding.CIDFromString(cid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hash := hex.EncodeToString(decodedCid.Hash.HashBytes())
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
err = h.accounts.DeletePinByHash(hash, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)))
|
2024-01-17 18:04:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusNoContent)
|
2024-01-17 18:13:37 +00:00
|
|
|
}
|
2024-01-17 18:04:32 +00:00
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) accountPin(jc jape.Context) {
|
2024-01-17 18:13:37 +00:00
|
|
|
var cid string
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errFailedToAddPinErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFailedToAddPin, zap.Error(err))
|
2024-01-17 18:13:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
decodedCid, err := encoding.CIDFromString(cid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Info("CID", zap.String("cidStr", cid))
|
|
|
|
h.logger.Info("hash", zap.String("hash", hex.EncodeToString(decodedCid.Hash.HashBytes())))
|
2024-01-18 19:38:15 +00:00
|
|
|
|
2024-01-17 18:13:37 +00:00
|
|
|
hash := hex.EncodeToString(decodedCid.Hash.HashBytes())
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
err = h.accounts.PinByHash(hash, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)))
|
2024-01-17 18:13:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusNoContent)
|
2024-01-17 18:04:32 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) directoryUpload(jc jape.Context) {
|
2024-01-17 19:46:37 +00:00
|
|
|
var tryFiles []string
|
|
|
|
var errorPages map[int]string
|
|
|
|
var name string
|
|
|
|
|
|
|
|
if jc.DecodeForm("tryFiles", &tryFiles) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if jc.DecodeForm("errorPages", &errorPages) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if jc.DecodeForm("name", &name) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r := jc.Request
|
|
|
|
contentType := r.Header.Get("Content-Type")
|
|
|
|
|
|
|
|
errored := func(err error) {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(contentType, "multipart/form-data") {
|
|
|
|
_ = jc.Error(errNotMultiformErr, http.StatusBadRequest)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errorNotMultiform)
|
2024-01-17 19:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
err := r.ParseMultipartForm(h.config.GetInt64("core.post-upload-limit"))
|
2024-01-17 19:46:37 +00:00
|
|
|
|
|
|
|
if jc.Check(errMultiformParse, err) != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errMultiformParse, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadMap := make(map[string]models.Upload, len(r.MultipartForm.File))
|
|
|
|
mimeMap := make(map[string]string, len(r.MultipartForm.File))
|
|
|
|
|
|
|
|
for _, files := range r.MultipartForm.File {
|
|
|
|
for _, fileHeader := range files {
|
|
|
|
// Open the file.
|
|
|
|
file, err := fileHeader.Open()
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func(file multipart.File) {
|
|
|
|
err := file.Close()
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errClosingStream, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
}
|
|
|
|
}(file)
|
|
|
|
|
|
|
|
var rs io.ReadSeeker
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
hash, err := h.storage.GetHashSmall(rs)
|
2024-01-17 19:46:37 +00:00
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
if exists, upload := h.storage.FileExists(hash.Hash); exists {
|
2024-01-17 19:46:37 +00:00
|
|
|
uploadMap[fileHeader.Filename] = upload
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
hash, err = h.storage.PutFileSmall(rs, "s5")
|
2024-01-17 19:46:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-26 00:15:01 +00:00
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-01-17 19:46:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-26 00:15:01 +00:00
|
|
|
var mimeBytes [512]byte
|
|
|
|
|
|
|
|
if _, err := file.Read(mimeBytes[:]); err != nil {
|
2024-01-17 19:46:37 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-26 00:15:01 +00:00
|
|
|
mimeType := http.DetectContentType(mimeBytes[:])
|
2024-01-17 19:46:37 +00:00
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
upload, err := h.storage.CreateUpload(hash.Hash, mimeType, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(fileHeader.Size), "s5")
|
2024-01-26 00:15:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-01-17 19:46:37 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the read pointer back to the start of the file.
|
2024-01-26 00:15:01 +00:00
|
|
|
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
2024-01-17 19:46:37 +00:00
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-26 00:15:01 +00:00
|
|
|
// Read a snippet of the file to determine its MIME type.
|
|
|
|
buffer := make([]byte, 512) // 512 bytes should be enough for http.DetectContentType to determine the type
|
|
|
|
if _, err := file.Read(buffer); err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-17 19:46:37 +00:00
|
|
|
|
|
|
|
uploadMap[fileHeader.Filename] = *upload
|
|
|
|
mimeMap[fileHeader.Filename] = mimeType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filesMap := make(map[string]metadata.WebAppMetadataFileReference, len(uploadMap))
|
|
|
|
|
|
|
|
for name, file := range uploadMap {
|
|
|
|
hashDecoded, err := hex.DecodeString(file.Hash)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := encoding.CIDFromHash(hashDecoded, file.Size, types.CIDTypeRaw, types.HashTypeBlake3)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filesMap[name] = *metadata.NewWebAppMetadataFileReference(cid, mimeMap[name])
|
|
|
|
}
|
|
|
|
|
|
|
|
app := metadata.NewWebAppMetadata(name, tryFiles, *metadata.NewExtraMetadata(map[int]interface{}{}), errorPages, filesMap)
|
|
|
|
|
|
|
|
appData, err := msgpack.Marshal(app)
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rs = bytes.NewReader(appData)
|
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
hash, err := h.storage.GetHashSmall(rs)
|
2024-01-17 19:46:37 +00:00
|
|
|
_, err = rs.Seek(0, io.SeekStart)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
if exists, upload := h.storage.FileExists(hash.Hash); exists {
|
2024-01-17 19:46:37 +00:00
|
|
|
cid, err := encoding.CIDFromHash(hash, upload.Size, types.CIDTypeMetadataWebapp, types.HashTypeBlake3)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errUploadingFile, zap.Error(err))
|
2024-01-17 19:46:37 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
jc.Encode(map[string]string{"hash": cidStr})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-02-09 20:28:50 +00:00
|
|
|
hash, err = h.storage.PutFileSmall(rs, "s5")
|
2024-01-17 19:46:37 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := encoding.CIDFromHash(hash, uint64(len(appData)), types.CIDTypeRaw, types.HashTypeBlake3)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidStr, err := cid.ToString()
|
|
|
|
if err != nil {
|
|
|
|
errored(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&AppUploadResponse{CID: cidStr})
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) debugDownloadUrls(jc jape.Context) {
|
2024-01-17 20:36:21 +00:00
|
|
|
var cid string
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedCid, err := encoding.CIDFromString(cid)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
node := h.getNode()
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
dlUriProvider := h.newStorageLocationProvider(&decodedCid.Hash, types.StorageLocationTypeFull, types.StorageLocationTypeFile, types.StorageLocationTypeBridge)
|
2024-01-17 20:36:21 +00:00
|
|
|
|
|
|
|
err = dlUriProvider.Start()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dlUriProvider.Next()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
locations, err := node.Services().Storage().GetCachedStorageLocations(&decodedCid.Hash, []types.StorageLocationType{
|
2024-01-17 20:36:21 +00:00
|
|
|
types.StorageLocationTypeFull, types.StorageLocationTypeFile, types.StorageLocationTypeBridge,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
availableNodes := lo.Keys[string, libs5storage.StorageLocation](locations)
|
2024-01-17 20:36:21 +00:00
|
|
|
|
|
|
|
availableNodesIds := make([]*encoding.NodeId, len(availableNodes))
|
|
|
|
|
|
|
|
for i, nodeIdStr := range availableNodes {
|
|
|
|
nodeId, err := encoding.DecodeNodeId(nodeIdStr)
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
availableNodesIds[i] = nodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
sorted, err := node.Services().P2P().SortNodesByScore(availableNodesIds)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
output := make([]string, len(sorted))
|
|
|
|
|
|
|
|
for i, nodeId := range sorted {
|
|
|
|
nodeIdStr, err := nodeId.ToString()
|
|
|
|
if err != nil {
|
|
|
|
_ = jc.Error(errFetchingUrlsErr, http.StatusInternalServerError)
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error(errFetchingUrls, zap.Error(err))
|
2024-01-17 20:36:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
output[i] = locations[nodeIdStr].BytesURL()
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = jc.ResponseWriter.Write([]byte(strings.Join(output, "\n")))
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) registryQuery(jc jape.Context) {
|
2024-01-17 21:05:11 +00:00
|
|
|
var pk string
|
|
|
|
|
|
|
|
if jc.DecodeForm("pk", &pk) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pkBytes, err := base64.RawURLEncoding.DecodeString(pk)
|
|
|
|
if jc.Check("error decoding pk", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entry, err := h.getNode().Services().Registry().Get(pkBytes)
|
|
|
|
if jc.Check("error getting registry entry", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if entry == nil {
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&RegistryQueryResponse{
|
|
|
|
Pk: base64.RawURLEncoding.EncodeToString(entry.PK()),
|
|
|
|
Revision: entry.Revision(),
|
|
|
|
Data: base64.RawURLEncoding.EncodeToString(entry.Data()),
|
|
|
|
Signature: base64.RawURLEncoding.EncodeToString(entry.Signature()),
|
|
|
|
})
|
|
|
|
}
|
2024-02-14 00:10:24 +00:00
|
|
|
|
|
|
|
func (h *HttpHandler) registrySet(jc jape.Context) {
|
2024-01-17 21:20:51 +00:00
|
|
|
var request RegistrySetRequest
|
|
|
|
|
|
|
|
if jc.Decode(&request) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pk, err := base64.RawURLEncoding.DecodeString(request.Pk)
|
|
|
|
if jc.Check("error decoding pk", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := base64.RawURLEncoding.DecodeString(request.Data)
|
|
|
|
if jc.Check("error decoding data", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
signature, err := base64.RawURLEncoding.DecodeString(request.Signature)
|
|
|
|
if jc.Check("error decoding signature", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
entry := libs5protocol.NewSignedRegistryEntry(pk, request.Revision, data, signature)
|
2024-01-17 21:20:51 +00:00
|
|
|
|
|
|
|
err = h.getNode().Services().Registry().Set(entry, false, nil)
|
|
|
|
if jc.Check("error setting registry entry", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2024-01-17 21:05:11 +00:00
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) registrySubscription(jc jape.Context) {
|
2024-01-17 21:46:13 +00:00
|
|
|
// Create a context for the WebSocket operations
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var listeners []func()
|
|
|
|
|
|
|
|
// Accept the WebSocket connection
|
|
|
|
c, err := websocket.Accept(jc.ResponseWriter, jc.Request, nil)
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error accepting websocket connection", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func(c *websocket.Conn, code websocket.StatusCode, reason string) {
|
|
|
|
err := c.Close(code, reason)
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error closing websocket connection", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, listener := range listeners {
|
|
|
|
listener()
|
|
|
|
}
|
|
|
|
}(c, websocket.StatusNormalClosure, "connection closed")
|
|
|
|
|
|
|
|
// Main loop for reading messages
|
|
|
|
for {
|
|
|
|
// Read a message (the actual reading and unpacking is skipped here)
|
|
|
|
_, data, err := c.Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
if websocket.CloseStatus(err) == websocket.StatusNormalClosure {
|
|
|
|
// Normal closure
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Info("websocket connection closed normally")
|
2024-01-17 21:46:13 +00:00
|
|
|
} else {
|
|
|
|
// Handle different types of errors
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error in websocket connection", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder := msgpack.NewDecoder(bytes.NewReader(data))
|
|
|
|
|
|
|
|
method, err := decoder.DecodeInt()
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error decoding method", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if method != 2 {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("invalid method", zap.Int64("method", int64(method)))
|
2024-01-17 21:46:13 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
sre, err := decoder.DecodeBytes()
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error decoding sre", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
off, err := h.getNode().Services().Registry().Listen(sre, func(entry libs5protocol.SignedRegistryEntry) {
|
2024-01-17 21:46:13 +00:00
|
|
|
encoded, err := msgpack.Marshal(entry)
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error encoding entry", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = c.Write(ctx, websocket.MessageBinary, encoded)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error writing to websocket", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error listening to registry", zap.Error(err))
|
2024-01-17 21:46:13 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners = append(listeners, off)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
func (h *HttpHandler) getNode() *libs5node.Node {
|
2024-01-28 07:20:59 +00:00
|
|
|
return h.protocol.Node()
|
2024-01-17 20:36:21 +00:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) downloadBlob(jc jape.Context) {
|
2024-01-18 02:23:33 +00:00
|
|
|
var cid string
|
|
|
|
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cid = strings.Split(cid, ".")[0]
|
|
|
|
|
|
|
|
cidDecoded, err := encoding.CIDFromString(cid)
|
|
|
|
if jc.Check("error decoding cid", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
dlUriProvider := h.newStorageLocationProvider(&cidDecoded.Hash, types.StorageLocationTypeFull, types.StorageLocationTypeFile, types.StorageLocationTypeBridge)
|
2024-01-18 02:23:33 +00:00
|
|
|
|
|
|
|
err = dlUriProvider.Start()
|
|
|
|
|
|
|
|
if jc.Check("error starting search", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next, err := dlUriProvider.Next()
|
|
|
|
if jc.Check("error fetching blob", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(jc.ResponseWriter, jc.Request, next.Location().BytesURL(), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) debugStorageLocations(jc jape.Context) {
|
2024-01-18 02:59:27 +00:00
|
|
|
var hash string
|
|
|
|
|
|
|
|
if jc.DecodeParam("hash", &hash) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var kinds string
|
|
|
|
|
|
|
|
if jc.DecodeForm("kinds", &kinds) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
decodedHash, err := encoding.MultihashFromBase64Url(hash)
|
|
|
|
if jc.Check("error decoding hash", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
typeList := strings.Split(kinds, ",")
|
|
|
|
typeIntList := make([]types.StorageLocationType, 0)
|
|
|
|
|
|
|
|
for _, typeStr := range typeList {
|
|
|
|
typeInt, err := strconv.Atoi(typeStr)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
typeIntList = append(typeIntList, types.StorageLocationType(typeInt))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(typeIntList) == 0 {
|
|
|
|
typeIntList = []types.StorageLocationType{
|
|
|
|
types.StorageLocationTypeFull,
|
|
|
|
types.StorageLocationTypeFile,
|
|
|
|
types.StorageLocationTypeBridge,
|
|
|
|
types.StorageLocationTypeArchive,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
dlUriProvider := h.newStorageLocationProvider(decodedHash, typeIntList...)
|
2024-01-18 02:59:27 +00:00
|
|
|
|
|
|
|
err = dlUriProvider.Start()
|
|
|
|
if jc.Check("error starting search", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dlUriProvider.Next()
|
|
|
|
if jc.Check("error fetching locations", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
locations, err := h.getNode().Services().Storage().GetCachedStorageLocations(decodedHash, typeIntList)
|
2024-01-18 02:59:27 +00:00
|
|
|
if jc.Check("error getting cached locations", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
availableNodes := lo.Keys[string, libs5storage.StorageLocation](locations)
|
2024-01-18 02:59:27 +00:00
|
|
|
availableNodesIds := make([]*encoding.NodeId, len(availableNodes))
|
|
|
|
|
|
|
|
for i, nodeIdStr := range availableNodes {
|
|
|
|
nodeId, err := encoding.DecodeNodeId(nodeIdStr)
|
|
|
|
if jc.Check("error decoding node id", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
availableNodesIds[i] = nodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
availableNodesIds, err = h.getNode().Services().P2P().SortNodesByScore(availableNodesIds)
|
|
|
|
|
|
|
|
if jc.Check("error sorting nodes", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debugLocations := make([]DebugStorageLocation, len(availableNodes))
|
|
|
|
|
|
|
|
for i, nodeId := range availableNodesIds {
|
|
|
|
nodeIdStr, err := nodeId.ToBase58()
|
|
|
|
if jc.Check("error encoding node id", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
score, err := h.getNode().Services().P2P().GetNodeScore(nodeId)
|
|
|
|
|
|
|
|
if jc.Check("error getting node score", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
debugLocations[i] = DebugStorageLocation{
|
|
|
|
Type: locations[nodeIdStr].Type(),
|
|
|
|
Parts: locations[nodeIdStr].Parts(),
|
|
|
|
Expiry: locations[nodeIdStr].Expiry(),
|
|
|
|
NodeId: nodeIdStr,
|
|
|
|
Score: score,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&DebugStorageLocationsResponse{
|
|
|
|
Locations: debugLocations,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) downloadMetadata(jc jape.Context) {
|
2024-01-18 03:16:04 +00:00
|
|
|
var cid string
|
|
|
|
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cidDecoded, err := encoding.CIDFromString(cid)
|
|
|
|
if jc.Check("error decoding cid", err) != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error decoding cid", zap.Error(err))
|
2024-01-18 03:16:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cidDecoded.Type {
|
|
|
|
case types.CIDTypeRaw:
|
|
|
|
_ = jc.Error(errors.New("Raw CIDs do not have metadata"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
|
|
|
|
case types.CIDTypeResolver:
|
|
|
|
_ = jc.Error(errors.New("Resolver CIDs not yet supported"), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
meta, err := h.getNode().Services().Storage().GetMetadataByCID(cidDecoded)
|
2024-01-18 03:16:04 +00:00
|
|
|
|
|
|
|
if jc.Check("error getting metadata", err) != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error getting metadata", zap.Error(err))
|
2024-01-18 03:16:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if cidDecoded.Type != types.CIDTypeBridge {
|
|
|
|
jc.ResponseWriter.Header().Set("Cache-Control", "public, max-age=31536000")
|
|
|
|
} else {
|
|
|
|
jc.ResponseWriter.Header().Set("Cache-Control", "public, max-age=60")
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.Encode(&meta)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-02-14 00:10:24 +00:00
|
|
|
func (h *HttpHandler) downloadFile(jc jape.Context) {
|
2024-01-24 06:27:05 +00:00
|
|
|
var cid string
|
|
|
|
|
|
|
|
if jc.DecodeParam("cid", &cid) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-24 22:15:21 +00:00
|
|
|
var hashBytes []byte
|
2024-02-09 20:49:34 +00:00
|
|
|
isProof := false
|
|
|
|
|
2024-02-09 20:58:15 +00:00
|
|
|
if strings.HasSuffix(cid, ".obao") {
|
2024-02-09 20:49:34 +00:00
|
|
|
isProof = true
|
2024-02-09 20:58:15 +00:00
|
|
|
cid = strings.TrimSuffix(cid, ".obao")
|
2024-02-09 20:49:34 +00:00
|
|
|
}
|
2024-01-24 22:15:21 +00:00
|
|
|
|
2024-01-24 06:27:05 +00:00
|
|
|
cidDecoded, err := encoding.CIDFromString(cid)
|
2024-01-24 22:15:21 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
hashDecoded, err := encoding.MultihashFromBase64Url(cid)
|
|
|
|
|
|
|
|
if jc.Check("error decoding as cid or hash", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
hashBytes = hashDecoded.HashBytes()
|
2024-01-25 00:10:19 +00:00
|
|
|
} else {
|
|
|
|
hashBytes = cidDecoded.Hash.HashBytes()
|
|
|
|
}
|
2024-01-24 22:15:21 +00:00
|
|
|
|
2024-01-28 07:20:59 +00:00
|
|
|
file := h.storage.NewFile(hashBytes)
|
2024-01-25 00:08:13 +00:00
|
|
|
|
|
|
|
if !file.Exists() {
|
|
|
|
jc.ResponseWriter.WriteHeader(http.StatusNotFound)
|
2024-01-24 06:27:05 +00:00
|
|
|
return
|
|
|
|
}
|
2024-01-25 00:08:13 +00:00
|
|
|
|
2024-01-24 06:27:05 +00:00
|
|
|
defer func(file io.ReadCloser) {
|
|
|
|
err := file.Close()
|
|
|
|
if err != nil {
|
2024-01-28 07:20:59 +00:00
|
|
|
h.logger.Error("error closing file", zap.Error(err))
|
2024-01-24 06:27:05 +00:00
|
|
|
}
|
|
|
|
}(file)
|
|
|
|
|
2024-02-09 20:49:34 +00:00
|
|
|
if isProof {
|
|
|
|
proof, err := file.Proof()
|
|
|
|
|
|
|
|
if jc.Check("error getting proof", err) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jc.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
|
2024-02-09 20:58:15 +00:00
|
|
|
http.ServeContent(jc.ResponseWriter, jc.Request, fmt.Sprintf("%.obao", file.Name()), file.Modtime(), bytes.NewReader(proof))
|
2024-02-09 20:49:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-26 00:18:35 +00:00
|
|
|
jc.ResponseWriter.Header().Set("Content-Type", file.Mime())
|
|
|
|
|
2024-01-25 00:23:42 +00:00
|
|
|
http.ServeContent(jc.ResponseWriter, jc.Request, file.Name(), file.Modtime(), file)
|
2024-01-24 06:27:05 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 20:11:57 +00:00
|
|
|
func (h *HttpHandler) newStorageLocationProvider(hash *encoding.Multihash, types ...types.StorageLocationType) libs5storage.StorageLocationProvider {
|
2024-01-30 05:33:57 +00:00
|
|
|
return libs5storageProvider.NewStorageLocationProvider(libs5storageProvider.StorageLocationProviderParams{
|
2024-01-29 20:11:57 +00:00
|
|
|
Services: h.getNode().Services(),
|
|
|
|
Hash: hash,
|
|
|
|
LocationTypes: types,
|
|
|
|
ServiceParams: libs5service.ServiceParams{
|
|
|
|
Logger: h.logger,
|
|
|
|
Config: h.getNode().Config(),
|
|
|
|
Db: h.getNode().Db(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-16 18:38:10 +00:00
|
|
|
func setAuthCookie(jwt string, jc jape.Context) {
|
|
|
|
authCookie := http.Cookie{
|
|
|
|
Name: "s5-auth-token",
|
|
|
|
Value: jwt,
|
|
|
|
Path: "/",
|
|
|
|
HttpOnly: true,
|
|
|
|
MaxAge: int(time.Hour.Seconds() * 24),
|
|
|
|
Secure: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
http.SetCookie(jc.ResponseWriter, &authCookie)
|
|
|
|
}
|