begin work on "test client"
This commit is contained in:
parent
66268e6702
commit
0a563d8b02
9 changed files with 113 additions and 279 deletions
|
@ -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 = []
|
||||
|
|
100
cmd/client/main.go
Normal file
100
cmd/client/main.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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) {
|
268
source.py
268
source.py
|
@ -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'<color={color_mapping[color_code]}>'
|
||||
else:
|
||||
translated_code += f'<color={color_code}>'
|
||||
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()
|
2
source.txt
Normal file
2
source.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
:py
|
||||
print("fuckme")
|
|
@ -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
|
||||
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
|
BIN
tmp/main.exe
Normal file → Executable file
BIN
tmp/main.exe
Normal file → Executable file
Binary file not shown.
Loading…
Reference in a new issue