fix: docker运行添加转义符
This commit is contained in:
parent
5997c37576
commit
28dec155f8
@ -1,6 +1,9 @@
|
|||||||
package exec
|
package exec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.lxtend.com/qqbot/constants"
|
"git.lxtend.com/qqbot/constants"
|
||||||
"git.lxtend.com/qqbot/handler"
|
"git.lxtend.com/qqbot/handler"
|
||||||
"git.lxtend.com/qqbot/model"
|
"git.lxtend.com/qqbot/model"
|
||||||
@ -9,8 +12,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
handler.RegisterHandler("/exec", runInDocker, constants.LEVEL_TRUSTED)
|
handler.RegisterHandler("exec", runInDocker, constants.LEVEL_TRUSTED)
|
||||||
handler.RegisterHandler("/restart", restartDocker, constants.LEVEL_TRUSTED)
|
handler.RegisterHandler("reboot", restartDocker, constants.LEVEL_TRUSTED)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runInDocker(msg model.Message) (reply model.Reply) {
|
func runInDocker(msg model.Message) (reply model.Reply) {
|
||||||
@ -20,12 +23,21 @@ func runInDocker(msg model.Message) (reply model.Reply) {
|
|||||||
reply.FromMsg = msg
|
reply.FromMsg = msg
|
||||||
return
|
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.ReplyMsg = "Error: " + err.Error()
|
||||||
reply.ReferOriginMsg = true
|
reply.ReferOriginMsg = true
|
||||||
reply.FromMsg = msg
|
reply.FromMsg = msg
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
|
pos := strings.LastIndex(res, "\n")
|
||||||
|
if pos != -1 {
|
||||||
|
res = res[:pos-1]
|
||||||
|
}
|
||||||
reply.ReplyMsg = res
|
reply.ReplyMsg = res
|
||||||
reply.ReferOriginMsg = true
|
reply.ReferOriginMsg = true
|
||||||
reply.FromMsg = msg
|
reply.FromMsg = msg
|
||||||
@ -40,7 +52,7 @@ func restartDocker(msg model.Message) (reply model.Reply) {
|
|||||||
reply.FromMsg = msg
|
reply.FromMsg = msg
|
||||||
return
|
return
|
||||||
} else {
|
} else {
|
||||||
reply.ReplyMsg = "成功重启容器"
|
reply.ReplyMsg = "Restarted"
|
||||||
reply.ReferOriginMsg = true
|
reply.ReferOriginMsg = true
|
||||||
reply.FromMsg = msg
|
reply.FromMsg = msg
|
||||||
return
|
return
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"git.lxtend.com/qqbot/constants"
|
"git.lxtend.com/qqbot/constants"
|
||||||
"git.lxtend.com/qqbot/handler"
|
"git.lxtend.com/qqbot/handler"
|
||||||
"git.lxtend.com/qqbot/model"
|
"git.lxtend.com/qqbot/model"
|
||||||
|
docker "git.lxtend.com/qqbot/service/exec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var hasVaildBuild = true
|
var hasVaildBuild = true
|
||||||
@ -95,6 +96,7 @@ func pullCode(msg model.Message) model.Reply {
|
|||||||
|
|
||||||
func restartProgram() error {
|
func restartProgram() error {
|
||||||
log.Println("重启程序...")
|
log.Println("重启程序...")
|
||||||
|
docker.DockerContainer.RemoveContainer()
|
||||||
time.Sleep(500 * time.Millisecond)
|
time.Sleep(500 * time.Millisecond)
|
||||||
path, err := os.Executable()
|
path, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
2
main.go
2
main.go
@ -5,6 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.lxtend.com/qqbot/health"
|
"git.lxtend.com/qqbot/health"
|
||||||
|
"git.lxtend.com/qqbot/service/exec"
|
||||||
"git.lxtend.com/qqbot/sqlite3"
|
"git.lxtend.com/qqbot/sqlite3"
|
||||||
wsclient "git.lxtend.com/qqbot/ws_client"
|
wsclient "git.lxtend.com/qqbot/ws_client"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -17,6 +18,7 @@ func main() {
|
|||||||
log.Print("Recovered in main:", err)
|
log.Print("Recovered in main:", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
defer exec.DockerContainer.RemoveContainer()
|
||||||
gin.SetMode(gin.ReleaseMode)
|
gin.SetMode(gin.ReleaseMode)
|
||||||
sqlite3.InitDB()
|
sqlite3.InitDB()
|
||||||
client, err := wsclient.NewWebSocketClient("ws", "localhost:3001", "")
|
client, err := wsclient.NewWebSocketClient("ws", "localhost:3001", "")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user