Skip to content

Commit f1ae54e

Browse files
committed
feat: check if we have already compiled agent
1 parent ce0dfcd commit f1ae54e

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

main.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import (
1717
"syscall"
1818
)
1919

20+
const (
21+
agentFilename = "_agent.js"
22+
)
23+
2024
//go:embed script.ts
2125
var scriptContent []byte
2226

@@ -202,6 +206,15 @@ var rootCmd = &cobra.Command{
202206

203207
os.MkdirAll(tempDir, os.ModePerm)
204208

209+
// save current directory because we might change it to compile the script
210+
currentDir, _ := os.Getwd()
211+
212+
if _, err = os.Stat(filepath.Join(tempDir, "script.ts")); os.IsNotExist(err) {
213+
for fl, data := range tempFiles {
214+
os.WriteFile(filepath.Join(tempDir, fl), data, os.ModePerm)
215+
}
216+
}
217+
205218
if _, err = os.Stat(filepath.Join(tempDir, "node_modules")); os.IsNotExist(err) {
206219
// Install modules
207220
command := exec.Command("npm", "install")
@@ -210,28 +223,40 @@ var rootCmd = &cobra.Command{
210223
}
211224
}
212225

213-
for fl, data := range tempFiles {
214-
os.WriteFile(filepath.Join(tempDir, fl), data, os.ModePerm)
215-
}
226+
agentPath := filepath.Join(tempDir, agentFilename)
227+
var scriptBody string
216228

217-
currentDir, _ := os.Getwd()
229+
// check if we have script.ts already compiled
230+
if _, err = os.Stat(agentPath); os.IsNotExist(err) {
231+
os.Chdir(tempDir)
218232

219-
os.Chdir(tempDir)
233+
comp := frida.NewCompiler()
220234

221-
comp := frida.NewCompiler()
235+
comp.On("finished", func() {
236+
logger.Infof("Done compiling script")
237+
})
222238

223-
comp.On("finished", func() {
224-
logger.Infof("Done compiling script")
225-
})
239+
bundle, err := comp.Build("script.ts")
240+
if err != nil {
241+
return fmt.Errorf("error compiling script: %v", err)
242+
}
226243

227-
bundle, err := comp.Build("script.ts")
228-
if err != nil {
229-
logger.Errorf("Error compiling script: %v", err)
230-
}
244+
if err := os.WriteFile(agentPath, []byte(bundle), os.ModePerm); err != nil {
245+
return fmt.Errorf("error saving agent script: %v", err)
246+
}
231247

232-
os.Chdir(currentDir)
248+
scriptBody = bundle
249+
250+
os.Chdir(currentDir)
251+
} else {
252+
data, err := os.ReadFile(agentPath)
253+
if err != nil {
254+
return fmt.Errorf("error reading agent script: %v", err)
255+
}
256+
scriptBody = string(data)
257+
}
233258

234-
script, err := session.CreateScript(bundle)
259+
script, err := session.CreateScript(scriptBody)
235260
if err != nil {
236261
return err
237262
}

0 commit comments

Comments
 (0)