Skip to content

Commit 63e14ea

Browse files
committed
Bluewing Server update to build 32 (HTML5 support)
• Added WebSocket compatibility via lw_ws (lacewing::webserver). Re-added http-parser and multipart-parser files. Due to SSL using lw_server_client tag, a relay_tag variable was added. Due to secure websocket needing them, uses the system's ssl and crypto libraries. • Restored C compatibility to C files instead of forcing C++ everywhere: lw_list required hacky workarounds in VS to deduce type in C. Type is now passed explicitly in the lw_list macros. std::atomic in refcount is now _Atomic, or in case of MSVC, an InterlockedXX compatible VOLATILE LONG. • Added an "always_log" function for lw_trace that hold significant errors, but don't have an on_error handler accessible. Later, the log system will be improved to have info and warnings and a global handler. • TLS certificate format support expanded; certificate chain and private key are now two different files. This allows two PEM files to be used on Windows, not just Unix. On Windows, you can use PEM files or one PFX; just make sure PFX has private key included. • Added timeout for connections that don't send Lacewing connect packet • Fixed cleanup of lw_timer in Unix; it leaked lw_thread and didn't remove its timer from pump • Fixed leaking memory in Unix address comparison - two separate layers of allocations • Standardized VIM file type header - even tho I don't use VIM, it's either 100% there or 0% • Fixed IPv6 address printing • Updated copyright for Lacewing files - should've done this long ago, since it was officially abandoned in 2015 • Bluewing Client is also now b100; b99 -> b100 is mostly Fusion-only file fixes outside this repo, bar the IPv6 printing fix. The first HTML5 Blue Client is b100, but b99 HTML5 was pre-released to around three Clickteam users.
1 parent f09efdf commit 63e14ea

File tree

130 files changed

+7066
-3836
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+7066
-3836
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,12 @@ __pycache__/
287287
*.odx.cs
288288
*.xsd.cs
289289
*.wew
290+
291+
# Hide private crypto keys
292+
*.pem
293+
*.pfx
294+
*.p7c
295+
*.p12
296+
*.cer
297+
*.cert
298+
*.crt

ConsoleColors.h renamed to ConsoleColors.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/* vim: set noet ts=4 sw=4 sts=4 ft=cpp:
2+
*
3+
* Created by Darkwire Software.
4+
*
5+
* This example console coloring file is available unlicensed; the MIT license of liblacewing/Lacewing Relay
6+
* does not apply to this file.
7+
*/
8+
19
#pragma once
210
#include <iostream>
311

Lacewing/CodePointAllowList.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/* vim: set noet ts=4 sw=4 sts=4 ft=cpp:
2+
*
3+
* liblacewing and Lacewing Relay/Blue source code are available under MIT license.
4+
* Copyright (C) 2021-2022 Darkwire Software.
5+
* All rights reserved.
6+
*
7+
* https://opensource.org/licenses/mit-license.php
8+
*/
9+
110
#include "Lacewing.h"
211
#include "deps/utf8proc.h"
312

Lacewing/FrameBuilder.h

Lines changed: 94 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,26 @@
1-
2-
/* vim: set noet ts=4 sw=4 ft=cpp:
3-
*
4-
* Copyright (C) 2011 James McLaughlin. All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
7-
* modification, are permitted provided that the following conditions
8-
* are met:
1+
/* vim: set noet ts=4 sw=4 sts=4 ft=cpp:
92
*
10-
* 1. Redistributions of source code must retain the above copyright
11-
* notice, this list of conditions and the following disclaimer.
3+
* Copyright (C) 2011 James McLaughlin.
4+
* Copyright (C) 2012-2022 Darkwire Software.
5+
* All rights reserved.
126
*
13-
* 2. Redistributions in binary form must reproduce the above copyright
14-
* notice, this list of conditions and the following disclaimer in the
15-
* documentation and/or other materials provided with the distribution.
16-
*
17-
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20-
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23-
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24-
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25-
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26-
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27-
* SUCH DAMAGE.
28-
*/
7+
* liblacewing and Lacewing Relay/Blue source code are available under MIT license.
8+
* https://opensource.org/licenses/mit-license.php
9+
*/
2910

3011
#include "MessageBuilder.h"
3112

13+
// TODO: This isn't an ideal workaround.
14+
extern "C" size_t lwp_stream_write(lw_stream ctx, const char* buffer, size_t size, int flags);
15+
3216
#ifndef lacewingframebuilder
3317
#define lacewingframebuilder
3418

