Skip to content

Commit

Permalink
Merge branch 'beta'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentee committed Apr 30, 2021
2 parents d9a8fd8 + 94f8d43 commit 9cb51b6
Show file tree
Hide file tree
Showing 21 changed files with 657 additions and 629 deletions.
104 changes: 5 additions & 99 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"eonza/users"

"github.com/gentee/gentee"
"github.com/kataras/golog"
"github.com/labstack/echo/v4"
)

Expand Down Expand Up @@ -145,100 +146,10 @@ func runHandle(c echo.Context) error {
return c.JSON(http.StatusOK, RunResponse{Success: true, Port: rs.Port, ID: rs.ID})
}

func runScriptHandle(c echo.Context) error {
var (
postScript es.PostScript
err error
)
if err = c.Bind(&postScript); err != nil {
return jsonError(c, err)
}
if !strings.HasPrefix(c.Request().Host, Localhost+`:`) && tasks[postScript.TaskID] == nil {
return echo.NewHTTPError(http.StatusForbidden, "Access denied")
}
rs := RunScript{
Name: postScript.Script,
Open: !postScript.Silent,
Console: false,
Data: postScript.Data,
User: users.User{
ID: postScript.TaskID,
Nickname: GetTaskName(postScript.TaskID),
RoleID: users.ScriptsID,
},
Role: users.Role{
ID: users.ScriptsID,
Name: users.ScriptsRole,
},
IP: c.RealIP(),
}
if err := systemRun(&rs); err != nil {
return jsonError(c, err)
}
return c.JSON(http.StatusOK, RunResponse{Success: true, Port: rs.Port, ID: rs.ID})
}

func pingHandle(c echo.Context) error {
return c.HTML(http.StatusOK, Success)
}

func taskStatusHandle(c echo.Context) error {
var (
taskStatus TaskStatus
err error
finish string
)
if !strings.HasPrefix(c.Request().Host, Localhost+`:`) {
return echo.NewHTTPError(http.StatusForbidden, "Access denied")
}

if err = c.Bind(&taskStatus); err != nil {
return jsonError(c, err)
}
if taskStatus.Time != 0 {
finish = time.Unix(taskStatus.Time, 0).Format(TimeFormat)
}
cmd := WsCmd{
TaskID: taskStatus.TaskID,
Cmd: WcStatus,
Status: taskStatus.Status,
Message: taskStatus.Message,
Time: finish,
}
if taskStatus.Status == TaskActive {
task := tasks[taskStatus.TaskID]
cmd.Task = &Task{
ID: task.ID,
Status: task.Status,
Name: task.Name,
StartTime: task.StartTime,
FinishTime: task.FinishTime,
UserID: task.UserID,
RoleID: task.RoleID,
Port: task.Port,
}
}

for id, client := range clients {
err := client.Conn.WriteJSON(cmd)
if err != nil {
client.Conn.Close()
delete(clients, id)
}
}
if ptask := tasks[taskStatus.TaskID]; ptask != nil {
ptask.Status = taskStatus.Status
if taskStatus.Status >= TaskFinished {
ptask.Message = taskStatus.Message
ptask.FinishTime = taskStatus.Time
if err = SaveTrace(ptask); err != nil {
return jsonError(c, err)
}
}
}
return jsonSuccess(c)
}

