Compare commits

114 Commits

Author SHA1 Message Date
4c88ec3bc2 using CURRENT_ARCH_NORMALIZED 2025-10-31 21:32:51 -03:00
ff5e271195 adding getsourcefunction sha256 check 2025-10-31 21:32:35 -03:00
cbea1dd8b5 normalize arch function 2025-10-31 21:31:58 -03:00
acf00bc5f8 added lua functions from old version of packets 2025-10-31 21:31:12 -03:00
b15d847fd2 function to generate random text 2025-10-31 21:30:48 -03:00
05af0969e9 methods for PacketLua struct 2025-10-31 18:15:42 -03:00
dff912928b removing useless part 2025-10-31 18:15:16 -03:00
0001118bd0 file for test and debuging got in codebase for mistake 2025-10-31 18:08:23 -03:00
43a24a4f36 changing pkg() to install() 2025-10-31 18:04:25 -03:00
d3c4a604c3 New Packet.lua format, test parsing and entire new lua ecossystem 2025-10-31 17:52:40 -03:00
6c5abdf4d4 new Packet.lua format, testing it with bat 2025-10-31 13:32:03 -03:00
1c4ade5db9 removing everything to do final version of packets client 2025-10-31 12:54:42 -03:00
b31630a6c0 Merge pull request #4 from Caio1w/Solved-Issue#3
Solved Issue #3
2025-10-28 21:25:51 -03:00
Caio1w
25eabc92ed Update const.go 2025-10-28 21:23:14 -03:00
a111b060f1 Merge pull request #2 from roboogg133/build-system
A scrap of Packet.lua ecosystem
2025-10-26 21:53:15 -03:00
a62ddf8270 simple checking for remote packets 2025-10-26 21:47:06 -03:00
aa65b28112 Packet.lua with right version, serial and better version constraint 2025-10-26 21:46:46 -03:00
72a5ab7c5d removing debug changes 2025-10-26 18:24:10 -03:00
89b3fdbc84 getting functions from right table now and reading os and arch in package table 2025-10-26 18:23:16 -03:00
0a531488a3 Packet.lua example test 2025-10-26 18:06:08 -03:00
7fbaef7bd4 try to clone Packet.lua 2025-10-26 18:05:57 -03:00
b411eff6f4 systemd managment for daemons 2025-10-26 18:05:34 -03:00
befa4e3ea4 changing home dir for packets user to /etc/packets (configuration folder) 2025-10-26 18:04:07 -03:00
807d9fa784 removing dependencies from table packages in database 2025-10-26 18:03:19 -03:00
9883fd92dc log messages for sockets 2025-10-26 18:02:43 -03:00
51c51b96bf bugfix, trying to read toml in a lua file, now returning Package.lua 2025-10-26 16:39:54 -03:00
a69de7e918 store buildDirs 2025-10-25 22:43:02 -03:00
4178387e2a checking if build or prepare is == nil 2025-10-25 22:42:16 -03:00
b6d7ec8a5f change dir for package dir while removing it 2025-10-25 22:41:59 -03:00
8feaf5d19b change dir for package dir to install it 2025-10-25 22:41:28 -03:00
820ffc299f code maybe confusing, but now using Packet.lua format 2025-10-25 20:17:20 -03:00
ecce74d2e9 changed old manifest.toml to Packet.lua and some improvements for future 100% Packet.lua implementation 2025-10-25 12:44:40 -03:00
5ba30c617a useless file 2025-10-25 11:16:20 -03:00
e3772d0944 some bug fixes, manifest.toml don't exist anyomre and all data for installation will be in one file name Packet.lua 2025-10-25 10:16:33 -03:00
df32178372 with afero functions need, to see more functions to use afero 2025-10-24 17:14:44 -03:00
cc9587821d modifications to build/lua.go 2025-10-24 17:04:02 -03:00
31af4d5389 new go.mod and go.sum 2025-10-24 17:01:23 -03:00
dbd63d371b new values in manifest.toml 2025-10-24 17:01:23 -03:00
52f675ab60 doing io.popen 2025-10-24 11:56:47 -03:00
26f9e20ae8 doing lua functions for build files, to run only in afero.FS 2025-10-24 11:10:43 -03:00
6377de7208 creating new build system 2025-10-23 23:04:35 -03:00
b58173837b bugfix, giving id to dependency resolver 2025-10-11 21:00:22 -03:00
97408da348 bugfix, starting a empty map for dependencies in GetPackage function 2025-10-11 19:39:20 -03:00
be96001d78 bugfix, fix command fixing leaving after remove 1 package 2025-10-11 19:38:50 -03:00
7c4fba5c86 now upgradeCmd is being used, message when downloading, and upgrade need to run as root 2025-10-11 18:50:45 -03:00
4cee062889 bugfix, fix missing wg.Add() and wg and go for upgrade async functions 2025-10-07 19:35:19 -03:00
93093382f4 bugfix, now ResolvDependencies check if depnList map is empty 2025-10-07 16:46:21 -03:00
e4b4d43163 progress doing upgrade all function 2025-10-07 11:56:24 -04:00
b89abb31df now sync needs to run as root 2025-10-06 11:55:51 -03:00
0a965c67c2 creating packets home dir 2025-10-05 18:16:15 -03:00
147fca375e implemented upgrade, and fixed some bugs 2025-10-05 15:47:21 -03:00
afc19b6e4d removing images 2025-10-05 14:31:50 -03:00
690f180687 removing web from this repository 2025-10-05 12:18:48 -03:00
7ef7b60cf5 fixed databaseschema again 2025-10-05 12:17:33 -03:00
4e2d506a01 fixing duplicate column 2025-10-05 11:42:56 -03:00
df4c76bd99 Refactor Manifest struct to change Dependencies from slice to map for better dependency management, and removing somethings from manifest struct too 2025-10-03 18:45:49 -03:00
af24fa84a2 don't need to verify index.db integrity anymore 2025-10-03 17:04:04 -03:00
4b54a9c74b Refactor database schema and update dependency handling in code 2025-10-03 17:03:23 -03:00
f25366d40c Refactor database schema and update dependency handling in code 2025-10-01 22:19:02 -03:00
cadf5fedcb Improving packets with better dependencie resolution, adding a function to auto resolve dependencies. 2025-10-01 18:11:53 -03:00
cfb11cf6e6 New database schema need to change all code files 2025-10-01 17:32:08 -03:00
bb4f221fc9 Adding "-r", to create a system user to not show up on user login screen 2025-09-29 19:21:24 -03:00
9cf5ae61d8 removing the second and useless exec.Command of useradd 2025-09-29 16:37:13 -03:00
ff4c61315e Removing unecessary param and variable on GetSandbox 2025-09-28 21:58:55 -03:00
76450789b0 io can be used, require too, and package 2025-09-28 21:55:50 -03:00
0e8db8b40e Enhance permission handling in package installation and removal commands; add user management functions for improved security. Now for every time packets will execute lua scripts, it will change process euid to an unprivileged user; Now lua scripts can execute more lua default functions 2025-09-28 21:55:13 -03:00
3591460214 Update search command description for clarity 2025-09-28 17:55:52 -03:00
96db4572b4 Add list command to manage installed packages, and search to see all packages avaiable 2025-09-28 17:55:36 -03:00
17e1b4b3ab Refactor logging and variable names for consistency; update log messages for clarity 2025-09-28 17:24:17 -03:00
73171424e4 Update dependencies: remove indirect reference to gin-gonic and add new dependencies for go-cmp, assert, and testify 2025-09-28 17:24:02 -03:00
b4f55ad36f Fixing some huge bugs, and implemented remove function 2025-09-28 16:45:51 -03:00
2c322d4de8 progress doing remove cmd, and removing GetPackage function from the pkg package, now GetPackage function can be found on package utils 2025-09-27 00:39:49 -03:00
2735749b12 Added a function to read manifest from the file manifest.toml and not only from a package file 2025-09-23 19:12:08 -03:00
05fbbde194 Setting data_dir variable to lua scripts to a string with the package data folder 2025-09-23 19:11:11 -03:00
fe81e6bf22 Revert "setting data_dir variable to a string with the package data folder"
This reverts commit 2cfe78721a.
2025-09-23 19:09:50 -03:00
2cfe78721a setting data_dir variable to a string with the package data folder 2025-09-23 19:08:21 -03:00
ff986ef943 now InstallPackage function from packets package don't try to read a config.toml, it get the path to install by a param; added async dependency install process 2025-09-21 22:52:03 -03:00
f34308367e Implement UDP socket server and add package installation check utility 2025-09-21 10:34:49 -03:00
68b394523d Add ed25519 public key and enhance package installation error handling
- Embed ed25519 public key for signature verification when doing sync prcess with servidordomal.fun
- Improve error handling in AddToInstalledDB to rollback on failure
- Update InstallPackage function to accept io.Reader instead of *os.File
2025-09-20 21:55:34 -03:00
b14bd1806a Zipping html files 2025-09-20 20:18:20 -03:00
3929493bfb enhance package database schema and configuration; add dependencies 2025-09-20 19:31:38 -03:00
2620ec00ab implement package synchronization and validation; update database schema and add error handling 2025-09-20 19:12:01 -03:00
0485b8325f new index.db schema, removed likes and reports 2025-09-20 16:05:53 -03:00
33d636b41d go.mod and go.sum with some indirect packages 2025-09-20 16:05:31 -03:00
ac236342b6 added web/ with some html and updated go.mod and go.sum to use gin 2025-09-20 13:04:32 -03:00
910cad2734 deleting old schema 2025-09-20 11:04:08 -03:00
fdb21aacc5 added .Execute to run cobracmds 2025-09-20 11:02:35 -03:00
a22a2a70c0 Database schemas added 2025-09-20 11:02:09 -03:00
f5399a66ba new index.db schema 2025-09-20 08:51:01 -03:00
9e09b1e3a4 renamed DownloadPackageHTTP function to GetFileHTTP 2025-09-19 23:33:12 -03:00
f8bda68a57 Added a const to index.db 2025-09-19 23:32:50 -03:00
b84d43200a index.db schema 2025-09-19 23:32:32 -03:00
35cbc2e47c Created copydir and copyfile functions in utils, now lua os.copy can copy an entire directory 2025-09-19 22:18:34 -03:00
3a068ed90b InstallPackage from pkg running luascript hook 2025-09-18 20:47:08 -03:00
bdbc580c82 GetSandbox aded on package utils_lua, it returns lua.LState with all sandboxfunctions 2025-09-18 20:40:05 -03:00
3c770c469d better file organization 2025-09-18 20:12:07 -03:00
1c00df24a4 Added ask for lan function 2025-09-13 23:11:48 -03:00
f3ccd6d683 Clean Install function and Download function 2025-09-13 22:57:52 -03:00
8de2eaced7 Doing organization improvements 2025-09-13 22:24:04 -03:00
c0057ca053 Deleting all to rewrite better 2025-09-13 20:45:07 -03:00
61149ae711 Added description for GetPackageByMirror function 2025-09-08 22:23:15 -03:00
bf10e39ffc Merge branch 'main' of https://github.com/roboogg133/packets 2025-09-08 22:22:07 -03:00
379f640f33 added Install function description 2025-09-08 22:22:03 -03:00
c7b0555076 Update README.md 2025-09-07 11:11:14 -03:00
a3d18ed3d7 Created package.lua example 2025-09-03 22:45:41 -03:00
5c39f7ab2c making LANDeadline const, more readable 2025-09-03 22:43:25 -03:00
b676ea873e Setting a global variable on lua scripts to check if is running safely or not 2025-09-03 22:26:39 -03:00
b6a67b30c5 Creating table packages on installed.db if doesn't exists 2025-09-03 22:22:18 -03:00
c0a8922c2e Setting a const variable to default deadline for lan deadline 2025-09-02 13:14:35 -03:00
fad209d23c Moving it to the test branch 2025-08-20 15:34:04 -03:00
8dd6c68a15 Moving this packages to test branch 2025-08-20 15:33:39 -03:00
0c81469566 missed return in mkdir function 2025-08-20 15:33:12 -03:00
6775002886 sorry for adding this to main branch 2025-08-20 14:47:05 -03:00
bf1f967581 test 2025-08-20 14:31:23 -03:00
15 changed files with 1011 additions and 0 deletions

