Files
swift-nio-extras/Sources/NIOSOCKS/State/ClientStateMachine.swift
T
David Evans 806a0ead2c Implement a SOCKSv5 client (#128)
Implement a SOCKSv5 client according to RFC 1928. Server implementation will be added in another PR to keep sizes down.

https://datatracker.ietf.org/doc/html/rfc1928

A few meaningful changes:

Add all relevant types used across SOCKS clients and servers
Add a state machine used to manage a connection from a clients side
Add a channel handler that should be added at the very start of a channel pipeline
2021-06-10 12:12:30 +01:00

152 lines
5.1 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import NIO
enum ClientState: Hashable {
case inactive
case waitingForClientGreeting
case waitingForAuthenticationMethod(ClientGreeting)
case waitingForClientRequest
case waitingForServerResponse(SOCKSRequest)
case active
case error
}
enum ClientAction: Hashable {
case waitForMoreData
case sendGreeting
case sendRequest
case proxyEstablished
}
struct ClientStateMachine {
private var state: ClientState
var proxyEstablished: Bool {
switch self.state {
case .active:
return true
case .error, .inactive, .waitingForAuthenticationMethod, .waitingForClientGreeting, .waitingForClientRequest, .waitingForServerResponse:
return false
}
}
var shouldBeginHandshake: Bool {
switch self.state {
case .inactive:
return true
case .active, .error, .waitingForAuthenticationMethod, .waitingForClientGreeting, .waitingForClientRequest, .waitingForServerResponse:
return false
}
}
init() {
self.state = .inactive
}
}
// MARK: - Incoming
extension ClientStateMachine {
mutating func receiveBuffer(_ buffer: inout ByteBuffer) throws -> ClientAction {
do {
switch self.state {
case .waitingForAuthenticationMethod(let greeting):
guard let action = try self.handleSelectedAuthenticationMethod(&buffer, greeting: greeting) else {
return .waitForMoreData
}
return action
case .waitingForServerResponse(let request):
guard let action = try self.handleServerResponse(&buffer, request: request) else {
return .waitForMoreData
}
return action
case .active, .error, .inactive, .waitingForClientGreeting, .waitingForClientRequest:
throw SOCKSError.UnexpectedRead()
}
} catch {
self.state = .error
throw error
}
}
mutating func handleSelectedAuthenticationMethod(_ buffer: inout ByteBuffer, greeting: ClientGreeting) throws -> ClientAction? {
return try buffer.parseUnwindingIfNeeded { buffer -> ClientAction? in
guard let selected = try buffer.readMethodSelection() else {
return nil
}
guard greeting.methods.contains(selected.method) else {
throw SOCKSError.InvalidAuthenticationSelection(selection: selected.method)
}
// we don't current support any form of authentication
return self.authenticate(&buffer, method: selected.method)
}
}
mutating func handleServerResponse(_ buffer: inout ByteBuffer, request: SOCKSRequest) throws -> ClientAction? {
return try buffer.parseUnwindingIfNeeded { buffer -> ClientAction? in
guard let response = try buffer.readServerResponse() else {
return nil
}
guard response.reply == .succeeded else {
throw SOCKSError.ConnectionFailed(reply: response.reply)
}
self.state = .active
return .proxyEstablished
}
}
mutating func authenticate(_ buffer: inout ByteBuffer, method: AuthenticationMethod) -> ClientAction {
precondition(method == .noneRequired, "No authentication mechanism is supported. Use .noneRequired only.")
// we don't currently support any authentication
// so assume all is fine, and instruct the client
// to send the request
self.state = .waitingForClientRequest
return .sendRequest
}
}
// MARK: - Outgoing
extension ClientStateMachine {
mutating func connectionEstablished() throws -> ClientAction {
guard self.state == .inactive else {
throw SOCKSError.InvalidState(expected: .inactive, actual: self.state)
}
self.state = .waitingForClientGreeting
return .sendGreeting
}
mutating func sendClientGreeting(_ greeting: ClientGreeting) throws {
guard self.state == .waitingForClientGreeting else {
throw SOCKSError.InvalidState(expected: .waitingForClientGreeting, actual: self.state)
}
self.state = .waitingForAuthenticationMethod(greeting)
}
mutating func sendClientRequest(_ request: SOCKSRequest) throws {
guard self.state == .waitingForClientRequest else {
throw SOCKSError.InvalidState(expected: .waitingForClientRequest, actual: self.state)
}
self.state = .waitingForServerResponse(request)
}
}