When I remove the WebSocket related handlers, I get the following data from channelRead
(I add a test handler After TLS handshake succeed):
NIOAny { head(HTTPRequestHead { method: GET, uri: "/ws/v2?aid=13&device_id=2410572936917666&access_key=671d0788ad0bbc7ab4b5e3f92de9d96d&fpid=1&sdk_version=3&iid=3255087214874926&pl=1&ne=1&version_code=80301&is_background=-1", version: HTTP/1.1, headers: [("Host", "ws-test.company.com"), ("Connection", "Upgrade"), ("Pragma", "no-cache"), ("Cache-Control", "no-cache"), ("x-support-ack", "1"), ("Upgrade", "websocket"), ("Origin", "wss://ws-test.company.com"), ("Sec-WebSocket-Version", "13"), ("x-tt-trace-id", "00-c1147c7c0d890673f9c31c8b761a000d-c1147c7c0d890673-01"), ("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Cronet Mobile/15E148 Safari/605.1"), ("Accept-Encoding", "gzip, deflate, br"), ("Cookie", "odin_tt=d46f16cbbdff09c4f628b8dc1dht8f66fa7737e7db7e7ee34c7f61e54b00e9f8900e1625483fe132c5db711eaaf3aae9; passport_csrf_token=82572faeb8d92882ea423a31266fc8dc; passport_csrf_token_default= 82572faeb8d92882ea423a31266fc8dc; install_id=3266027214874926; ttreq=1$c917312dc491edc3f6e09740ccce7e93342b854a"), ("Sec-WebSocket-Key", "r8s6WZhuOxB1UN+b8nteyA=="), ("Sec-WebSocket-Extensions", "permessage-deflate; client_max_window_bits"), ("Sec-WebSocket-Protocol", "pbbp2")] }) }
NIOAny { end(nil) }
This is WebSocket Upgrade request, and the data looks correct. But when I configure MITM as WebSocket Server, I can't receive channelRead
message in my handler, and my code looks like as follows (I write these code with reference to the SwiftNIO WebSocket example Demo):
let upgrader = NIOWebSocketServerUpgrader(shouldUpgrade: { (channel: Channel, head: HTTPRequestHead) in channel.eventLoop.makeSucceededFuture(HTTPHeaders()) },
upgradePipelineHandler: { (channel: Channel, _: HTTPRequestHead) in
channel.pipeline.addHandler(WebSocketTimeHandler())
})
let bootstrap = ServerBootstrap(group: group)
// Specify backlog and enable SO_REUSEADDR for the server itself
.serverChannelOption(ChannelOptions.backlog, value: 256)
.serverChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
// Set the handlers that are applied to the accepted Channels
.childChannelInitializer { channel in
let httpHandler = HTTPHandler()
let config: NIOHTTPServerUpgradeConfiguration = (
upgraders: [ upgrader ],
completionHandler: { _ in
channel.pipeline.removeHandler(httpHandler, promise: nil)
}
)
return channel.pipeline.configureHTTPServerPipeline(withServerUpgrade: config).flatMap {
channel.pipeline.addHandler(httpHandler)
}
}
// Enable SO_REUSEADDR for the accepted Channels
.childChannelOption(ChannelOptions.socketOption(.so_reuseaddr), value: 1)
Theoretically, I should receive all request traffics from httpHandler'schannelRead
, but httpHandler's been removed quickly before channelRead
could be invoked.
But Why SwiftNIO's demo runs normally?