Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Aug 9, 2021
2 parents 5811fda + 28b296b commit e6e8ae5
Show file tree
Hide file tree
Showing 16 changed files with 923 additions and 481 deletions.
889 changes: 497 additions & 392 deletions assets.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"eonza/lib"
"eonza/users"

"github.com/dgrijalva/jwt-go"
"github.com/golang-jwt/jwt"
"github.com/labstack/echo/v4"
"golang.org/x/crypto/bcrypt"
)
Expand Down
231 changes: 231 additions & 0 deletions browserext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// Copyright 2021 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 main

import (
"encoding/json"
"eonza/lib"
es "eonza/script"
"eonza/users"
"fmt"
"net/http"
"strconv"
"strings"

"github.com/labstack/echo/v4"
)

const (
ChromeExtension = ``
)

type BrowserSettings struct {
HTML bool `json:"html"`
}

type Browser struct {
ID uint32 `json:"id"`
URLs string `json:"urls"`
Scripts string `json:"scripts"`
Settings BrowserSettings `json:"settings"`
}

type BrowsersResponse struct {
List []*Browser `json:"list"`
Error string `json:"error,omitempty"`
}

type ExtScript struct {
Name string `json:"name"`
Title string `json:"title"`
Settings BrowserSettings `json:"settings"`
}

type ExtRun struct {
Name string `json:"name"`
Open bool `json:"open"`
URL string `json:"url"`
Title string `json:"title"`
HTML string `json:"html,omitempty"`
}

type ExtListResponse struct {
List []ExtScript `json:"list,omitempty"`
Error string `json:"error,omitempty"`
}

type ExtInfo struct {
Url string `json:"url"`
}

func browserExtHandle(c echo.Context) error {
var (
err error
ext ExtInfo
)
if err = c.Bind(&ext); err != nil {
return jsonError(c, err)
}
lang := c.(*Auth).Lang
glob := &langRes[GetLangId(c.(*Auth).User)]
list := make([]ExtScript, 0)
added := make(map[string]bool)
for _, item := range storage.Browsers {
url := strings.ReplaceAll(strings.TrimSpace(item.URLs), "\n", " ")
match := len(url) == 0
if !match {
for _, upath := range strings.Split(url, " ") {
upath = strings.TrimSpace(upath)
if strings.HasPrefix(upath, `http`) {
match = strings.HasPrefix(ext.Url, upath)
} else {
match = strings.Contains(ext.Url, upath)
}
if match {
break
}
}
}
if match {
for _, cmd := range strings.Split(item.Scripts, `,`) {
cmd = strings.TrimSpace(cmd)
if added[cmd] {
continue
}
var script *Script
if script = getScript(cmd); script == nil {
continue
}
user := c.(*Auth).User
if ScriptAccess(script.Settings.Name, script.Settings.Path, user.RoleID) == nil {
list = append(list, ExtScript{
Name: script.Settings.Name,
Title: es.ReplaceVars(script.Settings.Title, script.Langs[lang], glob),
Settings: item.Settings,
})
added[cmd] = true
}
}
}
}
return c.JSON(http.StatusOK, &ExtListResponse{
List: list,
})
}

func browserRunHandle(c echo.Context) error {
var (
err error
ext ExtRun
data []byte
)
if err = c.Bind(&ext); err != nil {
return jsonError(c, err)
}
if data, err = json.Marshal(ext); err != nil {
return jsonError(c, err)
}
user := c.(*Auth).User
rs := RunScript{
Name: ext.Name,
Open: ext.Open && cfg.HTTP.Host == Localhost,
Data: string(data),
User: *user,
/* users.User{
ID: users.XAdminID,
Nickname: users.RootUser,
RoleID: users.BrowserID,
},*/
Role: users.Role{
ID: users.BrowserID,
Name: users.BrowserRole,
},
IP: Localhost,
}
if err = systemRun(&rs); err != nil {
NewNotification(&Notification{
Text: fmt.Sprintf(`Browser extension error: %s`, err.Error()),
UserID: user.ID,
RoleID: users.BrowserID,
Script: rs.Name,
})
return jsonError(c, err)
}
return c.JSON(http.StatusOK, &RunResponse{Success: true, Port: rs.Port, ID: rs.ID})
}

func browsersResponse(c echo.Context) error {
return c.JSON(http.StatusOK, &BrowsersResponse{
List: storage.Browsers,
})
}

