feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,423 @@
|
||||
package nodefiles
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const maxExpanded = 256 << 20
|
||||
|
||||
type TrashEntry struct {
|
||||
ID string `json:"id"`
|
||||
OriginalPath string `json:"originalPath"`
|
||||
Name string `json:"name"`
|
||||
DeletedAt time.Time `json:"deletedAt"`
|
||||
Size int64 `json:"size"`
|
||||
}
|
||||
|
||||
func isAdvanced(op string) bool {
|
||||
switch op {
|
||||
case "move", "copy", "zip", "extract", "trash", "trash_list", "restore":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsProtectedPath identifies platform-owned configuration and runtime paths.
|
||||
func IsProtectedPath(name string) bool {
|
||||
return protected(path.Clean(strings.ReplaceAll(name, "\\", "/")))
|
||||
}
|
||||
|
||||
func protected(name string) bool {
|
||||
for _, v := range strings.Split(name, "/") {
|
||||
if v == ".sea-runtime" || v == ".user.ini" || v == ".sea-trash" || strings.HasPrefix(v, ".sea-op-") {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func userPath(root *os.Root, name string) error {
|
||||
if name == "." || path.Clean(name) != name || protected(name) {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
return check(root, name)
|
||||
}
|
||||
func absent(root *os.Root, name string) error {
|
||||
if _, e := root.Lstat(name); e == nil {
|
||||
return fs.ErrExist
|
||||
} else if !errors.Is(e, fs.ErrNotExist) {
|
||||
return e
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func opID() (string, error) {
|
||||
b := make([]byte, 16)
|
||||
_, e := rand.Read(b)
|
||||
return hex.EncodeToString(b), e
|
||||
}
|
||||
func advanced(root *os.Root, q Request, out *Response) error {
|
||||
if q.Operation == "trash_list" {
|
||||
return listTrash(root, out)
|
||||
}
|
||||
if q.Operation == "restore" {
|
||||
if len(q.Path) != 32 || strings.Trim(q.Path, "0123456789abcdef") != "" {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if e := userPath(root, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
if e := absent(root, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
from := ".sea-trash/" + q.Path
|
||||
if e := check(root, from); e != nil {
|
||||
return e
|
||||
}
|
||||
if e := root.MkdirAll(path.Dir(q.To), 0770); e != nil {
|
||||
return e
|
||||
}
|
||||
if e := root.Rename(from, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
_ = root.Remove(from + ".json")
|
||||
return nil
|
||||
}
|
||||
if e := userPath(root, q.Path); e != nil {
|
||||
return e
|
||||
}
|
||||
if q.Operation == "trash" {
|
||||
id, e := opID()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = root.MkdirAll(".sea-trash", 0770); e != nil {
|
||||
return e
|
||||
}
|
||||
if e = check(root, ".sea-trash"); e != nil {
|
||||
return e
|
||||
}
|
||||
i, e := root.Lstat(q.Path)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
entry := TrashEntry{ID: id, OriginalPath: q.Path, Name: path.Base(q.Path), DeletedAt: time.Now().UTC(), Size: i.Size()}
|
||||
raw, _ := json.Marshal(entry)
|
||||
f, e := root.OpenFile(".sea-trash/"+id+".json", os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
_, e = f.Write(raw)
|
||||
closeErr := f.Close()
|
||||
if e == nil {
|
||||
e = closeErr
|
||||
}
|
||||
if e == nil {
|
||||
e = root.Rename(q.Path, ".sea-trash/"+id)
|
||||
}
|
||||
if e != nil {
|
||||
root.Remove(".sea-trash/" + id + ".json")
|
||||
}
|
||||
return e
|
||||
}
|
||||
if e := userPath(root, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
if e := absent(root, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
if q.To == q.Path || strings.HasPrefix(q.To, q.Path+"/") {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if q.Operation == "move" {
|
||||
return root.Rename(q.Path, q.To)
|
||||
}
|
||||
budget := q.Budget
|
||||
if budget > maxExpanded {
|
||||
budget = maxExpanded
|
||||
}
|
||||
id, e := opID()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
stage := path.Join(path.Dir(q.To), ".sea-op-"+id)
|
||||
defer root.RemoveAll(stage)
|
||||
switch q.Operation {
|
||||
case "copy":
|
||||
if e = copyTree(root, q.Path, stage, &budget, new(int)); e != nil {
|
||||
return e
|
||||
}
|
||||
case "zip":
|
||||
f, e := root.OpenFile(stage, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
z := zip.NewWriter(&budgetWriter{w: f, left: budget})
|
||||
e = zipTree(root, z, q.Path, path.Base(q.Path), new(int), newZipBudget())
|
||||
if closeErr := z.Close(); e == nil {
|
||||
e = closeErr
|
||||
}
|
||||
if closeErr := f.Close(); e == nil {
|
||||
e = closeErr
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
case "extract":
|
||||
f, e := root.Open(q.Path)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
i, e := f.Stat()
|
||||
if e != nil || !i.Mode().IsRegular() || i.Size() > MaxFileBytes {
|
||||
f.Close()
|
||||
return fs.ErrPermission
|
||||
}
|
||||
data, e := io.ReadAll(io.LimitReader(f, MaxFileBytes+1))
|
||||
f.Close()
|
||||
if e != nil || len(data) > MaxFileBytes {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
z, e := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if len(z.File) > 10000 {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if e = root.Mkdir(stage, 0770); e != nil {
|
||||
return e
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
for _, entry := range z.File {
|
||||
name := strings.TrimSuffix(entry.Name, "/")
|
||||
if name == "" || path.Clean(name) != name || !validName(name) || strings.HasPrefix(name, "/") || protected(name) || entry.Mode()&fs.ModeSymlink != 0 || (!entry.Mode().IsRegular() && !entry.FileInfo().IsDir()) || seen[name] {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
seen[name] = true
|
||||
target := path.Join(stage, name)
|
||||
if e = check(root, target); e != nil {
|
||||
return e
|
||||
}
|
||||
if entry.FileInfo().IsDir() {
|
||||
if e = root.MkdirAll(target, 0770); e != nil {
|
||||
return e
|
||||
}
|
||||
continue
|
||||
}
|
||||
if entry.UncompressedSize64 > budget {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if e = root.MkdirAll(path.Dir(target), 0770); e != nil {
|
||||
return e
|
||||
}
|
||||
input, e := entry.Open()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
output, e := root.OpenFile(target, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
||||
if e != nil {
|
||||
input.Close()
|
||||
return e
|
||||
}
|
||||
n, e := io.Copy(output, io.LimitReader(input, int64(budget)+1))
|
||||
input.Close()
|
||||
output.Close()
|
||||
if e != nil || uint64(n) > budget {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
budget -= uint64(n)
|
||||
}
|
||||
default:
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if e = check(root, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
if e = absent(root, q.To); e != nil {
|
||||
return e
|
||||
}
|
||||
return root.Rename(stage, q.To)
|
||||
}
|
||||
|
||||
type budgetWriter struct {
|
||||
w io.Writer
|
||||
left uint64
|
||||
}
|
||||
|
||||
func (w *budgetWriter) Write(b []byte) (int, error) {
|
||||
if uint64(len(b)) > w.left {
|
||||
return 0, fs.ErrPermission
|
||||
}
|
||||
n, e := w.w.Write(b)
|
||||
w.left -= uint64(n)
|
||||
return n, e
|
||||
}
|
||||
func copyTree(root *os.Root, from, to string, budget *uint64, count *int) error {
|
||||
*count++
|
||||
if *count > 10000 {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if e := check(root, from); e != nil {
|
||||
return e
|
||||
}
|
||||
i, e := root.Lstat(from)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if i.IsDir() {
|
||||
if e = root.Mkdir(to, 0770); e != nil {
|
||||
return e
|
||||
}
|
||||
dir, e := root.Open(from)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
entries, e := dir.ReadDir(10001)
|
||||
dir.Close()
|
||||
if e != nil && e != io.EOF {
|
||||
return e
|
||||
}
|
||||
for _, entry := range entries {
|
||||
source := path.Join(from, entry.Name())
|
||||
if protected(source) {
|
||||
continue
|
||||
}
|
||||
if e = copyTree(root, source, path.Join(to, entry.Name()), budget, count); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !i.Mode().IsRegular() || uint64(i.Size()) > *budget {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
input, e := root.Open(from)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer input.Close()
|
||||
output, e := root.OpenFile(to, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
n, e := io.Copy(output, io.LimitReader(input, int64(*budget)+1))
|
||||
output.Close()
|
||||
if e != nil || uint64(n) > *budget {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
*budget -= uint64(n)
|
||||
return nil
|
||||
}
|
||||
func zipTree(root *os.Root, z *zip.Writer, name, archiveName string, count *int, inputBudget *uint64) error {
|
||||
*count++
|
||||
if *count > 10000 {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if e := check(root, name); e != nil {
|
||||
return e
|
||||
}
|
||||
i, e := root.Lstat(name)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if i.IsDir() {
|
||||
dir, e := root.Open(name)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
entries, e := dir.ReadDir(10001)
|
||||
dir.Close()
|
||||
if e != nil && e != io.EOF {
|
||||
return e
|
||||
}
|
||||
for _, entry := range entries {
|
||||
source := path.Join(name, entry.Name())
|
||||
if protected(source) {
|
||||
continue
|
||||
}
|
||||
if e = zipTree(root, z, source, path.Join(archiveName, entry.Name()), count, inputBudget); e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if !i.Mode().IsRegular() {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
if uint64(i.Size()) > *inputBudget {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
*inputBudget -= uint64(i.Size())
|
||||
header, e := zip.FileInfoHeader(i)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
header.Name = archiveName
|
||||
header.Method = zip.Deflate
|
||||
dst, e := z.CreateHeader(header)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
f, e := root.Open(name)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer f.Close()
|
||||
_, e = io.Copy(dst, io.LimitReader(f, maxExpanded+1))
|
||||
return e
|
||||
}
|
||||
func listTrash(root *os.Root, out *Response) error {
|
||||
out.Trash = []TrashEntry{}
|
||||
if e := check(root, ".sea-trash"); e != nil {
|
||||
return e
|
||||
}
|
||||
dir, e := root.Open(".sea-trash")
|
||||
if errors.Is(e, fs.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
entries, e := dir.ReadDir(10001)
|
||||
dir.Close()
|
||||
if e != nil && e != io.EOF {
|
||||
return e
|
||||
}
|
||||
if len(entries) > 10000 {
|
||||
return fs.ErrPermission
|
||||
}
|
||||
for _, entry := range entries {
|
||||
if !strings.HasSuffix(entry.Name(), ".json") {
|
||||
continue
|
||||
}
|
||||
name := ".sea-trash/" + entry.Name()
|
||||
if check(root, name) != nil {
|
||||
continue
|
||||
}
|
||||
f, e := root.Open(name)
|
||||
if e != nil {
|
||||
continue
|
||||
}
|
||||
var v TrashEntry
|
||||
e = json.NewDecoder(io.LimitReader(f, 4096)).Decode(&v)
|
||||
f.Close()
|
||||
if e == nil && len(v.ID) == 32 && strings.Trim(v.ID, "0123456789abcdef") == "" && entry.Name() == v.ID+".json" && userPath(root, v.OriginalPath) == nil {
|
||||
if _, e = root.Lstat(".sea-trash/" + v.ID); e == nil {
|
||||
out.Trash = append(out.Trash, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newZipBudget() *uint64 { v := uint64(maxExpanded); return &v }
|
||||
Reference in new issue
Block a user