forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoAttachUtils.cpp
More file actions
151 lines (134 loc) · 3.81 KB
/
AutoAttachUtils.cpp
File metadata and controls
151 lines (134 loc) · 3.81 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "AutoAttachUtils.h"
#ifdef _WINDOWS
namespace facebook {
namespace hermes {
namespace inspector {
namespace chrome {
bool isNetworkInspected(
const std::string &,
const std::string &,
const std::string &) {
return false;
}
} // namespace chrome
} // namespace inspector
} // namespace hermes
} // namespace facebook
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <folly/String.h>
namespace facebook {
namespace hermes {
namespace inspector {
namespace chrome {
// The following code is copied from
// https://phabricator.intern.facebook.com/diffusion/FBS/browse/master/xplat/js/react-native-github/ReactCommon/cxxreact/JSCExecutor.cpp;431c4d01b7072d9a1a52f8bd6c6ba2ff3e47e25d$250
bool isNetworkInspected(
const std::string &owner,
const std::string &app,
const std::string &device) {
auto connect_socket = [](int socket_desc, std::string address, int port) {
if (socket_desc < 0) {
close(socket_desc);
return false;
}
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
auto sock_opt_rcv_resp = setsockopt(
socket_desc,
SOL_SOCKET,
SO_RCVTIMEO,
(const char *)&tv,
sizeof(struct timeval));
if (sock_opt_rcv_resp < 0) {
close(socket_desc);
return false;
}
auto sock_opt_snd_resp = setsockopt(
socket_desc,
SOL_SOCKET,
SO_SNDTIMEO,
(const char *)&tv,
sizeof(struct timeval));
if (sock_opt_snd_resp < 0) {
close(socket_desc);
return false;
}
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr(address.c_str());
server.sin_family = AF_INET;
server.sin_port = htons(port);
auto connect_resp =
::connect(socket_desc, (struct sockaddr *)&server, sizeof(server));
if (connect_resp < 0) {
::close(socket_desc);
return false;
}
return true;
};
int socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (!connect_socket(socket_desc, "127.0.0.1", 8082)) {
#if defined(__ANDROID__)
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (!connect_socket(socket_desc, "10.0.2.2", 8082) /* emulator */) {
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (!connect_socket(socket_desc, "10.0.3.2", 8082) /* genymotion */) {
return false;
}
}
#else //! defined(__ANDROID__)
return false;
#endif // defined(__ANDROID__)
}
std::string escapedOwner =
folly::uriEscape<std::string>(owner, folly::UriEscapeMode::QUERY);
std::string escapedApp =
folly::uriEscape<std::string>(app, folly::UriEscapeMode::QUERY);
std::string escapedDevice =
folly::uriEscape<std::string>(device, folly::UriEscapeMode::QUERY);
std::string msg = folly::to<std::string>(
"GET /autoattach?title=",
escapedOwner,
"&app=",
escapedApp,
"&device=",
escapedDevice,
" HTTP/1.1\r\n\r\n");
auto send_resp = ::send(socket_desc, msg.c_str(), msg.length(), 0);
if (send_resp < 0) {
close(socket_desc);
return false;
}
char server_reply[200];
server_reply[199] = '\0';
auto recv_resp =
::recv(socket_desc, server_reply, sizeof(server_reply) - 1, 0);
if (recv_resp < 0) {
close(socket_desc);
return false;
}
std::string response(server_reply);
if (response.size() < 25) {
close(socket_desc);
return false;
}
auto responseCandidate = response.substr(response.size() - 25);
auto found =
responseCandidate.find("{\"autoattach\":true}") != std::string::npos;
close(socket_desc);
return found;
}
} // namespace chrome
} // namespace inspector
} // namespace hermes
} // namespace facebook
#endif