Compare commits
	
		
			100 Commits
		
	
	
		
			test
			...
			a111b060f1
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a111b060f1 | |||
| a62ddf8270 | |||
| aa65b28112 | |||
| 72a5ab7c5d | |||
| 89b3fdbc84 | |||
| 0a531488a3 | |||
| 7fbaef7bd4 | |||
| b411eff6f4 | |||
| befa4e3ea4 | |||
| 807d9fa784 | |||
| 9883fd92dc | |||
| 51c51b96bf | |||
| a69de7e918 | |||
| 4178387e2a | |||
| b6d7ec8a5f | |||
| 8feaf5d19b | |||
| 820ffc299f | |||
| ecce74d2e9 | |||
| 5ba30c617a | |||
| e3772d0944 | |||
| df32178372 | |||
| cc9587821d | |||
| 31af4d5389 | |||
| dbd63d371b | |||
| 52f675ab60 | |||
| 26f9e20ae8 | |||
| 6377de7208 | |||
| b58173837b | |||
| 97408da348 | |||
| be96001d78 | |||
| 7c4fba5c86 | |||
| 4cee062889 | |||
| 93093382f4 | |||
| e4b4d43163 | |||
| b89abb31df | |||
| 0a965c67c2 | |||
| 147fca375e | |||
| afc19b6e4d | |||
| 690f180687 | |||
| 7ef7b60cf5 | |||
| 4e2d506a01 | |||
| df4c76bd99 | |||
| af24fa84a2 | |||
| 4b54a9c74b | |||
| f25366d40c | |||
| cadf5fedcb | |||
| cfb11cf6e6 | |||
| bb4f221fc9 | |||
| 9cf5ae61d8 | |||
| ff4c61315e | |||
| 76450789b0 | |||
| 0e8db8b40e | |||
| 3591460214 | |||
| 96db4572b4 | |||
| 17e1b4b3ab | |||
| 73171424e4 | |||
| b4f55ad36f | |||
| 2c322d4de8 | |||
| 2735749b12 | |||
| 05fbbde194 | |||
| fe81e6bf22 | |||
| 2cfe78721a | |||
| ff986ef943 | |||
| f34308367e | |||
| 68b394523d | |||
| b14bd1806a | |||
| 3929493bfb | |||
| 2620ec00ab | |||
| 0485b8325f | |||
| 33d636b41d | |||
| ac236342b6 | |||
| 910cad2734 | |||
| fdb21aacc5 | |||
| a22a2a70c0 | |||
| f5399a66ba | |||
| 9e09b1e3a4 | |||
| f8bda68a57 | |||
| b84d43200a | |||
| 35cbc2e47c | |||
| 3a068ed90b | |||
| bdbc580c82 | |||
| 3c770c469d | |||
| 1c00df24a4 | |||
| f3ccd6d683 | |||
| 8de2eaced7 | |||
| c0057ca053 | |||
| 61149ae711 | |||
| bf10e39ffc | |||
| 379f640f33 | |||
| c7b0555076 | |||
| a3d18ed3d7 | |||
| 5c39f7ab2c | |||
| b676ea873e | |||
| b6a67b30c5 | |||
| c0a8922c2e | |||
| fad209d23c | |||
| 8dd6c68a15 | |||
| 0c81469566 | |||
| 6775002886 | |||
| bf1f967581 | 
							
								
								
									
										36
									
								
								Packet.lua
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								Packet.lua
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
return {
 | 
			
		||||
    package = {
 | 
			
		||||
        name = "packets",
 | 
			
		||||
        id   = "packets@1.0.0",
 | 
			
		||||
        version = "1.0.0",
 | 
			
		||||
        author  = "robogg133",
 | 
			
		||||
        description = "fast, opensource, easy to use package manager.",
 | 
			
		||||
        type = "remote",
 | 
			
		||||
        serial = 0,
 | 
			
		||||
 | 
			
		||||
        build_dependencies = {["go"] = ">=1.25.1"},
 | 
			
		||||
 | 
			
		||||
        git_url = "https://github.com/roboogg133/packets.git",
 | 
			
		||||
        git_branch = "main"
 | 
			
		||||
 | 
			
		||||
    },
 | 
			
		||||
    
 | 
			
		||||
    
 | 
			
		||||
    prepare = function(container)
 | 
			
		||||
        git.clone("https://github.com/roboogg133/packets.git", container.dir("/data"))
 | 
			
		||||
        os.remove(container.dir("/data/.git"))
 | 
			
		||||
 | 
			
		||||
    end,
 | 
			
		||||
    
 | 
			
		||||
    build = function()
 | 
			
		||||
        os.execute("go build ./data/cmd/packets")
 | 
			
		||||
    end,
 | 
			
		||||
    
 | 
			
		||||
    install = function(container)
 | 
			
		||||
        os.copy(container.dir("./packets"), BIN_DIR)
 | 
			
		||||
    end,
 | 
			
		||||
 | 
			
		||||
    remove = function ()
 | 
			
		||||
        os.remove(path_join(BIN_DIR, "packets"))
 | 
			
		||||
    end
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										163
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										163
									
								
								README.md
									
									
									
									
									
								
							@@ -1,163 +0,0 @@
 | 
			
		||||
# 📦 Packets – Custom Package Manager for Linux
 | 
			
		||||
 | 
			
		||||
> A fast and minimal package manager written in Go with Lua hooks, local network discovery, and SQLite-based indexing.
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 📘 Overview
 | 
			
		||||
 | 
			
		||||
**Packets** is a lightweight package manager for Linux, written in Go. It supports:
 | 
			
		||||
 | 
			
		||||
- Installation and removal of packages
 | 
			
		||||
- Dependency resolution and upgrading
 | 
			
		||||
- `.tar.zst` compressed packages with `manifest.toml` metadata
 | 
			
		||||
- Lua-based install/remove hooks
 | 
			
		||||
- Local cache with SHA-256 validation
 | 
			
		||||
- Peer-to-peer discovery over LAN
 | 
			
		||||
- Remote package syncing via HTTP
 | 
			
		||||
- SQLite-based local database
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
## 📁 Directory Structure
 | 
			
		||||
 | 
			
		||||
| Path                  | Description                      |
 | 
			
		||||
|-----------------------|----------------------------------|
 | 
			
		||||
| `/etc/packets/`       | Configuration files              |
 | 
			
		||||
| `/opt/packets/`       | Installed package data           |
 | 
			
		||||
| `/var/cache/packets/` | Cached `.tar.zst` package files  |
 | 
			
		||||
 | 
			
		||||
(This can be changed in `/etc/packets/config.toml`)
 | 
			
		||||
 | 
			
		||||
---
 | 
			
		||||
 | 
			
		||||
# Available Commands
 | 
			
		||||
 | 
			
		||||
| Command                   | Description                                                                |
 | 
			
		||||
|---------------------------|----------------------------------------------------------------------------|
 | 
			
		||||
|`packets install <name>`	|    Install a package (resolves dependencies, executes Lua install hook)    |
 | 
			
		||||
|`packets remove <name>`	|    Remove a package (executes Lua remove hook)                             |
 | 
			
		||||
|`packets upgrade <name>`	|    Upgrade a package by checking family and serial in the manifest         |
 | 
			
		||||
|`packets sync [url]`	    |    Synchronize index.db from remote HTTP source                            |
 | 
			
		||||
|`packets serve init/stop`  |    Starts and stop the LAN service daemon                                  |
 | 
			
		||||
|`packets list`	            |    List all installed packages                                             |
 | 
			
		||||
|`packets info`             |    Get technical package information                                       |
 | 
			
		||||
|`packets search`           |    List all packages in index.db                                           |
 | 
			
		||||
 | 
			
		||||
# 📦 Package Format
 | 
			
		||||
 | 
			
		||||
Packages must be compressed as .tar.zst and include:
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
- ├── manifest.toml       # Package metadata
 | 
			
		||||
- ├── data/               # Files to install
 | 
			
		||||
- ├── install.lua         # Lua install hook
 | 
			
		||||
- └── remove.lua          # Lua remove hook
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## Example manifest.toml
 | 
			
		||||
    [Info]
 | 
			
		||||
     name = "packets"
 | 
			
		||||
     version = "1.0.0"
 | 
			
		||||
     description = "offline and online packetmanager"
 | 
			
		||||
     dependencies = []
 | 
			
		||||
     author = "robo"
 | 
			
		||||
     family = "1f84ca15-5077-4f1d-a370-0ec860766eb2"
 | 
			
		||||
     serial = 0
 | 
			
		||||
 | 
			
		||||
    [Hooks]
 | 
			
		||||
     install = "install.lua"
 | 
			
		||||
     remove = "remove.lua"
 | 
			
		||||
 | 
			
		||||
--
 | 
			
		||||
# 🔄 Installation Process
 | 
			
		||||
 | 
			
		||||
- Check if package is already cached and validated via SHA-256.
 | 
			
		||||
 | 
			
		||||
- If not, search the package:
 | 
			
		||||
 | 
			
		||||
    Via LAN: Sends UDP broadcast (Q:filename) to peers.
 | 
			
		||||
 | 
			
		||||
    Via HTTP: Downloads from configured mirrors.
 | 
			
		||||
 | 
			
		||||
    Decompress .tar.zst, install files.
 | 
			
		||||
 | 
			
		||||
    Execute Lua install hook.
 | 
			
		||||
 | 
			
		||||
# 🧩 Core Features
 | 
			
		||||
✅ Dependency Resolution
 | 
			
		||||
 | 
			
		||||
Installs required dependencies listed in the manifest.
 | 
			
		||||
## 🌐 LAN Discovery
 | 
			
		||||
 | 
			
		||||
Broadcasts package request to devices in the same network via UDP.
 | 
			
		||||
## 📡 Remote Download
 | 
			
		||||
 | 
			
		||||
Downloads package via HTTP if not found on LAN.
 | 
			
		||||
## 🔒 Security
 | 
			
		||||
 | 
			
		||||
    SHA-256 checksum validation
 | 
			
		||||
 | 
			
		||||
    Path validation to avoid exploits (..)
 | 
			
		||||
 | 
			
		||||
    Safe, sandboxed Lua runtime with limited API
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# 🌍 Global Variables Available in Lua Scripts
 | 
			
		||||
 | 
			
		||||
During the execution of install.lua and remove.lua hooks, some global variables are automatically provided to the Lua environment. These help simplify file path handling and access to package-specific directories.
 | 
			
		||||
## Available variables:
 | 
			
		||||
 | 
			
		||||
|Name	             |Type	  |  Description
 | 
			
		||||
|--------------------|--------|--------------------------------------------------------------------------|
 | 
			
		||||
|packets_package_dir |	string|	Absolute path to the package's data directory (e.g., /opt/packets/...)   | 
 | 
			
		||||
| packets_bin_dir      |  string| Path where executables should be installed (e.g., /usr/bin)            |
 | 
			
		||||
|script              |  string|	Path to the currently executing script (e.g., "install.lua")             |
 | 
			
		||||
|data_dir            |  string|	Path to the /data folder of the current package                          |
 | 
			
		||||
### Example usage in Lua:
 | 
			
		||||
 | 
			
		||||
        print("Installing into: " .. packets_bin_dir)
 | 
			
		||||
        print("Package data in: " .. data_dir)
 | 
			
		||||
 | 
			
		||||
        -- Copy a binary to /usr/bin
 | 
			
		||||
        os.copy(path_join(data_dir, "htop"), path_join(packets_bin_dir, "htop"))
 | 
			
		||||
 | 
			
		||||
These variables are preloaded in the Lua environment—no need to manually declare or initialize them.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
## 🛠️ Allowed Lua API (install/remove hooks)
 | 
			
		||||
 | 
			
		||||
To ensure security, only a limited set of safe functions are exposed in Lua hooks:
 | 
			
		||||
 | 
			
		||||
    os.remove(path)
 | 
			
		||||
    os.rename(old, new)
 | 
			
		||||
    os.copy(source, target)
 | 
			
		||||
    os.symlink(source, target)
 | 
			
		||||
    io.open(path, mode)
 | 
			
		||||
    os.mkdir(path, filemode)
 | 
			
		||||
    path_join(...) 
 | 
			
		||||
 | 
			
		||||
### Note: Dangerous functions like os.execute, os.getenv, etc. are removed.
 | 
			
		||||
## 🗃️ Databases
 | 
			
		||||
 | 
			
		||||
    index.db: Available packages (after sync)
 | 
			
		||||
 | 
			
		||||
    installed.db: Packages currently installed
 | 
			
		||||
 | 
			
		||||
# ⚠️ Restrictions & Notes
 | 
			
		||||
 | 
			
		||||
    Linux only (//go:build linux)
 | 
			
		||||
 | 
			
		||||
    Root permissions required for most commands
 | 
			
		||||
 | 
			
		||||
    Changing dataDir triggers prompt to migrate installed packages
 | 
			
		||||
 | 
			
		||||
    Binaries in binDir are not automatically moved if path changes
 | 
			
		||||
 | 
			
		||||
    Do not manually edit lastDataDir
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
# TODO
 | 
			
		||||
 | 
			
		||||
- Auto upgrade all packages available
 | 
			
		||||
- Packets Universe
 | 
			
		||||
@@ -1,37 +0,0 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
 | 
			
		||||
	"packets/internal"
 | 
			
		||||
 | 
			
		||||
	"github.com/BurntSushi/toml"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		DefaultHttpPort int    `toml:"httpPort"`
 | 
			
		||||
		DefaultCacheDir string `toml:"cacheDir"`
 | 
			
		||||
	} `toml:"Config"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	internal.PacketsPackageDir()
 | 
			
		||||
	var cfg ConfigTOML
 | 
			
		||||
	toml.Decode(filepath.Join(internal.PacketsPackageDir(), "config.toml"), &cfg)
 | 
			
		||||
 | 
			
		||||
	pid := os.Getpid()
 | 
			
		||||
	if err := os.WriteFile(filepath.Join(internal.PacketsPackageDir(), "http.pid"), []byte(fmt.Sprint(pid)), 0644); err != nil {
 | 
			
		||||
		fmt.Println("error saving subprocess pid", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fs := http.FileServer(http.Dir(cfg.Config.DefaultCacheDir))
 | 
			
		||||
	http.Handle("/", fs)
 | 
			
		||||
 | 
			
		||||
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Config.DefaultHttpPort), nil))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										36
									
								
								cmd/httpsocket/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								cmd/httpsocket/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"packets/configs"
 | 
			
		||||
	"packets/internal/consts"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		DefaultHttpPort int    `toml:"httpPort"`
 | 
			
		||||
		DefaultCacheDir string `toml:"cacheDir"`
 | 
			
		||||
	} `toml:"Config"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	log.Println("Program started")
 | 
			
		||||
	cfg, err := configs.GetConfigTOML()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	pid := os.Getpid()
 | 
			
		||||
	if err := os.WriteFile(filepath.Join(consts.DefaultLinux_d, "http.pid"), []byte(fmt.Sprint(pid)), 0664); err != nil {
 | 
			
		||||
		fmt.Println("error saving subprocess pid", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fs := http.FileServer(http.Dir(cfg.Config.Cache_d))
 | 
			
		||||
	http.Handle("/", fs)
 | 
			
		||||
	log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Config.HttpPort), nil))
 | 
			
		||||
	log.Printf("Listening and serving on port %d\n", cfg.Config.HttpPort)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2215
									
								
								cmd/packets/main.go
									
									
									
									
									
								
							
							
						
						
									
										2215
									
								
								cmd/packets/main.go
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -5,45 +5,40 @@ import (
 | 
			
		||||
	"log"
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
	"packets/internal"
 | 
			
		||||
	"packets/configs"
 | 
			
		||||
	"packets/internal/consts"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/BurntSushi/toml"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		HttpPort int    `toml:"httpPort"`
 | 
			
		||||
		CacheDir string `toml:"cacheDir"`
 | 
			
		||||
	} `toml:"Config"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var cfg ConfigTOML
 | 
			
		||||
 | 
			
		||||
func CheckDownloaded(filename string) bool {
 | 
			
		||||
 | 
			
		||||
	_, err := os.Stat(filepath.Join(cfg.Config.CacheDir))
 | 
			
		||||
	if os.IsNotExist(err) {
 | 
			
		||||
		return false
 | 
			
		||||
	} else {
 | 
			
		||||
		return true
 | 
			
		||||
	cfg, err := configs.GetConfigTOML()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = os.Stat(filepath.Join(cfg.Config.Cache_d, filename))
 | 
			
		||||
	return err == nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	log.Println("Program started")
 | 
			
		||||
	pid := os.Getpid()
 | 
			
		||||
	if err := os.WriteFile(filepath.Join(internal.PacketsPackageDir(), "udp.pid"), []byte(fmt.Sprint(pid)), 0644); err != nil {
 | 
			
		||||
	if err := os.WriteFile(filepath.Join(consts.DefaultLinux_d, "udp.pid"), []byte(fmt.Sprint(pid)), 0664); err != nil {
 | 
			
		||||
		fmt.Println("error saving subprocess pid", err)
 | 
			
		||||
	}
 | 
			
		||||
	toml.Decode(filepath.Join(internal.PacketsPackageDir(), "config.toml"), &cfg)
 | 
			
		||||
	cfg, err := configs.GetConfigTOML()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	addr := net.UDPAddr{IP: net.IPv4zero, Port: 1333}
 | 
			
		||||
	conn, err := net.ListenUDP("udp", &addr)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	log.Println("Started connection listener")
 | 
			
		||||
	defer conn.Close()
 | 
			
		||||
	buf := make([]byte, 1500)
 | 
			
		||||
 | 
			
		||||
@@ -53,12 +48,15 @@ func main() {
 | 
			
		||||
			log.Println("error creating udp socket", err)
 | 
			
		||||
		}
 | 
			
		||||
		msg := string(buf[:n])
 | 
			
		||||
		log.Printf("Received message : %s\n", msg)
 | 
			
		||||
		if !strings.HasPrefix(msg, "Q:") {
 | 
			
		||||
			log.Println("error: invalid message, this message don't follow the protocol")
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
		filename := strings.TrimPrefix(msg, "Q:")
 | 
			
		||||
		if CheckDownloaded(filename) {
 | 
			
		||||
			reply := fmt.Sprintf("H:%s:%d", filename, cfg.Config.HttpPort)
 | 
			
		||||
			log.Printf("Package founded in cache dir, answering with: '%s'\n", reply)
 | 
			
		||||
			conn.WriteToUDP([]byte(reply), remote)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
							
								
								
									
										60
									
								
								configs/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								configs/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,60 @@
 | 
			
		||||
package configs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"os"
 | 
			
		||||
	"packets/internal/consts"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
 | 
			
		||||
	"github.com/pelletier/go-toml/v2"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// DefaultConfigTOML returns configTOML struct with all default values and create all directorys
 | 
			
		||||
func DefaultConfigTOML() (*ConfigTOML, error) {
 | 
			
		||||
 | 
			
		||||
	var config ConfigTOML
 | 
			
		||||
 | 
			
		||||
	_, err := os.Stat(consts.DefaultCache_d)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if os.IsNotExist(err) {
 | 
			
		||||
			err := os.MkdirAll(consts.DefaultCache_d, 0666)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = os.Stat(consts.DefaultCache_d)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if os.IsNotExist(err) {
 | 
			
		||||
			err := os.MkdirAll(consts.DefaultData_d, 0644)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				return nil, err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	config.Config.Cache_d = consts.DefaultCache_d
 | 
			
		||||
	config.Config.Data_d = consts.DefaultData_d
 | 
			
		||||
	config.Config.HttpPort = consts.DefaultHttpPort
 | 
			
		||||
	config.Config.Bin_d = consts.DefaultBin_d
 | 
			
		||||
	config.Config.StorePackages = true
 | 
			
		||||
 | 
			
		||||
	return &config, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetConfigTOML return settings values
 | 
			
		||||
func GetConfigTOML() (*ConfigTOML, error) {
 | 
			
		||||
	f, err := os.Open(filepath.Join(consts.DefaultLinux_d, "config.toml"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	decoder := toml.NewDecoder(f)
 | 
			
		||||
 | 
			
		||||
	var config ConfigTOML
 | 
			
		||||
	if err := decoder.Decode(&config); err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &config, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										39
									
								
								configs/structs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								configs/structs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,39 @@
 | 
			
		||||
package configs
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
type Manifest struct {
 | 
			
		||||
	Package struct {
 | 
			
		||||
		Name         string            `toml:"name"`
 | 
			
		||||
		Id           string            `toml:"id"`
 | 
			
		||||
		Version      string            `toml:"version"`
 | 
			
		||||
		Description  string            `toml:"description"`
 | 
			
		||||
		Dependencies map[string]string `toml:"dependencies"`
 | 
			
		||||
		Author       string            `toml:"author"`
 | 
			
		||||
		Architeture  string            `toml:"architeture"`
 | 
			
		||||
		Os           string            `toml:"os"`
 | 
			
		||||
		PacakgeType  string            `toml:"type"`
 | 
			
		||||
 | 
			
		||||
		GitUrl string `toml:"giturl,omitempty"`
 | 
			
		||||
		Branch string `toml:"gitbranch,omitempty"`
 | 
			
		||||
	} `toml:"Package"`
 | 
			
		||||
	Build struct {
 | 
			
		||||
		BuildDependencies map[string]string `toml:"dependencies"`
 | 
			
		||||
	}
 | 
			
		||||
	Hooks struct {
 | 
			
		||||
		Fetch   string `toml:"fetch,omitempty"`
 | 
			
		||||
		Install string `toml:"install"`
 | 
			
		||||
		Remove  string `toml:"remove"`
 | 
			
		||||
		Build   string `toml:"build"`
 | 
			
		||||
	} `toml:"Hooks"`
 | 
			
		||||
}
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		HttpPort      int    `toml:"httpPort"`
 | 
			
		||||
		Cache_d       string `toml:"cache_d"`
 | 
			
		||||
		Data_d        string `toml:"data_d"`
 | 
			
		||||
		Bin_d         string `toml:"bin_d"`
 | 
			
		||||
		StorePackages bool   `toml:"store_packages"`
 | 
			
		||||
	} `toml:"Config"`
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										56
									
								
								doc/indexdbSchema.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								doc/indexdbSchema.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
			
		||||
CREATE TABLE packages (
 | 
			
		||||
    id              TEXT NOT NULL UNIQUE PRIMARY KEY,
 | 
			
		||||
    query_name      TEXT NOT NULL,
 | 
			
		||||
    version         TEXT NOT NULL,
 | 
			
		||||
    serial          INTEGER NOT NULL,
 | 
			
		||||
    description     TEXT NOT NULL,
 | 
			
		||||
    image_url       TEXT NOT NULL,
 | 
			
		||||
    package_url     TEXT NOT NULL,
 | 
			
		||||
    public_key      BLOB NOT NULL,
 | 
			
		||||
    signature       BLOB NOT NULL,
 | 
			
		||||
    author          TEXT NOT NULL,
 | 
			
		||||
    author_verified INTEGER NOT NULL DEFAULT 0,
 | 
			
		||||
    arch            TEXT NOT NULL,
 | 
			
		||||
    os              TEXT NOT NULL,
 | 
			
		||||
    size            INTEGER NOT NULL DEFAULT 0,
 | 
			
		||||
    type            TEXT NOT NULL DEFAULT 'static',
 | 
			
		||||
 | 
			
		||||
    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 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 IF NOT EXISTS build_dependencies (
 | 
			
		||||
    id TEXT PRIMARY KEY,
 | 
			
		||||
    dir TEXT NOT NULL DEFAULT "/dev/null"
 | 
			
		||||
    uses INTEGER NOT NULL DEFAULT 0
 | 
			
		||||
);
 | 
			
		||||
							
								
								
									
										63
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								go.mod
									
									
									
									
									
								
							@@ -1,31 +1,68 @@
 | 
			
		||||
module packets
 | 
			
		||||
 | 
			
		||||
go 1.24.4
 | 
			
		||||
go 1.25.1
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/BurntSushi/toml v1.5.0
 | 
			
		||||
	github.com/gin-gonic/gin v1.11.0
 | 
			
		||||
	github.com/go-git/go-git/v6 v6.0.0-20251021092831-91c33c9361ce
 | 
			
		||||
	github.com/klauspost/compress v1.18.0
 | 
			
		||||
	github.com/schollz/progressbar/v3 v3.18.0
 | 
			
		||||
	github.com/spf13/cobra v1.9.1
 | 
			
		||||
	github.com/pelletier/go-toml/v2 v2.2.4
 | 
			
		||||
	github.com/spf13/afero v1.15.0
 | 
			
		||||
	github.com/spf13/cobra v1.10.1
 | 
			
		||||
	github.com/yuin/gopher-lua v1.1.1
 | 
			
		||||
	golang.org/x/net v0.41.0
 | 
			
		||||
	modernc.org/sqlite v1.38.0
 | 
			
		||||
	golang.org/x/net v0.46.0
 | 
			
		||||
	modernc.org/sqlite v1.38.2
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/Microsoft/go-winio v0.6.2 // indirect
 | 
			
		||||
	github.com/ProtonMail/go-crypto v1.3.0 // indirect
 | 
			
		||||
	github.com/bytedance/sonic v1.14.0 // indirect
 | 
			
		||||
	github.com/bytedance/sonic/loader v0.3.0 // indirect
 | 
			
		||||
	github.com/cloudflare/circl v1.6.1 // indirect
 | 
			
		||||
	github.com/cloudwego/base64x v0.1.6 // indirect
 | 
			
		||||
	github.com/cyphar/filepath-securejoin v0.5.0 // indirect
 | 
			
		||||
	github.com/dustin/go-humanize v1.0.1 // indirect
 | 
			
		||||
	github.com/emirpasic/gods v1.18.1 // indirect
 | 
			
		||||
	github.com/gabriel-vasile/mimetype v1.4.8 // indirect
 | 
			
		||||
	github.com/gin-contrib/sse v1.1.0 // indirect
 | 
			
		||||
	github.com/go-git/gcfg/v2 v2.0.2 // indirect
 | 
			
		||||
	github.com/go-git/go-billy/v6 v6.0.0-20251016063423-4289a4e54aa4 // indirect
 | 
			
		||||
	github.com/go-playground/locales v0.14.1 // indirect
 | 
			
		||||
	github.com/go-playground/universal-translator v0.18.1 // indirect
 | 
			
		||||
	github.com/go-playground/validator/v10 v10.27.0 // indirect
 | 
			
		||||
	github.com/goccy/go-json v0.10.2 // indirect
 | 
			
		||||
	github.com/goccy/go-yaml v1.18.0 // indirect
 | 
			
		||||
	github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
 | 
			
		||||
	github.com/google/uuid v1.6.0 // indirect
 | 
			
		||||
	github.com/inconshreveable/mousetrap v1.1.0 // indirect
 | 
			
		||||
	github.com/json-iterator/go v1.1.12 // indirect
 | 
			
		||||
	github.com/kevinburke/ssh_config v1.4.0 // indirect
 | 
			
		||||
	github.com/klauspost/cpuid/v2 v2.3.0 // indirect
 | 
			
		||||
	github.com/leodido/go-urn v1.4.0 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.20 // indirect
 | 
			
		||||
	github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
 | 
			
		||||
	github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
 | 
			
		||||
	github.com/modern-go/reflect2 v1.0.2 // indirect
 | 
			
		||||
	github.com/ncruces/go-strftime v0.1.9 // indirect
 | 
			
		||||
	github.com/pjbgf/sha1cd v0.5.0 // indirect
 | 
			
		||||
	github.com/quic-go/qpack v0.5.1 // indirect
 | 
			
		||||
	github.com/quic-go/quic-go v0.54.0 // indirect
 | 
			
		||||
	github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
 | 
			
		||||
	github.com/rivo/uniseg v0.4.7 // indirect
 | 
			
		||||
	github.com/spf13/pflag v1.0.7 // indirect
 | 
			
		||||
	golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.33.0 // indirect
 | 
			
		||||
	golang.org/x/term v0.32.0 // indirect
 | 
			
		||||
	modernc.org/libc v1.65.10 // indirect
 | 
			
		||||
	github.com/sergi/go-diff v1.4.0 // indirect
 | 
			
		||||
	github.com/spf13/pflag v1.0.10 // indirect
 | 
			
		||||
	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 | 
			
		||||
	github.com/ugorji/go/codec v1.3.0 // indirect
 | 
			
		||||
	go.uber.org/mock v0.5.0 // indirect
 | 
			
		||||
	golang.org/x/arch v0.20.0 // indirect
 | 
			
		||||
	golang.org/x/crypto v0.43.0 // indirect
 | 
			
		||||
	golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
 | 
			
		||||
	golang.org/x/mod v0.28.0 // indirect
 | 
			
		||||
	golang.org/x/sync v0.17.0 // indirect
 | 
			
		||||
	golang.org/x/sys v0.37.0 // indirect
 | 
			
		||||
	golang.org/x/text v0.30.0 // indirect
 | 
			
		||||
	golang.org/x/tools v0.37.0 // indirect
 | 
			
		||||
	google.golang.org/protobuf v1.36.9 // indirect
 | 
			
		||||
	modernc.org/libc v1.66.3 // indirect
 | 
			
		||||
	modernc.org/mathutil v1.7.1 // indirect
 | 
			
		||||
	modernc.org/memory v1.11.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										181
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										181
									
								
								go.sum
									
									
									
									
									
								
							@@ -1,74 +1,173 @@
 | 
			
		||||
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
 | 
			
		||||
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
 | 
			
		||||
github.com/chengxilo/virtualterm v1.0.4 h1:Z6IpERbRVlfB8WkOmtbHiDbBANU7cimRIof7mk9/PwM=
 | 
			
		||||
github.com/chengxilo/virtualterm v1.0.4/go.mod h1:DyxxBZz/x1iqJjFxTFcr6/x+jSpqN0iwWCOK1q10rlY=
 | 
			
		||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
 | 
			
		||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
 | 
			
		||||
github.com/ProtonMail/go-crypto v1.3.0 h1:ILq8+Sf5If5DCpHQp4PbZdS1J7HDFRXz/+xKBiRGFrw=
 | 
			
		||||
github.com/ProtonMail/go-crypto v1.3.0/go.mod h1:9whxjD8Rbs29b4XWbB8irEcE8KHMqaR2e7GWU1R+/PE=
 | 
			
		||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
 | 
			
		||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
 | 
			
		||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio=
 | 
			
		||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
 | 
			
		||||
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
 | 
			
		||||
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
 | 
			
		||||
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
 | 
			
		||||
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
 | 
			
		||||
github.com/cloudflare/circl v1.6.1 h1:zqIqSPIndyBh1bjLVVDHMPpVKqp8Su/V+6MeDzzQBQ0=
 | 
			
		||||
github.com/cloudflare/circl v1.6.1/go.mod h1:uddAzsPgqdMAYatqJ0lsjX1oECcQLIlRpzZh3pJrofs=
 | 
			
		||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
 | 
			
		||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
 | 
			
		||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
 | 
			
		||||
github.com/cyphar/filepath-securejoin v0.5.0 h1:hIAhkRBMQ8nIeuVwcAoymp7MY4oherZdAxD+m0u9zaw=
 | 
			
		||||
github.com/cyphar/filepath-securejoin v0.5.0/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 | 
			
		||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 | 
			
		||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
 | 
			
		||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
 | 
			
		||||
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
 | 
			
		||||
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
 | 
			
		||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
 | 
			
		||||
github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ=
 | 
			
		||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
 | 
			
		||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
 | 
			
		||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
 | 
			
		||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
 | 
			
		||||
github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk=
 | 
			
		||||
github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls=
 | 
			
		||||
github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=
 | 
			
		||||
github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=
 | 
			
		||||
github.com/go-git/gcfg/v2 v2.0.2 h1:MY5SIIfTGGEMhdA7d7JePuVVxtKL7Hp+ApGDJAJ7dpo=
 | 
			
		||||
github.com/go-git/gcfg/v2 v2.0.2/go.mod h1:/lv2NsxvhepuMrldsFilrgct6pxzpGdSRC13ydTLSLs=
 | 
			
		||||
github.com/go-git/go-billy/v6 v6.0.0-20251016063423-4289a4e54aa4 h1:xQLkKmWcw9n5CUkl//bKQB7SPWQbaoKSVnHXH9V8sg8=
 | 
			
		||||
github.com/go-git/go-billy/v6 v6.0.0-20251016063423-4289a4e54aa4/go.mod h1:TpCYxdQ0tWZkrnAkd7yqK+z1C8RKcyjcaYAJNAcnUnM=
 | 
			
		||||
github.com/go-git/go-git-fixtures/v5 v5.1.1 h1:OH8i1ojV9bWfr0ZfasfpgtUXQHQyVS8HXik/V1C099w=
 | 
			
		||||
github.com/go-git/go-git-fixtures/v5 v5.1.1/go.mod h1:Altk43lx3b1ks+dVoAG2300o5WWUnktvfY3VI6bcaXU=
 | 
			
		||||
github.com/go-git/go-git/v6 v6.0.0-20251021092831-91c33c9361ce h1:DFvDcCiFZk2yDXVreC9+B+SAZYHH2RssMmtR2zBwANE=
 | 
			
		||||
github.com/go-git/go-git/v6 v6.0.0-20251021092831-91c33c9361ce/go.mod h1:xtOWa43AoQlsqYogmpf0MnjBJHKPL2/3teh4fmZ/k+Y=
 | 
			
		||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
 | 
			
		||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
 | 
			
		||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
 | 
			
		||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
 | 
			
		||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
 | 
			
		||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
 | 
			
		||||
github.com/go-playground/validator/v10 v10.27.0 h1:w8+XrWVMhGkxOaaowyKH35gFydVHOvC0/uWoy2Fzwn4=
 | 
			
		||||
github.com/go-playground/validator/v10 v10.27.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
 | 
			
		||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
 | 
			
		||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
 | 
			
		||||
github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw=
 | 
			
		||||
github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
 | 
			
		||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
 | 
			
		||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
 | 
			
		||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
 | 
			
		||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
 | 
			
		||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
 | 
			
		||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
 | 
			
		||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
 | 
			
		||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
 | 
			
		||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 | 
			
		||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
 | 
			
		||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
 | 
			
		||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
 | 
			
		||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
 | 
			
		||||
github.com/kevinburke/ssh_config v1.4.0 h1:6xxtP5bZ2E4NF5tuQulISpTO2z8XbtH8cg1PWkxoFkQ=
 | 
			
		||||
github.com/kevinburke/ssh_config v1.4.0/go.mod h1:q2RIzfka+BXARoNexmF9gkxEX7DmvbW9P4hIVx2Kg4M=
 | 
			
		||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
 | 
			
		||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
 | 
			
		||||
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
 | 
			
		||||
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
 | 
			
		||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
 | 
			
		||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
 | 
			
		||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
 | 
			
		||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
 | 
			
		||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
 | 
			
		||||
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
 | 
			
		||||
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
 | 
			
		||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
 | 
			
		||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
 | 
			
		||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
 | 
			
		||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
 | 
			
		||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
 | 
			
		||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
 | 
			
		||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
 | 
			
		||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
 | 
			
		||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
 | 
			
		||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
 | 
			
		||||
github.com/pjbgf/sha1cd v0.5.0 h1:a+UkboSi1znleCDUNT3M5YxjOnN1fz2FhN48FlwCxs0=
 | 
			
		||||
github.com/pjbgf/sha1cd v0.5.0/go.mod h1:lhpGlyHLpQZoxMv8HcgXvZEhcGs0PG/vsZnEJ7H0iCM=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
 | 
			
		||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
 | 
			
		||||
github.com/quic-go/qpack v0.5.1 h1:giqksBPnT/HDtZ6VhtFKgoLOWmlyo9Ei6u9PqzIMbhI=
 | 
			
		||||
github.com/quic-go/qpack v0.5.1/go.mod h1:+PC4XFrEskIVkcLzpEkbLqq1uCoxPhQuvK5rH1ZgaEg=
 | 
			
		||||
github.com/quic-go/quic-go v0.54.0 h1:6s1YB9QotYI6Ospeiguknbp2Znb/jZYjZLRXn9kMQBg=
 | 
			
		||||
github.com/quic-go/quic-go v0.54.0/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
 | 
			
		||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
 | 
			
		||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
 | 
			
		||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
 | 
			
		||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
 | 
			
		||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
 | 
			
		||||
github.com/schollz/progressbar/v3 v3.18.0 h1:uXdoHABRFmNIjUfte/Ex7WtuyVslrw2wVPQmCN62HpA=
 | 
			
		||||
github.com/schollz/progressbar/v3 v3.18.0/go.mod h1:IsO3lpbaGuzh8zIMzgY3+J8l4C8GjO0Y9S69eFvNsec=
 | 
			
		||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
 | 
			
		||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
 | 
			
		||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
			
		||||
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
 | 
			
		||||
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
			
		||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
 | 
			
		||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
 | 
			
		||||
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
 | 
			
		||||
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
 | 
			
		||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
 | 
			
		||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
 | 
			
		||||
github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
 | 
			
		||||
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0=
 | 
			
		||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
			
		||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
 | 
			
		||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
 | 
			
		||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
 | 
			
		||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
 | 
			
		||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
 | 
			
		||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
 | 
			
		||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 | 
			
		||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 | 
			
		||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
 | 
			
		||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
 | 
			
		||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
 | 
			
		||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
 | 
			
		||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
 | 
			
		||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
 | 
			
		||||
github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA=
 | 
			
		||||
github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
 | 
			
		||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
 | 
			
		||||
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
 | 
			
		||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 h1:R84qjqJb5nVJMxqWYb3np9L5ZsaDtB+a39EqjV0JSUM=
 | 
			
		||||
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0/go.mod h1:S9Xr4PYopiDyqSyp5NjCrhFrqg6A5zA2E/iPHPhqnS8=
 | 
			
		||||
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
 | 
			
		||||
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
 | 
			
		||||
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
 | 
			
		||||
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
 | 
			
		||||
golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ=
 | 
			
		||||
golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
 | 
			
		||||
go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU=
 | 
			
		||||
go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM=
 | 
			
		||||
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
 | 
			
		||||
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
 | 
			
		||||
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
 | 
			
		||||
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
 | 
			
		||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
 | 
			
		||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
 | 
			
		||||
golang.org/x/mod v0.28.0 h1:gQBtGhjxykdjY9YhZpSlZIsbnaE2+PgjfLWUQTnoZ1U=
 | 
			
		||||
golang.org/x/mod v0.28.0/go.mod h1:yfB/L0NOf/kmEbXjzCPOx1iK1fRutOydrCMsqRhEBxI=
 | 
			
		||||
golang.org/x/net v0.46.0 h1:giFlY12I07fugqwPuWJi68oOnpfqFnJIJzaIIm2JVV4=
 | 
			
		||||
golang.org/x/net v0.46.0/go.mod h1:Q9BGdFy1y4nkUwiLvT5qtyhAnEHgnQ/zd8PfU6nc210=
 | 
			
		||||
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
 | 
			
		||||
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
 | 
			
		||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
 | 
			
		||||
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
 | 
			
		||||
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
 | 
			
		||||
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
 | 
			
		||||
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
 | 
			
		||||
golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc=
 | 
			
		||||
golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI=
 | 
			
		||||
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
 | 
			
		||||
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
 | 
			
		||||
golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q=
 | 
			
		||||
golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss=
 | 
			
		||||
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
 | 
			
		||||
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
 | 
			
		||||
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
 | 
			
		||||
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
 | 
			
		||||
google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw=
 | 
			
		||||
google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
 | 
			
		||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
			
		||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 | 
			
		||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 | 
			
		||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 | 
			
		||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 | 
			
		||||
modernc.org/cc/v4 v4.26.1 h1:+X5NtzVBn0KgsBCBe+xkDC7twLb/jNVj9FPgiwSQO3s=
 | 
			
		||||
modernc.org/cc/v4 v4.26.1/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
 | 
			
		||||
modernc.org/cc/v4 v4.26.2 h1:991HMkLjJzYBIfha6ECZdjrIYz2/1ayr+FL8GN+CNzM=
 | 
			
		||||
modernc.org/cc/v4 v4.26.2/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
 | 
			
		||||
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=
 | 
			
		||||
modernc.org/ccgo/v4 v4.28.0/go.mod h1:JygV3+9AV6SmPhDasu4JgquwU81XAKLd3OKTUDNOiKE=
 | 
			
		||||
modernc.org/fileutil v1.3.3 h1:3qaU+7f7xxTUmvU1pJTZiDLAIoJVdUSSauJNHg9yXoA=
 | 
			
		||||
modernc.org/fileutil v1.3.3/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
 | 
			
		||||
modernc.org/fileutil v1.3.8 h1:qtzNm7ED75pd1C7WgAGcK4edm4fvhtBsEiI/0NQ54YM=
 | 
			
		||||
modernc.org/fileutil v1.3.8/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
 | 
			
		||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
 | 
			
		||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
 | 
			
		||||
modernc.org/libc v1.65.10 h1:ZwEk8+jhW7qBjHIT+wd0d9VjitRyQef9BnzlzGwMODc=
 | 
			
		||||
modernc.org/libc v1.65.10/go.mod h1:StFvYpx7i/mXtBAfVOjaU0PWZOvIRoZSgXhrwXzr8Po=
 | 
			
		||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
 | 
			
		||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
 | 
			
		||||
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
 | 
			
		||||
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
 | 
			
		||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
 | 
			
		||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
 | 
			
		||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
 | 
			
		||||
@@ -77,8 +176,8 @@ modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
 | 
			
		||||
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
 | 
			
		||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
 | 
			
		||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
 | 
			
		||||
modernc.org/sqlite v1.38.0 h1:+4OrfPQ8pxHKuWG4md1JpR/EYAh3Md7TdejuuzE7EUI=
 | 
			
		||||
modernc.org/sqlite v1.38.0/go.mod h1:1Bj+yES4SVvBZ4cBOpVZ6QgesMCKpJZDq0nxYzOpmNE=
 | 
			
		||||
modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
 | 
			
		||||
modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
 | 
			
		||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
 | 
			
		||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
 | 
			
		||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										80
									
								
								internal/build/complete.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								internal/build/complete.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
			
		||||
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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										167
									
								
								internal/build/install.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								internal/build/install.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,167 @@
 | 
			
		||||
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
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										224
									
								
								internal/build/lua.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										224
									
								
								internal/build/lua.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,224 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										183
									
								
								internal/build/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										183
									
								
								internal/build/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,183 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										45
									
								
								internal/build/manager.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								internal/build/manager.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,45 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								internal/build/specs.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								internal/build/specs.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package build
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	BinDir        = "/usr/bin"
 | 
			
		||||
	SymLinkBinDir = "/bin"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type BuildID string // Json dependencies encoded to base64 stdEncoding
 | 
			
		||||
							
								
								
									
										79
									
								
								internal/build/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								internal/build/utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										50
									
								
								internal/consts/consts.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								internal/consts/consts.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
			
		||||
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
 | 
			
		||||
);
 | 
			
		||||
`
 | 
			
		||||
							
								
								
									
										11
									
								
								internal/errors/errors.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								internal/errors/errors.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
			
		||||
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,552 +0,0 @@
 | 
			
		||||
