-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
webrepl: Implement file transfer protocol in python.
Signed-off-by: Felix Dörre <[email protected]>
- Loading branch information
1 parent
47e1338
commit 772d240
Showing
3 changed files
with
110 additions
and
33 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,53 @@ | ||
class LegacyFileTransfer: | ||
def __init__(self): | ||
self.opbuf = bytearray(82) | ||
self.opptr = 0 | ||
self.op = 0 | ||
|
||
def handle(self, buf, sock): | ||
if self.op == 2: | ||
import struct | ||
|
||
ret = self.file.readinto(memoryview(self.filebuf)[2:]) | ||
memoryview(self.filebuf)[0:2] = struct.pack("<h", ret) | ||
sock.ioctl(9, 2) | ||
sock.write(memoryview(self.filebuf)[0 : (2 + ret)]) | ||
if ret == 0: | ||
sock.write(b"WB\x00\x00") | ||
self.op = 0 | ||
self.filebuf = None | ||
sock.ioctl(9, 1) | ||
return | ||
self.opbuf[self.opptr] = buf[0] | ||
self.opptr += 1 | ||
if self.opptr != 82: # or bytes(buf[0:2]) != b"WA": | ||
return | ||
self.opptr = 0 | ||
sock.ioctl(9, 2) | ||
sock.write(b"WB\x00\x00") | ||
sock.ioctl(9, 1) | ||
type = self.opbuf[2] | ||
if type == 2: # GET_FILE | ||
self.op = type | ||
name = self.opbuf[18:82].rstrip(b"\x00") | ||
self.filebuf = bytearray(2 + 256) | ||
self.file = open(name.decode(), "rb") | ||
elif type == 1: # PUT_FILE | ||
import struct | ||
|
||
name = self.opbuf[18:82].rstrip(b"\x00") | ||
size = struct.unpack("<I", self.opbuf[12:16])[0] | ||
filebuf = bytearray(512) | ||
with open(name.decode(), "wb") as file: | ||
while size > 0: | ||
ret = sock.readinto(filebuf) | ||
if ret is None: | ||
continue | ||
if ret > 0: | ||
file.write(memoryview(filebuf)[0:ret]) | ||
size -= ret | ||
elif ret < 0: | ||
break | ||
sock.ioctl(9, 2) | ||
sock.write(b"WB\x00\x00") | ||
sock.ioctl(9, 1) |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
metadata(description="WebREPL server.", version="1.0.0") | ||
|
||
module("webrepl.py", opt=3) | ||
module("legacy_file_transfer.py", opt=3) | ||
module("webrepl_setup.py", opt=3) |
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