func browsersHandle(c echo.Context) error {
if err := CheckAdmin(c); err != nil {
return jsonError(c, err)
}
return browsersResponse(c)
}

func saveBrowserHandle(c echo.Context) error {
if err := CheckAdmin(c); err != nil {
return jsonError(c, err)
}
var browser Browser
if err := c.Bind(&browser); err != nil {
return jsonError(c, err)
}
if len(browser.Scripts) == 0 {
return jsonError(c, Lang(DefLang, `errreq`, `Scripts`))
}
isBrowser := func(id uint32) int {
for i, item := range storage.Browsers {
if item.ID == id {
return i + 1
}
}
return 0
}
curID := isBrowser(browser.ID)
if browser.ID == 0 {
for {
browser.ID = lib.RndNum()
if isBrowser(browser.ID) == 0 {
break
}
}
storage.Browsers = append(storage.Browsers, &browser)
} else if curID == 0 {
return jsonError(c, fmt.Errorf(`Access denied`))
} else {
storage.Browsers[curID-1] = &browser
}
if err := SaveStorage(); err != nil {
return jsonError(c, err)
}
return browsersResponse(c)
}

func removeBrowserHandle(c echo.Context) error {
if err := CheckAdmin(c); err != nil {
return jsonError(c, err)
}

id, _ := strconv.ParseUint(c.Param("id"), 10, 64)
for i, item := range storage.Browsers {
if item.ID == uint32(id) {
if i < len(storage.Browsers)-1 {
storage.Browsers = append(storage.Browsers[:i], storage.Browsers[i+1:]...)
} else {
storage.Browsers = storage.Browsers[:i]
}
if err := SaveStorage(); err != nil {
return jsonError(c, err)
}
break
}
}
return browsersResponse(c)
}
2 changes: 1 addition & 1 deletion 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.19.0"
Version = "1.20.0"
// DefPort is the default web-server port
DefPort = 3234
// DefTheme is the default web-server theme
Expand Down
3 changes: 1 addition & 2 deletions gensource.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,6 @@ func (src *Source) Script(node scriptTree) (string, error) {
return ``, err
}
if !src.Linked[idname] || script.Settings.Name == SourceCode || len(node.Children) > 0 {
src.Linked[idname] = true

tmp, err := src.Tree(node.Children)
if err != nil {
return ``, err
Expand All @@ -342,6 +340,7 @@ func (src *Source) Script(node scriptTree) (string, error) {
idname = fmt.Sprintf("%s%d", idname, src.Counter)
src.Counter++
}
src.Linked[idname] = true
code = strings.TrimRight(code, "\r\n")
var parNames, prefix, suffix, initcmd string
if script.Settings.Name != SourceCode {
Expand Down
27 changes: 15 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
module eonza

go 1.15
go 1.16

require (
github.com/alecthomas/chroma v0.9.1
github.com/PuerkitoBio/goquery v1.7.1
github.com/alecthomas/chroma v0.9.2
github.com/alecthomas/colour v0.1.0 // indirect
github.com/alecthomas/repr v0.0.0-20210301060118-828286944d6a // indirect
github.com/atotto/clipboard v0.1.4
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/boombuler/barcode v1.0.1 // indirect
github.com/gentee/eonza-pro v0.0.0-00010101000000-000000000000
github.com/gentee/gentee v1.20.0
github.com/gentee/systray v1.3.1
github.com/go-sql-driver/mysql v1.6.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/gorilla/websocket v1.4.2
github.com/kataras/golog v0.1.7
github.com/kr/text v0.2.0 // indirect
github.com/labstack/echo/v4 v4.2.2
github.com/lib/pq v1.10.1
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/labstack/echo/v4 v4.4.0
github.com/lib/pq v1.10.2
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/pquerna/otp v1.3.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/sergi/go-diff v1.2.0 // indirect
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/xhit/go-simple-mail/v2 v2.8.1
github.com/yuin/goldmark v1.3.5
github.com/yuin/goldmark-highlighting v0.0.0-20210428103930-3a9678dbb86c
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781 // indirect
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887
github.com/xhit/go-simple-mail/v2 v2.10.0
github.com/yuin/goldmark v1.4.0
github.com/yuin/goldmark-highlighting v0.0.0-20210516132338-9216f9c5aa01
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
golang.org/x/text v0.3.6
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.62.0
gopkg.in/yaml.v2 v2.4.0
Expand Down
Loading

0 comments on commit e6e8ae5

Please sign in to comment.