代码拉取完成,页面将自动刷新
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package server
import (
"context"
"errors"
"io"
"os"
"os/exec"
"strings"
"time"
"github.com/admpub/log"
"github.com/admpub/nging/application/dbschema"
"github.com/admpub/nging/application/handler"
"github.com/admpub/nging/application/library/charset"
"github.com/admpub/nging/application/library/config"
"github.com/admpub/nging/application/library/cron"
"github.com/admpub/nging/application/model"
"github.com/admpub/sockjs-go/sockjs"
"github.com/admpub/websocket"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/param"
)
func Cmd(ctx echo.Context) error {
var err error
id := ctx.Formx(`id`).Uint()
m := model.NewCommand(ctx)
if id > 0 {
err := m.Get(nil, `id`, id)
if err != nil {
handler.SendFail(ctx, err.Error())
return ctx.Redirect(handler.URLFor(`/manager/command`))
}
}
ctx.Set(`id`, id)
ctx.Set(`cmd`, m.Command)
return ctx.Render(`server/cmd`, err)
}
func CmdSendBySockJS(c sockjs.Session) error {
send := make(chan string)
//push(writer)
go func() {
for {
message := <-send
handler.WebSocketLogger.Debug(`Push message: `, message)
if err := c.Send(message); err != nil {
handler.WebSocketLogger.Error(`Push error: `, err.Error())
return
}
}
}()
timeout := c.Request().URL.Query().Get(`timeout`)
duration := config.ParseTimeDuration(timeout)
//echo
exec := func(session sockjs.Session) error {
var (
w io.WriteCloser
cmd *exec.Cmd
)
for {
command, err := session.Recv()
if err != nil {
return err
}
if len(command) == 0 {
continue
}
var (
workdir string
env []string
)
if command[0] == '>' {
id := param.String(command[1:]).Uint()
if id > 0 {
m, result, err := ExecCommand(id)
if err != nil {
send <- err.Error()
continue
}
if m.Remote == `Y` {
send <- result
continue
}
workdir = m.WorkDirectory
env = config.ParseEnvSlice(m.Env)
command = m.Command
} else {
return errors.New(`Invalid ID: ` + command[1:])
}
} else {
}
if w == nil {
w, cmd, err = cmdRunner(workdir, env, command, send, func() {
w.Close()
w = nil
}, duration, c.Request().Context())
if err != nil {
return err
}
continue
}
err = cmdContinue(command, w, cmd)
if err != nil {
return err
}
}
}
err := exec(c)
if err != nil {
handler.WebSocketLogger.Error(err)
}
close(send)
return nil
}
func cmdRunner(workdir string, env []string, command string, send chan string, onEnd func(), timeout time.Duration, ctx context.Context) (w io.WriteCloser, cmd *exec.Cmd, err error) {
params := cron.CmdParams(command)
cmd = com.CreateCmd(params, func(b []byte) (e error) {
if com.IsWindows {
b, e = charset.Convert(`gbk`, `utf-8`, b)
if e != nil {
return e
}
}
send <- string(b)
return nil
})
if len(workdir) > 0 {
cmd.Dir = workdir
}
if len(env) > 0 {
cmd.Env = env
}
w, err = cmd.StdinPipe()
if err != nil {
return
}
done := make(chan struct{})
go func() {
log.Info(`[command] running: `, command)
if e := cmd.Run(); e != nil {
cmd.Stderr.Write([]byte(e.Error()))
}
done <- struct{}{}
onEnd()
}()
cmdMaxTimeout := config.DefaultConfig.Sys.CmdTimeoutDuration
if timeout <= 0 {
timeout = time.Minute * 5
}
if timeout > cmdMaxTimeout {
timeout = cmdMaxTimeout
}
go func() {
ticker := time.NewTicker(timeout)
defer ticker.Stop()
for {
select {
case <-done:
log.Info(`[command] exited: `, command)
return
case <-ticker.C:
if cmd == nil {
return
}
if cmd.Process == nil {
return
}
cmd.Stderr.Write([]byte(`timeout`))
log.Info(`[command] timeout: `, command)
err := cmd.Process.Kill()
if err != nil {
log.Error(err)
}
return
case <-ctx.Done():
if cmd == nil {
return
}
if cmd.Process == nil {
return
}
cmd.Stderr.Write([]byte(`request is cancelled`))
log.Info(`[command] request is cancelled: `, command)
err := cmd.Process.Kill()
if err != nil {
log.Error(err)
}
return
}
}
}()
return
}
func cmdContinue(command string, w io.WriteCloser, cmd *exec.Cmd) (err error) {
if cmd == nil {
return nil
}
switch command {
case `^C`:
err = cmd.Process.Signal(os.Interrupt)
if err != nil {
if !strings.HasPrefix(err.Error(), `not supported by `) {
handler.WebSocketLogger.Error(err)
}
err = cmd.Process.Kill()
if err != nil {
handler.WebSocketLogger.Error(err)
}
}
default:
w.Write([]byte(command + "\n"))
}
return nil
}
func CmdSendByWebsocket(c *websocket.Conn, ctx echo.Context) error {
check, _ := ctx.Funcs()[`CheckPerm`].(func(string) error)
send := make(chan string)
//push(writer)
go func() {
for {
message := <-send
handler.WebSocketLogger.Debug(`Push message: `, message)
if err := c.WriteMessage(websocket.TextMessage, []byte(message)); err != nil {
handler.WebSocketLogger.Error(`Push error: `, err.Error())
return
}
}
}()
timeout := ctx.Query(`timeout`)
duration := config.ParseTimeDuration(timeout)
//echo
exec := func(conn *websocket.Conn) error {
var (
w io.WriteCloser
cmd *exec.Cmd
)
for {
_, message, err := conn.ReadMessage()
if err != nil {
return err
}
command := string(message)
if len(command) == 0 {
continue
}
//TEST:
//notice.OpenMessage(`test`, ``)
//notice.Send(`test`, notice.NewMessageWithValue(``, `from: admin`, `test user message`))
var (
workdir string
env []string
)
if command[0] == '>' {
id := param.String(command[1:]).Uint()
if id > 0 {
if check != nil {
err := check(command[1:])
if err != nil {
send <- err.Error()
continue
}
}
m, result, err := ExecCommand(id)
if err != nil {
send <- err.Error()
continue
}
if m.Remote == `Y` {
send <- result
continue
}
workdir = m.WorkDirectory
env = config.ParseEnvSlice(m.Env)
command = m.Command
} else {
err := errors.New(`Invalid ID: ` + command[1:])
send <- err.Error()
continue
}
} else {
if check != nil {
err := check(``)
if err != nil {
return err
}
}
}
if w == nil {
w, cmd, err = cmdRunner(workdir, env, command, send, func() {
w.Close()
w = nil
}, duration, ctx.Request().StdRequest().Context())
if err != nil {
return err
}
continue
}
err = cmdContinue(command, w, cmd)
if err != nil {
return err
}
}
}
err := exec(c)
if err != nil {
handler.WebSocketLogger.Error(err)
}
close(send)
return nil
}
func ExecCommand(id uint) (*dbschema.Command, string, error) {
m := model.NewCommand(nil)
err := m.Get(nil, `id`, id)
if err != nil {
return m.Command, "", err
}
if m.Command.Disabled == `Y` {
return m.Command, "", errors.New(echo.T(`该命令已禁用`))
}
//m.Command.Remote = `Y`
//m.Command.SshAccountId = 4
if m.Command.Remote == `Y` {
if m.Command.SshAccountId < 1 {
return m.Command, "", errors.New("Error, you did not choose ssh account")
}
sshUser := model.NewSshUser(nil)
err = sshUser.Get(nil, `id`, m.Command.SshAccountId)
if err != nil {
if err == db.ErrNoMoreRows {
return m.Command, "", errors.New("The specified ssh account does not exist")
}
return m.Command, "", err
}
sshUser.Passphrase = config.DefaultConfig.Decode(sshUser.Passphrase)
sshUser.Password = config.DefaultConfig.Decode(sshUser.Password)
cmdList := []string{}
if len(m.WorkDirectory) > 0 {
cmdList = append(cmdList, `cd `+m.WorkDirectory)
}
if len(m.Env) > 0 {
for _, env := range strings.Split(m.Env, "\n") {
env = strings.TrimSpace(env)
if len(env) < 1 {
continue
}
cmdList = append(cmdList, `export `+env)
}
}
cmdList = append(cmdList, m.Command.Command)
w := cron.NewCmdRec(1000)
err = sshUser.ExecMultiCMD(w, cmdList...)
if err != nil {
return m.Command, "", err
}
//panic(echo.Dump(w.String(), false))
return m.Command, w.String(), nil
}
return m.Command, "", err
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。