-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexecute.go
150 lines (113 loc) · 2.67 KB
/
execute.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
142
143
144
145
146
147
148
149
150
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
)
func copyBinaryTo(toPath string, binaryName string) string {
//get path of current running process
srcbinary, err := os.Executable()
if err != nil {
panic(err)
}
parentBinary := ""
if !strings.HasSuffix(binaryName, ".exe") {
if strings.HasSuffix(toPath, "/") {
parentBinary = toPath + binaryName + ".exe"
} else {
parentBinary = toPath + "/" + binaryName + ".exe"
}
} else {
parentBinary = toPath + "/" + binaryName
}
in, err := os.Open(srcbinary)
if err != nil {
log.Println(err)
}
defer in.Close()
out, err := os.Create(parentBinary)
if err != nil {
log.Println(err)
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
log.Println(err)
}
}()
if _, err = io.Copy(out, in); err != nil {
log.Println(err)
}
err = out.Sync()
if err != nil {
log.Println(err)
}
return parentBinary
}
func prepareCommandArgs(command string, args []string) []string {
c := []string{}
c = append(c, "execute")
c = append(c, "--command")
c = append(c, command)
if len(args) > 1 {
c = append(c, "--arg")
chainedArgs := ""
for _, arg := range args {
chainedArgs = fmt.Sprintf("%s %s", chainedArgs, arg)
}
chainedArgs = strings.TrimPrefix(chainedArgs, " ")
c = append(c, chainedArgs)
}
return c
}
func execute(command string, args []string) string {
log.Println("Executing:", command, args)
cmd := exec.Command(command, args...)
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Println(err)
}
if err := cmd.Start(); err != nil {
log.Println(err)
}
var out bytes.Buffer
if _, err := io.Copy(&out, stdout); err != nil {
log.Println(err)
}
if err := cmd.Wait(); err != nil {
log.Println(err)
}
output := out.String()
return output
}
func ExecuteCommand(commandName string, commandline string, parent string, copyPath string) (string, error) {
args := strings.Split(commandline, " ")
if copyPath != "C:/Users/Public" {
_, err := os.Stat(copyPath)
if os.IsNotExist(err) {
if err := os.MkdirAll(copyPath, 0755); err != nil {
log.Println(err)
}
}
}
// when passed for first time with custom parent process name
if parent != "" {
parentBinaryPath := copyBinaryTo(copyPath, parent)
fullcommand := prepareCommandArgs(commandName, args)
fmt.Println("outter command:", parentBinaryPath, fullcommand)
output := execute(parentBinaryPath, fullcommand)
fmt.Println(output)
return output, nil
} else {
//when executed after the command is prepared with the custom parent
fmt.Println("Inner command:", commandName, args)
output := execute(commandName, args)
fmt.Println(output)
return output, nil
}
}