-
Notifications
You must be signed in to change notification settings - Fork 5
/
connect.hpp
359 lines (288 loc) · 8.24 KB
/
connect.hpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <sys/types.h>
#include <string.h>
#ifdef WIN32
#include <winsock.h>
#include <ws2tcpip.h>
#else
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <net/if.h>
#include <netdb.h>
#endif
#include "QR_Encode.h"
string format_ip(uint32_t ip)
{
struct in_addr a;
a.s_addr = ip;
return inet_ntoa(a);
}
bool get_ip_with_inet(uint32_t *ip)
{
int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct sockaddr_in serv = { 0 };
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr("8.8.8.8");
serv.sin_port = htons(53);
if (connect(sock, (const sockaddr*) &serv, sizeof(serv)))
return false;
sockaddr_in name;
socklen_t namelen = sizeof(name);
if (getsockname(sock, (sockaddr*) &name, &namelen))
return false;
*ip = name.sin_addr.s_addr;
#ifdef WIN32
closesocket(sock);
#else
close(sock);
#endif
return true;
}
bool get_all_ips(vector<uint32_t> &ips)
{
#ifdef WIN32
char szBuffer[1024];
if(gethostname(szBuffer, sizeof(szBuffer)) == -1)
return false;
struct hostent *host = gethostbyname(szBuffer);
if(!host)
return false;
bool found = false;
for (int i = 0; host->h_addr_list[i]; i++)
{
uint32_t ip = ((struct in_addr*)host->h_addr_list[i])->s_addr;
if ((ip & htonl(0xff000000)) != htonl(0x7f000000))
{
ips.push_back(ip);
found = true;
}
}
return found;
#else
struct ifaddrs *ifas;
if (getifaddrs (&ifas))
return false;
bool found = false;
for (struct ifaddrs *ifa = ifas; ifa; ifa = ifa->ifa_next)
{
if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET)
continue;
if ((ifa->ifa_flags & IFF_LOOPBACK) || !(ifa->ifa_flags & IFF_RUNNING))
continue;
uint32_t ip = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
ips.push_back(ip);
found = true;
}
freeifaddrs (ifas);
return true;
#endif
}
bool get_private_ip_list(color_ostream &out, vector<uint32_t> &ips, bool debug)
{
// First try finding address of an interface with a route to Internet (8.8.8.8)
uint32_t ip;
if (get_ip_with_inet(&ip))
{
if (debug)
out << "Found IP with Internet route: " << format_ip(ip) << std::endl;
ips.push_back(ip);
return true;
}
// If that fails, get addresses for all network adapters, and send them all to the app
if (get_all_ips(ips))
{
if (debug)
{
out << "Found all IPs:";
for (auto it = ips.cbegin(); it < ips.cend();it++)
out << " " << format_ip(*it);
out << std::endl;
}
return true;
}
return false;
}
bool get_public_ip(uint32_t *ip, int *port)
{
ENetAddress mediation_address;
enet_address_set_host (&mediation_address, "dfmed.mifki.com");
mediation_address.port = 1233;
ip_check_done = false;
ip_check_peer = enet_host_connect (server, &mediation_address, 2, 0);
if (!ip_check_peer)
return false;
// Wait for the network thread to do the job
enet_uint32 start = enet_time_get();
do {
#ifdef WIN32
Sleep (200); // 200 milliseconds
#else
usleep(200*1000); // 200 milliseconds in microseconds
#endif
} while (!ip_check_done && enet_time_get() - start < 5000);
//TODO: call enet_peer_disconnect() ? enet_peer_disconnect_now() ?
enet_peer_reset(ip_check_peer);
ip_check_peer = NULL;
if (ip_check_done)
{
*ip = ext_addr.host;
*port = ext_addr.port;
return true;
}
return false;
}
void ensure_publish_details(bool debug, bool randomize)
{
// Generate name and password
// Don't generate password if there was a name, i.e. user deliberately didn't set password
#ifdef arc4random
#define RND arc4random
#else
srand(time(NULL));
#define RND rand
#endif
if (randomize || !publish_name.size())
{
string s = "";
for (int j = 0; j < 3; j++)
{
for (int i = 0; i < 4; i++)
s += 'a' + (RND()%26);
if (j < 2)
s += '-';
}
publish_name = s;
save_config();
if (randomize || !pwd_hash.size())
{
string s = "";
for (int i = 0; i < 16; i++)
s += 1 + (RND()%255);
pwd_hash = hash_password(s);
save_config();
}
}
}
void output_qrcode(color_ostream &out, uint8_t *data, int width)
{
#ifdef WIN32
// On Windows, setting color passes the value directly to SetConsoleTextAttribute, which can set bg color too
#define WHITE (color_value)(BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE) << " "
#define BLACK (color_value)0 << " "
#else
#define WHITE "\033[47m \033[0m"
#define BLACK "\033[40m \033[0m"
#endif
for (int x = 0; x < width+2; x++)
out << WHITE;
out << COLOR_RESET << std::endl;
for (int y = 0; y < width; y++) {
out << WHITE;
for (int x = 0; x < width; x++) {
int byte = (x * width + y) / 8;
int bit = (x * width + y) % 8;
int value = data[byte] & (0x80 >> bit);
if (value)
out << BLACK;
else
out << WHITE;
}
out << WHITE;
out << COLOR_RESET << std::endl;
}
for (int x = 0; x < width+2; x++)
out << WHITE;
out << COLOR_RESET << std::endl;
}
void show_qrcode_with_data(color_ostream &out, uint8_t *rawdata, int rawsz)
{
// Convert binary to numeric as built-in iOS QR Code decoding can return strings only
char *buf = new char[rawsz*3];
for (int i = 0; i < rawsz; i++)
sprintf(buf+i*3, "%03d", rawdata[i]);
//out << buf << std::endl;
uint8_t data[MAX_BITDATA];
int width = EncodeData(QR_LEVEL_L, 0, buf, 0, data);
delete[] buf;
output_qrcode(out, data, width);
}
void remote_connect(color_ostream &out, bool debug, bool no_external, bool no_publish, bool randomize, bool firewall)
{
#ifdef WIN32
check_open_firewall(&out, enet_port);
#endif
if (firewall)
return;
if (!remote_start())
{
out << COLOR_RED << "Error starting Remote server, can not proceed" << std::endl;
out << COLOR_RESET;
return;
}
vector<uint32_t> ips;
get_private_ip_list(out, ips, debug);
//TODO: check error and don't proceed if no ips
//TODO: show warning if > 7 ips
//TODO: check public ip only if private ip is from inet route
bool publish = !no_publish;
if (!no_external && publish_name.empty())
{
uint32_t pub_ip;
int pub_port;
bool pub_ok = get_public_ip(&pub_ip, &pub_port);
if (enet_port == pub_port && std::find(ips.begin(), ips.end(), pub_ip) != ips.end())
{
out << "Computer seems to have an externally accessible IP " << format_ip(pub_ip) << std::endl;
out << "therefore server will not be published; use `remote connect -no-external` to change this." << std::endl;
ips.clear();
ips.push_back(pub_ip);
publish = false;
}
else if (debug)
{
out << "External address " << format_ip(pub_ip) << ":" << pub_port << " does not match private IPs and port " << enet_port << std::endl;
}
}
if (publish)
ensure_publish_details(debug, randomize);
bool has_pwd = !pwd_hash.empty();
// Status byte + IPs + port + password (if any) + published name
int rawsz = 1 + ips.size()*4 + 2 + (publish ? ((has_pwd ? 32 : 0) + publish_name.length()) : 0);
uint8_t *rawdata = new uint8_t[rawsz];
uint8_t *rawptr = rawdata;
// 1. Status byte - flags & number of IPs
*rawptr++ = (publish << 4) | (has_pwd << 3) | (ips.size() & 3);
// 2. List of IPs
for (int i = 0; i < ips.size() & 3; i++)
{
*(uint32_t*)rawptr = ips[i];
rawptr += 4;
}
// 3. Port
*(uint16_t*)rawptr = enet_port;
rawptr += 2;
if (publish)
{
// 4. Password hash
if (has_pwd)
{
for (int i = 0; i < 32; i++)
sscanf(pwd_hash.c_str()+i*2, "%02x", rawptr++);
}
// 5. Published name
memcpy(rawptr, publish_name.c_str(), publish_name.length());
rawptr += publish_name.length();
if (debug)
out << "Publishing server with name " << publish_name << " and password hash " << pwd_hash << std::endl;
}
out << COLOR_LIGHTGREEN << "Scan the QR code below with Dwarf Fortress Remote iOS app to connect to this server" << std::endl;
show_qrcode_with_data(out, rawdata, rawsz);
delete[] rawdata;
out << COLOR_LIGHTGREEN << "Scan the QR code above with Dwarf Fortress Remote iOS app to connect to this server" << std::endl;
// So that any messages from another thread during connection don't interrupt QR code output
if (publish)
remote_publish(publish_name);
out << "If you like the game, consider supporting Dwarf Fortress authors, Tarn and Zach Adams. Visit bay12games.com/support.html" << std::endl;
}