Skip to content

set password via webrepl_cli on first connect #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 53 additions & 11 deletions webrepl_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import os
import struct
import time
try:
import usocket as socket
except ImportError:
Expand Down Expand Up @@ -88,13 +89,39 @@ def ioctl(self, req, val):
assert req == 9 and val == 2


class FirstTimeReset(Exception):
"""After logging in the first time, we need to re-connect because of
reset."""
pass


def login(ws, passwd):
"""Login or setup password."""
msg = b""
while True:
c = ws.read(1, text_ok=True)
msg = msg + c
if c == b":":
assert ws.read(1, text_ok=True) == b" "
break
ws.write(passwd.encode("utf-8") + b"\r")
assert ws.read(1, text_ok=True) == b" ", "received invalid char"
ws.write(passwd.encode("utf-8") + b"\r")

if msg.endswith("New password:"):
print("This is the first time you connect to WebREPL, setting"
" password permanently...")
elif msg.endswith("Confirm password:"):
msg = ''
while True:
msg += ws.read(1, text_ok=True)
if msg.endswith('Password successfully set, '
'restarting...\r\n'):
break
raise FirstTimeReset("please reconnect and login again")
elif msg.endswith("Password:"):
break
else:
print("msg={}".format(msg))
raise RuntimeError("invalid WebREPL protocol")


def read_resp(ws):
data = ws.read(4)
Expand Down Expand Up @@ -190,6 +217,15 @@ def parse_remote(remote):
return (host, port, fname)


def create_websocket(addr):
s = socket.socket()
s.connect(addr)
#s = s.makefile("rwb")
websocket_helper.client_handshake(s)
ws = websocket(s)
return ws


def main():

if len(sys.argv) != 3:
Expand Down Expand Up @@ -219,20 +255,26 @@ def main():
print(op, host, port)
print(src_file, "->", dst_file)

s = socket.socket()

ai = socket.getaddrinfo(host, port)
addr = ai[0][4]

s.connect(addr)
#s = s.makefile("rwb")
websocket_helper.client_handshake(s)

ws = websocket(s)
ws = create_websocket(addr)

import getpass
passwd = getpass.getpass()
login(ws, passwd)
try:
login(ws, passwd)
except FirstTimeReset:
# reconnecting and relogin after setting password
ws.s.close()

# avoid race condition: if too fast here, we get again into
# the password setup dialog
time.sleep(2)
ws = create_websocket(addr)
login(ws, passwd)

print("Remote WebREPL version:", get_ver(ws))

# Set websocket to send data marked as "binary"
Expand All @@ -243,7 +285,7 @@ def main():
elif op == "put":
put_file(ws, src_file, dst_file)

s.close()
ws.s.close()


if __name__ == "__main__":
Expand Down