-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from orlandos-nl/jo/direct-tcpip-server
Add DirectTCPIP support to SSH servers
- Loading branch information
Showing
6 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
/*.xcodeproj | ||
xcuserdata/ | ||
Package.resolved | ||
.vscode/launch.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
62 changes: 62 additions & 0 deletions
62
Sources/Citadel/DirectTCPIP/Server/DirectTCPIP+Server.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import NIO | ||
import NIOSSH | ||
|
||
fileprivate final class ProxyChannelHandler: ChannelOutboundHandler { | ||
typealias OutboundIn = ByteBuffer | ||
|
||
private let write: (ByteBuffer, EventLoopPromise<Void>?) -> Void | ||
|
||
init(write: @escaping (ByteBuffer, EventLoopPromise<Void>?) -> Void) { | ||
self.write = write | ||
} | ||
|
||
func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) { | ||
let data = self.unwrapOutboundIn(data) | ||
write(data, promise) | ||
} | ||
} | ||
|
||
public protocol DirectTCPIPDelegate { | ||
func initializeDirectTCPIPChannel(_ channel: Channel, request: SSHChannelType.DirectTCPIP, context: SSHContext) -> EventLoopFuture<Void> | ||
} | ||
|
||
public struct DirectTCPIPForwardingDelegate: DirectTCPIPDelegate { | ||
internal enum Error: Swift.Error { | ||
case forbidden | ||
} | ||
|
||
public var whitelistedHosts: [String]? | ||
public var whitelistedPorts: [Int]? | ||
|
||
public init() {} | ||
|
||
public func initializeDirectTCPIPChannel(_ channel: Channel, request: SSHChannelType.DirectTCPIP, context: SSHContext) -> EventLoopFuture<Void> { | ||
if let whitelistedHosts, !whitelistedHosts.contains(request.targetHost) { | ||
return channel.eventLoop.makeFailedFuture(Error.forbidden) | ||
} | ||
|
||
if let whitelistedPorts, !whitelistedPorts.contains(request.targetPort) { | ||
return channel.eventLoop.makeFailedFuture(Error.forbidden) | ||
} | ||
|
||
return ClientBootstrap(group: channel.eventLoop) | ||
.connect(host: request.targetHost, port: request.targetPort) | ||
.flatMap { remote in | ||
channel.pipeline.addHandlers([ | ||
DataToBufferCodec() | ||
]).flatMap { | ||
channel.pipeline.addHandler(ProxyChannelHandler { data, promise in | ||
remote.writeAndFlush(data, promise: promise) | ||
}) | ||
}.flatMap { | ||
remote.pipeline.addHandler(ProxyChannelHandler { [weak channel] data, promise in | ||
guard let channel else { | ||
promise?.fail(ChannelError.ioOnClosedChannel) | ||
return | ||
} | ||
channel.writeAndFlush(data, promise: promise) | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters