Skip to content

Commit 49ebe59

Browse files
Combined device serial id
1 parent 7424b31 commit 49ebe59

File tree

6 files changed

+138
-182
lines changed

6 files changed

+138
-182
lines changed

helpers/adb_device_serial_id.py

Lines changed: 0 additions & 95 deletions
This file was deleted.

helpers/device_serial_id.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import os
2+
import platform
3+
import subprocess as sp
4+
5+
from custom_ci import custom_input, custom_print
6+
7+
8+
def init(mode, tcp_ip='', tcp_port=''):
9+
custom_print(
10+
f'>>> I am in device_serial_id.init({mode=!s}, {tcp_ip=!s}, {tcp_port=!s})', is_print=False)
11+
# Detect OS
12+
is_windows = False
13+
is_linux = False
14+
if platform.system() == 'Windows':
15+
is_windows = True
16+
if platform.system() == 'Linux':
17+
is_linux = True
18+
19+
# Global command line helpers
20+
curr_dir = os.path.dirname(os.path.realpath(__file__))
21+
root_dir = os.path.abspath(os.path.join(curr_dir, '..'))
22+
23+
if(is_windows):
24+
adb = f'{root_dir}\\bin\\adb.exe'
25+
else:
26+
adb = 'adb'
27+
28+
# Kill server before getting list to avoid daemon texts.
29+
os.system(f'{adb} kill-server')
30+
os.system(f'{adb} start-server')
31+
32+
combo = f'{tcp_ip}:{tcp_port}'
33+
cmd = ''
34+
if mode == 'USB':
35+
cmd = f'{adb} devices'
36+
elif mode == 'TCP':
37+
cmd = f'{adb} connect {combo}'
38+
else:
39+
pass
40+
# FIXME: Wrong choice.
41+
proc = sp.Popen(cmd.split(), stdin=sp.PIPE, stdout=sp.PIPE,
42+
stderr=sp.PIPE, shell=False)
43+
output, error = proc.communicate()
44+
output = output.decode('utf-8')
45+
error = error.decode('utf-8')
46+
47+
if len(output) == 0 or error:
48+
output = None
49+
custom_print(error, 'red')
50+
kill_me()
51+
52+
if mode == 'USB':
53+
output = [x.strip() for x in output.split('\n') if len(x.strip()) > 0]
54+
55+
if(len(output) == 1):
56+
custom_print(
57+
'Could not find any connected device. Is USB Debugging on?', 'red')
58+
return ''
59+
60+
device_to_connect = None
61+
i = 1
62+
if(len(output) == 2):
63+
if(output[1].split()[1] == 'offline'):
64+
custom_print(
65+
'Device is offline, try turning off USB debugging and turn on again.', 'yellow')
66+
kill_me()
67+
if(output[1].split()[1] == 'unauthorized'):
68+
custom_print(
69+
'Device unauthorized. Please check the confirmation dialog on your device.', 'red')
70+
kill_me()
71+
return output[1].split()[0]
72+
73+
custom_print(output[0])
74+
custom_print('\n', is_get_time=False)
75+
if device_to_connect is None:
76+
for index, device in enumerate(output[1:]):
77+
name = f'{adb} -s {device.split()[0]} shell getprop ro.product.model'
78+
custom_print(
79+
f'{index}. {device.split()[0]} {device.split()[1]} {sp.getoutput(name).strip()}')
80+
81+
while device_to_connect is None:
82+
device_index = int(custom_input(
83+
'Enter device number (for ex: 2): '))
84+
if device_index <= 0 or device_index + 1 > len(output):
85+
continue
86+
device_to_connect = output[device_index]
87+
88+
if(device_to_connect.split()[1] == 'offline'):
89+
custom_print(
90+
'Device is offline, try turning off USB debugging and turn on again.', 'yellow')
91+
kill_me()
92+
if(device_to_connect.split()[1] == 'unauthorized'):
93+
custom_print(
94+
'Device unauthorized. Please check the confirmation dialog on your device.', 'red')
95+
kill_me()
96+
return device_to_connect.split()[0]
97+
98+
elif mode == 'TCP':
99+
output = [x.strip() for x in output.split() if len(x.strip()) > 0]
100+
if('connected' in (x.lower() for x in output)):
101+
return combo
102+
if('authenticate' in (x.lower() for x in output)):
103+
custom_print(
104+
'Device unauthorized. Please check the confirmation dialog on your device.', 'red')
105+
kill_me()
106+
if('refused' in (x.lower() for x in output)):
107+
custom_print(
108+
'Could not find any connected device. Either USB Debugging is off or device is not running ADB over TCP', 'red')
109+
return ''
110+
''' Possible outputs
111+
['connected', 'to', '192.168.43.130:5555']
112+
['failed', 'to', 'authenticate', 'to', '192.168.43.130:5555']
113+
['cannot', 'connect', 'to', '192.168.43.130:5555:', 'No', 'connection', 'could', 'be', 'made', 'because', 'the', 'target', 'machine', 'actively', 'refused', 'it.', '(10061)']
114+
'''
115+
else:
116+
pass
117+
# FIXME: Wrong choice.
118+
119+
120+
def kill_me():
121+
custom_print(
122+
'>>> I am in device_serial_id.kill_me()', is_print=False)
123+
custom_print('\n', is_get_time=False)
124+
custom_print('Exiting...')
125+
custom_print(
126+
'Turn off USB debugging [and USB debugging (Security Settings)] if you\'re done.', 'cyan')
127+
custom_input('Hit \"Enter\" key to continue....', 'cyan')
128+
quit()

