Skip to content

Commit b2fc08f

Browse files
committed
add support for proxy encryption
1 parent 4185eea commit b2fc08f

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

Sources/HTTPServer/HTTP.Server.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ extension HTTP.Server
113113
func serve(origin server:HTTP.ServerOrigin,
114114
host:String,
115115
port:Int,
116-
with context:NIOSSLContext? = nil,
116+
with encryption:HTTP.ServerEncryptionLayer? = nil,
117117
policy:(any HTTP.ServerPolicy)? = nil) async throws
118118
{
119119
let bootstrap:ServerBootstrap = .init(group: MultiThreadedEventLoopGroup.singleton)
@@ -129,13 +129,25 @@ extension HTTP.Server
129129
HTTPPart<HTTPResponseHead, ByteBuffer>>,
130130
(any Channel, NIOHTTP2Handler.AsyncStreamMultiplexer<HTTP.Stream>)>>, Never>
131131

132-
if let context:NIOSSLContext
132+
133+
if let encryption:HTTP.ServerEncryptionLayer
133134
{
134135
listener = try await bootstrap.bind(host: host, port: port)
135136
{
136137
(channel:any Channel) in
137138

138-
channel.pipeline.addHandler(NIOSSLServerHandler.init(context: context))
139+
let encryptionHandlers:[any ChannelHandler]
140+
141+
switch encryption
142+
{
143+
case .local(let context):
144+
encryptionHandlers = [NIOSSLServerHandler.init(context: context)]
145+
146+
case .proxy:
147+
encryptionHandlers = []
148+
}
149+
150+
return channel.pipeline.addHandlers(encryptionHandlers)
139151
.flatMap
140152
{
141153
channel.configureAsyncHTTPServerPipeline
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import NIOSSL
2+
3+
extension HTTP
4+
{
5+
@frozen public
6+
enum ServerEncryptionLayer
7+
{
8+
/// Encryption happens on this server.
9+
case local(NIOSSLContext)
10+
/// Encryption happens on an upstream proxy.
11+
case proxy
12+
}
13+
}

Sources/UnidocServer/HTTP.Server (ext).swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import NIOSSL
66
extension HTTP.Server where Self:Unidoc.Server
77
{
88
public
9-
func run(on port:Int, with niossl:NIOSSLContext? = nil) async throws
9+
func run(on port:Int, with encryption:HTTP.ServerEncryptionLayer? = nil) async throws
1010
{
1111
try await self._setup()
1212
try await withThrowingTaskGroup(of: Void.self)
@@ -26,7 +26,7 @@ extension HTTP.Server where Self:Unidoc.Server
2626
try await self.serve(origin: self.options.origin,
2727
host: "::",
2828
port: port,
29-
with: niossl,
29+
with: encryption,
3030
policy: self.policy)
3131
}
3232
tasks.addTask

Sources/unidoc-linkerd/Main.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ struct Main
2727
var db:Unidoc.DatabaseOptions
2828
@OptionGroup
2929
var s3:Unidoc.BucketOptions
30+
31+
@Flag(
32+
name: [.customLong("assume-encrypted")],
33+
help: "Assume that the connection is encrypted")
34+
var assumeEncrypted:Bool = false
3035
}
3136

3237
@main
@@ -73,7 +78,7 @@ extension Main:AsyncParsableCommand
7378
logger: $0,
7479
db: .init(settings: settings, sessions: pool, unidoc: "unidoc"))
7580

76-
try await server.run(on: self.port)
81+
try await server.run(on: self.port, with: self.assumeEncrypted ? .proxy : nil)
7782
}
7883
}
7984
}

Sources/unidoc-tools/Main.Preview.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ extension Main.Preview:AsyncParsableCommand
110110
logger: $0,
111111
db: .init(settings: settings, sessions: pool, unidoc: "unidoc"))
112112

113-
try await server.run(on: port, with: serverIdentity)
113+
try await server.run(on: port,
114+
with: serverIdentity.map(HTTP.ServerEncryptionLayer.local(_:)))
114115
}
115116
}
116117
}

0 commit comments

Comments
 (0)