gofmt peernet (#103)

This commit is contained in:
Akilan Selvacoumar
2023-02-12 09:59:31 +00:00
committed by GitHub
parent 3a1eece580
commit 9775525d35
95 changed files with 15798 additions and 15794 deletions

View File

@@ -1,91 +1,91 @@
/*
File Name: Execute.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package system
import (
"archive/zip"
"errors"
"io"
"os"
"path/filepath"
"strings"
)
// Execute the actions described in the info header
func (update *UpdatePackage) Execute(DataFolder, PluginFolder string) (err error) {
for _, action := range update.Header.Actions {
switch action.Action {
case "extract":
destination := resolveFolders(action.Target, DataFolder, PluginFolder)
for _, f := range update.Reader.File {
// filter the filename based on source
if !strings.HasPrefix(f.Name, action.Source) {
continue
}
err := unzipFile(f, destination)
if err != nil {
return err
}
}
}
}
return nil
}
// Deletes the update package file
func (update *UpdatePackage) Delete() (err error) {
return os.Remove(update.Filename)
}
func unzipFile(f *zip.File, destination string) error {
// 4. Check if file paths are not vulnerable to Zip Slip
filePath := filepath.Join(destination, f.Name)
if !strings.HasPrefix(filePath, filepath.Clean(destination)+string(os.PathSeparator)) {
return errors.New("invalid file path: " + filePath)
}
// 5. Create directory tree
if f.FileInfo().IsDir() {
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
return err
}
return nil
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return err
}
// 6. Create a destination file for unzipped content
destinationFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer destinationFile.Close()
// 7. Unzip the content of a file and copy it to the destination file
zippedFile, err := f.Open()
if err != nil {
return err
}
defer zippedFile.Close()
if _, err := io.Copy(destinationFile, zippedFile); err != nil {
return err
}
return nil
}
func resolveFolders(folder, dataFolder, pluginFolder string) (resolved string) {
resolved = strings.Replace(folder, "%plugin%", pluginFolder, 1)
resolved = strings.Replace(resolved, "%data%", dataFolder, 1)
return resolved
}
/*
File Name: Execute.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package system
import (
"archive/zip"
"errors"
"io"
"os"
"path/filepath"
"strings"
)
// Execute the actions described in the info header
func (update *UpdatePackage) Execute(DataFolder, PluginFolder string) (err error) {
for _, action := range update.Header.Actions {
switch action.Action {
case "extract":
destination := resolveFolders(action.Target, DataFolder, PluginFolder)
for _, f := range update.Reader.File {
// filter the filename based on source
if !strings.HasPrefix(f.Name, action.Source) {
continue
}
err := unzipFile(f, destination)
if err != nil {
return err
}
}
}
}
return nil
}
// Deletes the update package file
func (update *UpdatePackage) Delete() (err error) {
return os.Remove(update.Filename)
}
func unzipFile(f *zip.File, destination string) error {
// 4. Check if file paths are not vulnerable to Zip Slip
filePath := filepath.Join(destination, f.Name)
if !strings.HasPrefix(filePath, filepath.Clean(destination)+string(os.PathSeparator)) {
return errors.New("invalid file path: " + filePath)
}
// 5. Create directory tree
if f.FileInfo().IsDir() {
if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
return err
}
return nil
}
if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
return err
}
// 6. Create a destination file for unzipped content
destinationFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer destinationFile.Close()
// 7. Unzip the content of a file and copy it to the destination file
zippedFile, err := f.Open()
if err != nil {
return err
}
defer zippedFile.Close()
if _, err := io.Copy(destinationFile, zippedFile); err != nil {
return err
}
return nil
}
func resolveFolders(folder, dataFolder, pluginFolder string) (resolved string) {
resolved = strings.Replace(folder, "%plugin%", pluginFolder, 1)
resolved = strings.Replace(resolved, "%data%", dataFolder, 1)
return resolved
}

View File

@@ -1,137 +1,137 @@
/*
File Name: Read Package.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package system
import (
"archive/zip"
"io/ioutil"
"path"
"strings"
"gopkg.in/ini.v1"
)
type UpdatePackage struct {
Filename string // Filename of the update package
Err error // Parsing error if any
Header *IniFile // Header info
Reader *zip.ReadCloser // Access to files in the ZIP file
}
// IniFile contains the parsed data from the info.ini file
type IniFile struct {
Name string // Name of the package
Organization string // Organization that created this package
Architecture string // Target architecture. For example "windows/amd64".
Actions []IniAction // Actions to take
}
type IniAction struct {
Action string `ini:"action"` // Action: extract, delete
Source string `ini:"source"` // Folder or file in the ZIP file
Target string `ini:"target"` // Target folder or file. Certain virtual folders such as "%plugins%" are supported.
}
const IniFilename = "info.ini"
// ParseUpdateFiles returns a list of parsed update packages.
// It will check each file in the directory if a ZIP file containing a valid info.ini file.
// The caller must close all returned readers.
func ParseUpdateFiles(Directory string) (files []UpdatePackage, err error) {
// check all files in the directory
filesDir, err := ioutil.ReadDir(Directory)
if err != nil {
return nil, err
}
for _, file := range filesDir {
if file.IsDir() {
continue
}
filename := file.Name()
// ZIP file?
if strings.ToLower(path.Ext(filename)) == ".zip" {
filenamePath := path.Join(Directory, filename)
// check if the ZIP archive contains a info.ini file
reader, err := zip.OpenReader(filenamePath)
if err != nil { // invalid ZIP file
continue
}
// read info.ini file
file, err := reader.Open(IniFilename)
if err != nil {
continue
}
data, err := ioutil.ReadAll(file)
if err != nil {
continue
}
header, err := ParseIniFile(data)
files = append(files, UpdatePackage{Filename: filenamePath, Err: err, Header: header, Reader: reader})
}
}
return files, nil
}
func ParseIniFile(data []byte) (header *IniFile, err error) {
inidata, err := ini.Load(data)
if err != nil {
return nil, err
}
// parse the main section first
section, err := inidata.GetSection("main")
if err != nil {
return nil, err
}
name, err := section.GetKey("name")
if err != nil {
return nil, err
}
organization, err := section.GetKey("organization")
if err != nil {
return nil, err
}
architecture, err := section.GetKey("architecture")
if err != nil {
return nil, err
}
header = &IniFile{
Name: name.String(),
Organization: organization.String(),
Architecture: architecture.String(),
}
// parse any other section
for _, section := range inidata.Sections() {
if section.Name() == "main" || section.Name() == "DEFAULT" {
continue
}
var action IniAction
if section.MapTo(&action) != nil {
continue
}
header.Actions = append(header.Actions, action)
}
return header, nil
}
/*
File Name: Read Package.go
Copyright: 2021 Peernet s.r.o.
Author: Peter Kleissner
*/
package system
import (
"archive/zip"
"io/ioutil"
"path"
"strings"
"gopkg.in/ini.v1"
)
type UpdatePackage struct {
Filename string // Filename of the update package
Err error // Parsing error if any
Header *IniFile // Header info
Reader *zip.ReadCloser // Access to files in the ZIP file
}
// IniFile contains the parsed data from the info.ini file
type IniFile struct {
Name string // Name of the package
Organization string // Organization that created this package
Architecture string // Target architecture. For example "windows/amd64".
Actions []IniAction // Actions to take
}
type IniAction struct {
Action string `ini:"action"` // Action: extract, delete
Source string `ini:"source"` // Folder or file in the ZIP file
Target string `ini:"target"` // Target folder or file. Certain virtual folders such as "%plugins%" are supported.
}
const IniFilename = "info.ini"
// ParseUpdateFiles returns a list of parsed update packages.
// It will check each file in the directory if a ZIP file containing a valid info.ini file.
// The caller must close all returned readers.
func ParseUpdateFiles(Directory string) (files []UpdatePackage, err error) {
// check all files in the directory
filesDir, err := ioutil.ReadDir(Directory)
if err != nil {
return nil, err
}
for _, file := range filesDir {
if file.IsDir() {
continue
}
filename := file.Name()
// ZIP file?
if strings.ToLower(path.Ext(filename)) == ".zip" {
filenamePath := path.Join(Directory, filename)
// check if the ZIP archive contains a info.ini file
reader, err := zip.OpenReader(filenamePath)
if err != nil { // invalid ZIP file
continue
}
// read info.ini file
file, err := reader.Open(IniFilename)
if err != nil {
continue
}
data, err := ioutil.ReadAll(file)
if err != nil {
continue
}
header, err := ParseIniFile(data)
files = append(files, UpdatePackage{Filename: filenamePath, Err: err, Header: header, Reader: reader})
}
}
return files, nil
}
func ParseIniFile(data []byte) (header *IniFile, err error) {
inidata, err := ini.Load(data)
if err != nil {
return nil, err
}
// parse the main section first
section, err := inidata.GetSection("main")
if err != nil {
return nil, err
}
name, err := section.GetKey("name")
if err != nil {
return nil, err
}
organization, err := section.GetKey("organization")
if err != nil {
return nil, err
}
architecture, err := section.GetKey("architecture")
if err != nil {
return nil, err
}
header = &IniFile{
Name: name.String(),
Organization: organization.String(),
Architecture: architecture.String(),
}
// parse any other section
for _, section := range inidata.Sections() {
if section.Name() == "main" || section.Name() == "DEFAULT" {
continue
}
var action IniAction
if section.MapTo(&action) != nil {
continue
}
header.Actions = append(header.Actions, action)
}
return header, nil
}