-
Notifications
You must be signed in to change notification settings - Fork 0
/
retirejs.go
215 lines (175 loc) · 3.88 KB
/
retirejs.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"context"
"fmt"
"github.com/sebdeveloper6952/malware-dvm/helpers"
"net/url"
"os"
"os/exec"
"strings"
"github.com/go-git/go-git/v5"
goNostr "github.com/nbd-wtf/go-nostr"
"github.com/sebdeveloper6952/godvm"
"github.com/sirupsen/logrus"
)
type retirejsDVM struct {
sk string
pk string
apiKey string
apiURL string
version string
name string
about string
picture string
npmPath string
retirePath string
log *logrus.Logger
}
func NewRetireJSDVM(
sk string,
version string,
name string,
about string,
picture string,
npmPath string,
retirePath string,
log *logrus.Logger,
) (godvm.Dvmer, error) {
dvm := &retirejsDVM{
version: version,
name: name,
about: about,
picture: picture,
npmPath: npmPath,
retirePath: retirePath,
log: log,
}
return dvm, dvm.SetSk(sk)
}
func (d *retirejsDVM) SetSk(sk string) error {
d.sk = sk
pk, err := goNostr.GetPublicKey(d.sk)
if err != nil {
return err
}
d.pk = pk
return nil
}
func (d *retirejsDVM) PublicKeyHex() string {
return d.pk
}
func (d *retirejsDVM) Sign(e *goNostr.Event) error {
return e.Sign(d.sk)
}
func (d *retirejsDVM) Profile() *godvm.ProfileMetadata {
return &godvm.ProfileMetadata{
Name: d.name,
About: d.about,
Picture: d.picture,
}
}
func (d *retirejsDVM) KindSupported() int {
return godvm.KindReqMalwareScan
}
func (d *retirejsDVM) Version() string {
return d.version
}
func (d *retirejsDVM) acceptJob(input *godvm.Nip90Input) bool {
// if the job request tagged and DVM and it is not us, don't respond
if _, tagged := input.TaggedPubkeys[d.pk]; len(input.TaggedPubkeys) > 0 && !tagged {
return false
}
if input.Inputs == nil || len(input.Inputs) != 1 {
return false
}
i := input.Inputs[0]
if _, err := url.ParseRequestURI(i.Value); err != nil {
return false
}
if i.Type != "url" {
return false
}
return true
}
func (d *retirejsDVM) Run(
ctx context.Context,
input *godvm.Nip90Input,
chanToDvm <-chan *godvm.JobUpdate,
chanToEngine chan<- *godvm.JobUpdate,
) bool {
if !d.acceptJob(input) {
return false
}
go func() {
chanToEngine <- &godvm.JobUpdate{
Status: godvm.StatusProcessing,
}
path, err := d.cloneRepo(input.Inputs[0].Value)
if err != nil {
chanToEngine <- &godvm.JobUpdate{
Status: godvm.StatusError,
FailureMsg: "could not find repo",
}
return
}
defer func() {
if err := os.RemoveAll(path); err != nil {
d.log.Errorf("[retirejs] couldn't delete directory %s %+v\n", path, err)
}
}()
if output, err := d.npmInstall(path); err != nil {
if err != nil {
d.log.Errorf("%+v", fmt.Errorf("[retirejs][npm] %w %s", err, output))
chanToEngine <- &godvm.JobUpdate{
Status: godvm.StatusError,
FailureMsg: "failed running npm install",
}
return
}
}
jobOutput := "CLEAN"
output, err := d.retireJs(path)
if err != nil {
d.log.Errorf("%+v", fmt.Errorf("[retirejs][run] %w %s", err, output))
jobOutput = output
}
chanToEngine <- &godvm.JobUpdate{
Status: godvm.StatusSuccessWithPayment,
Result: jobOutput,
AmountSats: 1,
}
}()
return true
}
func (d *retirejsDVM) cloneRepo(repoURL string) (string, error) {
var (
path = "./tmp/" + helpers.RandomBase16String(16)
output = &strings.Builder{}
)
_, err := git.PlainClone(path, false, &git.CloneOptions{
URL: repoURL,
SingleBranch: true,
Progress: output,
})
return path, err
}
func (d *retirejsDVM) npmInstall(path string) (string, error) {
var (
cmd = exec.Command(
d.npmPath,
"install",
"--legacy-peer-deps",
)
)
cmd.Dir = path
output, err := cmd.CombinedOutput()
return string(output), err
}
func (d *retirejsDVM) retireJs(path string) (string, error) {
cmd := exec.Command(
"retire",
)
cmd.Dir = path
output, err := cmd.CombinedOutput()
return string(output), err
}