Skip to content

Commit de8e232

Browse files
committed
Fix crash when json parse failed, allow to specify listen address, salt ip hashes, allow users to change their color
1 parent b81104f commit de8e232

File tree

6 files changed

+222
-33
lines changed

6 files changed

+222
-33
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ all: CPPFLAGS += $(OPT_REL)
3434
all: LDFLAGS += $(LD_REL)
3535
all: preconditions $(TARGET)
3636

37+
owopp: CPPFLAGS += $(OPT_REL) -D FOR_OWOPP
38+
owopp: LDFLAGS += $(LD_REL)
39+
owopp: preconditions $(TARGET)
40+
3741
g: CPPFLAGS += $(OPT_DBG)
3842
g: LDFLAGS += $(LD_DBG)
3943
g: preconditions $(TARGET)

lib/json

Submodule json updated 203 files

lib/uWebSockets-unix-sockets.patch

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
diff --git a/src/Networking.h b/src/Networking.h
2+
index e26888c..d0364a2 100644
3+
--- a/src/Networking.h
4+
+++ b/src/Networking.h
5+
@@ -55,12 +55,15 @@ inline SOCKET dup(SOCKET socket) {
6+
}
7+
#else
8+
#include <sys/socket.h>
9+
+#include <sys/un.h>
10+
#include <netinet/in.h>
11+
#include <netinet/tcp.h>
12+
#include <netdb.h>
13+
#include <unistd.h>
14+
#include <arpa/inet.h>
15+
#include <cstring>
16+
+// Unix Domain Sockets support
17+
+#define UWS_UDS
18+
#define SOCKET_ERROR -1
19+
#define INVALID_SOCKET -1
20+
#define WIN32_EXPORT
21+
diff --git a/src/Node.h b/src/Node.h
22+
index 5c31caf..8c7649a 100644
23+
--- a/src/Node.h
24+
+++ b/src/Node.h
25+
@@ -128,37 +128,55 @@ public:
26+
// todo: hostname, backlog
27+
template <void A(Socket *s)>
28+
bool listen(const char *host, int port, uS::TLS::Context sslContext, int options, uS::NodeData *nodeData, void *user) {
29+
- addrinfo hints, *result;
30+
+ uv_os_sock_t listenFd = SOCKET_ERROR;
31+
+ Context *netContext = nodeData->netContext;
32+
+
33+
+ addrinfo hints, *result = nullptr;
34+
+ addrinfo *listenAddr;
35+
+
36+
memset(&hints, 0, sizeof(addrinfo));
37+
38+
hints.ai_flags = AI_PASSIVE;
39+
hints.ai_family = AF_UNSPEC;
40+
hints.ai_socktype = SOCK_STREAM;
41+
42+
- Context *netContext = nodeData->netContext;
43+
+#ifdef UWS_UDS
44+
+ sockaddr_un unixsv;
45+
+ if (host && strncmp(host, "unix:", 5) == 0) {
46+
+ listenFd = netContext->createSocket(AF_UNIX, hints.ai_socktype, hints.ai_protocol);
47+
48+
- if (getaddrinfo(host, std::to_string(port).c_str(), &hints, &result)) {
49+
- return true;
50+
- }
51+
+ unixsv.sun_family = AF_UNIX;
52+
+ strncpy(unixsv.sun_path, host + 5, strlen(host + 5) + 1);
53+
+
54+
+ hints.ai_addr = (sockaddr *) &unixsv;
55+
+ hints.ai_addrlen = sizeof(unixsv);
56+
+ listenAddr = &hints;
57+
+
58+
+ unlink(unixsv.sun_path);
59+
+ } else
60+
+#endif
61+
+ {
62+
+ if (getaddrinfo(host, std::to_string(port).c_str(), &hints, &result)) {
63+
+ return true;
64+
+ }
65+
+
66+
+ if ((options & uS::ONLY_IPV4) == 0) {
67+
+ for (addrinfo *a = result; a && listenFd == SOCKET_ERROR; a = a->ai_next) {
68+
+ if (a->ai_family == AF_INET6) {
69+
+ listenFd = netContext->createSocket(a->ai_family, a->ai_socktype, a->ai_protocol);
70+
+ listenAddr = a;
71+
+ }
72+
+ }
73+
+ }
74+
75+
- uv_os_sock_t listenFd = SOCKET_ERROR;
76+
- addrinfo *listenAddr;
77+
- if ((options & uS::ONLY_IPV4) == 0) {
78+
for (addrinfo *a = result; a && listenFd == SOCKET_ERROR; a = a->ai_next) {
79+
- if (a->ai_family == AF_INET6) {
80+
+ if (a->ai_family == AF_INET) {
81+
listenFd = netContext->createSocket(a->ai_family, a->ai_socktype, a->ai_protocol);
82+
listenAddr = a;
83+
}
84+
}
85+
}
86+
87+
- for (addrinfo *a = result; a && listenFd == SOCKET_ERROR; a = a->ai_next) {
88+
- if (a->ai_family == AF_INET) {
89+
- listenFd = netContext->createSocket(a->ai_family, a->ai_socktype, a->ai_protocol);
90+
- listenAddr = a;
91+
- }
92+
- }
93+
-
94+
if (listenFd == SOCKET_ERROR) {
95+
freeaddrinfo(result);
96+
return true;
97+
diff --git a/src/Socket.cpp b/src/Socket.cpp
98+
index c35bbf8..bf0ac35 100644
99+
--- a/src/Socket.cpp
100+
+++ b/src/Socket.cpp
101+
@@ -18,10 +18,13 @@ Socket::Address Socket::getAddress()
102+
sockaddr_in *ipv4 = (sockaddr_in *) &addr;
103+
inet_ntop(AF_INET, &ipv4->sin_addr, buf, sizeof(buf));
104+
return {ntohs(ipv4->sin_port), buf, "IPv4"};
105+
- } else {
106+
+ } else if (addr.ss_family == AF_INET6) {
107+
sockaddr_in6 *ipv6 = (sockaddr_in6 *) &addr;
108+
inet_ntop(AF_INET6, &ipv6->sin6_addr, buf, sizeof(buf));
109+
return {ntohs(ipv6->sin6_port), buf, "IPv6"};
110+
+ } else {
111+
+ //sockaddr_un *unix = (sockaddr_un *) &addr;
112+
+ return {0, "", "UNIX"};
113+
}
114+
}
115+

src/msg.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void server::msg::ch(server* sv, json& j, uWS::WebSocket<uWS::SERVER> * s){
141141
Room* room = sv->rooms.at(nr);
142142
if(!room) return;
143143
res[0] = room->get_json(nr, true);
144-
144+
145145
/*room->broadcast(res, s); this can be changed for the following, less data sent. */
146146
if(info.newclient){
147147
json upd = json::array();
@@ -150,7 +150,7 @@ void server::msg::ch(server* sv, json& j, uWS::WebSocket<uWS::SERVER> * s){
150150
upd[0]["m"] = "p";
151151
room->broadcast(upd, s);
152152
}
153-
153+
154154
res[0]["p"] = info.id;
155155
res[1] = {
156156
{"m", "c"},
@@ -210,13 +210,34 @@ void server::msg::chset(server* sv, json& j, uWS::WebSocket<uWS::SERVER> * s){
210210
}
211211

212212
void server::msg::userset(server* sv, json& j, uWS::WebSocket<uWS::SERVER> * s){
213-
if(j["set"].is_object() && j["set"]["name"].is_string()){
213+
if(j["set"].is_object()){
214214
std::string ip = *(std::string *) s->getUserData();
215215
auto search = sv->clients.find(ip);
216216
if(search != sv->clients.end() && search->second.user->quota.name.can_spend()){
217-
std::string newn = j["set"]["name"].get<std::string>();
218-
if(getUTF8strlen(newn) <= 40){
219-
search->second.user->set_name(newn);
217+
bool updated = false;
218+
219+
if (j["set"]["name"].is_string()) {
220+
std::string newn = j["set"]["name"].get<std::string>();
221+
if(getUTF8strlen(newn) <= 40){
222+
search->second.user->set_name(newn);
223+
updated = true;
224+
}
225+
}
226+
227+
if (j["set"]["color"].is_string()) {
228+
std::string newc = j["set"]["color"].get<std::string>();
229+
if (newc.size() > 1 && newc[0] == '#') {
230+
newc.erase(0, 1);
231+
try {
232+
uint32_t ncolor = std::stoul(std::string("0x") + newc, nullptr, 16);
233+
search->second.user->set_color(ncolor);
234+
updated = true;
235+
} catch(const std::invalid_argument&) { }
236+
catch(const std::out_of_range&) { }
237+
}
238+
}
239+
240+
if (updated) {
220241
sv->user_upd(search->second);
221242
}
222243
}

0 commit comments

Comments
 (0)