7
go.mod Normal file
View File

@@ -0,0 +1,7 @@
module github.com/roboogg133/packets
go 1.25.3
require github.com/yuin/gopher-lua v1.1.1
require github.com/klauspost/compress v1.18.1

4
go.sum Normal file
View File

@@ -0,0 +1,4 @@
github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=
github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=

View 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
View 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
}

10
main.go Normal file
View File

@@ -0,0 +1,10 @@
package main
import (
"fmt"
"runtime"
)
func main() {
fmt.Println(runtime.GOARCH)
}

Binary file not shown.

View File

@@ -0,0 +1,344 @@
package packet
import (
"strings"
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(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
}
type version struct {
Name string
Constraint VersionConstraint
}
func getDependenciesFromTable(table *lua.LTable, key string) *PkgDependencies {
value := table.RawGetString(key)
if value.Type() != lua.LTTable {
return &PkgDependencies{}
}
var pkgDeps PkgDependencies
depnTable := value.(*lua.LTable)
pkgDeps.RuntimeDependencies = depsParse(depnTable, "runtime")
pkgDeps.BuildDependencies = depsParse(depnTable, "build")
pkgDeps.Conflicts = depsParse(depnTable, "conflicts")
return &pkgDeps
}
func getSourcesFromTable(table *lua.LTable, key string) *[]Source {
value := table.RawGetString(key)
if value.Type() != lua.LTTable {
return nil
}
var srcList []Source
srcTable := value.(*lua.LTable)
srcTable.ForEach(func(_, value lua.LValue) {
if value.Type() == lua.LTTable {
src := value.(*lua.LTable)
var srcInfo Source
method := src.RawGetString("method")
if method.Type() == lua.LTString {
srcInfo.Method = method.String()
}
url := src.RawGetString("url")
if url.Type() == lua.LTString {
srcInfo.Url = url.String()
}
switchlabel:
switch srcInfo.Method {
case "GET":
var getSpecs GETSpecs
getSpecs.SHA256 = new(string)
sha256sumL := src.RawGetString("sha256")
if sha256sumL.Type() == lua.LTString {
*getSpecs.SHA256 = sha256sumL.String()
}
headersLT := src.RawGetString("headers")
if headersLT.Type() == lua.LTTable {
headers := headersLT.(*lua.LTable)
tmpMap := make(map[string]string)
headers.ForEach(func(headerKey, value lua.LValue) {
if value.Type() == lua.LTString {
tmpMap[headerKey.String()] = value.String()
}
})
getSpecs.Headers = &tmpMap
}
srcInfo.Specs = getSpecs
break switchlabel
case "git":
var gitSpecs GitSpecs
branchL := src.RawGetString("branch")
if branchL.Type() == lua.LTString {
gitSpecs.Branch = branchL.String()
}
tagL := src.RawGetString("tag")
if tagL.Type() == lua.LTString {
*gitSpecs.Tag = tagL.String()
}
srcInfo.Specs = gitSpecs
break switchlabel
case "POST":
var postSpecs POSTSpecs
sha256sumL := src.RawGetString("sha256")
if sha256sumL.Type() == lua.LTString {
*postSpecs.SHA256 = sha256sumL.String()
}
headersLT := src.RawGetString("headers")
if headersLT.Type() == lua.LTTable {
headers := headersLT.(*lua.LTable)
tmpMap := make(map[string]string)
headers.ForEach(func(headerKey, value lua.LValue) {
if value.Type() == lua.LTString {
tmpMap[headerKey.String()] = value.String()
}
})
postSpecs.Headers = &tmpMap
}
bodyLt := src.RawGetString("body")
if bodyLt.Type() == lua.LTString {
*postSpecs.Body = bodyLt.String()
}
srcInfo.Specs = postSpecs
break switchlabel
}
srcList = append(srcList, srcInfo)
}
})
return &srcList
}
func getPlataformsFromTable(table *lua.LTable, key string) *map[OperationalSystem]Plataform {
value := table.RawGetString(key)
if value.Type() != lua.LTTable {
return nil
}
tmpMap := make(map[OperationalSystem]Plataform)
plataform := value.(*lua.LTable)
plataform.ForEach(func(osString, value lua.LValue) {
if value.Type() != lua.LTTable {
return
}
var plat Plataform
plat.Architetures = getStringArrayFromTable(value.(*lua.LTable), "arch")
plat.Name = osString.String()
plat.Sources = getSourcesFromTable(value.(*lua.LTable), "sources")
plat.Dependencies = getDependenciesFromTable(value.(*lua.LTable), "dependencies")
tmpMap[OperationalSystem(osString.String())] = plat
})
if len(tmpMap) == 0 {
return nil
}
return &tmpMap
}
func depsParse(depnTable *lua.LTable, key string) *map[string]*VersionConstraint {
if runLTable := depnTable.RawGetString(key); runLTable.Type() == lua.LTTable {
runtimeTable := runLTable.(*lua.LTable)
mapTemp := make(map[string]*VersionConstraint)
var found bool
runtimeTable.ForEach(func(_, value lua.LValue) {
if value.Type() == lua.LTString {
version := parseVersionString(value.String())
mapTemp[version.Name] = &version.Constraint
found = true
}
})
if !found {
return nil
} else {
return &mapTemp
}
}
return nil
}
func parseVersionString(s string) version {
// >=go@1.25.3 | <=go@1.25.3 | go | >go@1.25.3 | <go@1.25.3 | go@1.25.3
if strings.ContainsAny(s, "@") {
slice := strings.Split(s, "@")
switch {
case !strings.ContainsAny(s, "<=>"):
return version{
Name: slice[0],
Constraint: VersionConstraint(slice[1]),
}
case s[0] == '>' && s[1] == '=':
return version{
Name: slice[0][2:],
Constraint: VersionConstraint(">=" + slice[1]),
}
case s[0] == '<' && s[1] == '=':
return version{
Name: slice[0][2:],
Constraint: VersionConstraint("<=" + slice[1]),
}
case s[0] == '>' && s[1] != '=':
return version{
Name: slice[0][1:],
Constraint: VersionConstraint(">" + slice[1]),
}
case s[0] == '<' && s[1] != '=':
return version{
Name: slice[0][1:],
Constraint: VersionConstraint("<" + slice[1]),
}
}
} else if !strings.ContainsAny(s, "@=<>") {
return version{
Name: s,
Constraint: VersionConstraint(0x000),
}
}
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
}
}

