mostly works now! Maybe?

This commit is contained in:
Merith-TK 2024-03-16 08:15:31 -07:00
parent 7482f8e1bf
commit 66268e6702
8 changed files with 105 additions and 50 deletions

7
go.mod
View file

@ -1,10 +1,7 @@
module bingus-exec module git.merith.xyz/merith-tk/reso-execution
go 1.21.3 go 1.21.3
require ( require github.com/gorilla/websocket v1.5.1
github.com/gorilla/mux v1.8.1
github.com/gorilla/websocket v1.5.1
)
require golang.org/x/net v0.22.0 // indirect require golang.org/x/net v0.22.0 // indirect

2
go.sum
View file

@ -1,5 +1,3 @@
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=

52
main.go
View file

@ -1,5 +1,15 @@
package main package main
/*
fn fetch_python_location() -> String {
let output = Command::new("which python")
.output()
.expect("Failed to execute command");
let output = String::from_utf8_lossy(&output.stdout);
output.trim().to_string()
output
}
*/
import ( import (
"fmt" "fmt"
"log" "log"
@ -22,6 +32,9 @@ var Upgrader = websocket.Upgrader{
func main() { func main() {
http.HandleFunc("/", HandleWs) http.HandleFunc("/", HandleWs)
// output handlers
http.HandleFunc("/lang", HandleLang)
// if ./workspace/ directory does not exist, create it // if ./workspace/ directory does not exist, create it
if _, err := os.Stat("./workspace/"); os.IsNotExist(err) { if _, err := os.Stat("./workspace/"); os.IsNotExist(err) {
os.Mkdir("./workspace/", 0755) os.Mkdir("./workspace/", 0755)
@ -33,6 +46,7 @@ func main() {
} }
func HandleWs(w http.ResponseWriter, r *http.Request) { func HandleWs(w http.ResponseWriter, r *http.Request) {
log.Println("[/] Connection from", r.RemoteAddr)
c, err := Upgrader.Upgrade(w, r, nil) c, err := Upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Print("upgrade:", err) log.Print("upgrade:", err)
@ -51,8 +65,40 @@ func HandleWs(w http.ResponseWriter, r *http.Request) {
// seperate message into chunks by newline // seperate message into chunks by newline
msg := strings.Split(string(message), "\n") msg := strings.Split(string(message), "\n")
sendMessage := handleCase(msg[0], strings.Join(msg[1:], "\n")) handleCase(msg[0], strings.Join(msg[1:], "\n"))
// send the message back to the client
err = c.WriteMessage(websocket.TextMessage, []byte(sendMessage.String())) c.WriteMessage(websocket.TextMessage, []byte("Forwarding to output handler: "+msg[0]+"\n"))
} }
} }
func HandleLang(w http.ResponseWriter, r *http.Request) {
log.Println("[/lang] Connection from", r.RemoteAddr)
// upgrade the connection to a websocket
c, err := Upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
// defer c.Close()
c.WriteMessage(websocket.TextMessage, []byte("Connected to output handler"))
go func() {
for {
// wait for message from client
_, message, err := c.ReadMessage()
if err != nil {
log.Println("Error reading from client", err)
break
}
// convert message to string
msg := string(message)
log.Println("Received message from client:", msg)
// convert pyReader to string
newstr := make([]byte, 1024)
n, err := pyReader.Read(newstr)
if err != nil {
log.Println("Error reading from pyReader")
break
}
c.WriteMessage(websocket.TextMessage, newstr[:n])
}
}()
}

8
output.go Normal file
View file

@ -0,0 +1,8 @@
package main
import (
"io"
)
// pythonOut is a channel that will be written to by the python runtime via exec.Command
var pyReader, pyWriter = io.Pipe()

View file

@ -3,6 +3,7 @@ package main
import ( import (
"log" "log"
"os/exec" "os/exec"
"path/filepath"
"strings" "strings"
// import file writer // import file writer
@ -10,54 +11,59 @@ import (
// import websocket // import websocket
) )
func handleCase(caseName string, data string) strings.Builder { func handleCase(caseName, data string) string {
// get the first line of the message from the client caseName = strings.ReplaceAll(caseName, "\n", "")
// strip : from the caseName caseName = strings.ReplaceAll(caseName, ":", "")
caseName = strings.Replace(caseName, ":", "", -1)
returnData := strings.Builder{} log.Println("Handling case", caseName)
switch caseName { switch caseName {
case "py", "python", "python3": case "python", "python3", "py":
log.Println("Python is not supported yet") runPython(data)
returnData = runPython(data)
case "go", "golang":
log.Println("Golang is not supported yet")
// returnData = runGo(data)
case "js", "javascript":
log.Println("Javascript is not supported yet")
// returnData = runJs(data)
case "cpp", "c++":
log.Println("C++ is not supported yet")
} }
return returnData return ""
} }
func runPython(data string) strings.Builder { func runPython(data string) {
// write Data to `./workspace/main.py` log.Println("Running python")
file, err := os.Create("./workspace/main.py") // create a file in the workspace directory
f, err := os.Create("./workspace/main.py")
if err != nil { if err != nil {
log.Println("Error creating file") log.Println("Error creating file")
return
}
defer f.Close()
// write the data to the file
_, err = f.WriteString(data)
if err != nil {
log.Println("Error writing to file")
return
} }
defer file.Close()
file.WriteString(data)
// find python, python3, or py in the system // find python, python3, or py in the system
fname, err := exec.LookPath("python3") // fname, err := exec.LookPath("python3")
if err != nil { // if err != nil {
log.Println("Python not found") // log.Println("Python not found")
// } else {
// log.Println("Python found at", fname)
// }
fname := "C:\\Users\\zachd\\scoop\\shims\\python3.exe"
// if main.py does not exist, return
if _, err := os.Stat("./workspace/main.py"); os.IsNotExist(err) {
log.Println("File does not exist")
return
} }
// run the python file cmd := exec.Command(fname, "./main.py")
cmd := exec.Command(fname, "./workspace/main.py") cmd.Dir = filepath.ToSlash("./workspace/")
// write output to value, and then return it // set the command's stdout to the pipe writer
cmdOut := &strings.Builder{} cmd.Stdout = pyWriter
cmd.Stdout = cmdOut
cmdErr := &strings.Builder{}
cmd.Stderr = cmdErr
cmd.Run()
// combine the output and error // set the command's stderr to the pipe writer
cmdOut.WriteString(cmdErr.String()) cmd.Stderr = pyWriter
// print the output to the console
log.Println(cmdOut.String()) // start the command
return *cmdOut cmd.Run()
log.Println("Python runtime finished")
} }

View file

@ -1 +1 @@
exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1 exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1exit status 1

Binary file not shown.

View file

@ -1,4 +1,4 @@
import time import time
for i in range(50): for i in range(10):
print(i) print(i)
time.sleep(0.5) time.sleep(0.5)