-
Notifications
You must be signed in to change notification settings - Fork 0
/
IRCWebClientServer.swift
277 lines (231 loc) · 9.02 KB
/
IRCWebClientServer.swift
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the swift-nio-irc open source project
//
// Copyright (c) 2018-2020 ZeeZide GmbH. and the swift-nio-irc project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIOIRC project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import class NIOConcurrencyHelpers.NIOAtomic
import NIO
import NIOHTTP1
import NIOWebSocket
import class NIOConcurrencyHelpers.Atomic
import struct Foundation.TimeInterval
import IRC
/**
* A small HTTP server which can deliver the single-page webapp included in the
* module, and which also serves as the WebSocket endpoint (does the WebSocket
* upgrade).
*
* Usage is trivial:
*
* let webServer = IRCWebClientServer()
* webServer.listen()
*
* Options can be passed in using the `Configuration` object.
*/
open class IRCWebClientServer {
open class Configuration {
open var host : String? = "localhost"
open var port : Int = 1337
open var eventLoopGroup : EventLoopGroup? = nil
open var backlog : Int = 256
open var ircHost : String? = nil
open var ircPort : Int? = nil
open var externalHost : String? = nil
open var externalPort : Int? = nil
// When the IRC client successfully connects, those channels are
// automagically joined.
open var autoJoinChannels : Set<String> = [ "#NIO", "#SwiftDE" ]
// We can automatically send some messages after a timeout
open var autoSendMessageTimeout : TimeInterval = 3.0
open var autoSendMessages : [ ( String, String ) ] = []
public init(eventLoopGroup: EventLoopGroup? = nil) {
self.eventLoopGroup = eventLoopGroup
}
}
public let configuration : Configuration
public let eventLoopGroup : EventLoopGroup
public init(configuration: Configuration = Configuration()) {
self.configuration = configuration
self.eventLoopGroup = configuration.eventLoopGroup
?? MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
}
public private(set) var serverChannel : Channel?
open func listenAndWait() {
listen()
do {
try serverChannel?.closeFuture.wait() // no close, no exit
}
catch {
print("ERROR: failed to wait on server:", error)
}
}
open func listen() {
let bootstrap = makeBootstrap()
do {
let address : SocketAddress
if let host = configuration.host {
address = try SocketAddress
.makeAddressResolvingHost(host, port: configuration.port)
}
else {
var addr = sockaddr_in()
addr.sin_port = in_port_t(configuration.port).bigEndian
address = SocketAddress(addr, host: "*")
}
serverChannel = try bootstrap.bind(to: address)
.wait()
if let addr = serverChannel?.localAddress {
print("IRCWebClientServer running on:", addr)
}
}
catch {
print("failed to start server:", type(of:error), error)
}
}
open func stopOnSignal(_ signal: Int32) {
print("Received SIGINT scheduling shutdown...")
// Safe? Unsafe. No idea. Probably not :-)
exit(0)
}
// MARK: - Bootstrap
lazy var upgrader : HTTPServerProtocolUpgrader = {
var sessionCounter = NIOAtomic.makeAtomic(value: 1)
let config = configuration
func shouldUpgrade(channel: Channel, head: HTTPRequestHead)
-> EventLoopFuture<HTTPHeaders?>
{
let promise = channel.eventLoop.makePromise(of: HTTPHeaders?.self)
promise.succeed(HTTPHeaders())
return promise.futureResult
}
func upgradeHandler(channel: Channel, head: HTTPRequestHead)
-> EventLoopFuture<Void>
{
return channel.pipeline
.removeHandler(name: "de.zeezide.irc.miniirc.web.http")
.flatMap { ( _ ) -> EventLoopFuture<Void> in
let nick = IRCNickName("Guest\(sessionCounter.add(1))")!
let options = IRCClientOptions(
port : config.ircPort ?? DefaultIRCPort,
host : config.ircHost ?? config.host ?? "localhost",
nickname : nick,
eventLoopGroup : channel.eventLoop
)
return channel.pipeline
.addHandler(IRCWebSocketBridge(options: options),
name: "de.zeezide.irc.miniirc.web.socket")
}
}
return NIOWebSocketServerUpgrader(shouldUpgrade: shouldUpgrade,
upgradePipelineHandler: upgradeHandler)
}()
open func makeBootstrap() -> ServerBootstrap {
let upgrader = self.upgrader
let endPoint = IRCWebClientEndPoint(content)
let reuseAddrOpt = ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET),
SO_REUSEADDR)
let bootstrap = ServerBootstrap(group: eventLoopGroup)
// Specify backlog and enable SO_REUSEADDR for the server itself
.serverChannelOption(ChannelOptions.backlog,
value: Int32(256))
.serverChannelOption(reuseAddrOpt, value: 1)
// Set the handlers that are applied to the accepted Channels
.childChannelInitializer { channel in
let config: NIOHTTPServerUpgradeConfiguration = (
upgraders: [ upgrader ], completionHandler: { _ in }
)
return channel.pipeline
.configureHTTPServerPipeline(withServerUpgrade: config)
.flatMap {
channel.pipeline
.addHandler(endPoint, name: "de.zeezide.irc.miniirc.web.http")
}
}
// Enable TCP_NODELAY and SO_REUSEADDR for the accepted Channels
.childChannelOption(ChannelOptions.socket(IPPROTO_TCP, TCP_NODELAY),
value: 1)
.childChannelOption(reuseAddrOpt, value: 1)
.childChannelOption(ChannelOptions.maxMessagesPerRead, value: 1)
return bootstrap
}
// MARK: - HTML Payload
open lazy var content : ByteBuffer = {
let host = configuration.externalHost ?? configuration.host ?? "localhost"
let port = configuration.externalPort ?? configuration.port
let scheme = port == 443 ? "wss" : "ws"
func escapeJSString(_ s: String) -> String { return s } // TODO
let onConnect = configuration.autoJoinChannels.map { channel in
"self.connection.call('JOIN', '\(escapeJSString(channel))');"
}.joined(separator: "\n")
let onStartup : String = {
// TBD: this should probably just move to onConnect
guard !configuration.autoSendMessages.isEmpty else { return "" }
let timeoutInMS = Int(configuration.autoSendMessageTimeout * 1000)
var ms = "window.setTimeout(function() {\n"
for ( recipient, message ) in configuration.autoSendMessages {
ms += " self.sendMessageToTarget("
ms += "'\(escapeJSString(recipient))'"
ms += ", "
ms += "'\(escapeJSString(message))'"
ms += ");\n"
}
ms += "}, \(timeoutInMS));\n"
return ms
}()
let scripts = [
"style" : rsrc_Client_css,
"script.vc.MainVC.onConnect" : onConnect,
"script.app.onStartup" : onStartup,
"script.model.ClientUtils" : rsrc_ClientUtils_js,
"script.model.ClientConnection" : rsrc_ClientConnection_js,
"script.model.ChatItem" : rsrc_ChatItem_js,
"script.vc.SidebarVC" : rsrc_SidebarVC_js,
"script.vc.MessagesVC" : rsrc_MessagesVC_js,
"script.vc.MainVC" : rsrc_MainVC_js,
]
var patterns = [
"title" : "MiniIRC ✭ ZeeZide",
"endpoint" : "\(scheme)://\(host):\(port)/websocket",
"defaultNick" : "nick",
]
func replacePatterns(in s: String, using variables: [ String : String ])
-> String
{
var s = s
for ( variable, text ) in variables {
if variable.lowercased().contains("script") {
s = s.replacingOccurrences(of: "{{\(variable)}}", with: text)
}
else {
s = s.replacingOccurrences(of: "{{\(variable)}}",
with: text.htmlEscaped)
}
}
return s
}
for ( name, script ) in scripts {
patterns[name] = replacePatterns(in: script, using: patterns)
}
var s = replacePatterns(in: rsrc_ClientInline_html, using: patterns)
var bb = ByteBufferAllocator().buffer(capacity: 4096)
bb.reserveCapacity(s.utf8.count)
bb.writeString(s)
return bb
}()
}
fileprivate extension String {
var htmlEscaped : String {
let escapeMap : [ Character : String ] = [
"<" : "<", ">": ">", "&": "&", "\"": """
]
return map { escapeMap[$0] ?? String($0) }.reduce("", +)
}
}