removing everything to do final version of packets client
This commit is contained in:
@@ -1,80 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"packets/internal/packet"
|
||||
utils_lua "packets/internal/utils/lua"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (container Container) ExecutePrepare(packetLua packet.PacketLua, L *lua.LState) error {
|
||||
|
||||
if packetLua.Prepare == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
gitTable := L.NewTable()
|
||||
|
||||
gitTable.RawSetString("clone", L.NewFunction(utils_lua.LGitClone))
|
||||
gitTable.RawSetString("checkout", L.NewFunction(utils_lua.LGitCheckout))
|
||||
gitTable.RawSetString("pull", L.NewFunction(utils_lua.LGitPUll))
|
||||
|
||||
containerTable := L.NewTable()
|
||||
|
||||
containerTable.RawSetString("dir", L.NewFunction(container.lDir))
|
||||
|
||||
L.SetGlobal("git", gitTable)
|
||||
L.Push(packetLua.Prepare)
|
||||
L.Push(containerTable)
|
||||
|
||||
return L.PCall(1, 0, nil)
|
||||
|
||||
}
|
||||
|
||||
func (container Container) ExecuteBuild(packetLua packet.PacketLua, L *lua.LState) error {
|
||||
|
||||
if packetLua.Build == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
ioObject := L.GetGlobal("io").(*lua.LTable)
|
||||
|
||||
OnlyContainerOS := L.NewTable()
|
||||
|
||||
OnlyContainerOS.RawSetString("copy", L.NewFunction(container.lCopy))
|
||||
OnlyContainerOS.RawSetString("mkdir", L.NewFunction(container.lMkdir))
|
||||
OnlyContainerOS.RawSetString("rename", L.NewFunction(container.lRename))
|
||||
OnlyContainerOS.RawSetString("remove", L.NewFunction(container.lRemove))
|
||||
OnlyContainerOS.RawSetString("execute", L.NewFunction(container.lexecute))
|
||||
OnlyContainerOS.RawSetString("open", L.NewFunction(container.lOpen))
|
||||
|
||||
OnlyContainerIO := L.NewTable()
|
||||
|
||||
OnlyContainerIO.RawSetString("popen", L.NewFunction(container.lpopen))
|
||||
|
||||
L.SetGlobal("io", OnlyContainerIO)
|
||||
L.SetGlobal("os", OnlyContainerOS)
|
||||
|
||||
L.Push(packetLua.Build)
|
||||
err := L.PCall(0, 0, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
L.SetGlobal("os", osObject)
|
||||
L.SetGlobal("io", ioObject)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (container Container) ExecuteInstall(packetLua packet.PacketLua, L *lua.LState) error {
|
||||
|
||||
containerTable := L.NewTable()
|
||||
|
||||
containerTable.RawSetString("dir", L.NewFunction(container.lDir))
|
||||
|
||||
L.Push(packetLua.Install)
|
||||
L.Push(containerTable)
|
||||
|
||||
return L.PCall(1, 0, nil)
|
||||
}
|
||||
@@ -1,167 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"packets/internal/packet"
|
||||
"packets/internal/utils"
|
||||
utils_lua "packets/internal/utils/lua"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
)
|
||||
|
||||
func (container Container) installPackage(file []byte, destDir string) error {
|
||||
manifest, err := packet.ReadPacketFromFile(bytes.NewReader(file))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
zstdReader, err := zstd.NewReader(bytes.NewReader(file))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer zstdReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(zstdReader)
|
||||
|
||||
uid, err := utils.GetPacketsUID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for {
|
||||
hdr, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rel := filepath.Clean(hdr.Name)
|
||||
|
||||
if rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Join(container.Root, destDir), 0775); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Chown(filepath.Join(container.Root, destDir), uid, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
absPath := filepath.Join(filepath.Join(container.Root, destDir), rel)
|
||||
|
||||
switch hdr.Typeflag {
|
||||
|
||||
case tar.TypeDir:
|
||||
err = os.MkdirAll(absPath, os.FileMode(hdr.Mode))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chown(absPath, uid, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case tar.TypeReg:
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(absPath), 0775)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
out, err := os.Create(absPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(out, tarReader)
|
||||
out.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.Chmod(absPath, os.FileMode(0775))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if filepath.Base(hdr.Name) == "Packet.lua" {
|
||||
err = os.Chmod(absPath, os.FileMode(0755))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := os.Chown(absPath, uid, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
L, err := utils_lua.GetSandBox()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bootstrapcontainer, err := NewContainer(manifest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
os.Chdir(destDir)
|
||||
|
||||
if err := bootstrapcontainer.ExecutePrepare(manifest, &L); err != nil {
|
||||
return fmt.Errorf("error executing prepare: %s", err)
|
||||
}
|
||||
|
||||
if err := bootstrapcontainer.ExecuteBuild(manifest, &L); err != nil {
|
||||
return fmt.Errorf("error executing build: %s", err)
|
||||
}
|
||||
|
||||
if err := utils.ChangeToNoPermission(); err != nil {
|
||||
return fmt.Errorf("error changing to packet user: %s", err)
|
||||
}
|
||||
if err := bootstrapcontainer.ExecuteInstall(manifest, &L); err != nil {
|
||||
return fmt.Errorf("error executing build: %s", err)
|
||||
}
|
||||
|
||||
if err := utils.ElevatePermission(); err != nil {
|
||||
return fmt.Errorf("error changing to root: %s", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (container Container) asyncFullInstallDependencie(dep string, storePackages bool, installPath string, wg *sync.WaitGroup) {
|
||||
defer wg.Done()
|
||||
|
||||
fmt.Printf(" downloading %s \n", dep)
|
||||
p, err := utils.GetPackage(dep)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Printf(" installing %s \n", dep)
|
||||
|
||||
if err := container.installPackage(p.PackageF, installPath); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if storePackages {
|
||||
_, err := p.Write()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,224 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
utils_lua "packets/internal/utils/lua"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func (container Container) lRemove(L *lua.LState) int {
|
||||
filename := L.CheckString(1)
|
||||
|
||||
err := container.FS.RemoveAll(filename)
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (container Container) lRename(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
if err := container.FS.Rename(oldname, newname); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
return 1
|
||||
}
|
||||
|
||||
func (container Container) lCopy(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
if err := container.copyContainer(oldname, newname); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func modeFlags(mode string) int {
|
||||
switch mode {
|
||||
case "r", "rb":
|
||||
return os.O_RDONLY
|
||||
case "w", "wb":
|
||||
return os.O_CREATE | os.O_WRONLY | os.O_TRUNC
|
||||
case "a", "ab":
|
||||
return os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
||||
case "r+", "r+b", "rb+", "br+":
|
||||
return os.O_RDWR
|
||||
case "w+", "w+b", "wb+", "bw+":
|
||||
return os.O_CREATE | os.O_RDWR | os.O_TRUNC
|
||||
case "a+", "a+b", "ab+", "ba+":
|
||||
return os.O_CREATE | os.O_RDWR | os.O_APPEND
|
||||
default:
|
||||
return os.O_RDONLY
|
||||
}
|
||||
}
|
||||
|
||||
func (container Container) lOpen(L *lua.LState) int {
|
||||
path := L.CheckString(1)
|
||||
mode := L.OptString(2, "r")
|
||||
|
||||
file, err := container.FS.OpenFile(path, modeFlags(mode), 0o644)
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
ud := L.NewUserData()
|
||||
ud.Value = file
|
||||
L.SetMetatable(ud, L.GetTypeMetatable("file"))
|
||||
L.Push(ud)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (container Container) lMkdir(L *lua.LState) int {
|
||||
path := L.CheckString(1)
|
||||
perm := L.CheckInt(2)
|
||||
|
||||
if err := container.FS.MkdirAll(path, os.FileMode(perm)); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func (container Container) lexecute(L *lua.LState) int {
|
||||
cmdString := L.CheckString(1)
|
||||
|
||||
cmdSlice := strings.Fields(cmdString)
|
||||
|
||||
files, err := afero.ReadDir(container.FS, BinDir)
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString("exit"))
|
||||
L.Push(lua.LNumber(127))
|
||||
return 3
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if !file.IsDir() && file.Name() == cmdSlice[0] {
|
||||
err := exec.Command(cmdSlice[0], cmdSlice[1:]...).Run()
|
||||
if err != nil {
|
||||
if errr := err.(*exec.ExitError); errr != nil {
|
||||
L.Push(lua.LFalse)
|
||||
|
||||
if err.(*exec.ExitError).Exited() {
|
||||
L.Push(lua.LString("exit"))
|
||||
} else {
|
||||
L.Push(lua.LString("signal"))
|
||||
}
|
||||
|
||||
L.Push(lua.LNumber(err.(*exec.ExitError).ExitCode()))
|
||||
return 3
|
||||
}
|
||||
}
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LString("exit"))
|
||||
L.Push(lua.LNumber(0))
|
||||
}
|
||||
}
|
||||
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString("exit"))
|
||||
L.Push(lua.LNumber(127))
|
||||
return 3
|
||||
}
|
||||
|
||||
func (container Container) lpopen(L *lua.LState) int {
|
||||
cmdString := L.CheckString(1)
|
||||
mode := L.CheckString(2)
|
||||
|
||||
cmdSlice := strings.Fields(cmdString)
|
||||
|
||||
files, err := afero.ReadDir(container.FS, BinDir)
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString("can't find executable"))
|
||||
return 2
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if !file.IsDir() && file.Name() == cmdSlice[0] {
|
||||
cmd := exec.Command(cmdSlice[0], cmdSlice[1:]...)
|
||||
output, _ := cmd.CombinedOutput()
|
||||
|
||||
switch mode {
|
||||
case "r":
|
||||
ud := L.NewUserData()
|
||||
ud.Value = string(output)
|
||||
L.SetMetatable(ud, L.GetTypeMetatable("file"))
|
||||
L.Push(ud)
|
||||
L.Push(lua.LNil)
|
||||
case "w":
|
||||
ud := L.NewUserData()
|
||||
ud.Value = string(output)
|
||||
os.Stdout.Write(output)
|
||||
L.SetMetatable(ud, L.GetTypeMetatable("file"))
|
||||
L.Push(ud)
|
||||
L.Push(lua.LNil)
|
||||
default:
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(fmt.Sprintf("%s: Invalid argument", cmdString)))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString("can't find any executable"))
|
||||
return 2
|
||||
}
|
||||
|
||||
func (container Container) lDir(L *lua.LState) int {
|
||||
dir := L.CheckString(1)
|
||||
|
||||
L.Push(lua.LString(filepath.Join(container.Root, filepath.Clean(dir))))
|
||||
return 1
|
||||
}
|
||||
|
||||
func (container Container) GetLuaState() {
|
||||
|
||||
L := lua.NewState()
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
ioObject := L.GetGlobal("io").(*lua.LTable)
|
||||
|
||||
L.SetGlobal("path_join", L.NewFunction(utils_lua.Ljoin))
|
||||
|
||||
osObject.RawSetString("remove", L.NewFunction(container.lRemove))
|
||||
osObject.RawSetString("rename", L.NewFunction(container.lRename))
|
||||
osObject.RawSetString("copy", L.NewFunction(container.lCopy))
|
||||
|
||||
osObject.RawSetString("mkdir", L.NewFunction(container.lMkdir))
|
||||
|
||||
ioObject.RawSetString("popen", L.NewFunction(container.lpopen))
|
||||
ioObject.RawSetString("open", L.NewFunction(container.lOpen))
|
||||
|
||||
container.LuaState = *L
|
||||
}
|
||||
@@ -1,183 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"packets/internal/consts"
|
||||
"packets/internal/packet"
|
||||
"path/filepath"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
BuildID BuildID
|
||||
Root string
|
||||
FS afero.Fs
|
||||
LuaState lua.LState
|
||||
Manifest packet.PacketLua
|
||||
uses int
|
||||
DeleteAfter bool
|
||||
}
|
||||
|
||||
func NewContainer(manifest packet.PacketLua) (Container, error) {
|
||||
|
||||
var container Container
|
||||
var err error
|
||||
container.BuildID, err = getBuildId(manifest.BuildDependencies)
|
||||
if err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
baseFs := afero.NewOsFs()
|
||||
|
||||
db, err := sql.Open("sqlite", consts.InstalledDB)
|
||||
if err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
if err := db.QueryRow("SELECT uses, dir FROM build_dependencies WHERE id = ? ", container.BuildID).Scan(&container.uses, container.Root); err != nil {
|
||||
db.Close()
|
||||
return Container{}, err
|
||||
}
|
||||
db.Close()
|
||||
|
||||
if container.Root != "/dev/null" {
|
||||
if _, err := os.Stat(container.Root); err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
if err := container.createNew(); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
container.DeleteAfter = true
|
||||
if err := container.createNew(); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
}
|
||||
|
||||
container.GetLuaState()
|
||||
fileSystem := afero.NewBasePathFs(baseFs, container.Root)
|
||||
|
||||
container.Manifest = manifest
|
||||
container.FS = fileSystem
|
||||
|
||||
if err := container.FS.MkdirAll(BinDir, 0777); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
if err := container.FS.MkdirAll("/etc/packets", 0777); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
if err := afero.Symlinker.SymlinkIfPossible(container.FS.(afero.Symlinker), BinDir, SymLinkBinDir); err != nil {
|
||||
return Container{}, err
|
||||
}
|
||||
|
||||
return container, nil
|
||||
}
|
||||
|
||||
func (container Container) CopyHostToContainer(src string, dest string) error {
|
||||
stats, err := os.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if stats.IsDir() {
|
||||
files, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.FS.MkdirAll(dest, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
srcPath := filepath.Join(src, file.Name())
|
||||
destPath := filepath.Join(dest, file.Name())
|
||||
|
||||
if file.IsDir() {
|
||||
if err := container.CopyHostToContainer(srcPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := container.copySingleFile(srcPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := container.copySingleFile(src, dest); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (container Container) copySingleFile(source string, destination string) error {
|
||||
src, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
stats, err := src.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.FS.MkdirAll(filepath.Dir(destination), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
dst, err := container.FS.Create(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, src); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.FS.Chmod(destination, stats.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getBuildId(buildDependencies map[string]string) (BuildID, error) {
|
||||
blobs, err := json.Marshal(buildDependencies)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return BuildID(base64.StdEncoding.EncodeToString(blobs)), nil
|
||||
}
|
||||
|
||||
func (container Container) saveBuild() error {
|
||||
db, err := sql.Open("sqlite", consts.InstalledDB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
buildID := container.BuildID
|
||||
var exists bool
|
||||
if err := db.QueryRow("SELECT EXISTS(SELECT 1 FROM build_dependencies WHERE id = ?)", buildID).Scan(exists); err != nil {
|
||||
return err
|
||||
}
|
||||
if exists {
|
||||
_, err := db.Exec("UPDATE FROM build_dependencies WHERE id = ? SET uses = uses + 1", buildID)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = db.Exec("INSERT INTO build_dependencies (id) VALUES (?)", buildID)
|
||||
return err
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"os"
|
||||
"packets/configs"
|
||||
"packets/internal/consts"
|
||||
"packets/internal/utils"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func (container Container) createNew() error {
|
||||
if err := os.MkdirAll(filepath.Join(consts.BuildImagesDir, string(container.BuildID)), 0775); err != nil {
|
||||
return err
|
||||
}
|
||||
packetsuid, err := utils.GetPacketsUID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Chown(filepath.Join(consts.BuildImagesDir, string(container.BuildID)), packetsuid, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
dependencies, err := utils.ResolvDependencies(container.Manifest.BuildDependencies)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg, err := configs.GetConfigTOML()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
for _, depn := range dependencies {
|
||||
wg.Add(1)
|
||||
go container.asyncFullInstallDependencie(depn, cfg.Config.StorePackages, depn, &wg)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
container.Root = filepath.Join(consts.BuildImagesDir, string(container.BuildID))
|
||||
|
||||
container.saveBuild()
|
||||
return nil
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package build
|
||||
|
||||
const (
|
||||
BinDir = "/usr/bin"
|
||||
SymLinkBinDir = "/bin"
|
||||
)
|
||||
|
||||
type BuildID string // Json dependencies encoded to base64 stdEncoding
|
||||
@@ -1,79 +0,0 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
func (container Container) copyContainer(src string, dest string) error {
|
||||
stats, err := container.FS.Stat(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if stats.IsDir() {
|
||||
files, err := afero.ReadDir(container.FS, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.FS.MkdirAll(dest, 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
srcPath := filepath.Join(src, file.Name())
|
||||
destPath := filepath.Join(dest, file.Name())
|
||||
|
||||
if file.IsDir() {
|
||||
if err := container.CopyHostToContainer(srcPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := container.copySingleFileLtoL(srcPath, destPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := container.copySingleFileLtoL(src, dest); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (container Container) copySingleFileLtoL(source string, destination string) error {
|
||||
src, err := container.FS.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
stats, err := src.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.FS.MkdirAll(filepath.Dir(destination), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
dst, err := container.FS.Create(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dst.Close()
|
||||
|
||||
if _, err := io.Copy(dst, src); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := container.FS.Chmod(destination, stats.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
package consts
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
DefaultLinux_d = "/etc/packets"
|
||||
DefaultCache_d = "/var/cache/packets"
|
||||
DefaultHttpPort = 9123
|
||||
DefaultBin_d = "/usr/local/bin"
|
||||
DefaultData_d = "/opt/packets"
|
||||
LANDeadline = 2 * time.Second
|
||||
IndexDB = "/etc/packets/index.db"
|
||||
InstalledDB = "/etc/packets/installed.db"
|
||||
BuildImagesDir = "/etc/packets/temp"
|
||||
DefaultSyncUrl = "https://servidordomal.fun/index.db"
|
||||
)
|
||||
|
||||
const InstalledDatabaseSchema = `CREATE TABLE IF NOT EXISTS packages (
|
||||
query_name TEXT NOT NULL UNIQUE PRIMARY KEY,
|
||||
id TEXT NOT NULL UNIQUE,
|
||||
version TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
package_d TEXT NOT NULL,
|
||||
filename TEXT NOT NULL,
|
||||
os TEXT NOT NULL,
|
||||
arch TEXT NOT NULL,
|
||||
in_cache INTEGER NOT NULL DEFAULT 1,
|
||||
serial INTEGER NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
|
||||
UNIQUE(query_name, version),
|
||||
UNIQUE(query_name, serial)
|
||||
);
|
||||
|
||||
CREATE TABLE package_dependencies(
|
||||
package_id TEXT NOT NULL,
|
||||
dependency_name TEXT NOT NULL,
|
||||
version_constraint TEXT NOT NULL,
|
||||
|
||||
PRIMARY KEY (package_id, dependency_name)
|
||||
);
|
||||
|
||||
CREATE INDEX index_dependency_name ON package_dependencies(dependency_name);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS build_dependencies (
|
||||
id TEXT PRIMARY KEY,
|
||||
dir TEXT NOT NULL DEFAULT "/dev/null",
|
||||
uses INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
`
|
||||
@@ -1,11 +0,0 @@
|
||||
package errors_packets
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrResponseNot200OK = errors.New("the request is not 200, download failed")
|
||||
ErrCantFindPacketDotLua = errors.New("can't find manifest.toml when trying to read the packagefile")
|
||||
ErrInvalidSignature = errors.New("the signature is invalid")
|
||||
ErrNotInstalled = errors.New("the package isn't installed")
|
||||
ErrAlredyUpToDate = errors.New("alredy up to date")
|
||||
)
|
||||
@@ -1,65 +0,0 @@
|
||||
package packet
|
||||
|
||||
import lua "github.com/yuin/gopher-lua"
|
||||
|
||||
func getStringFromTable(table *lua.LTable, key string) string {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() == lua.LTString {
|
||||
return value.String()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getIntFromTable(table *lua.LTable, key string) int {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() == lua.LTNumber {
|
||||
if num, ok := value.(lua.LNumber); ok {
|
||||
return int(num)
|
||||
}
|
||||
}
|
||||
return -133
|
||||
}
|
||||
|
||||
func getStringArrayFromTable(L *lua.LState, table *lua.LTable, key string) []string {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() != lua.LTTable {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
arrayTable := value.(*lua.LTable)
|
||||
var result []string
|
||||
|
||||
arrayTable.ForEach(func(_, value lua.LValue) {
|
||||
if value.Type() == lua.LTString {
|
||||
result = append(result, value.String())
|
||||
}
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func getFunctionFromTable(table *lua.LTable, key string) *lua.LFunction {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() == lua.LTFunction {
|
||||
return value.(*lua.LFunction)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getDependenciesFromTable(L *lua.LState, table *lua.LTable, key string) map[string]string {
|
||||
value := table.RawGetString(key)
|
||||
if value.Type() != lua.LTTable {
|
||||
return map[string]string{}
|
||||
}
|
||||
|
||||
depsTable := value.(*lua.LTable)
|
||||
dependencies := make(map[string]string)
|
||||
|
||||
depsTable.ForEach(func(key, value lua.LValue) {
|
||||
if key.Type() == lua.LTString && value.Type() == lua.LTString {
|
||||
dependencies[key.String()] = value.String()
|
||||
}
|
||||
})
|
||||
|
||||
return dependencies
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
package packet
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"fmt"
|
||||
"io"
|
||||
"packets/configs"
|
||||
errors_packets "packets/internal/errors"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
"github.com/go-git/go-git/v6/storage/memory"
|
||||
"github.com/klauspost/compress/zstd"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
type PacketLua struct {
|
||||
Name string
|
||||
Id string
|
||||
Version string
|
||||
Description string
|
||||
Dependencies map[string]string
|
||||
Author string
|
||||
Architetures []string
|
||||
Os []string
|
||||
Serial int
|
||||
Type string
|
||||
|
||||
PkgType string
|
||||
GitUrl string
|
||||
GitBranch string
|
||||
|
||||
BuildDependencies map[string]string
|
||||
|
||||
Prepare *lua.LFunction
|
||||
Build *lua.LFunction
|
||||
Install *lua.LFunction
|
||||
Remove *lua.LFunction
|
||||
}
|
||||
|
||||
// ReadPacket read a Packet.lua and alredy set global vars
|
||||
func ReadPacket(f []byte) (PacketLua, error) {
|
||||
cfg, err := configs.GetConfigTOML()
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
ioObject := L.GetGlobal("io").(*lua.LTable)
|
||||
|
||||
L.SetGlobal("os", lua.LNil)
|
||||
L.SetGlobal("io", lua.LNil)
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.Config.Bin_d))
|
||||
L.SetGlobal("ARCH", lua.LString(runtime.GOARCH))
|
||||
L.SetGlobal("OS", lua.LString(runtime.GOOS))
|
||||
|
||||
if err := L.DoString(string(f)); err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
L.SetGlobal("os", osObject)
|
||||
L.SetGlobal("io", ioObject)
|
||||
|
||||
tableLua := L.Get(-1)
|
||||
|
||||
if tableLua.Type() != lua.LTTable {
|
||||
return PacketLua{}, fmt.Errorf("invalid Packet.lua format: the file do not return a table")
|
||||
}
|
||||
|
||||
table := tableLua.(*lua.LTable)
|
||||
|
||||
pkgTableLua := table.RawGetString("package")
|
||||
if pkgTableLua.Type() != lua.LTTable {
|
||||
return PacketLua{}, fmt.Errorf("invalid Packet.lua format: can't find package table")
|
||||
}
|
||||
pkgTable := pkgTableLua.(*lua.LTable)
|
||||
|
||||
packetLua := &PacketLua{
|
||||
Name: getStringFromTable(pkgTable, "name"),
|
||||
Id: getStringFromTable(pkgTable, "id"),
|
||||
Version: getStringFromTable(pkgTable, "version"),
|
||||
Author: getStringFromTable(pkgTable, "author"),
|
||||
Description: getStringFromTable(pkgTable, "description"),
|
||||
PkgType: getStringFromTable(pkgTable, "type"),
|
||||
Serial: getIntFromTable(pkgTable, "serial"),
|
||||
|
||||
Dependencies: getDependenciesFromTable(L, pkgTable, "dependencies"),
|
||||
BuildDependencies: getDependenciesFromTable(L, pkgTable, "build_dependencies"),
|
||||
|
||||
GitUrl: getStringFromTable(pkgTable, "git_url"),
|
||||
GitBranch: getStringFromTable(pkgTable, "git_branch"),
|
||||
|
||||
Os: getStringArrayFromTable(L, pkgTable, "os"),
|
||||
Architetures: getStringArrayFromTable(L, pkgTable, "arch"),
|
||||
|
||||
Prepare: getFunctionFromTable(table, "prepare"),
|
||||
Build: getFunctionFromTable(table, "build"),
|
||||
Install: getFunctionFromTable(table, "install"),
|
||||
Remove: getFunctionFromTable(table, "remove"),
|
||||
}
|
||||
|
||||
if packetLua.Install == nil || packetLua.Remove == nil {
|
||||
return PacketLua{}, fmt.Errorf("install or remove function is not valid")
|
||||
}
|
||||
|
||||
return *packetLua, nil
|
||||
}
|
||||
|
||||
func (packetLua PacketLua) ExecuteRemove(L *lua.LState) error {
|
||||
L.Push(packetLua.Remove)
|
||||
return L.PCall(0, 0, nil)
|
||||
}
|
||||
|
||||
func ReadPacketFromFile(file io.Reader) (PacketLua, error) {
|
||||
|
||||
zstdReader, err := zstd.NewReader(file)
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
defer zstdReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(zstdReader)
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
if filepath.Base(header.Name) == "Packet.lua" {
|
||||
|
||||
packageLuaBlob, err := io.ReadAll(tarReader)
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
return ReadPacket(packageLuaBlob)
|
||||
}
|
||||
|
||||
}
|
||||
return PacketLua{}, errors_packets.ErrCantFindPacketDotLua
|
||||
}
|
||||
|
||||
func GetPackageDotLuaFromRemote(url string, branch string) (PacketLua, error) {
|
||||
|
||||
repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
Depth: 1,
|
||||
URL: url,
|
||||
SingleBranch: true,
|
||||
ReferenceName: plumbing.ReferenceName("refs/heads/" + branch),
|
||||
})
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
ref, err := repo.Head()
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
commit, err := repo.CommitObject(ref.Hash())
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
f, err := commit.File("Packet.lua")
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
content, err := f.Contents()
|
||||
if err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
|
||||
return ReadPacket([]byte(content))
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
package packet
|
||||
@@ -1,80 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"packets/internal/consts"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"golang.org/x/net/ipv4"
|
||||
)
|
||||
|
||||
type Peer struct {
|
||||
IP net.IP
|
||||
Port int
|
||||
}
|
||||
|
||||
func BroadcastAddr(ip net.IP, mask net.IPMask) net.IP {
|
||||
b := make(net.IP, len(ip))
|
||||
for i := range ip {
|
||||
b[i] = ip[i] | ^mask[i]
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func AskLAN(filename string) ([]Peer, error) {
|
||||
var peers []Peer
|
||||
query := []byte("Q:" + filename)
|
||||
|
||||
pc, err := net.ListenPacket("udp", ":0")
|
||||
if err != nil {
|
||||
return []Peer{}, err
|
||||
}
|
||||
defer pc.Close()
|
||||
|
||||
if pconn := ipv4.NewPacketConn(pc); pconn != nil {
|
||||
_ = pconn.SetTTL(1)
|
||||
}
|
||||
|
||||
ifaces, _ := net.Interfaces()
|
||||
for _, ifc := range ifaces {
|
||||
if ifc.Flags&net.FlagUp == 0 || ifc.Flags&net.FlagLoopback != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
addrs, _ := ifc.Addrs()
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP.To4() == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
bcast := BroadcastAddr(ipnet.IP.To4(), ipnet.Mask)
|
||||
dst := &net.UDPAddr{IP: bcast, Port: 1333}
|
||||
|
||||
_, err = pc.WriteTo(query, dst)
|
||||
if err != nil {
|
||||
fmt.Printf(":: (%s) can't send to %s: %s\n", ifc.Name, bcast, err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = pc.SetDeadline(time.Now().Add(consts.LANDeadline))
|
||||
buf := make([]byte, 1500)
|
||||
|
||||
for {
|
||||
n, addr, err := pc.ReadFrom(buf)
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
msg := string(buf[:n])
|
||||
|
||||
if strings.HasPrefix(msg, "H:"+filename) {
|
||||
parts := strings.Split(msg, ":")
|
||||
port, _ := strconv.Atoi(parts[2])
|
||||
peers = append(peers, Peer{IP: addr.(*net.UDPAddr).IP, Port: port})
|
||||
}
|
||||
}
|
||||
return peers, nil
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
package utils_lua
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/go-git/go-git/v6/plumbing"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func LGitClone(L *lua.LState) int {
|
||||
uri := L.CheckString(1)
|
||||
output := L.CheckString(2)
|
||||
|
||||
depth := 1
|
||||
if L.GetTop() > 2 {
|
||||
depth = L.CheckInt(3)
|
||||
}
|
||||
|
||||
_, err := git.PlainClone(output, &git.CloneOptions{
|
||||
URL: uri,
|
||||
Progress: os.Stdout,
|
||||
Depth: depth,
|
||||
})
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func LGitCheckout(L *lua.LState) int {
|
||||
dir := L.CheckString(1)
|
||||
branchorid := L.CheckString(2)
|
||||
|
||||
repo, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
worktree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
if err := tryCheckout(*worktree, branchorid); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
func LGitPUll(L *lua.LState) int {
|
||||
dir := L.CheckString(1)
|
||||
|
||||
|
||||
git.PlainClone("/tmp", &git.CloneOptions{})
|
||||
|
||||
depth := 1
|
||||
if L.GetTop() > 1 {
|
||||
depth = L.CheckInt(2)
|
||||
}
|
||||
|
||||
repo, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
worktree, err := repo.Worktree()
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
if err := worktree.Pull(&git.PullOptions{Depth: depth}); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func tryCheckout(worktree git.Worktree, reference string) error {
|
||||
err := worktree.Checkout(&git.CheckoutOptions{
|
||||
Branch: plumbing.ReferenceName(reference),
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = worktree.Checkout(&git.CheckoutOptions{
|
||||
Branch: plumbing.ReferenceName("refs/heads/" + reference),
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
err = worktree.Checkout(&git.CheckoutOptions{
|
||||
Branch: plumbing.ReferenceName("refs/tags/" + reference),
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
hash := plumbing.NewHash(reference)
|
||||
err = worktree.Checkout(&git.CheckoutOptions{
|
||||
Hash: hash,
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot checkout '%s' as branch, tag, or commit", reference)
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
package utils_lua
|
||||
|
||||
import (
|
||||
"packets/configs"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func GetSandBox() (lua.LState, error) {
|
||||
|
||||
cfg, err := configs.GetConfigTOML()
|
||||
if err != nil {
|
||||
return *lua.NewState(), err
|
||||
}
|
||||
L := lua.NewState()
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
L.SetGlobal("SAFE_MODE", lua.LTrue)
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.Config.Bin_d))
|
||||
|
||||
L.SetGlobal("path_join", L.NewFunction(Ljoin))
|
||||
|
||||
osObject.RawSetString("remove", L.NewFunction(LSafeRemove))
|
||||
osObject.RawSetString("rename", L.NewFunction(LSafeRename))
|
||||
osObject.RawSetString("copy", L.NewFunction(LSafeCopy))
|
||||
osObject.RawSetString("symlink", L.NewFunction(LSymlink))
|
||||
osObject.RawSetString("mkdir", L.NewFunction(LMkdir))
|
||||
|
||||
//ioObject.RawSetString("open", L.NewFunction(LOpen))
|
||||
|
||||
return *L, nil
|
||||
}
|
||||
@@ -1,260 +0,0 @@
|
||||
package utils_lua
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"packets/configs"
|
||||
"packets/internal/consts"
|
||||
"packets/internal/utils"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func IsSafe(str string) bool {
|
||||
s, err := filepath.EvalSymlinks(filepath.Clean(str))
|
||||
if err != nil {
|
||||
s = filepath.Clean(str)
|
||||
}
|
||||
|
||||
var cfg configs.ConfigTOML
|
||||
|
||||
f, err := os.Open(filepath.Join(consts.DefaultLinux_d, "config.toml"))
|
||||
if err != nil {
|
||||
log.Println("error here opening config.toml")
|
||||
return false
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
decoder := toml.NewDecoder(f)
|
||||
|
||||
if err := decoder.Decode(&cfg); err != nil {
|
||||
log.Println("error decoding")
|
||||
return false
|
||||
}
|
||||
|
||||
if strings.HasPrefix(s, cfg.Config.Data_d) || strings.HasPrefix(s, cfg.Config.Bin_d) {
|
||||
return true
|
||||
|
||||
} else if strings.Contains(s, ".ssh") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/etc") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/usr") || strings.HasPrefix(s, "/bin") {
|
||||
fmt.Println(s, "está dentro de usr")
|
||||
return strings.HasPrefix(s, "/usr/share")
|
||||
|
||||
} else if strings.HasPrefix(s, "/var/mail") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/proc") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/sys") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/var/run") || strings.HasPrefix(s, "/run") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/tmp") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/dev") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/boot") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/home") {
|
||||
if strings.Contains(s, "/Pictures") || strings.Contains(s, "/Videos") || strings.Contains(s, "/Documents") || strings.Contains(s, "/Downloads") {
|
||||
return false
|
||||
}
|
||||
|
||||
} else if strings.HasPrefix(s, "/lib") || strings.HasPrefix(s, "/lib64") || strings.HasPrefix(s, "/var/lib64") || strings.HasPrefix(s, "/lib") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/sbin") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/srv") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/mnt") {
|
||||
return false
|
||||
|
||||
} else if strings.HasPrefix(s, "/media") {
|
||||
return false
|
||||
} else if strings.HasPrefix(s, "/snap") {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// lua functions
|
||||
|
||||
func LSafeRemove(L *lua.LState) int {
|
||||
filename := L.CheckString(1)
|
||||
fmt.Printf(" remove %s\n", filename)
|
||||
|
||||
err := os.RemoveAll(filename)
|
||||
if err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func LSafeRename(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
fmt.Printf(" move %s -> %s\n", oldname, newname)
|
||||
|
||||
if err := os.Rename(oldname, newname); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
return 1
|
||||
}
|
||||
func LSafeCopy(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
fmt.Printf(" copy %s -> %s\n", oldname, newname)
|
||||
|
||||
if err := utils.CopyDir(oldname, newname); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
|
||||
}
|
||||
|
||||
func LSymlink(L *lua.LState) int {
|
||||
fileName := L.CheckString(1)
|
||||
destination := L.CheckString(2)
|
||||
|
||||
fmt.Printf(" symlink %s -> %s\n", fileName, destination)
|
||||
|
||||
_ = os.RemoveAll(destination)
|
||||
if err := os.Symlink(fileName, destination); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func modeFlags(mode string) int {
|
||||
switch mode {
|
||||
case "r", "rb":
|
||||
return os.O_RDONLY
|
||||
case "w", "wb":
|
||||
return os.O_CREATE | os.O_WRONLY | os.O_TRUNC
|
||||
case "a", "ab":
|
||||
return os.O_CREATE | os.O_WRONLY | os.O_APPEND
|
||||
case "r+", "r+b", "rb+", "br+":
|
||||
return os.O_RDWR
|
||||
case "w+", "w+b", "wb+", "bw+":
|
||||
return os.O_CREATE | os.O_RDWR | os.O_TRUNC
|
||||
case "a+", "a+b", "ab+", "ba+":
|
||||
return os.O_CREATE | os.O_RDWR | os.O_APPEND
|
||||
default:
|
||||
return os.O_RDONLY
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func LOpen(L *lua.LState) int {
|
||||
path := L.CheckString(1)
|
||||
mode := L.OptString(2, "r")
|
||||
|
||||
file, err := os.OpenFile(path, modeFlags(mode), 0644)
|
||||
if err != nil {
|
||||
L.Push(lua.LNil)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
ud := L.NewUserData()
|
||||
ud.Value = file
|
||||
L.SetMetatable(ud, L.GetTypeMetatable("file"))
|
||||
L.Push(ud)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
*/
|
||||
|
||||
func Ljoin(L *lua.LState) int {
|
||||
|
||||
n := L.GetTop()
|
||||
parts := make([]string, 0, n)
|
||||
|
||||
for i := 1; i <= n; i++ {
|
||||
val := L.Get(i)
|
||||
parts = append(parts, val.String())
|
||||
}
|
||||
|
||||
result := filepath.Join(parts...)
|
||||
L.Push(lua.LString(result))
|
||||
return 1
|
||||
}
|
||||
|
||||
func LMkdir(L *lua.LState) int {
|
||||
path := L.CheckString(1)
|
||||
perm := L.CheckInt(2)
|
||||
fmt.Printf(" mkdir %s \n", path)
|
||||
|
||||
/*
|
||||
if !IsSafe(path) {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString("unsafe filepath"))
|
||||
return 2
|
||||
}
|
||||
*/
|
||||
|
||||
if err := os.MkdirAll(path, os.FileMode(perm)); err != nil {
|
||||
L.Push(lua.LFalse)
|
||||
L.Push(lua.LString(err.Error()))
|
||||
return 2
|
||||
}
|
||||
|
||||
L.Push(lua.LTrue)
|
||||
L.Push(lua.LNil)
|
||||
return 2
|
||||
}
|
||||
|
||||
func LError(L *lua.LState) int {
|
||||
n := L.GetTop()
|
||||
parts := make([]any, 0, n)
|
||||
|
||||
for i := 1; i <= n; i++ {
|
||||
val := L.Get(i)
|
||||
parts = append(parts, val.String())
|
||||
}
|
||||
|
||||
llogger().Panic(parts...)
|
||||
return 0
|
||||
}
|
||||
|
||||
func llogger() *log.Logger { return log.New(os.Stderr, " script error: ", 0) }
|
||||
@@ -1,482 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"packets/internal/consts"
|
||||
errors_packets "packets/internal/errors"
|
||||
"packets/internal/packet"
|
||||
)
|
||||
|
||||
type Package struct {
|
||||
PackageF []byte
|
||||
Version string
|
||||
ImageUrl string
|
||||
QueryName string
|
||||
Description string
|
||||
Author string
|
||||
AuthorVerified bool
|
||||
OS string
|
||||
Arch string
|
||||
Filename string
|
||||
Size int64
|
||||
Dependencies map[string]string
|
||||
|
||||
Manifest packet.PacketLua
|
||||
|
||||
Signature []byte
|
||||
PublicKey ed25519.PublicKey
|
||||
|
||||
Serial int
|
||||
}
|
||||
|
||||
func GetFileHTTP(url string) ([]byte, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors_packets.ErrResponseNot200OK
|
||||
}
|
||||
|
||||
fileBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fileBytes, nil
|
||||
}
|
||||
|
||||
// CopyDir copies a directory from source to destination
|
||||
func CopyDir(src string, dest string) error {
|
||||
if stats, err := os.Stat(src); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if stats.IsDir() {
|
||||
files, err := os.ReadDir(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(dest, 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
CopyDir(filepath.Join(src, file.Name()), filepath.Join(dest, file.Name()))
|
||||
continue
|
||||
}
|
||||
srcFile := filepath.Join(src, file.Name())
|
||||
|
||||
f, err := os.Create(filepath.Join(dest, file.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
opennedSrcFile, err := os.Open(srcFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer opennedSrcFile.Close()
|
||||
if _, err := io.Copy(f, opennedSrcFile); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
if err := CopyFile(src, dest); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CopyFile copies a file from source to destination
|
||||
func CopyFile(source string, destination string) error {
|
||||
src, err := os.Open(source)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
status, err := src.Stat()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = os.MkdirAll(filepath.Dir(destination), 0o755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
dst, err := os.Create(destination)
|
||||
if err != nil {
|
||||
if !os.IsExist(err) {
|
||||
dst, err = os.Open(destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
defer dst.Close()
|
||||
if err := dst.Chmod(status.Mode()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = io.Copy(dst, src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write writes the package file to the cache directory and returns the path to it
|
||||
|
||||
func (p *Package) Write() (string, error) {
|
||||
if err := os.WriteFile(filepath.Join(consts.DefaultCache_d, p.Filename), p.PackageF, 0o644); err != nil {
|
||||
_ = os.Remove(filepath.Join(consts.DefaultCache_d, p.Filename))
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filepath.Join(consts.DefaultCache_d, p.Filename), nil
|
||||
}
|
||||
|
||||
func (p *Package) AddToInstalledDB(inCache int, packagePath string) error {
|
||||
db, err := sql.Open("sqlite", consts.InstalledDB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var success bool
|
||||
|
||||
defer func() {
|
||||
if !success {
|
||||
_, err := db.Exec("DELETE FROM packages WHERE id = ?", p.Manifest.Id)
|
||||
if err != nil {
|
||||
log.Println("failed to rollback package addition:", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = db.Exec(`
|
||||
INSERT INTO packages (
|
||||
query_name, id, version, description,
|
||||
serial, package_d, filename, os, arch, in_cache
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
p.QueryName,
|
||||
p.Manifest.Id,
|
||||
p.Version,
|
||||
p.Description,
|
||||
p.Serial,
|
||||
packagePath,
|
||||
p.Filename,
|
||||
p.OS,
|
||||
p.Arch,
|
||||
inCache,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for depnName, versionConstraint := range p.Dependencies {
|
||||
_, err = db.Exec("INSERT INTO package_dependencies (package_id, dependency_name, version_constraint) VALUES (?, ?, ?)", p.Manifest.Id, depnName, versionConstraint)
|
||||
}
|
||||
|
||||
success = true
|
||||
return err
|
||||
}
|
||||
|
||||
func CheckIfPackageInstalled(name string) (bool, error) {
|
||||
db, err := sql.Open("sqlite", consts.InstalledDB)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
if strings.Contains(name, "@") {
|
||||
name = strings.SplitN(name, "@", 2)[0]
|
||||
}
|
||||
|
||||
var exists bool
|
||||
err = db.QueryRow("SELECT EXISTS(SELECT 1 FROM packages WHERE query_name = ?)", name).Scan(&exists)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func GetDependencies(db *sql.DB, id string) (map[string]string, error) {
|
||||
|
||||
rows, err := db.Query("SELECT dependency_name, version_constraint FROM package_dependencies WHERE package_id = ?", id)
|
||||
if err != nil {
|
||||
return map[string]string{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
dependencies := make(map[string]string)
|
||||
|
||||
for rows.Next() {
|
||||
var a, versionConstraint string
|
||||
if err := rows.Scan(&a, &versionConstraint); err != nil {
|
||||
return map[string]string{}, err
|
||||
}
|
||||
dependencies[a] = versionConstraint
|
||||
}
|
||||
|
||||
return dependencies, nil
|
||||
|
||||
}
|
||||
|
||||
func ResolvDependencies(depnList map[string]string) ([]string, error) {
|
||||
|
||||
db, err := sql.Open("sqlite", consts.IndexDB)
|
||||
if err != nil {
|
||||
return []string{}, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var resolved []string
|
||||
for dependencieName, constraint := range depnList {
|
||||
|
||||
var filter, order string
|
||||
value := strings.TrimLeft(constraint, "<>=")
|
||||
|
||||
switch {
|
||||
case constraint == "any":
|
||||
filter = ""
|
||||
order = "ORDER BY serial DESC LIMIT 1"
|
||||
case strings.HasPrefix(constraint, ">"):
|
||||
filter = fmt.Sprintf("AND serial > %s", value)
|
||||
order = "ORDER BY serial DESC LIMIT 1"
|
||||
case strings.HasPrefix(constraint, "<="):
|
||||
filter = fmt.Sprintf("AND serial <= %s", value)
|
||||
order = "ORDER BY serial ASC LIMIT 1"
|
||||
case strings.HasPrefix(constraint, "<"):
|
||||
filter = fmt.Sprintf("AND serial < %s", value)
|
||||
order = "ORDER BY serial ASC LIMIT 1"
|
||||
case strings.HasPrefix(constraint, "="):
|
||||
filter = fmt.Sprintf("AND serial = %s", value)
|
||||
order = ""
|
||||
}
|
||||
|
||||
var packageId string
|
||||
|
||||
err := db.QueryRow(fmt.Sprintf("SELECT id FROM packages WHERE query_name = ? %s %s", filter, order), dependencieName).Scan(&packageId)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
continue
|
||||
}
|
||||
return []string{}, err
|
||||
}
|
||||
|
||||
resolved = append(resolved, packageId)
|
||||
dp, err := GetDependencies(db, packageId)
|
||||
if err != nil {
|
||||
return resolved, err
|
||||
}
|
||||
|
||||
depn, err := ResolvDependencies(dp)
|
||||
if err != nil {
|
||||
return resolved, err
|
||||
}
|
||||
|
||||
resolved = append(resolved, depn...)
|
||||
}
|
||||
|
||||
return resolved, nil
|
||||
}
|
||||
|
||||
func RemoveFromInstalledDB(id string) error {
|
||||
db, err := sql.Open("sqlite", consts.InstalledDB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err = db.Exec("DELETE FROM packages WHERE id = ? OR query_name = ?", id, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPackage retrieves package information from the index database and downloads the package file
|
||||
func GetPackage(id string) (Package, error) {
|
||||
|
||||
var this Package
|
||||
this.Dependencies = make(map[string]string)
|
||||
var peers []Peer
|
||||
|
||||
db, err := sql.Open("sqlite", consts.IndexDB)
|
||||
if err != nil {
|
||||
return this, err
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var packageUrl string
|
||||
err = db.QueryRow("SELECT query_name, version, package_url, image_url, description, author, author_verified, os, arch, signature, public_key, serial, size FROM packages WHERE id = ?", id).
|
||||
Scan(
|
||||
&this.QueryName,
|
||||
&this.Version,
|
||||
&packageUrl,
|
||||
&this.ImageUrl,
|
||||
&this.Description,
|
||||
&this.Author,
|
||||
&this.AuthorVerified,
|
||||
&this.OS,
|
||||
&this.Arch,
|
||||
&this.Signature,
|
||||
&this.PublicKey,
|
||||
&this.Serial,
|
||||
&this.Size,
|
||||
)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
|
||||
rows, err := db.Query("SELECT dependency_name, version_constraint FROM package_dependencies WHERE package_id = ?", id)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var a, vConstraint string
|
||||
if err := rows.Scan(&a, &vConstraint); err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
|
||||
this.Dependencies[a] = vConstraint
|
||||
}
|
||||
|
||||
filename := path.Base(packageUrl)
|
||||
this.Filename = filename
|
||||
|
||||
dirEntry, err := os.ReadDir(consts.DefaultCache_d)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
|
||||
for _, v := range dirEntry {
|
||||
if v.Name() == filename {
|
||||
this.PackageF, err = os.ReadFile(filepath.Join(consts.DefaultCache_d, filename))
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
goto skipping
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
peers, err = AskLAN(filename)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
|
||||
if len(peers) == 0 {
|
||||
fmt.Printf(":: Pulling from %s\n", packageUrl)
|
||||
this.PackageF, err = GetFileHTTP(packageUrl)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
} else {
|
||||
var totalerrors int = 0
|
||||
for _, peer := range peers {
|
||||
fmt.Printf(":: Pulling from local network (%s)\n", peer.IP)
|
||||
this.PackageF, err = GetFileHTTP(fmt.Sprintf("http://%s:%d/%s", peer.IP, peer.Port, filename))
|
||||
if err == nil {
|
||||
break
|
||||
} else {
|
||||
totalerrors++
|
||||
}
|
||||
}
|
||||
if totalerrors == len(peers) {
|
||||
this.PackageF, err = GetFileHTTP(packageUrl)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
skipping:
|
||||
|
||||
this.Manifest, err = packet.ReadPacket(this.PackageF)
|
||||
if err != nil {
|
||||
return Package{}, err
|
||||
}
|
||||
|
||||
if !ed25519.Verify(this.PublicKey, this.PackageF, this.Signature) {
|
||||
return Package{}, errors_packets.ErrInvalidSignature
|
||||
}
|
||||
|
||||
return this, nil
|
||||
}
|
||||
|
||||
func GetPacketsUID() (int, error) {
|
||||
_ = exec.Command("useradd", "-M", "-N", "-r", "-s", "/bin/false", "-d", "/etc/packets", "packets").Run()
|
||||
cmd := exec.Command("id", "-u", "packets")
|
||||
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
s := strings.TrimSpace(string(out))
|
||||
uid, err := strconv.Atoi(s)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
func ChangeToNoPermission() error {
|
||||
uid, err := GetPacketsUID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = os.Chown("/var/lib/packets", uid, 0)
|
||||
|
||||
return syscall.Setresuid(0, uid, 0)
|
||||
|
||||
}
|
||||
|
||||
func ElevatePermission() error { return syscall.Setresuid(0, 0, 0) }
|
||||
|
||||
func getFileHTTP(url string) ([]byte, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, errors_packets.ErrResponseNot200OK
|
||||
}
|
||||
|
||||
fileBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fileBytes, nil
|
||||
}
|
||||
Reference in New Issue
Block a user