Add automatic detection of WAN IP address

This commit is contained in:
Cameron Gutman 2018-11-17 11:27:59 -08:00
parent cb80160605
commit 4613d4977c
4 changed files with 53 additions and 1 deletions

View File

@ -10,6 +10,8 @@
#include "ppapi/cpp/input_event.h" #include "ppapi/cpp/input_event.h"
#include <netinet/in.h> #include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
// Requests the NaCl module to connection to the server specified after the : // Requests the NaCl module to connection to the server specified after the :
#define MSG_START_REQUEST "startRequest" #define MSG_START_REQUEST "startRequest"
@ -162,6 +164,8 @@ void MoonlightInstance::HandleMessage(const pp::Var& var_message) {
MakeCert(callbackId, params); MakeCert(callbackId, params);
} else if (strcmp(method.c_str(), "pair") == 0) { } else if (strcmp(method.c_str(), "pair") == 0) {
HandlePair(callbackId, params); HandlePair(callbackId, params);
} else if (strcmp(method.c_str(), "STUN") == 0) {
HandleSTUN(callbackId, params);
} else { } else {
pp::Var response("Unhandled message received: " + method); pp::Var response("Unhandled message received: " + method);
PostMessage(response); PostMessage(response);
@ -277,6 +281,29 @@ void MoonlightInstance::PairCallback(int32_t /*result*/, int32_t callbackId, pp:
PostMessage(ret); PostMessage(ret);
} }
void MoonlightInstance::HandleSTUN(int32_t callbackId, pp::VarArray args) {
m_HttpThreadPool[m_HttpThreadPoolSequence++ % HTTP_HANDLER_THREADS]->message_loop().PostWork(
m_CallbackFactory.NewCallback(&MoonlightInstance::STUNCallback, callbackId, args));
}
void MoonlightInstance::STUNCallback(int32_t /*result*/, int32_t callbackId, pp::VarArray args) {
unsigned int wanAddr;
char addrStr[128] = {};
pp::VarDictionary ret;
ret.Set("callbackId", pp::Var(callbackId));
ret.Set("type", pp::Var("resolve"));
if (LiFindExternalAddressIP4("stun.stunprotocol.org", 3478, &wanAddr) == 0) {
inet_ntop(AF_INET, &wanAddr, addrStr, sizeof(addrStr));
ret.Set("ret", pp::Var(addrStr));
} else {
ret.Set("ret", pp::Var());
}
PostMessage(ret);
}
bool MoonlightInstance::Init(uint32_t argc, bool MoonlightInstance::Init(uint32_t argc,
const char* argn[], const char* argn[],
const char* argv[]) { const char* argv[]) {

View File

@ -97,7 +97,9 @@ class MoonlightInstance : public pp::Instance, public pp::MouseLock {
void HandleStartStream(int32_t callbackId, pp::VarArray args); void HandleStartStream(int32_t callbackId, pp::VarArray args);
void HandleStopStream(int32_t callbackId, pp::VarArray args); void HandleStopStream(int32_t callbackId, pp::VarArray args);
void HandleOpenURL(int32_t callbackId, pp::VarArray args); void HandleOpenURL(int32_t callbackId, pp::VarArray args);
void HandleSTUN(int32_t callbackId, pp::VarArray args);
void PairCallback(int32_t /*result*/, int32_t callbackId, pp::VarArray args); void PairCallback(int32_t /*result*/, int32_t callbackId, pp::VarArray args);
void STUNCallback(int32_t /*result*/, int32_t callbackId, pp::VarArray args);
bool HandleInputEvent(const pp::InputEvent& event); bool HandleInputEvent(const pp::InputEvent& event);
void ReportMouseMovement(); void ReportMouseMovement();

View File

@ -107,6 +107,7 @@ function restoreUiAfterNaClLoad() {
if (hosts[returneMdnsDiscoveredHost.serverUid] != null) { if (hosts[returneMdnsDiscoveredHost.serverUid] != null) {
// if we're seeing a host we've already seen before, update it for the current local IP. // if we're seeing a host we've already seen before, update it for the current local IP.
hosts[returneMdnsDiscoveredHost.serverUid].address = returneMdnsDiscoveredHost.address; hosts[returneMdnsDiscoveredHost.serverUid].address = returneMdnsDiscoveredHost.address;
hosts[returneMdnsDiscoveredHost.serverUid].updateExternalAddressIP4();
} else { } else {
// Host must be in the grid before starting background polling // Host must be in the grid before starting background polling
addHostToGrid(returneMdnsDiscoveredHost, true); addHostToGrid(returneMdnsDiscoveredHost, true);
@ -405,6 +406,9 @@ function addHostToGrid(host, ismDNSDiscovered) {
} }
$('#host-grid').append(outerDiv); $('#host-grid').append(outerDiv);
hosts[host.serverUid] = host; hosts[host.serverUid] = host;
if (ismDNSDiscovered) {
hosts[host.serverUid].updateExternalAddressIP4();
}
} }
function removeClicked(host) { function removeClicked(host) {

View File

@ -244,7 +244,14 @@ NvHTTP.prototype = {
this.serverMajorVersion = parseInt(this.appVersion.substring(0, 1), 10); this.serverMajorVersion = parseInt(this.appVersion.substring(0, 1), 10);
this.serverUid = $root.find('uniqueid').text().trim(); this.serverUid = $root.find('uniqueid').text().trim();
this.hostname = $root.find('hostname').text().trim(); this.hostname = $root.find('hostname').text().trim();
this.externalIP = $root.find('ExternalIP').text().trim();
var externIP = $root.find('ExternalIP').text().trim();
if (externIP) {
// New versions of GFE don't have this field, so don't overwrite
// the one we found via STUN
this.externalIP = externIP;
}
try { // these aren't critical for functionality, and don't necessarily exist in older GFE versions. try { // these aren't critical for functionality, and don't necessarily exist in older GFE versions.
this.GfeVersion = $root.find('GfeVersion').text().trim(); this.GfeVersion = $root.find('GfeVersion').text().trim();
this.gputype = $root.find('gputype').text().trim(); this.gputype = $root.find('gputype').text().trim();
@ -439,6 +446,18 @@ NvHTTP.prototype = {
.then(this.refreshServerInfo()); .then(this.refreshServerInfo());
}, },
updateExternalAddressIP4: function() {
console.log('%c[utils.js, updateExternalAddressIP4]', 'color: gray;', 'Finding external IPv4 address for ' + this.hostname);
return sendMessage('STUN').then(function(addr) {
if (addr) {
this.externalIP = addr
console.log('%c[utils.js, updateExternalAddressIP4]', 'color: gray;', 'Found external IPv4 address: ' + this.hostname + ' -> ' + this.externalIP);
} else {
console.log('%c[utils.js, updateExternalAddressIP4]', 'color: gray;', 'External IPv4 address lookup failed');
}
}.bind(this))
},
pair: function(randomNumber) { pair: function(randomNumber) {
return this.refreshServerInfo().then(function() { return this.refreshServerInfo().then(function() {
if (this.paired) if (this.paired)