Compare commits
5 Commits
05af0969e9
...
4c88ec3bc2
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c88ec3bc2 | |||
| ff5e271195 | |||
| cbea1dd8b5 | |||
| acf00bc5f8 | |||
| b15d847fd2 |
120
internal/lua/luafunctions.go
Normal file
120
internal/lua/luafunctions.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
func LRemove(L *lua.LState) int {
|
||||
filename := L.CheckString(1)
|
||||
|
||||
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 LRename(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
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 LCopy(L *lua.LState) int {
|
||||
oldname := L.CheckString(1)
|
||||
newname := L.CheckString(2)
|
||||
|
||||
if err := 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)
|
||||
|
||||
_ = 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 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)
|
||||
|
||||
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 LSetEnv(L *lua.LState) int {
|
||||
env := L.CheckString(1)
|
||||
value := L.CheckString(2)
|
||||
os.Setenv(env, value)
|
||||
return 0
|
||||
}
|
||||
|
||||
func llogger() *log.Logger { return log.New(os.Stderr, "script error: ", 0) }
|
||||
95
internal/lua/utils.go
Normal file
95
internal/lua/utils.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package lua
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -287,3 +287,58 @@ func parseVersionString(s string) version {
|
||||
|
||||
return version{}
|
||||
}
|
||||
|
||||
func normalizeArch(arch string) string {
|
||||
switch arch {
|
||||
case "386":
|
||||
return "i686"
|
||||
case "amd64":
|
||||
return "x86_64"
|
||||
case "amd64p32":
|
||||
return "x86_64"
|
||||
case "arm":
|
||||
return "arm"
|
||||
case "arm64":
|
||||
return "aarch64"
|
||||
case "arm64be":
|
||||
return "aarch64_be"
|
||||
case "armbe":
|
||||
return "armbe"
|
||||
case "loong64":
|
||||
return "loongarch64"
|
||||
case "mips":
|
||||
return "mips"
|
||||
case "mips64":
|
||||
return "mips64"
|
||||
case "mips64le":
|
||||
return "mips64el"
|
||||
case "mips64p32":
|
||||
return "mips64"
|
||||
case "mips64p32le":
|
||||
return "mips64el"
|
||||
case "mipsle":
|
||||
return "mipsel"
|
||||
case "ppc":
|
||||
return "powerpc"
|
||||
case "ppc64":
|
||||
return "ppc64"
|
||||
case "ppc64le":
|
||||
return "ppc64le"
|
||||
case "riscv":
|
||||
return "riscv"
|
||||
case "riscv64":
|
||||
return "riscv64"
|
||||
case "s390":
|
||||
return "s390"
|
||||
case "s390x":
|
||||
return "s390x"
|
||||
case "sparc":
|
||||
return "sparc"
|
||||
case "sparc64":
|
||||
return "sparc64"
|
||||
case "wasm":
|
||||
return "wasm"
|
||||
default:
|
||||
return arch
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,24 +1,39 @@
|
||||
package packet
|
||||
|
||||
type Config struct {
|
||||
BinDir string
|
||||
BinDir *string
|
||||
}
|
||||
|
||||
const defaultBinDir = "/usr/bin"
|
||||
|
||||
func checkConfig(cfg *Config) *Config {
|
||||
|
||||
if cfg == nil {
|
||||
bin := defaultBinDir
|
||||
return &Config{
|
||||
BinDir: defaultBinDir,
|
||||
BinDir: &bin,
|
||||
}
|
||||
}
|
||||
if cfg.BinDir == "" {
|
||||
if *cfg.BinDir == "" || cfg.BinDir == nil {
|
||||
bin := defaultBinDir
|
||||
return &Config{
|
||||
BinDir: defaultBinDir,
|
||||
BinDir: &bin,
|
||||
}
|
||||
} else {
|
||||
return cfg
|
||||
}
|
||||
|
||||
}
|
||||
func checkConfigSrc(cfg *GetSourceConfig) *GetSourceConfig {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch {
|
||||
case *cfg.PacketDir == "" || cfg.PacketDir == nil:
|
||||
s := randStringBytes(12)
|
||||
cfg.PacketDir = &s
|
||||
}
|
||||
|
||||
return cfg
|
||||
|
||||
}
|
||||
|
||||
@@ -2,14 +2,20 @@ package packet
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
|
||||
lua_utils "github.com/roboogg133/packets/internal/lua"
|
||||
lua "github.com/yuin/gopher-lua"
|
||||
)
|
||||
|
||||
@@ -26,14 +32,15 @@ type PacketLua struct {
|
||||
GlobalSources *[]Source
|
||||
GlobalDependencies *PkgDependencies
|
||||
|
||||
Build *lua.LFunction
|
||||
Install *lua.LFunction
|
||||
Build *lua.LFunction
|
||||
Install *lua.LFunction
|
||||
PreRemove *lua.LFunction
|
||||
}
|
||||
|
||||
type Source struct {
|
||||
Method string
|
||||
Url string
|
||||
Specs interface{}
|
||||
Specs any
|
||||
}
|
||||
|
||||
type VersionConstraint string
|
||||
@@ -70,6 +77,8 @@ type GETSpecs struct {
|
||||
var ErrCantFindPacketDotLua = errors.New("can't find Packet.lua in .tar.zst file")
|
||||
var ErrFileDontReturnTable = errors.New("invalid Packet.lua format: the file do not return a table")
|
||||
var ErrCannotFindPackageTable = errors.New("invalid Packet.lua format: can't find package table")
|
||||
var ErrInstallFunctionDoesNotExist = errors.New("can not find instal()")
|
||||
var ErrSha256Sum = errors.New("false checksum")
|
||||
|
||||
// ReadPacket read a Packet.lua and alredy set global vars
|
||||
func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
|
||||
@@ -79,15 +88,19 @@ func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
|
||||
defer L.Close()
|
||||
|
||||
osObject := L.GetGlobal("os").(*lua.LTable)
|
||||
osObject.RawSetString("setenv", L.NewFunction(lua_utils.LSetEnv))
|
||||
ioObject := L.GetGlobal("io").(*lua.LTable)
|
||||
|
||||
L.SetGlobal("os", lua.LNil)
|
||||
L.SetGlobal("io", lua.LNil)
|
||||
|
||||
L.SetGlobal("BIN_DIR", lua.LString(cfg.BinDir))
|
||||
L.SetGlobal("BIN_DIR", lua.LString(*cfg.BinDir))
|
||||
L.SetGlobal("CURRENT_ARCH", lua.LString(runtime.GOARCH))
|
||||
L.SetGlobal("CURRENT_ARCH_NORMALIZED", lua.LString(normalizeArch(runtime.GOARCH)))
|
||||
L.SetGlobal("CURRENT_PLATAFORM", lua.LString(runtime.GOOS))
|
||||
|
||||
L.SetGlobal("pathjoin", L.NewFunction(lua_utils.Ljoin))
|
||||
|
||||
if err := L.DoString(string(f)); err != nil {
|
||||
return PacketLua{}, err
|
||||
}
|
||||
@@ -121,12 +134,13 @@ func ReadPacket(f []byte, cfg *Config) (PacketLua, error) {
|
||||
GlobalDependencies: getDependenciesFromTable(pkgTable, "build_dependencies"),
|
||||
GlobalSources: getSourcesFromTable(pkgTable, "sources"),
|
||||
|
||||
Build: getFunctionFromTable(table, "build"),
|
||||
Install: getFunctionFromTable(table, "install"),
|
||||
Build: getFunctionFromTable(table, "build"),
|
||||
Install: getFunctionFromTable(table, "install"),
|
||||
PreRemove: getFunctionFromTable(table, "pre_remove"),
|
||||
}
|
||||
|
||||
if packetLua.Install == nil {
|
||||
return PacketLua{}, fmt.Errorf("install() does not exist")
|
||||
return PacketLua{}, ErrInstallFunctionDoesNotExist
|
||||
}
|
||||
|
||||
return *packetLua, nil
|
||||
@@ -164,3 +178,90 @@ func ReadPacketFromZSTDF(file io.Reader, cfg *Config) (PacketLua, error) {
|
||||
}
|
||||
return PacketLua{}, ErrCantFindPacketDotLua
|
||||
}
|
||||
|
||||
type GetSourceConfig struct {
|
||||
PacketDir *string
|
||||
}
|
||||
|
||||
func GetSource(url, method string, info any) ([]byte, error) {
|
||||
|
||||
switch method {
|
||||
case "GET":
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
specs := info.(GETSpecs)
|
||||
|
||||
for k, v := range *specs.Headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
client := http.Client{Timeout: 5 * time.Minute}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !verifySHA256(*specs.SHA256, data) {
|
||||
return nil, ErrSha256Sum
|
||||
}
|
||||
|
||||
return data, nil
|
||||
case "POST":
|
||||
specs := info.(POSTSpecs)
|
||||
var body *bytes.Reader
|
||||
|
||||
if specs.Body != nil {
|
||||
body = bytes.NewReader([]byte(*specs.Body))
|
||||
} else {
|
||||
body = nil
|
||||
}
|
||||
req, err := http.NewRequest("POST", url, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for k, v := range *specs.Headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
|
||||
client := http.Client{Timeout: 5 * time.Minute}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !verifySHA256(*specs.SHA256, data) {
|
||||
return nil, ErrSha256Sum
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("invalid method")
|
||||
}
|
||||
|
||||
func verifySHA256(checksum string, src []byte) bool {
|
||||
|
||||
check := sha256.Sum256(src)
|
||||
|
||||
return hex.EncodeToString(check[:]) == checksum
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package packet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func (pkg PacketLua) IsValid() bool {
|
||||
|
||||
var a, b int
|
||||
@@ -15,8 +11,7 @@ func (pkg PacketLua) IsValid() bool {
|
||||
|
||||
a += len(*pkg.GlobalSources)
|
||||
|
||||
if a <= 0 || b <= 0 {
|
||||
fmt.Println("invalid")
|
||||
if a < 1 || len(*pkg.Plataforms) > b {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -26,7 +21,5 @@ func (pkg PacketLua) IsValid() bool {
|
||||
case pkg.Description == "" || pkg.Maintaner == "" || pkg.Name == "" || pkg.Version == "":
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Println("valid")
|
||||
return true
|
||||
}
|
||||
|
||||
13
pkg/packet.lua.d/rand.go
Normal file
13
pkg/packet.lua.d/rand.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package packet
|
||||
|
||||
import "math/rand"
|
||||
|
||||
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$%!@%&*()-=+[]{}:;.,1234567890"
|
||||
|
||||
func randStringBytes(n int) string {
|
||||
b := make([]byte, n)
|
||||
for i := range b {
|
||||
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
@@ -1,11 +1,3 @@
|
||||
local arch_map = {
|
||||
amd64 = "x86_64",
|
||||
aarch64 = "aarch64",
|
||||
arm64 = "aarch64",
|
||||
['386'] = "i686"
|
||||
}
|
||||
local srcarch = arch_map[CURRENT_ARCH]
|
||||
|
||||
return {
|
||||
package = {
|
||||
name = "bat-bin", -- required
|
||||
@@ -19,7 +11,7 @@ return {
|
||||
arch = {"amd64"},
|
||||
sources = {
|
||||
{
|
||||
url = "https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-" ..srcarch .."-pc-windows-msvc.zip",
|
||||
url = "https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-" ..CURRENT_ARCH_NORMALIZED.."-pc-windows-msvc.zip",
|
||||
method = "GET",
|
||||
sha256="a8a6862f14698b45e101b0932c69bc47a007f4c0456f3a129fdcef54d443d501"
|
||||
}
|
||||
@@ -34,7 +26,7 @@ return {
|
||||
arch = {"amd64"},
|
||||
sources = {
|
||||
{
|
||||
url = "https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-".. srcarch .."-unknown-linux-gnu.tar.gz",
|
||||
url = "https://github.com/sharkdp/bat/releases/download/v0.26.0/bat-v0.26.0-".. CURRENT_ARCH_NORMALIZED .."-unknown-linux-gnu.tar.gz",
|
||||
method = "GET",
|
||||
sha256 = "7efed0c768fae36f18ddbbb4a38f5c4b64db7c55a170dfc89fd380805809a44b"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user