3519
class framebuilder : public messagebuilder
3620
{
3721
protected:
3822

39-
void preparefortransmission()
23+
void preparefortransmission(bool iswebsocketclient)
4024
{
4125
if (tosend)
4226
return;
@@ -46,6 +30,56 @@ class framebuilder : public messagebuilder
4630

4731
lw_ui32 headersize;
4832

33+
// We're sending to a websocket client, we need to mash this into WebSocket format
34+
if (iswebsocketclient)
35+
{
36+
// If we're sending to a websocket client, we must be a server.
37+
// If we're a server, the UDP header has one byte: the type.
38+
if (origUDP != UINT32_MAX)
39+
type = buffer[7] | 0x8;
40+
41+
// Since we send text messages to channels and so on, we can't use text opcode for text messages
42+
const lw_ui8 flagopcode = 0b10000010; // fin flag enabled + binary message
43+
if (messagesize + 1 <= 125)
44+
{
45+
(*(lw_ui8*)(buffer + 5)) = flagopcode;
46+
(*(lw_ui8*)(buffer + 6)) = (lw_ui8)(messagesize + 1);
47+
(*(lw_ui8*)(buffer + 7)) = (lw_ui8)type;
48+
headersize = 3;
49+
tosend = (buffer + 8) - headersize;
50+
tosendsize = messagesize + headersize;
51+
}
52+
else if (messagesize <= 0xFFFF)
53+
{
54+
(*(lw_ui8*)(buffer + 3)) = flagopcode;
55+
(*(lw_ui8*)(buffer + 4)) = (lw_ui8)126; // indicate uint16 following size
56+
(*(lw_ui16*)(buffer + 5)) = htons((lw_ui16)(messagesize + 1));
57+
(*(lw_ui8*)(buffer + 7)) = (lw_ui8)type;
58+
headersize = 5;
59+
tosend = buffer + 8 - headersize;
60+
tosendsize = messagesize + headersize;
61+
}
62+
else
63+
{
64+
// The TCP header uses only 8 bytes, and we need 10 for uint64 size, so hack an extra two bytes in
65+
// It's not efficient, but anyone passing this much data shouldn't expect speed
66+
add<lw_ui16>(0);
67+
memmove(buffer + 10, buffer + 8, size - 8);
68+
69+
assert(!"Host to native!");
70+
71+
(*(lw_ui8*)(buffer)) = flagopcode;
72+
(*(lw_ui8*)(buffer + 1)) = (lw_ui8)127; // indicate uint64 following size
73+
(*(lw_ui64*)(buffer + 2)) = messagesize + 1;
74+
(*(lw_ui8*)(buffer + 10)) = (lw_ui8)type;
75+
76+
tosend = buffer;
77+
tosendsize = messagesize + 10;
78+
}
79+
80+
return;
81+
}
82+
4983
// Message size < 254; store as type byte + size byte
5084
if (messagesize < 0xfe)
5185
{
@@ -79,12 +113,15 @@ class framebuilder : public messagebuilder
79113

80114
tosend = (buffer + 8) - headersize;
81115
tosendsize = messagesize + headersize;
116+
82117
}
83118

84119
bool isudpclient;
85120

86-
char * tosend;
121+
char* tosend;
87122
int tosendsize;
123+
lw_ui32 origUDP;
124+
lw_i8 wasWebLast;
88125

89126
public:
90127

@@ -93,6 +130,8 @@ class framebuilder : public messagebuilder
93130
this->isudpclient = isudpclient;
94131
tosend = nullptr;
95132
tosendsize = 0;
133+
origUDP = UINT32_MAX;
134+
wasWebLast = -1;
96135
}
97136

98137
inline void addheader(lw_ui8 type, lw_ui8 variant, bool forudp = false, int udpclientid = -1)
@@ -102,38 +141,57 @@ class framebuilder : public messagebuilder
102141
if (!forudp)
103142
{
104143
add <lw_ui32> ((type << 4) | variant);
105-
add <lw_ui32> (0);
144+
add <lw_ui32> (0); // this is used for reserving space for adding message size later, in preparefortransmission()
106145

107146
return;
108147
}
148+
// Pad to 8 bytes in buffer
149+
add(std::string(8 - 1 - (isudpclient ? 2 : 0), '\xCD'));
109150

110-
add <lw_ui8> ((lw_ui8)((type << 4) | variant));
151+
add <lw_ui8> ((lw_ui8)((type << 4) | variant));
111152

112153
if (isudpclient)
113154
add <lw_ui16> ((lw_ui16)udpclientid);
155+
else
156+
origUDP = ((lw_ui32*)buffer)[1];
114157
}
115158