View File

@@ -0,0 +1,39 @@
package packet
type Config struct {
BinDir *string
}
const defaultBinDir = "/usr/bin"
func checkConfig(cfg *Config) *Config {
if cfg == nil {
bin := defaultBinDir
return &Config{
BinDir: &bin,
}
}
if *cfg.BinDir == "" || cfg.BinDir == nil {
bin := defaultBinDir
return &Config{
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
}

267
pkg/packet.lua.d/main.go Normal file
View File

@@ -0,0 +1,267 @@
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"
)
type OperationalSystem string
type PacketLua struct {
Name string
Version string
Maintaner string
Description string
Serial int
Plataforms *map[OperationalSystem]Plataform
GlobalSources *[]Source
GlobalDependencies *PkgDependencies
Build *lua.LFunction
Install *lua.LFunction
PreRemove *lua.LFunction
}
type Source struct {
Method string
Url string
Specs any
}
type VersionConstraint string
type PkgDependencies struct {
RuntimeDependencies *map[string]*VersionConstraint
BuildDependencies *map[string]*VersionConstraint
Conflicts *map[string]*VersionConstraint
}
type Plataform struct {
Name string
Architetures []string
Sources *[]Source
Dependencies *PkgDependencies
}
type GitSpecs struct {
Branch string
Tag *string
}
type POSTSpecs struct {
SHA256 *string
Body *string
Headers *map[string]string
}
type GETSpecs struct {
SHA256 *string
Headers *map[string]string
}
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) {
cfg = checkConfig(cfg)
L := lua.NewState()
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("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
}
L.SetGlobal("os", osObject)
L.SetGlobal("io", ioObject)
tableLua := L.Get(-1)
if tableLua.Type() != lua.LTTable {
return PacketLua{}, ErrFileDontReturnTable
}
table := tableLua.(*lua.LTable)
pkgTableLua := table.RawGetString("package")
if pkgTableLua.Type() != lua.LTTable {
return PacketLua{}, ErrCannotFindPackageTable
}
pkgTable := pkgTableLua.(*lua.LTable)
packetLua := &PacketLua{
Name: getStringFromTable(pkgTable, "name"),
Version: getStringFromTable(pkgTable, "version"),
Maintaner: getStringFromTable(pkgTable, "maintainer"),
Description: getStringFromTable(pkgTable, "description"),
Serial: getIntFromTable(pkgTable, "serial"),
Plataforms: getPlataformsFromTable(pkgTable, "plataforms"),
GlobalDependencies: getDependenciesFromTable(pkgTable, "build_dependencies"),
GlobalSources: getSourcesFromTable(pkgTable, "sources"),
Build: getFunctionFromTable(table, "build"),
Install: getFunctionFromTable(table, "install"),
PreRemove: getFunctionFromTable(table, "pre_remove"),
}
if packetLua.Install == nil {
return PacketLua{}, ErrInstallFunctionDoesNotExist
}
return *packetLua, nil
}
func ReadPacketFromZSTDF(file io.Reader, cfg *Config) (PacketLua, error) {
cfg = checkConfig(cfg)
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, cfg)
}
}
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
}