func sysTaskHandle(c echo.Context) error {
var (
err error
Expand All @@ -248,21 +159,14 @@ func sysTaskHandle(c echo.Context) error {
if taskid, err = strconv.ParseUint(c.QueryParam(`taskid`), 10, 32); err != nil {
return jsonError(c, err)
}
/*if !strings.HasPrefix(c.Request().Host, Localhost+`:`) {
return echo.NewHTTPError(http.StatusForbidden, "Access denied")
}*/
user := c.(*Auth).User
for _, item := range tasks {
if item.ID == uint32(taskid) {
if user.RoleID != users.XAdminID && user.ID != item.UserID {
return jsonError(c, fmt.Errorf(`Access denied`))
}
url := fmt.Sprintf("http://%s:%d/sys?cmd=%s&taskid=%d", Localhost, item.Port, cmd, taskid)
go func() {
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
}
lib.LocalGet(item.LocalPort, fmt.Sprintf("sys?cmd=%s&taskid=%d", cmd, taskid))
}()
break
}
Expand Down Expand Up @@ -338,8 +242,10 @@ func removeTaskHandle(c echo.Context) error {
return jsonError(c, fmt.Errorf(`Access denied`))
}
}
delete(tasks, uint32(idTask))
RemoveTask(uint32(idTask))
if errSave := SaveTasks(); errSave != nil {
golog.Error(errSave)
}
return tasksHandle(c)
}

Expand Down
436 changes: 218 additions & 218 deletions assets.go

Large diffs are not rendered by default.

20 changes: 5 additions & 15 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ func accessIP(curIP, originalIP string) bool {
func AuthHandle(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) (err error) {
var (
access string
isAccess, ok bool
)
ip := c.RealIP()
Expand Down Expand Up @@ -115,20 +114,11 @@ func AuthHandle(next echo.HandlerFunc) echo.HandlerFunc {
host = host[:offPort]
}
if IsScript {
access = scriptTask.Header.HTTP.Access
isAccess = host == scriptTask.Header.HTTP.Host && accessIP(ip, scriptTask.Header.IP)
} else {
access = cfg.HTTP.Access
isAccess = host == cfg.HTTP.Host
}
if access == AccessPrivate {
isAccess = lib.IsPrivate(host, ip)
} else if access == AccessHost {
if IsScript {
isAccess = (host == scriptTask.Header.HTTP.Host && accessIP(ip, scriptTask.Header.IP)) ||
host == Localhost
} else {
isAccess = host == cfg.HTTP.Host || host == Localhost
}
} else {
if isAccess && host == Localhost {
isAccess = lib.IsLocalhost(host, ip)
}
if !isAccess {
Expand Down Expand Up @@ -208,8 +198,8 @@ func AuthHandle(next echo.HandlerFunc) echo.HandlerFunc {
if !valid {
if url == `/` {
c.Request().URL.Path = `login`
} else if url != `/api/login` && url != `/api/taskstatus` && url != `/api/sys` &&
url != `/api/notification` && url != `/api/runscript` && url != `/api/event` &&
} else if url != `/api/login` && /*url != `/api/taskstatus` &&*/ url != `/api/sys` &&
/*url != `/api/notification` && url != `/api/runscript` && url != `/api/event` &&*/
url != `/api/randid` {
return AccessDenied(http.StatusUnauthorized)
}
Expand Down
36 changes: 21 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package main
import (
"eonza/lib"
"eonza/users"
"fmt"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -43,21 +44,12 @@ type Config struct {
HTTP lib.HTTPConfig `yaml:"http"` // Web-server settings
Playground lib.PlaygroundConfig `yaml:"playground"` // Playground settings
Whitelist []string `yaml:"whitelist,omitempty"` // Whitelist of IP-addresses
// undocumented fields
PortShift int64 `yaml:"portshift,omitempty"` // shift of the port
CDN string `yaml:"cdn,omitempty"` // url for static files in task

path string // path to cfg file
develop bool
playground bool
}

const (
AccessLocalhost = Localhost
AccessPrivate = `private`
AccessHost = `host`
)

var (
cfg = Config{
Version: GetVersion(),
Expand All @@ -71,8 +63,9 @@ var (
Port: DefPort,
Open: true,
Theme: `default`,
Access: AccessLocalhost,
JWTKey: lib.UniqueName(12),
Cert: ``,
Priv: ``,
},
}
firstRun bool
Expand Down Expand Up @@ -131,14 +124,27 @@ func LoadConfig() {
if cfg.HTTP.Port == 0 {
cfg.HTTP.Port = DefPort
}
if cfg.HTTP.LocalPort == 0 {
if cfg.HTTP.LocalPort, err = getPort(); err != nil {
golog.Fatal(err)
}
}
if len(cfg.HTTP.Theme) == 0 {
cfg.HTTP.Theme = DefTheme
}
switch cfg.HTTP.Access {
case AccessHost:
case AccessPrivate:
default:
cfg.HTTP.Access = AccessLocalhost
if cfg.HTTP.Host != Localhost {
if cfg.HTTP.Cert == nil {
golog.Fatal(`Specify the path to the certificate pem file in config file`)
}
if cfg.HTTP.Priv == nil {
golog.Fatal(`Specify the path to the private key pem file in config file`)
}
if cfg.HTTP.Cert, err = os.ReadFile(fmt.Sprint(cfg.HTTP.Cert)); err != nil {
golog.Fatal(err)
}
if cfg.HTTP.Priv, err = os.ReadFile(fmt.Sprint(cfg.HTTP.Priv)); err != nil {
golog.Fatal(err)
}
}
cfg.develop = cfg.Mode == ModeDevelop
cfg.playground = cfg.Mode == ModePlayground
Expand Down
41 changes: 13 additions & 28 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,16 @@ package main

import (
"encoding/json"
"eonza/lib"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/gentee/gentee/core"
)

func request(url string) ([]byte, error) {
var (
res *http.Response
err error
)
buf := core.NewBuffer()
res, err = http.Get(fmt.Sprintf(`http://%s:%s`, Localhost, url))
if err == nil {
buf.Data, err = io.ReadAll(res.Body)
res.Body.Close()
}
return buf.Data, err
}

func CheckConsole() bool {
port := fmt.Sprint(DefPort)
port := DefPort
appname, err := os.Executable()
if err != nil {
fmt.Println(err)
Expand All @@ -46,17 +30,18 @@ func CheckConsole() bool {
fmt.Printf("Usage: %s <scriptname>\r\n", appname)
return true
}
answer, _ := request(fmt.Sprintf("%s/ping", port))
if string(answer) != Success {
if port = os.Getenv(`EZPORT`); len(port) > 0 {
answer, _ = request(fmt.Sprintf("%s/ping", port))
if !pingHost(port) {
if ezport := os.Getenv(`EZPORT`); len(ezport) > 0 {
if uport, err := strconv.ParseUint(ezport, 10, 32); err == nil {
port = int(uport)
if !pingHost(port) {
return output("eonza has not been run or listens to custom port")
}
}
}
}
if string(answer) != Success {
return output("eonza has not been run or listens to custom port")
}
answer, err = request(fmt.Sprintf("%s/api/run?name=%s&silent=true&console=true",
port, os.Args[1]))
answer, err := lib.LocalGet(port, fmt.Sprintf("api/run?name=%s&silent=true&console=true",
os.Args[1]))
if err != nil {
return output(err)
}
Expand Down
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.17.0"
Version = "1.18.0"
// DefPort is the default web-server port
DefPort = 3234
// DefTheme is the default web-server theme
Expand Down
31 changes: 15 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@ module eonza
go 1.15

require (
github.com/alecthomas/chroma v0.8.2
github.com/alecthomas/chroma v0.9.1
github.com/alecthomas/colour v0.1.0 // indirect
github.com/alecthomas/repr v0.0.0-20201120212035-bb82daffcca2 // indirect
github.com/atotto/clipboard v0.1.2
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/dlclark/regexp2 v1.4.0 // 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.5.0
github.com/go-sql-driver/mysql v1.6.0
github.com/gorilla/websocket v1.4.2
github.com/kataras/golog v0.1.6
github.com/kataras/golog v0.1.7
github.com/kr/text v0.2.0 // indirect
github.com/labstack/echo/v4 v4.1.17
github.com/lib/pq v1.9.0
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/pquerna/otp v1.3.0 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/sergi/go-diff v1.1.0 // indirect
github.com/xhit/go-simple-mail/v2 v2.7.0
github.com/yuin/goldmark v1.3.0
github.com/yuin/goldmark-highlighting v0.0.0-20200307114337-60d527fdb691
golang.org/x/crypto v0.0.0-20201217014255-9d1352758620
golang.org/x/net v0.0.0-20201216054612-986b41b23924 // indirect
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e
golang.org/x/text v0.3.4
github.com/sergi/go-diff v1.2.0 // 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
golang.org/x/text v0.3.6
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
Loading

0 comments on commit 9cb51b6

Please sign in to comment.