mirror of
https://github.com/LizardByte/Sunshine.git
synced 2025-03-28 19:20:26 +00:00
Determine local ip-address at runtime
This commit is contained in:
parent
810ff31c0e
commit
734a03be51
@ -1 +1 @@
|
||||
Subproject commit d1bf544d9266bdf5b86517e4a70de63216529e5b
|
||||
Subproject commit e70509e078ade2eb9679862ab6a48b4ee2fefb66
|
13
nvhttp.cpp
13
nvhttp.cpp
@ -19,6 +19,7 @@
|
||||
#include "utility.h"
|
||||
#include "stream.h"
|
||||
#include "nvhttp.h"
|
||||
#include "platform/common.h"
|
||||
|
||||
|
||||
namespace nvhttp {
|
||||
@ -34,6 +35,7 @@ namespace pt = boost::property_tree;
|
||||
|
||||
std::string read_file(const char *path);
|
||||
|
||||
std::string local_ip;
|
||||
using https_server_t = SimpleWeb::Server<SimpleWeb::HTTPS>;
|
||||
using http_server_t = SimpleWeb::Server<SimpleWeb::HTTP>;
|
||||
|
||||
@ -396,10 +398,10 @@ void serverinfo(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> res
|
||||
tree.put("root.GfeVersion", GFE_VERSION);
|
||||
tree.put("root.uniqueid", config::nvhttp.unique_id);
|
||||
tree.put("root.mac", "42:45:F0:65:D6:F4");
|
||||
tree.put("root.LocalIP", "192.168.0.195"); //FIXME: Should be determined at runtime
|
||||
tree.put("root.LocalIP", local_ip);
|
||||
|
||||
if(config::nvhttp.external_ip.empty()) {
|
||||
tree.put("root.ExternalIP", "192.168.0.195");
|
||||
tree.put("root.ExternalIP", local_ip);
|
||||
}
|
||||
else {
|
||||
tree.put("root.ExternalIP", config::nvhttp.external_ip);
|
||||
@ -497,6 +499,13 @@ void appasset(std::shared_ptr<typename SimpleWeb::ServerBase<T>::Response> respo
|
||||
}
|
||||
|
||||
void start() {
|
||||
local_ip = platf::get_local_ip();
|
||||
if(local_ip.empty()) {
|
||||
std::cout << "Error: Could not determine the local ip-address"sv << std::endl;
|
||||
|
||||
exit(8);
|
||||
}
|
||||
|
||||
load_devices();
|
||||
|
||||
conf_intern.pkey = read_file(config::nvhttp.pkey.c_str());
|
||||
|
@ -5,6 +5,7 @@
|
||||
#ifndef SUNSHINE_COMMON_H
|
||||
#define SUNSHINE_COMMON_H
|
||||
|
||||
#include <string>
|
||||
#include <utility.h>
|
||||
|
||||
namespace platf {
|
||||
@ -19,6 +20,7 @@ using img_t = util::safe_ptr<void, freeImage>;
|
||||
using mic_t = util::safe_ptr<void, freeMic>;
|
||||
using audio_t = util::safe_ptr<void, freeAudio>;
|
||||
|
||||
std::string get_local_ip();
|
||||
display_t display();
|
||||
img_t snapshot(display_t &display);
|
||||
mic_t microphone();
|
||||
@ -34,7 +36,6 @@ void move_mouse(display_t::element_type *display, int deltaX, int deltaY);
|
||||
void button_mouse(display_t::element_type *display, int button, bool release);
|
||||
void scroll(display_t::element_type *display, int distance);
|
||||
void keyboard(display_t::element_type *display, uint16_t modcode, bool release);
|
||||
|
||||
}
|
||||
|
||||
#endif //SUNSHINE_COMMON_H
|
||||
|
@ -4,6 +4,10 @@
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
|
||||
#include <X11/X.h>
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/Xutil.h>
|
||||
@ -15,9 +19,72 @@
|
||||
#include <pulse/error.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <bitset>
|
||||
|
||||
namespace platf {
|
||||
using namespace std::literals;
|
||||
|
||||
using ifaddr_t = util::safe_ptr<ifaddrs, freeifaddrs>;
|
||||
|
||||
ifaddr_t get_ifaddrs() {
|
||||
ifaddrs *p { nullptr };
|
||||
|
||||
getifaddrs(&p);
|
||||
|
||||
return ifaddr_t { p };
|
||||
}
|
||||
|
||||
std::string from_sockaddr(const sockaddr *const ip_addr) {
|
||||
char data[INET6_ADDRSTRLEN];
|
||||
|
||||
auto family = ip_addr->sa_family;
|
||||
if(family == AF_INET6) {
|
||||
inet_ntop(AF_INET6, &((sockaddr_in6*)ip_addr)->sin6_addr, data, INET6_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
if(family == AF_INET) {
|
||||
inet_ntop(AF_INET, &((sockaddr_in*)ip_addr)->sin_addr, data, INET_ADDRSTRLEN);
|
||||
}
|
||||
|
||||
return std::string { data };
|
||||
}
|
||||
|
||||
std::string get_local_ip(int family) {
|
||||
std::bitset<2> family_f {};
|
||||
|
||||
if(family == 0) {
|
||||
family_f[0] = true;
|
||||
family_f[1] = true;
|
||||
}
|
||||
|
||||
if(family == AF_INET) {
|
||||
family_f[0] = true;
|
||||
}
|
||||
|
||||
if(family == AF_INET6) {
|
||||
family_f[1] = true;
|
||||
}
|
||||
|
||||
|
||||
std::string ip_addr;
|
||||
auto ifaddr = get_ifaddrs();
|
||||
for(auto pos = ifaddr.get(); pos != nullptr; pos = pos->ifa_next) {
|
||||
if(pos->ifa_addr && pos->ifa_flags & IFF_UP && !(pos->ifa_flags & IFF_LOOPBACK)) {
|
||||
if(
|
||||
(family_f[0] && pos->ifa_addr->sa_family == AF_INET) ||
|
||||
(family_f[1] && pos->ifa_addr->sa_family == AF_INET6)
|
||||
){
|
||||
ip_addr = from_sockaddr(pos->ifa_addr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ip_addr;
|
||||
}
|
||||
|
||||
std::string get_local_ip() { return get_local_ip(AF_INET); }
|
||||
|
||||
struct display_attr_t {
|
||||
display_attr_t() : display { XOpenDisplay(nullptr) }, window { DefaultRootWindow(display) }, attr {} {
|
||||
XGetWindowAttributes(display, window, &attr);
|
||||
|
Loading…
x
Reference in New Issue
Block a user