View File

@@ -0,0 +1,25 @@
package packet
func (pkg PacketLua) IsValid() bool {
var a, b int
for _, v := range *pkg.Plataforms {
a += len(*v.Sources)
b += len(v.Architetures)
}
a += len(*pkg.GlobalSources)
if a < 1 || len(*pkg.Plataforms) > b {
return false
}
switch {
case pkg.Serial == -133:
return false
case pkg.Description == "" || pkg.Maintaner == "" || pkg.Name == "" || pkg.Version == "":
return false
}
return true
}

13
pkg/packet.lua.d/rand.go Normal file
View 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)
}

Binary file not shown.

53
test/bat/Packet.lua Normal file
View File

@@ -0,0 +1,53 @@
return {
package = {
name = "bat-bin", -- required
version = "0.26.0", -- required
maintainer = "robogg133", -- required
description = "A cat(1) clone with syntax highlighting and Git integration.", -- required
serial = 0,-- required
plataforms = {
windows = {
arch = {"amd64"},
sources = {
{
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"
}
},
dependencies = {
build = {},
runtime = {},
conflicts = {}
}
},
linux = {
arch = {"amd64"},
sources = {
{
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"
}
},
dependencies = {
build = {},
runtime = {},
conflicts = {}
}
}
},
sources = {}
},
build = function()
end,
install = function() -- required
print("oi amores")
end,
}

View File

@@ -0,0 +1,34 @@
return {
package = {
name = "utctimerightnow", -- required
version = "0.1.0", -- required
maintainer = "robogg133", -- required
description = "shows utc time", -- required
serial = 0,-- required
dependencies = {
build = {"go"},
runtime = {},
conflicts = {}
},
sources = { --optional
{
url = "https://git.opentty.xyz/robogg133/utctimerightnow.git", -- required
method = "git", -- required
branch = "main" -- required
-- tag = ""
}
}
},
build = function()
end,
install = function() -- required
print("goku")
end,
}

Binary file not shown.