-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
119 lines (96 loc) · 3.62 KB
/
main.cpp
File metadata and controls
119 lines (96 loc) · 3.62 KB
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
// avahi-browse regex: ^= (.*?) (ipv4) (.*?) *(_adb-tls-connect._tcp) local$\n hostname = \[(.*?)\]$\n address = \[(.*?)\]\n port = \[[0-9]*\]$\n txt = \[(.*?)\]$
#include <cstdlib>
#include <iostream>
#include <regex>
#include <string>
#include <array>
const std::string protocol = "_adb-tls-connect._tcp";
struct Device{
std::string network_device;
bool ipv; // 0 = ipv4, 1 = ipv6
std::string serial;
std::string avahi_protocol;
std::string scope;
std::string hostname;
std::string address;
std::string port;
std::string txt;
};
int test_cmd(const char* cmd){
std::string test = "which " + std::string(cmd) + " > /dev/null 2>&1";
return system(test.c_str());
}
// https://stackoverflow.com/a/478960
std::string exec(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), static_cast<int>(buffer.size()), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
int main(int argc, char** argv){
if(test_cmd("avahi-browse") != 0){
std::cerr << "avahi-browse not found" << std::endl;
return 1;
}
if(test_cmd("adb") != 0){
std::cerr << "adb not found" << std::endl;
return 1;
}
if(test_cmd("dig") != 0){
std::cerr << "dig not found" << std::endl;
return 1;
}
std::string avahi_cmd = "avahi-browse -r -t " + protocol;
std::string avahi_out = exec(avahi_cmd.c_str());
if(avahi_out.empty()){
std::cerr << "No devices found" << std::endl;
return 1;
}
std::regex regex(R"(^= ([^\s]+) ([^\s]+) ([^\s]+) *([^\s]+) ([^\s]+)\n *hostname = \[([^\s]+)\]\n *address = \[([^\s]+)\]\n *port = \[([^\s]+)\]\n *txt = \[\"([^\s]+)\"\]\n)", std::regex_constants::ECMAScript | std::regex_constants::icase | std::regex_constants::multiline);
std::smatch matches;
bool result = std::regex_search(avahi_out, matches, regex);
if(!result){
std::cerr << "Sum fucky happened with the regex" << std::endl;
return 1;
}
Device device;
device.network_device = matches[1];
device.ipv = matches[2] == "IPv4";
device.serial = matches[3].str().substr(matches[3].str().find('-') + 1, 16);
device.avahi_protocol = matches[4];
device.scope = matches[5];
device.hostname = matches[6];
device.address = matches[7];
device.port = matches[8];
device.txt = matches[9];
bool mdns = false;
if(device.scope == "local"){
// make reverse dns lookup using dig
std::string dig_command = "dig -x " + device.address + " +short";
std::string dig_out = exec(dig_command.c_str());
if(dig_out.empty()){
std::cerr << "Reverse dns lookup failed" << std::endl;
} else {
char c = dig_out[dig_out.length() - 2];
if(c == '.'){
dig_out = dig_out.substr(0, dig_out.length() - 2);
}
device.hostname = dig_out;
mdns = true;
}
}
int adb_result = system(("adb connect " + (mdns ? device.hostname : device.address) + ":" + device.port + " > /dev/null 2>&1").c_str());
if(adb_result != 0){
std::cerr << "Failed to connect to device" << std::endl;
return 1;
}
std::string friendly_name = mdns ? "" + device.hostname + " [" + device.serial + "]" : device.address + " [" + device.serial + "]";
std::cout << "Connected to " << friendly_name << " on port " << device.port << std::endl;
return 0;
}