116159
inline void send(lacewing::server_client client, bool clear = true)
117160
{
118-
preparefortransmission();
119-
client->write(tosend, tosendsize);
161+
if (wasWebLast == -1 || client->is_websocket() != wasWebLast)
162+
{
163+
wasWebLast = client->is_websocket();
164+
tosend = nullptr; // or preparefortransmission does nothing
165+
preparefortransmission(wasWebLast);
166+
}
167+
168+
if (wasWebLast)
169+
lwp_stream_write((lw_stream)client, tosend, tosendsize, 2 /* lwp_stream_write_ignore_busy */);
170+
else
171+
client->write(tosend, tosendsize);
120172

121173
if (clear)
122174
framereset();
123175
}
124176

125177
inline void send(lacewing::client client, bool clear = true)
126178
{
127-
preparefortransmission();
179+
preparefortransmission(false);
128180
client->write(tosend, tosendsize);
129181

130182
if (clear)
131183
framereset();
132184
}
133185

186+
inline void revert() {
187+
((lw_ui32*)buffer)[1] = origUDP;
188+
tosend = nullptr;
189+
tosendsize = 0;
190+
}
191+
134192
inline void send(lacewing::udp udp, lacewing::address address, bool clear = true)
135193
{
136-
udp->send(address, buffer, size);
194+
udp->send(address, &buffer[isudpclient ? 5 : 7], size - (isudpclient ? 5 : 7));
137195

138196
if (clear)
139197
framereset();
@@ -142,8 +200,9 @@ class framebuilder : public messagebuilder
142200
inline void framereset()
143201
{
144202
reset();
145-
tosend = 0;
203+
tosend = NULL;
146204
tosendsize = 0;
205+
wasWebLast = -1;
147206
}
148207

149208
};

Lacewing/FrameReader.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
2-
/* vim: set et ts=4 sw=4 ft=cpp:
3-
*
4-
* Copyright (C) 2011 James McLaughlin. All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
7-
* modification, are permitted provided that the following conditions
8-
* are met:
9-
*
10-
* 1. Redistributions of source code must retain the above copyright
11-
* notice, this list of conditions and the following disclaimer.
1+
/* vim: set noet ts=4 sw=4 sts=4 ft=cpp:
122
*
13-
* 2. Redistributions in binary form must reproduce the above copyright
14-
* notice, this list of conditions and the following disclaimer in the
15-
* documentation and/or other materials provided with the distribution.
3+
* Copyright (C) 2011 James McLaughlin.
4+
* Copyright (C) 2012-2022 Darkwire Software.
5+
* All rights reserved.
166
*
17-
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20-
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23-
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24-
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25-
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26-
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27-
* SUCH DAMAGE.
28-
*/
7+
* liblacewing and Lacewing Relay/Blue source code are available under MIT license.
8+
* https://opensource.org/licenses/mit-license.php
9+
*/
2910

3011
#include "MessageBuilder.h"
3112

Lacewing/IDPool.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
1-
2-
/* vim: set et ts=4 sw=4 ft=cpp:
3-
*
4-
* Copyright (C) 2011 James McLaughlin. All rights reserved.
5-
*
6-
* Redistribution and use in source and binary forms, with or without
7-
* modification, are permitted provided that the following conditions
8-
* are met:
9-
*
10-
* 1. Redistributions of source code must retain the above copyright
11-
* notice, this list of conditions and the following disclaimer.
1+
/* vim: set et ts=4 sw=4 sts=4 ft=cpp:
122
*
13-
* 2. Redistributions in binary form must reproduce the above copyright
14-
* notice, this list of conditions and the following disclaimer in the
15-
* documentation and/or other materials provided with the distribution.
3+
* Copyright (C) 2011 James McLaughlin.
4+
* Copyright (C) 2012-2022 Darkwire Software.
5+
* All rights reserved.
166
*
17-
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18-
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20-
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21-
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22-
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23-
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24-
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25-
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26-
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27-
* SUCH DAMAGE.
28-
*/
7+
* liblacewing and Lacewing Relay/Blue source code are available under MIT license.
8+
* https://opensource.org/licenses/mit-license.php
9+
*/
2910
#include <vector>
3011
#include <set>
3112

0 commit comments

Comments
 (0)