diff --git a/.air.toml b/.air.toml index 90e83bf..8ac00e9 100644 --- a/.air.toml +++ b/.air.toml @@ -4,8 +4,8 @@ tmp_dir = "tmp" [build] args_bin = [] - bin = "tmp\\main.exe" - cmd = "go build -o ./tmp/main.exe ." + bin = "tmp/main.exe" + cmd = "go build -o ./tmp/main.exe ./cmd/server" delay = 1000 exclude_dir = ["assets", "tmp", "vendor", "testdata"] exclude_file = [] diff --git a/cmd/client/main.go b/cmd/client/main.go new file mode 100644 index 0000000..4e53f70 --- /dev/null +++ b/cmd/client/main.go @@ -0,0 +1,100 @@ +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/url" + "os" + "os/signal" + "time" + + "github.com/gorilla/websocket" +) + +func main() { + interrupt := make(chan os.Signal, 1) + signal.Notify(interrupt, os.Interrupt) + + host := "127.0.0.1:5000" + langURL := fmt.Sprintf("ws://%s/lang", host) + cmdURL := fmt.Sprintf("ws://%s/cmd", host) + debugURL := fmt.Sprintf("ws://%s/debug", host) + execURL := fmt.Sprintf("ws://%s/", host) + + execConn, err := connectWebSocket(execURL) + if err != nil { + log.Fatal("Failed to connect to lang websocket:", err) + } + defer execConn.Close() + + langConn, err := connectWebSocket(langURL) + if err != nil { + log.Fatal("Failed to connect to lang websocket:", err) + } + defer langConn.Close() + + cmdConn, err := connectWebSocket(cmdURL) + if err != nil { + log.Fatal("Failed to connect to cmd websocket:", err) + } + defer cmdConn.Close() + + debugConn, err := connectWebSocket(debugURL) + if err != nil { + log.Fatal("Failed to connect to debug websocket:", err) + } + defer debugConn.Close() + + done := make(chan struct{}) + go readMessages(langConn, "lang", done) + go readMessages(cmdConn, "cmd", done) + go readMessages(debugConn, "debug", done) + // Read the contents of source.txt + sourceFile := "source.txt" + sourceData, err := ioutil.ReadFile(sourceFile) + if err != nil { + log.Fatal("Failed to read source file:", err) + } + + // create execConn off of / endpoint + + // Send the contents over the lang websocket + err = execConn.WriteMessage(websocket.TextMessage, sourceData) + if err != nil { + log.Fatal("Failed to send source data over lang websocket:", err) + } + + select { + case <-interrupt: + fmt.Println("Interrupt signal received, closing connections...") + close(done) + time.Sleep(1 * time.Second) + return + } +} + +func connectWebSocket(urlStr string) (*websocket.Conn, error) { + u, err := url.Parse(urlStr) + if err != nil { + return nil, err + } + + conn, _, err := websocket.DefaultDialer.Dial(u.String(), nil) + if err != nil { + return nil, err + } + + return conn, nil +} + +func readMessages(conn *websocket.Conn, name string, done chan struct{}) { + for { + _, message, err := conn.ReadMessage() + if err != nil { + log.Println("Error reading message from", name, "websocket:", err) + return + } + fmt.Printf("Received message from %s: %s\n", name, message) + } +} diff --git a/main.go b/cmd/server/main.go similarity index 100% rename from main.go rename to cmd/server/main.go diff --git a/output.go b/cmd/server/output.go similarity index 100% rename from output.go rename to cmd/server/output.go diff --git a/runtime.go b/cmd/server/runtime.go similarity index 78% rename from runtime.go rename to cmd/server/runtime.go index b9a7eca..ce538b3 100644 --- a/runtime.go +++ b/cmd/server/runtime.go @@ -12,7 +12,7 @@ import ( ) func handleCase(caseName, data string) string { - caseName = strings.ReplaceAll(caseName, "\n", "") + // caseName = strings.ReplaceAll(caseName, "\n", "") caseName = strings.ReplaceAll(caseName, ":", "") log.Println("Handling case", caseName) @@ -41,13 +41,13 @@ func runPython(data string) { } // find python, python3, or py in the system - // fname, err := exec.LookPath("python3") - // if err != nil { - // log.Println("Python not found") - // } else { - // log.Println("Python found at", fname) - // } - fname := "C:\\Users\\zachd\\scoop\\shims\\python3.exe" + fname, err := exec.LookPath("python3") + if err != nil { + 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) { diff --git a/source.py b/source.py deleted file mode 100644 index c115790..0000000 --- a/source.py +++ /dev/null @@ -1,268 +0,0 @@ -import asyncio -import websockets -import pexpect -import os -import subprocess -import re - -WS_HOST = "127.0.0.1" -WS_PORT = 5000 -ws_port2 = 5001 -ws_port3 = 5002 -ws_port4 = 5003 -ws_port5 = 5004 -ws_port6 = 5005 -ws_port7 = 5006 -ws_port8 = 5007 -ws_port9 = 5008 -ws_port10 = 5009 - -SHELL = "/bin/bash" -TEMP_PYTHON_FILE = "code/temp.py" -TEMP_BASH_FILE = "code/temp.sh" -TEMP_CPP_FILE = "code/temp.cpp" -TEMP_NODE_FILE = "code/temp.js" -TEMP_GO_FILE = "code/temp.go" - -DIRECTORY= "code" -if not os.path.exists(DIRECTORY): - os.makedirs(DIRECTORY) -def translate_terminal_colors(code): - color_mapping = { - '0': 'black', - '1': 'red', - '2': 'green', - '3': 'yellow', - '4': 'blue', - '5': 'magenta', - '6': 'cyan', - '7': 'white', - '8': 'black', - '9': 'red', - '10': 'green', - '11': 'yellow', - '12': 'blue', - '13': 'magenta', - '14': 'cyan', - '15': 'white', - '9': 'red', - '10': 'green', - '11': 'yellow', - '12': 'blue', - '13': 'magenta', - '14': 'cyan', - '15': 'white', - '30': 'black', - '31': 'red', - '32': 'green', - '33': 'yellow', - '34': 'blue', - '35': 'magenta', - '36': 'cyan', - '37': 'white', - '100': 'black', - '101': 'red', - '110': 'green', - '111': 'yellow', - '112': 'blue', - '113': 'magenta', - '114': 'cyan', - '115': 'white', - '40': 'black', - '41': 'red', - '42': 'green', - '43': 'yellow', - '44': 'blue', - '45': 'magenta', - '46': 'cyan', - '47': 'white', - '100': 'black', - '101': 'red', - '102': 'green', - '103': 'yellow', - '104': 'blue', - '105': 'magenta', - '106': 'cyan', - '107': 'white', - '108': 'black' - - } - - translated_code = '' - i = 0 - while i < len(code): - if code[i] == '\x1b' and code[i+1] == '[': - j = i + 2 - while code[j].isdigit() or code[j] == ';': - j += 1 - if code[j] == 'm': - color_codes = code[i+2:j].split(';') - for color_code in color_codes: - if color_code in color_mapping: - translated_code += f'' - else: - translated_code += f'' - i = j + 1 - continue - translated_code += code[i] - i += 1 - - return translated_code -### START -async def execute_GO(code, websocket): - with open(TEMP_GO_FILE, 'w') as file: - file.write(code) - - child = pexpect.spawn(f"g++ {TEMP_GO_FILE} -o temp", encoding="utf-8") - - while True: - try: - index = child.expect(['\n', pexpect.EOF, pexpect.TIMEOUT], timeout=1) - if index == 0: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - elif index == 1: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - break - except pexpect.exceptions.TIMEOUT: - break - os.remove(TEMP_GO_FILE) - -async def GO(websocket, path): - try: - - async for code in websocket: - await execute_GO(code, websocket) - except websockets.exceptions.ConnectionClosedOK: - pass -#### END -### START -async def execute_CPP(code, websocket): - with open(TEMP_CPP_FILE, 'w') as file: - file.write(code) - - child = pexpect.spawn(f"g++ {TEMP_CPP_FILE} -o temp", encoding="utf-8") - - while True: - try: - index = child.expect(['\n', pexpect.EOF, pexpect.TIMEOUT], timeout=1) - if index == 0: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - elif index == 1: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - break - except pexpect.exceptions.TIMEOUT: - break - os.remove(TEMP_CPP_FILE) - -async def CPP(websocket, path): - try: - - async for code in websocket: - await execute_CPP(code, websocket) - except websockets.exceptions.ConnectionClosedOK: - pass -#### END -### START -async def execute_NODE(code, websocket): - with open(TEMP_NODE_FILE, 'w') as file: - file.write(code) - - child = pexpect.spawn(f"node {TEMP_NODE_FILE}", encoding="utf-8") - - while True: - try: - index = child.expect(['\n', pexpect.EOF, pexpect.TIMEOUT], timeout=1) - if index == 0: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - elif index == 1: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - break - except pexpect.exceptions.TIMEOUT: - break - os.remove(TEMP_NODE_FILE) - -async def NODE(websocket, path): - try: - async for code in websocket: - await execute_NODE(code, websocket) - except websockets.exceptions.ConnectionClosedOK: - pass -#### END -### START -async def execute_shell(code, websocket): - with open(TEMP_BASH_FILE, 'w') as file: - file.write(code) - - child = pexpect.spawn(f"/bin/bash {TEMP_BASH_FILE}", encoding="utf-8") - - while True: - try: - index = child.expect(['\n', pexpect.EOF, pexpect.TIMEOUT], timeout=1) - if index == 0: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - elif index == 1: - coded_text = translate_terminal_colors(child.before) - await websocket.send(coded_text) - break - except pexpect.exceptions.TIMEOUT: - break - os.remove(TEMP_BASH_FILE) - -async def shell(websocket, path): - try: - async for code in websocket: - await execute_shell(code, websocket) - except websockets.exceptions.ConnectionClosedOK: - pass -#### END -async def execute_code(code, websocket): - with open(TEMP_PYTHON_FILE, 'w') as file: - file.write(code) - - child = pexpect.spawn(f"python3 {TEMP_PYTHON_FILE}", encoding="utf-8") - - while True: - try: - index = child.expect(['\n', pexpect.EOF, pexpect.TIMEOUT], timeout=1) - if index == 0: - await websocket.send(child.before) - elif index == 1: - await websocket.send(child.before) - break - except pexpect.exceptions.TIMEOUT: - break - os.remove(TEMP_PYTHON_FILE) - -async def server(websocket, path): - try: - async for code in websocket: - await execute_code(code, websocket) - except websockets.exceptions.ConnectionClosedOK: - pass - - -ws_server = websockets.serve(server, WS_HOST, WS_PORT) -ws_shell = websockets.serve(shell, WS_HOST, ws_port2) -ws_node = websockets.serve(NODE, WS_HOST, ws_port3) -ws_cpp = websockets.serve(CPP, WS_HOST, ws_port4) -ws_go = websockets.serve(GO, WS_HOST, ws_port5) - - -print("Server started at port", WS_PORT) -print("Shell started at port", ws_port2) -print("Node started at port", ws_port3) -print("C++ started at port", ws_port4) -print("Go started at port", ws_port5) -asyncio.get_event_loop().run_until_complete(ws_go) -asyncio.get_event_loop().run_until_complete(ws_shell) -asyncio.get_event_loop().run_until_complete(ws_server) -asyncio.get_event_loop().run_until_complete(ws_node) -asyncio.get_event_loop().run_until_complete(ws_cpp) -asyncio.get_event_loop().run_forever() \ No newline at end of file diff --git a/source.txt b/source.txt new file mode 100644 index 0000000..f36556a --- /dev/null +++ b/source.txt @@ -0,0 +1,2 @@ +:py +print("fuckme") \ No newline at end of file diff --git a/tmp/build-errors.log b/tmp/build-errors.log index 09a09eb..d4bc9c7 100644 --- a/tmp/build-errors.log +++ b/tmp/build-errors.log @@ -1 +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 \ No newline at end of file +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 1exit status 1 \ No newline at end of file diff --git a/tmp/main.exe b/tmp/main.exe old mode 100644 new mode 100755 index b704648..00c1cf8 Binary files a/tmp/main.exe and b/tmp/main.exe differ