-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
442 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.code-editor, .code-lines { | ||
font-family: lucida console, courier new, courier, monospace; | ||
margin: 0; | ||
height: 75vh; | ||
border-radius: 0; | ||
resize: none; | ||
line-height: 1.2; | ||
outline: none; | ||
-moz-box-sizing: border-box; | ||
-webkit-box-sizing: border-box; | ||
box-sizing: border-box; | ||
font-family: Inconsolata; | ||
} | ||
.code-lines:focus-visible, | ||
.code-editor:focus-visible { | ||
outline:none; | ||
} | ||
.code-lines { | ||
display: flex; | ||
border-color: transparent; | ||
overflow-y: hidden; | ||
text-align: right; | ||
box-shadow: none; | ||
color: #707070; | ||
background-color: #d8d8d8; | ||
position: absolute; | ||
width: 3.5rem; | ||
background-color:#3E3D32; | ||
border-color:#3E3D32; | ||
color:#928869; | ||
} | ||
.code-editor { | ||
padding-left: calc(3.5rem + 5px); | ||
width:100%; | ||
background-color:#272822; | ||
border-color:#272822; | ||
color:#ffffff; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Rizin Notebook</title> | ||
<link rel="stylesheet" href="{{ .root }}static/spectre-icons.min.css"> | ||
<link rel="stylesheet" href="{{ .root }}static/spectre.min.css"> | ||
<link rel="stylesheet" href="{{ .root }}static/simplemde.min.css"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<style type="text/css"> | ||
html, body { width: 100%; height: 100%; margin: 0 } | ||
.button-edit { width: 75px ! important; margin-top: 9px; margin-right: 9px; } | ||
@font-face { font-family: 'Inconsolata'; src: URL('{{ .root }}static/inconsolata.ttf') format('truetype'); } | ||
</style> | ||
<link rel="stylesheet" href="{{ .root }}static/codelines.css"> | ||
</head> | ||
<body> | ||
<form style="margin-top: 15px; height: 100%;" action="{{ .root }}output/script/{{ .unique }}" method="POST"> | ||
<div class="form-group"> | ||
<p><textarea id="codelines" class="code-lines" wrap="off" readonly>1 </textarea><textarea id="codeeditor" class="code-editor" wrap="off" name="script">console.log(rizin.cmd("?e hello world").trim(), "from the script");</textarea></p> | ||
</div> | ||
<div class="form-group"> | ||
<button class="btn btn-primary" type="submit">Run</button> | ||
<a class="btn" href="{{ .root }}output/deleted">Cancel</a> | ||
</div> | ||
</form> | ||
<script type="text/javascript"> | ||
var editor = document.getElementById('codeeditor'); | ||
var lines = document.getElementById('codelines'); | ||
editor.addEventListener('scroll', function() { | ||
lines.scrollTop = editor.scrollTop; | ||
lines.scrollLeft = editor.scrollLeft; | ||
}); | ||
editor.addEventListener('keydown', function(e) { | ||
if (e.keyCode === 9) { | ||
// TAB = 9 | ||
e.preventDefault(); | ||
editor.value = editor.value.slice(0, editor.selectionStart) + '\t' + value.slice(editor.selectionEnd); | ||
editor.setSelectionRange(editor.selectionStart + 2, editor.selectionStart + 2) | ||
} | ||
}); | ||
var lineCountCache = 0; | ||
function updateLines() { | ||
var editorLines = editor.value.split('\n'); | ||
if (lineCountCache != editorLines.length) { | ||
lines.value = editorLines.map(function(e, n) { | ||
return (n + 1).toString() + ' '; | ||
}).join('\n'); | ||
} | ||
lineCountCache = lineCount; | ||
} | ||
editor.addEventListener('input', function() { | ||
updateLines(); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"github.com/dop251/goja" | ||
"golang.org/x/sync/semaphore" | ||
"time" | ||
) | ||
|
||
type JavaScript struct { | ||
semaphore *semaphore.Weighted | ||
runtime *goja.Runtime | ||
rizin *Rizin | ||
output string | ||
} | ||
|
||
func rizinCmd(command string, rizin *Rizin) (string, error) { | ||
rizin.exec("scr.color=0") | ||
result, err := rizin.exec(command) | ||
rizin.exec("scr.color=3") | ||
return result, err | ||
} | ||
|
||
func NewJavaScript() *JavaScript { | ||
runtime := goja.New() | ||
if runtime == nil { | ||
fmt.Println("cannot create a JavaScript runtime.") | ||
return nil | ||
} | ||
sem := semaphore.NewWeighted(1) | ||
js := &JavaScript{semaphore: sem, runtime: runtime, rizin: nil} | ||
rizin := map[string]interface{}{} | ||
rizin["cmd"] = func(args ...interface{}) goja.Value { | ||
if js.rizin == nil { | ||
panic(js.runtime.ToValue("Rizin pipe is closed.")) | ||
} else if len(args) < 1 { | ||
panic(js.runtime.ToValue("No string was passed.")) | ||
} | ||
switch value := args[0].(type) { | ||
case string: | ||
result, err := rizinCmd(value, js.rizin) | ||
if err != nil { | ||
panic(js.runtime.ToValue(err)) | ||
} | ||
return js.runtime.ToValue(result) | ||
default: | ||
panic(js.runtime.ToValue("input is not a string.")) | ||
} | ||
} | ||
rizin["cmdj"] = func(args ...interface{}) goja.Value { | ||
if js.rizin == nil { | ||
panic(js.runtime.ToValue("Rizin pipe is closed.")) | ||
} else if len(args) < 1 { | ||
panic(js.runtime.ToValue("No string was passed.")) | ||
} | ||
switch value := args[0].(type) { | ||
case string: | ||
result, err := rizinCmd(value, js.rizin) | ||
if err != nil { | ||
panic(js.runtime.ToValue(err)) | ||
} | ||
array := []interface{}{} | ||
var data interface{} | ||
err = json.Unmarshal([]byte(result), &data) | ||
if err != nil { | ||
err = json.Unmarshal([]byte(result), &array) | ||
data = array | ||
} | ||
if err != nil { | ||
panic(js.runtime.ToValue(err)) | ||
} | ||
return js.runtime.ToValue(data) | ||
default: | ||
panic(js.runtime.ToValue("input is not a string.")) | ||
} | ||
} | ||
|
||
console := map[string]interface{}{} | ||
console["log"] = func(args ...interface{}) { | ||
n_args := len(args) | ||
for i, value := range args { | ||
js.output += fmt.Sprintf("%v", value) | ||
if i+1 < n_args { | ||
js.output += " " | ||
} | ||
} | ||
js.output += "\n" | ||
} | ||
|
||
runtime.Set("rizin", rizin) | ||
runtime.Set("console", console) | ||
return js | ||
} | ||
|
||
func (js *JavaScript) exec(script string, rizin *Rizin) (string, error) { | ||
if !js.semaphore.TryAcquire(1) { | ||
return "", errors.New("A script is already running.") | ||
} | ||
defer js.semaphore.Release(1) | ||
js.rizin = rizin | ||
js.output = "" | ||
timer := time.AfterFunc(5*time.Minute, func() { | ||
js.runtime.Interrupt("The script execution has timed out.") | ||
}) | ||
_, err := js.runtime.RunScript("script.js", script) | ||
result := js.output | ||
js.rizin = nil | ||
js.output = "" | ||
timer.Stop() | ||
return result, err | ||
} |
Oops, something went wrong.