package internal
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"archive/tar"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/fs"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/BurntSushi/toml"
 | 
			
		||||
	"github.com/klauspost/compress/zstd"
 | 
			
		||||
	lua "github.com/yuin/gopher-lua"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var AllowedCmds = map[string]string{
 | 
			
		||||
	"go":          "go",          // "Go code compiler"
 | 
			
		||||
	"gcc":         "gcc",         // "C"
 | 
			
		||||
	"g++":         "g++",         // "C++"
 | 
			
		||||
	"rustc":       "rustc",       // "Rust"
 | 
			
		||||
	"javac":       "javac",       // "Java"
 | 
			
		||||
	"luac":        "luac",        // "Lua"
 | 
			
		||||
	"pyinstaller": "pyinstaller", // "Python"
 | 
			
		||||
	"kotlinc":     "kotlinc",     // "Kotlin"
 | 
			
		||||
	"mcs":         "mcs",         // "C# compiler"
 | 
			
		||||
	"swiftc":      "swiftc",      // "Swift compiler"
 | 
			
		||||
	"ts":          "tsc",         // "TypeScript compiler"
 | 
			
		||||
	"ruby":        "rubyc",       // "Ruby compiler"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		HttpPort           int    `toml:"httpPort"`
 | 
			
		||||
		CacheDir           string `toml:"cacheDir"`
 | 
			
		||||
		AutoDeleteCacheDir bool   `toml:"dayToDeleteCacheDir"`
 | 
			
		||||
		DaysToDelete       int    `toml:"daysToDelete"`
 | 
			
		||||
		DataDir            string `toml:"dataDir"`
 | 
			
		||||
		BinDir             string `toml:"binDir"`
 | 
			
		||||
		LastDataDir        string `toml:"lastDataDir"`
 | 
			
		||||
	} `toml:"Config"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type Manifest struct {
 | 
			
		||||
	Info struct {
 | 
			
		||||
		Name         string   `toml:"name"`
 | 
			
		||||
		Version      string   `toml:"version"`
 | 
			
		||||
		Description  string   `toml:"description"`
 | 
			
		||||
		Dependencies []string `toml:"dependencies"`
 | 
			
		||||
		Author       string   `toml:"author"`
 | 
			
		||||
		Family       string   `toml:"family"`
 | 
			
		||||
		Serial       uint     `toml:"serial"`
 | 
			
		||||
	} `toml:"Info"`
 | 
			
		||||
	Hooks struct {
 | 
			
		||||
		Install string `toml:"install"`
 | 
			
		||||
		Remove  string `toml:"remove"`
 | 
			
		||||
	} `toml:"Hooks"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var SandboxDir string
 | 
			
		||||
 | 
			
		||||
