forked from HappyZ/dpt-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpt-tools.py
121 lines (107 loc) · 3.24 KB
/
dpt-tools.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3
# builtins
import argparse
# lib
from python_api.libDPT import DPT
from python_api.libInteractive import diagnosis_mode
from python_api.libInteractive import update_firmware
def print_info():
print("""===========
DPT Tools
===========
Thanks for using DPT Tools. Type `help` to show this message.
Supported commands:
fw -- update firmware
diagnosis -- enter diagnosis mode (to gain adb, su, etc.)
exit/quit -- leave the tool
""")
def interactive(dpt, diagnosis=False):
'''
interactive shell to run commands
@param dpt: DPT object
@param diagnosis: if set True, will directly enter diagnosis mode
'''
firstTime = True
if diagnosis:
diagnosis_mode(dpt)
return
while(1):
if firstTime:
print_info()
firstTime = False
try:
cmd = input(">>> ")
cmd = cmd.lower() # convert to lower case
except KeyboardInterrupt:
print()
dpt.info_print("Press Ctrl + D to exit")
continue
except EOFError:
print()
dpt.info_print("Exiting... Thanks for using...")
break
except BaseException as e:
print()
cmd = ''
dpt.err_print(str(e))
# reauthenticate after every command
if not dpt.reauthenticate():
dpt.err_print("Cannot reauthenticate, did you reboot into normal mode?")
dpt.err_print("Client id filepath: {}".format(dpt.client_id_fp))
dpt.err_print("Client key filepath: {}".format(dpt.key_fp))
break
if cmd == 'exit' or cmd == 'quit':
dpt.info_print("Exiting... Thanks for using...")
break
elif cmd == 'fw':
update_firmware(dpt)
elif cmd == 'help' or cmd == 'h':
print_info()
elif cmd == 'diagnosis':
diagnosis_mode(dpt)
def main():
'''
main func to initalize dpt object
'''
p = argparse.ArgumentParser(description="DPT Tools")
p.add_argument(
'--client-id', '-id',
dest="dpt_id",
default="",
help="File containing the device's client id")
p.add_argument(
'--key', '-k',
dest="dpt_key",
default="",
help="File containing the device's private key")
p.add_argument(
'--addr', '-ip',
dest="dpt_addr",
default=None,
help="Hostname or IP address of the device")
p.add_argument(
'--diagnosis',
action='store_true',
help="Run diagnosis mode directly")
p.add_argument(
'--debug', '-d',
action='store_true',
help="Run with debugging mode")
try:
args = vars(p.parse_args())
except Exception as e:
print(e)
sys.exit()
dpt = DPT(args.get('dpt_addr', None), args.get('debug', False))
if (
not args.get('diagnosis', False) and
not dpt.authenticate(args.get('dpt_id', ""), args.get('dpt_key', ""))
):
dpt.err_print(
"Cannot authenticate. " +
"Make sure your id, key, and ip addresses are correct."
)
exit(1)
interactive(dpt, diagnosis=args.get('diagnosis', False))
if __name__ == '__main__':
main()