fix: docker运行添加转义符
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package exec
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.lxtend.com/qqbot/constants"
|
||||
"git.lxtend.com/qqbot/handler"
|
||||
"git.lxtend.com/qqbot/model"
|
||||
@@ -9,8 +12,8 @@ import (
|
||||
)
|
||||
|
||||
func init() {
|
||||
handler.RegisterHandler("/exec", runInDocker, constants.LEVEL_TRUSTED)
|
||||
handler.RegisterHandler("/restart", restartDocker, constants.LEVEL_TRUSTED)
|
||||
handler.RegisterHandler("exec", runInDocker, constants.LEVEL_TRUSTED)
|
||||
handler.RegisterHandler("reboot", restartDocker, constants.LEVEL_TRUSTED)
|
||||
}
|
||||
|
||||
func runInDocker(msg model.Message) (reply model.Reply) {
|
||||
@@ -20,12 +23,21 @@ func runInDocker(msg model.Message) (reply model.Reply) {
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
}
|
||||
if res, err := exec.DockerContainer.CreateAndExecuteCommand(token[1]); err != nil {
|
||||
decodedCMD, _ := url.QueryUnescape(token[1])
|
||||
decodedCMD = strings.ReplaceAll(decodedCMD, ",", ",")
|
||||
decodedCMD = strings.ReplaceAll(decodedCMD, "[", "[")
|
||||
decodedCMD = strings.ReplaceAll(decodedCMD, "]", "]")
|
||||
decodedCMD = strings.ReplaceAll(decodedCMD, "&", "&")
|
||||
if res, err := exec.DockerContainer.ExecCommandInContainer(decodedCMD); err != nil {
|
||||
reply.ReplyMsg = "Error: " + err.Error()
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
} else {
|
||||
pos := strings.LastIndex(res, "\n")
|
||||
if pos != -1 {
|
||||
res = res[:pos-1]
|
||||
}
|
||||
reply.ReplyMsg = res
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
@@ -40,7 +52,7 @@ func restartDocker(msg model.Message) (reply model.Reply) {
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
} else {
|
||||
reply.ReplyMsg = "成功重启容器"
|
||||
reply.ReplyMsg = "Restarted"
|
||||
reply.ReferOriginMsg = true
|
||||
reply.FromMsg = msg
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user