func PacketsPackageDir() string {
 | 
			
		||||
 | 
			
		||||
	out, _ := exec.Command("uname", "-s").Output()
 | 
			
		||||
 | 
			
		||||
	if uname := strings.TrimSpace(string(out)); uname == "J2ME" {
 | 
			
		||||
 | 
			
		||||
		_, err := os.Stat("packets.help")
 | 
			
		||||
		if os.IsNotExist(err) {
 | 
			
		||||
			err = nil
 | 
			
		||||
 | 
			
		||||
			var thedirectory string
 | 
			
		||||
			err := filepath.WalkDir("/mnt", func(path string, d fs.DirEntry, err error) error {
 | 
			
		||||
				if d.IsDir() {
 | 
			
		||||
 | 
			
		||||
					thedirectory = filepath.Join(path, "packets")
 | 
			
		||||
					if err := os.Mkdir(thedirectory, 0644); err != nil {
 | 
			
		||||
						return err
 | 
			
		||||
					}
 | 
			
		||||
					if err := os.WriteFile("packets.help", []byte(thedirectory), 0644); err != nil {
 | 
			
		||||
						return err
 | 
			
		||||
					}
 | 
			
		||||
					if err := os.Mkdir(filepath.Join(filepath.Join(thedirectory, "cache")), 0644); err != nil {
 | 
			
		||||
						return err
 | 
			
		||||
					}
 | 
			
		||||
					if err := os.Mkdir(filepath.Join(filepath.Join(thedirectory, "packages")), 0644); err != nil {
 | 
			
		||||
						return err
 | 
			
		||||
					}
 | 
			
		||||
					if err := os.Mkdir(filepath.Join(filepath.Join(thedirectory, "bin")), 0644); err != nil {
 | 
			
		||||
						return err
 | 
			
		||||
					}
 | 
			
		||||
					return nil
 | 
			
		||||
				}
 | 
			
		||||
				return nil
 | 
			
		||||
			})
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				log.Fatal(err)
 | 
			
		||||
			}
 | 
			
		||||
			return thedirectory
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		byt, err := os.ReadFile("packets.help")
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Fatal(err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return string(byt)
 | 
			
		||||
 | 
			
		||||
	} else {
 | 
			
		||||
		return "/etc/packets"
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ManifestReadXZ(path string) (*Manifest, error) {
 | 
			
		||||
	f, err := os.Open(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer f.Close()
 | 
			
		||||
 | 
			
		||||
	zr, err := zstd.NewReader(f)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	defer zr.Close()
 | 
			
		||||
 | 
			
		||||
	tarReader := tar.NewReader(zr)
 | 
			
		||||
 | 
			
		||||
	for {
 | 
			
		||||
		header, err := tarReader.Next()
 | 
			
		||||
		if err == io.EOF {
 | 
			
		||||
			break
 | 
			
		||||
		}
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if filepath.Base(header.Name) == "manifest.toml" {
 | 
			
		||||
			decoder := toml.NewDecoder(tarReader)
 | 
			
		||||
 | 
			
		||||
			var manifest Manifest
 | 
			
		||||
 | 
			
		||||
			if _, err := decoder.Decode(&manifest); err != nil {
 | 
			
		||||
				log.Fatal(err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			return &manifest, nil
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	return nil, fmt.Errorf("can't find manifest.toml")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func DefaultConfigTOML() *ConfigTOML {
 | 
			
		||||
 | 
			
		||||
	var cfg ConfigTOML
 | 
			
		||||
	out, _ := exec.Command("uname", "-s").Output()
 | 
			
		||||
 | 
			
		||||
	if uname := strings.TrimSpace(string(out)); uname == "J2ME" {
 | 
			
		||||
		cfg.Config.HttpPort = 9123
 | 
			
		||||
		cfg.Config.AutoDeleteCacheDir = false
 | 
			
		||||
		cfg.Config.CacheDir = filepath.Join(PacketsPackageDir(), "cache")
 | 
			
		||||
		cfg.Config.DataDir = filepath.Join(PacketsPackageDir(), "data")
 | 
			
		||||
		cfg.Config.DaysToDelete = -1
 | 
			
		||||
		cfg.Config.BinDir = filepath.Join(PacketsPackageDir(), "bin")
 | 
			
		||||
		cfg.Config.LastDataDir = filepath.Join(PacketsPackageDir(), "data")
 | 
			
		||||
		return &cfg
 | 
			
		||||
	} else {
 | 
			
		||||
 | 
			
		||||
		cfg.Config.HttpPort = 9123
 | 
			
		||||
		cfg.Config.AutoDeleteCacheDir = false
 | 
			
		||||
		cfg.Config.CacheDir = "/var/cache/packets"
 | 
			
		||||
		cfg.Config.DataDir = "/opt/packets"
 | 
			
		||||
		cfg.Config.DaysToDelete = -1
 | 
			
		||||
		cfg.Config.BinDir = "/usr/bin"
 | 
			
		||||
		cfg.Config.LastDataDir = "/opt/packets"
 | 
			
		||||
 | 
			
		||||
		return &cfg
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func IsSafe(str string) bool {
 | 
			
		||||
	s, err := filepath.EvalSymlinks(filepath.Clean(str))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		s = filepath.Clean(str)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var cfg ConfigTOML
 | 
			
		||||
	toml.DecodeFile(filepath.Join(PacketsPackageDir(), "config.toml"), &cfg)
 | 
			
		||||
 | 
			
		||||
	if strings.HasPrefix(s, cfg.Config.DataDir) || strings.HasPrefix(s, cfg.Config.BinDir) {
 | 
			
		||||
		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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SafeRemove(L *lua.LState) int {
 | 
			
		||||
	filename := L.CheckString(1)
 | 
			
		||||
	if !IsSafe(filename) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	err := os.RemoveAll(filename)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] remove failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SafeRename(L *lua.LState) int {
 | 
			
		||||
	oldname := L.CheckString(1)
 | 
			
		||||
	newname := L.CheckString(2)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(oldname) || !IsSafe(newname) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := os.Rename(oldname, newname); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] rename failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	return 1
 | 
			
		||||
}
 | 
			
		||||
func SafeCopy(L *lua.LState) int {
 | 
			
		||||
	oldname := L.CheckString(1)
 | 
			
		||||
	newname := L.CheckString(2)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(oldname) || !IsSafe(newname) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	src, err := os.Open(oldname)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
	defer src.Close()
 | 
			
		||||
 | 
			
		||||
	status, err := src.Stat()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = os.MkdirAll(filepath.Dir(newname), 0755)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dst, err := os.Create(newname)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if !os.IsExist(err) {
 | 
			
		||||
			dst, err = os.Open(newname)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			L.Push(lua.LFalse)
 | 
			
		||||
			L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer dst.Close()
 | 
			
		||||
	if err := dst.Chmod(status.Mode()); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = io.Copy(dst, src)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] copy failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func SymbolicLua(L *lua.LState) int {
 | 
			
		||||
	fileName := L.CheckString(1)
 | 
			
		||||
	destination := L.CheckString(2)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(fileName) || !IsSafe(destination) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_ = os.RemoveAll(destination)
 | 
			
		||||
	if err := os.Symlink(fileName, destination); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] symlink failed\n" + 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 SafeOpen(L *lua.LState) int {
 | 
			
		||||
	path := L.CheckString(1)
 | 
			
		||||
	mode := L.OptString(2, "r")
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(path) {
 | 
			
		||||
		L.Push(lua.LNil)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	file, err := os.OpenFile(path, modeFlags(mode), 0644)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LNil)
 | 
			
		||||
		L.Push(lua.LString("[packets] open failed\n" + 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)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(path) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath\n"))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := os.MkdirAll(path, os.FileMode(perm)); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] mkdir failed\n" + err.Error()))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func LuaCompile(L *lua.LState) int {
 | 
			
		||||
	lang := L.CheckString(1)
 | 
			
		||||
	args := []string{}
 | 
			
		||||
	for i := 2; i <= L.GetTop(); i++ {
 | 
			
		||||
 | 
			
		||||
		if strings.Contains(L.CheckString(i), "/") {
 | 
			
		||||
 | 
			
		||||
			tryintoacess, err := filepath.Abs(filepath.Clean(L.CheckString(i)))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] invalid filepath\n" + err.Error()))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
			if !strings.HasPrefix(tryintoacess, SandboxDir) {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		args = append(args, L.CheckString(i))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bin, suc := AllowedCmds[lang]
 | 
			
		||||
	if !suc {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsupported language"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cmd := exec.Command(bin, args...)
 | 
			
		||||
	cmd.Dir = SandboxDir
 | 
			
		||||
	out, err := cmd.CombinedOutput()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] compile failed\n" + err.Error() + "\n" + string(out)))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	if err := cmd.Run(); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] compile failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LString(string(out)))
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CompileRequirements(L *lua.LState) int {
 | 
			
		||||
 | 
			
		||||
	cmdLang := L.CheckString(1)
 | 
			
		||||
 | 
			
		||||
	if strings.Contains(L.CheckString(2), "/") {
 | 
			
		||||
 | 
			
		||||
		tryintoacess, err := filepath.Abs(filepath.Clean(L.CheckString(2)))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			L.Push(lua.LFalse)
 | 
			
		||||
			L.Push(lua.LString("[packets] invalid filepath\n" + err.Error()))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
		if !strings.HasPrefix(tryintoacess, SandboxDir) {
 | 
			
		||||
			L.Push(lua.LFalse)
 | 
			
		||||
			L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	switch cmdLang {
 | 
			
		||||
	case "python":
 | 
			
		||||
		cmd := exec.Command("pip", "install", "--target", filepath.Join(SandboxDir, "tmp/build"), "-r", L.CheckString(2))
 | 
			
		||||
		cmd.Dir = filepath.Join(SandboxDir, "data")
 | 
			
		||||
		err = cmd.Run()
 | 
			
		||||
	case "java":
 | 
			
		||||
		cmd := exec.Command("mvn", "dependency:copy-dependencies", "-DoutputDirectory="+filepath.Join(SandboxDir, "tmp/build"))
 | 
			
		||||
		cmd.Dir = L.CheckString(2)
 | 
			
		||||
		err = cmd.Run()
 | 
			
		||||
	case "ruby":
 | 
			
		||||
		cmd := exec.Command("bundle", "install", "--path", filepath.Join(SandboxDir, "tmp/build"))
 | 
			
		||||
		cmd.Dir = L.CheckString(2)
 | 
			
		||||
		err = cmd.Run()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] requirements install failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										65
									
								
								internal/packet/auxiliar_functions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								internal/packet/auxiliar_functions.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,65 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										182
									
								
								internal/packet/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										182
									
								
								internal/packet/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,182 @@
 | 
			
		||||
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
									
								
								internal/packet/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/packet/utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
package packet
 | 
			
		||||
							
								
								
									
										80
									
								
								internal/utils/lan.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								internal/utils/lan.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										130
									
								
								internal/utils/lua/git.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										130
									
								
								internal/utils/lua/git.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,130 @@
 | 
			
		||||
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)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								internal/utils/lua/lua.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								internal/utils/lua/lua.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										260
									
								
								internal/utils/lua/luafunctions.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										260
									
								
								internal/utils/lua/luafunctions.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,260 @@
 | 
			
		||||
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) }
 | 
			
		||||
							
								
								
									
										482
									
								
								internal/utils/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										482
									
								
								internal/utils/utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,482 @@
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										80
									
								
								pkg/lan.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								pkg/lan.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,80 @@
 | 
			
		||||
package packets
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										354
									
								
								pkg/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										354
									
								
								pkg/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,354 @@
 | 
			
		||||
package packets
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"archive/tar"
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"crypto/ed25519"
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"packets/internal/build"
 | 
			
		||||
	"packets/internal/consts"
 | 
			
		||||
	errors_packets "packets/internal/errors"
 | 
			
		||||
	"packets/internal/packet"
 | 
			
		||||
	"packets/internal/utils"
 | 
			
		||||
	"path"
 | 
			
		||||
 | 
			
		||||
	utils_lua "packets/internal/utils/lua"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/klauspost/compress/zstd"
 | 
			
		||||
	_ "modernc.org/sqlite"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 | 
			
		||||
	Signature []byte
 | 
			
		||||
	PublicKey ed25519.PublicKey
 | 
			
		||||
 | 
			
		||||
	Serial int
 | 
			
		||||
 | 
			
		||||
	Manifest packet.PacketLua
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Install exctract and fully install from a package file ( tar.zst )
 | 
			
		||||
func InstallPackage(file []byte, destDir string) error {
 | 
			
		||||
 | 
			
		||||
	packetLua, err := packet.ReadPacket(file)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		L, err := utils_lua.GetSandBox()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		bootstrapcontainer, err := build.NewContainer(packetLua)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		os.Chdir(destDir)
 | 
			
		||||
 | 
			
		||||
		if err := utils.ChangeToNoPermission(); err != nil {
 | 
			
		||||
			return fmt.Errorf("error changing to packet user: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err := bootstrapcontainer.ExecutePrepare(packetLua, &L); err != nil {
 | 
			
		||||
			return fmt.Errorf("error executing prepare: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err := bootstrapcontainer.ExecuteBuild(packetLua, &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)
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err := bootstrapcontainer.ExecuteInstall(packetLua, &L); err != nil {
 | 
			
		||||
			return fmt.Errorf("error executing build: %s", err)
 | 
			
		||||
		}
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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(destDir, 0775); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		if err := os.Chown(destDir, uid, 0); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		absPath := filepath.Join(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 := build.NewContainer(manifest)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	os.Chdir(destDir)
 | 
			
		||||
 | 
			
		||||
	if err := utils.ChangeToNoPermission(); err != nil {
 | 
			
		||||
		return fmt.Errorf("error changing to packet user: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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.ElevatePermission(); err != nil {
 | 
			
		||||
		return fmt.Errorf("error changing to root: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := bootstrapcontainer.ExecuteInstall(manifest, &L); err != nil {
 | 
			
		||||
		return fmt.Errorf("error executing build: %s", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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, typePackage string
 | 
			
		||||
	err = db.QueryRow("SELECT query_name, version, package_url, image_url, description, author, author_verified, os, arch, signature, public_key, serial, size, type 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,
 | 
			
		||||
			&typePackage,
 | 
			
		||||
		)
 | 
			
		||||
	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
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if strings.Contains(typePackage, "     ") {
 | 
			
		||||
 | 
			
		||||
		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
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		filds := strings.Fields(typePackage)
 | 
			
		||||
		pkt, err := packet.GetPackageDotLuaFromRemote(filds[0], filds[1])
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return Package{}, err
 | 
			
		||||
		}
 | 
			
		||||
		this.Manifest = pkt
 | 
			
		||||
		return this, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
skipping:
 | 
			
		||||
 | 
			
		||||
	reader := bytes.NewReader(this.PackageF)
 | 
			
		||||
	this.Manifest, err = packet.ReadPacketFromFile(reader)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return Package{}, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !ed25519.Verify(this.PublicKey, this.PackageF, this.Signature) {
 | 
			
		||||
		return Package{}, errors_packets.ErrInvalidSignature
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								systemd/packets-http.service
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								systemd/packets-http.service
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
[Unit]
 | 
			
		||||
 Description=Serving packages for everyone in the same network.
 | 
			
		||||
 After=network.target packets-udp.service
 | 
			
		||||
 Requires=packets-udp.service
 | 
			
		||||
 | 
			
		||||
[Service]
 | 
			
		||||
 Type=simple
 | 
			
		||||
 User=packets
 | 
			
		||||
 WorkingDirectory=/var/cache/packets
 | 
			
		||||
 ExecStart=/etc/packets/httpsocket
 | 
			
		||||
 Restart=always
 | 
			
		||||
 | 
			
		||||
[Install]
 | 
			
		||||
 WantedBy=multi-user.target
 | 
			
		||||
							
								
								
									
										14
									
								
								systemd/packets-udp.service
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								systemd/packets-udp.service
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
[Unit]
 | 
			
		||||
 Description=Respond to every request for packages in local network.
 | 
			
		||||
 After=network.target
 | 
			
		||||
 | 
			
		||||
[Service]
 | 
			
		||||
 Type=simple
 | 
			
		||||
 User=packets
 | 
			
		||||
 WorkingDirectory=/etc/packets
 | 
			
		||||
 ExecStart=/etc/packets/udpsocket
 | 
			
		||||
 Restart=always
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
[Install]
 | 
			
		||||
 WantedBy=multi-user.target
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user