Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Mar 27, 2022
2 parents 7366466 + 208222e commit 1ebd313
Show file tree
Hide file tree
Showing 20 changed files with 512 additions and 12 deletions.
14 changes: 14 additions & 0 deletions assets/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ languages:
- ru.yaml

packages:
file-utilities:
version: "1.0.0"
info:
title: '#.fileutils#'
desc: '#_desc#'
help: file-utilities
helplang: ru
app: true
langs:
en:
_desc: Scripts for working with files and directories
ru:
_desc: Скрипты для работы с файлами и директориями

tests:
version: 1.0
info:
Expand Down
Binary file modified assets/packages.tar.gz
Binary file not shown.
Binary file modified assets/stdlib.tar.gz
Binary file not shown.
Binary file modified assets/web.tar.gz
Binary file not shown.
16 changes: 9 additions & 7 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package main

const (
// Version of the application
Version = "1.25.1"
Version = "1.26.0"
// DefPort is the default web-server port
DefPort = 3234
// DefTheme is the default web-server theme
Expand All @@ -26,12 +26,14 @@ const (
HistoryLimit = 7
RunLimit = 20
// Number of reserved ports
PortsPool = 1000
TimeFormat = `2006/01/02 15:04:05`
TimeoutOpen = 4000
SourceCode = `source-code`
Return = `return.eonza`
DefLang = 0
PortsPool = 1000
TimeFormat = `2006/01/02 15:04:05`
TimeoutOpen = 2000
SourceCode = `source-code`
Function = `function`
CallFunction = `call-function`
Return = `return.eonza`
DefLang = 0
// ConsolePrefix is the prefix of eonza console version
ConsolePrefix = `ez`
Localhost = `localhost`
Expand Down
11 changes: 10 additions & 1 deletion gensource.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (src *Source) Script(node scriptTree) (string, error) {
if predef, err = src.Predefined(script); err != nil {
return ``, err
}
if !src.Linked[idname] || script.Settings.Name == SourceCode || len(node.Children) > 0 {
if !src.Linked[idname] || script.Settings.Name == SourceCode || script.Settings.Name == Function || len(node.Children) > 0 {
tmp, err := src.Tree(node.Children)
if err != nil {
return ``, err
Expand All @@ -357,7 +357,16 @@ func (src *Source) Script(node scriptTree) (string, error) {
src.Funcs += code + "\r\n"
return ``, nil
}
} else if script.Settings.Name == Function {
code = strings.Replace(code, `%funcname%`, values[0].Value, 1)
src.Funcs += code + "\r\n"
return ``, nil
} else if script.Settings.Name == CallFunction {
code = strings.Replace(code, `%funcname%`, values[0].Value, 1)
code = strings.Replace(code, `%params%`, values[1].Value, 1)
return code + "\r\n", nil
}

if script.Settings.Name == SourceCode || len(node.Children) > 0 {
idname = fmt.Sprintf("%s%d", idname, src.Counter)
src.Counter++
Expand Down
51 changes: 51 additions & 0 deletions lib/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
package lib

import (
"archive/tar"
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
)

Expand Down Expand Up @@ -42,3 +47,49 @@ func GzipDecompress(input []byte) (out []byte, err error) {
out, err = io.ReadAll(gz)
return
}

func unpackFile(finfo os.FileInfo, reader io.Reader, dest string) error {
if finfo.IsDir() {
return os.MkdirAll(dest, finfo.Mode())
}
target, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, finfo.Mode())
if err != nil {
return err
}
defer func() {
target.Close()
os.Chtimes(dest, finfo.ModTime(), finfo.ModTime())
}()
_, err = io.Copy(target, reader)
return err
}

func UnpackTar(reader io.Reader, dir string) error {
tr := tar.NewReader(reader)

for {
header, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
name := header.Name
path := dir
folder := filepath.Dir(strings.TrimRight(name, `/`))
if len(folder) > 0 {
path = filepath.Join(path, folder)
}
path = filepath.Join(path, header.FileInfo().Name())
switch header.Typeflag {
case tar.TypeDir, tar.TypeReg:
if err = unpackFile(header.FileInfo(), tr, path); err != nil {
return err
}
default:
return fmt.Errorf("UnpackTar: uknown type: %d in %s", header.Typeflag, header.Name)
}
}
return nil
}
32 changes: 32 additions & 0 deletions lib/net.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.

package lib

import (
"io"
"net/http"
"os"
)

func Download(url string) ([]byte, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}

func DownloadFile(url, filename string) error {
data, err := Download(url)
f, err := os.Create(filename)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(data)

return err
}
1 change: 1 addition & 0 deletions localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func RunLocalServer(port int) *echo.Echo {
if IsScript {
e.GET("/info", infoHandle)
e.GET("/sys", sysHandle)
es.CmdServer(e)
} else {
e.GET("/api/run", runHandle)
e.GET("/api/randid", randidHandle)
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ func main() {
settings := initTask()
setStatus(TaskActive)
_, err := scriptTask.Run(settings)
if script.IsTimeout {
time.Sleep(time.Until(script.Timeout))
}
if err == nil {
setStatus(TaskFinished)
} else if err.Error() == `code execution has been terminated` {
Expand Down
53 changes: 49 additions & 4 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package main

import (
"bytes"
"encoding/json"
"eonza/lib"
es "eonza/script"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"sort"
"strings"

Expand All @@ -20,11 +22,14 @@ import (
"gopkg.in/yaml.v2"
)

const PackagesURL = `https://www.eonza.org/downloads/packages`

type PackageInfo struct {
Title string `json:"title" yaml:"title"`
Desc string `json:"desc,omitempty" yaml:"desc,omitempty"`
Help string `json:"help,omitempty" yaml:"help,omitempty"`
HelpLang string `json:"helplang,omitempty" yaml:"helplang,omitempty"`
App bool `json:"app,omitempty" yaml:"app,omitempty"`
// Calculated fields
Installed bool `json:"installed"`
}
Expand Down Expand Up @@ -55,6 +60,16 @@ type Package struct {
json string // json of package values
}

type PackageRelease struct {
File string `yaml:"file"`
MD5 string `yaml:"md5,omitempty"`
}

type PackageDownload struct {
Version string `yaml:"version"`
Releases map[string]PackageRelease `yaml:"releases"`
}

func GetPackageJSON(name string) string {
pkg := Assets.Packages[name]
if pkg == nil || !pkg.Installed || len(pkg.Params) == 0 {
Expand Down Expand Up @@ -266,17 +281,47 @@ func packageInstallHandle(c echo.Context) error {
if err := os.MkdirAll(path, 0777); err != nil {
return jsonError(c, err)
}
pkgError := func(err error) error {
os.RemoveAll(path)
return jsonError(c, err)
}
if pkg.App {
var data []byte
if data, err = lib.Download(PackagesURL + `/` + name + `/package.yaml`); err != nil {
return pkgError(err)
}
var release PackageDownload
if err = yaml.Unmarshal(data, &release); err != nil {
return pkgError(err)
}
if err = os.WriteFile(filepath.Join(path, `package.yaml`), data, 0777); err != nil {
return pkgError(err)
}

if r, ok := release.Releases[runtime.GOOS+`-`+runtime.GOARCH]; !ok {
return pkgError(fmt.Errorf(`cannot find package's release`))
} else {
if data, err = lib.Download(PackagesURL + `/` + name + `/` + r.File); err != nil {
return pkgError(err)
}
if data, err = lib.GzipDecompress(data); err != nil {
return pkgError(err)
}
buf := bytes.NewBuffer(data)
if err = lib.UnpackTar(buf, path); err != nil {
return pkgError(err)
}
}
}
for _, f := range PackagesFS.List {
if strings.HasPrefix(f.Name, name+`/files`) {
fullName := filepath.Join(cfg.PackagesDir, f.Name)
if f.Dir {
if err := os.MkdirAll(fullName, 0777); err != nil {
os.RemoveAll(path)
return jsonError(c, err)
return pkgError(err)
}
} else if err = os.WriteFile(fullName, f.Data, 0666); err != nil {
os.RemoveAll(path)
return jsonError(c, err)
return pkgError(err)
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions pro.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"eonza/users"
"fmt"
"net/http"
"time"

pro "github.com/gentee/eonza-pro"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -123,10 +124,24 @@ func GetTrialMode() int {
return storage.Trial.Mode
}

func TaskCheck(taskID uint32, userID uint32) (bool, error) {
var status int

if v, ok := tasks[taskID]; ok && v.UserID == userID {
status = v.Status
if v.Status == TaskActive || v.Status == TaskWaiting ||
(v.Status == TaskFinished && time.Now().Unix() <= v.FinishTime+1) {
return v.Status < TaskFinished, nil
}
}
return status < TaskFinished, fmt.Errorf(`access denied task %d / user %d`, taskID, userID)
}

func ProInit(psw []byte, counter uint32) {
pro.CallbackPassCounter = StoragePassCounter
pro.CallbackTitle = GetTitle
pro.CallbackTrial = GetTrialMode
pro.CallbackTaskCheck = TaskCheck
pro.LoadPro(psw, counter, cfg.path, cfg.Users.Dir)
}

Expand Down
1 change: 1 addition & 0 deletions run.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func systemRun(rs *RunScript) error {
header := script.Header{
Name: rs.Name,
Title: title,
PackagesDir: cfg.PackagesDir,
AssetsDir: cfg.AssetsDir,
LogDir: cfg.Log.Dir,
CDN: cdn,
Expand Down
Loading

0 comments on commit 1ebd313

Please sign in to comment.