helpers/handler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from curses.ascii import isprint
21
import os
32
import re
43
import shutil

helpers/tcp_device_serial_id.py

Lines changed: 0 additions & 70 deletions
This file was deleted.

restore_whatsapp.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import platform
55
import subprocess
66

7-
import helpers.adb_device_serial_id as adb_device_id
8-
import helpers.tcp_device_serial_id as tcp_device_id
7+
import helpers.device_serial_id as device_id
98
from helpers.custom_ci import custom_input, custom_print
109

1110
# Detect OS
@@ -83,19 +82,17 @@ def show_banner():
8382
parser = argparse.ArgumentParser()
8483
parser.add_argument('-tip',
8584
'--tcp-ip', help='Connects to a remote device via TCP mode.')
86-
parser.add_argument('-tp',
87-
'--tcp-port', help='Port number to connect to. Default: 5555')
85+
parser.add_argument('-tp', '--tcp-port', default='5555',
86+
help='Port number to connect to. Default: 5555')
8887
args = parser.parse_args()
89-
# args = parser.parse_args('--tcp-ip 192.168.43.130'.split())
88+
# args = parser.parse_args('--tcp-ip 192.168.168.117'.split())
9089

9190
tcp_ip = args.tcp_ip
9291
tcp_port = args.tcp_port
9392
if(tcp_ip):
94-
if(not tcp_port):
95-
tcp_port = '5555'
96-
adb_device_serial_id = tcp_device_id.init(tcp_ip, tcp_port)
93+
adb_device_serial_id = device_id.init('TCP', tcp_ip, tcp_port)
9794
else:
98-
adb_device_serial_id = adb_device_id.init()
95+
adb_device_serial_id = device_id.init('USB')
9996
if(not adb_device_serial_id):
10097
kill_me()
10198

wa_kdbe.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
import subprocess
2828
import time
2929

30-
import helpers.adb_device_serial_id as adb_device_id
31-
import helpers.tcp_device_serial_id as tcp_device_id
30+
import helpers.device_serial_id as device_id
3231
from helpers.custom_ci import custom_input, custom_print
3332
from helpers.handler import handler
3433
from view_extract import extract_ab
@@ -411,7 +410,7 @@ def usb_mode():
411410
help='Allow reboot of device before installation of LegacyWhatsApp.apk to prevent some issues like [INSTALL_FAILED_VERSION_DOWNGRADE]')
412411
parser.add_argument('-tip', '--tcp-ip',
413412
help='Connects to a remote device via TCP mode.')
414-
parser.add_argument('-tp', '--tcp-port',
413+
parser.add_argument('-tp', '--tcp-port', default='5555',
415414
help='Port number to connect to. Default: 5555')
416415
parser.add_argument('-s', '--scrcpy', action='store_true',
417416
help='Run ScrCpy to see and control Android device.')
@@ -426,11 +425,9 @@ def usb_mode():
426425
is_scrcpy = args.scrcpy
427426
is_tar_only = args.tar_only
428427
if(tcp_ip):
429-
if(not tcp_port):
430-
tcp_port = '5555'
431-
adb_device_serial_id = tcp_device_id.init(tcp_ip, tcp_port)
428+
adb_device_serial_id = device_id.init('TCP', tcp_ip, tcp_port)
432429
else:
433-
adb_device_serial_id = adb_device_id.init()
430+
adb_device_serial_id = device_id.init('USB')
434431
if(not adb_device_serial_id):
435432
kill_me()
436433

0 commit comments

Comments
 (0)