This repository has been archived by the owner on Mar 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ctrl.go
141 lines (125 loc) · 3.91 KB
/
ctrl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// This file is part of ezBastion.
// ezBastion 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.
// ezBastion 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 ezBastion. If not, see <https://www.gnu.org/licenses/>.
package exec
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"time"
"github.com/ezbastion/ezb_wks/models"
"github.com/ezbastion/ezb_wks/models/tasks"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func dealwithErr(err error) {
if err != nil {
fmt.Println(err)
}
}
var (
conf models.Configuration
xtrack string
)
func run(c *gin.Context) {
conf, _ = c.MustGet("conf").(models.Configuration)
polling := c.GetHeader("X-Polling")
xtrack = c.GetHeader("X-Track")
var params EzbParams
err := c.ShouldBindJSON(¶ms)
if err != nil {
c.String(http.StatusInternalServerError, "#E0002 bind parameters error", err)
return
}
psParams := fmt.Sprintf("-xtrack '%s' ", xtrack)
for i, h := range params.Data {
psParams = fmt.Sprintf("%s -%s '%s' ", psParams, i, h)
}
psscript := filepath.Join(conf.ScriptPath, params.Meta.Job.Path)
if polling == "true" {
runTask(c, psscript, psParams)
} else {
runJob(c, psscript, psParams)
}
}
func runJob(c *gin.Context, psscript string, psParams string) {
logg := log.WithFields(log.Fields{
"controller": "exec",
"xtrack": xtrack,
})
logg.Debug("start")
cmd := exec.Command("powershell", "-NoLogo", "-NonInteractive", "-Command", "&{", psscript, " ", psParams, "}")
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Run()
if stderr.Len() != 0 {
errStr := stderr.String()
logg.Error("#E0001", errStr)
c.String(http.StatusInternalServerError, "#E0001 Powershell error: %s", errStr)
return
}
if stdout.Len() != 0 {
ret := json.RawMessage(stdout.Bytes())
c.JSON(http.StatusOK, ret)
} else {
c.JSON(http.StatusNoContent, "")
}
}
func runTask(c *gin.Context, psscript string, psParams string) {
tokenID := c.GetHeader("x-ezb-tokenid")
logg := log.WithFields(log.Fields{
"controller": "exec",
"xtrack": xtrack,
})
t := time.Now()
taskID := fmt.Sprintf("%s%s", t.Format("20060102"), xtrack)
jobPath := path.Join(strings.Replace(conf.JobPath, "\\", "/", -1), t.Format("2006/01/02"), xtrack)
if _, err := os.Stat(jobPath); os.IsNotExist(err) {
err = os.MkdirAll(jobPath, 0600)
if err != nil {
logg.Error("CANNOT CREATE JOB FOLDER")
c.JSON(http.StatusInternalServerError, "CANNOT CREATE JOB FOLDER")
return
}
}
stdOUT := filepath.Join(jobPath, "output.json")
stdTrace := filepath.Join(jobPath, "trace.log")
statusFile := filepath.Join(jobPath, "status.json")
task := tasks.EzbTasks{}
task.UUID = taskID
task.TokenID = tokenID
task.CreateDate = time.Now()
task.UpdateDate = time.Now()
task.Parameters = psParams
cmd := exec.Command("powershell", "-NoLogo", "-NonInteractive", "-Command", "&{", psscript, " ", psParams, "} 1>", stdOUT, " *>", stdTrace)
cmd.Start()
task.PID = cmd.Process.Pid
task.Status = tasks.TaksStatus(int(tasks.RUNNING))
c.JSON(http.StatusOK, task)
go waitTask(cmd, &task, statusFile)
}
func waitTask(cmd *exec.Cmd, task *tasks.EzbTasks, statusFile string) {
ta, _ := json.Marshal(task)
ioutil.WriteFile(statusFile, ta, 0600)
cmd.Wait()
task.Status = tasks.TaksStatus(int(tasks.FINISH))
task.UpdateDate = time.Now()
ta, _ = json.Marshal(task)
ioutil.WriteFile(statusFile, ta, 0600)
}