-
Notifications
You must be signed in to change notification settings - Fork 125
Implement SOCKS proxy functionality #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5a49c2c
Basic socks functionality
Davidde94 b45f4d9
Remove auth field
Davidde94 8e1232a
Remove test addresses
Davidde94 974677f
Working tests
Davidde94 122f650
Update packages
Davidde94 f1598be
Merge branch 'main' into de/socks-client
Davidde94 481e1b1
Rename to MockSOCKSServer
Davidde94 064edcb
Code review cleanup
Davidde94 210c4d7
Cleanup public authorization
Davidde94 18c7103
Throw error on auth data
Davidde94 323eefd
Add missing socks server test
Davidde94 c273847
Fabians test cleanup
Davidde94 3c0e120
Remove redundant auth complete write
Davidde94 ba629a4
Add test for speaking to wrong server type
Davidde94 e4c1b52
Add misbehaving server test
Davidde94 d718299
Guard against multiple requests
Davidde94 eee59e3
Soundness
Davidde94 112d5b1
Soundness
Davidde94 976fe2e
Update Sources/AsyncHTTPClient/HTTPClientProxyHandler.swift
Davidde94 ea04cfa
Support bogus addresses
Davidde94 ddc009a
Merge branch 'de/socks-client' of github.com:Davidde94/async-http-cli…
Davidde94 221420f
Swift format
Davidde94 91a1636
Soundness
Davidde94 15e5334
Address PR comments
Davidde94 c61184e
Cory PR nits
Davidde94 97c6b7f
Soundness
Davidde94 dd405af
Fabian nit
Davidde94 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the AsyncHTTPClient project authors | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@testable import AsyncHTTPClient | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import NIO | ||
import NIOHTTP1 | ||
import NIOSOCKS | ||
import XCTest | ||
|
||
class MockSocksServer { | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
let channel: Channel | ||
|
||
public init(expectedURL: String, expectedResponse: String, file: String = (#file), line: UInt = #line) throws { | ||
let elg = MultiThreadedEventLoopGroup(numberOfThreads: 1) | ||
let bootstrap = ServerBootstrap.init(group: elg) | ||
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1) | ||
.childChannelInitializer { channel in | ||
let handshakeHandler = SOCKSServerHandshakeHandler() | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return channel.pipeline.addHandlers([ | ||
handshakeHandler, | ||
SOCKSTestHandler(handshakeHandler: handshakeHandler), | ||
SOCKSTestHTTPClient(expectedURL: expectedURL, expectedResponse: expectedResponse, file: file, line: line) | ||
]) | ||
} | ||
self.channel = try bootstrap.bind(host: "127.0.0.1", port: 1080).wait() | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func shutdown() throws { | ||
try self.channel.close().wait() | ||
} | ||
|
||
} | ||
|
||
class SOCKSTestHTTPClient: ChannelInboundHandler { | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
typealias InboundIn = HTTPServerRequestPart | ||
typealias OutboundOut = HTTPServerResponsePart | ||
|
||
let expectedURL: String | ||
let expectedResponse: String | ||
let file: String | ||
let line: UInt | ||
|
||
init(expectedURL: String, expectedResponse: String, file: String, line: UInt) { | ||
self.expectedURL = expectedURL | ||
self.expectedResponse = expectedResponse | ||
self.file = file | ||
self.line = line | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let message = self.unwrapInboundIn(data) | ||
switch message { | ||
case .head(let head): | ||
XCTAssertEqual(head.uri, self.expectedURL) | ||
Lukasa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case .body: | ||
break | ||
case .end: | ||
context.write(self.wrapOutboundOut(.head(.init(version: .http1_1, status: .ok))), promise: nil) | ||
context.write(self.wrapOutboundOut(.body(.byteBuffer(context.channel.allocator.buffer(string: self.expectedResponse)))), promise: nil) | ||
context.writeAndFlush(self.wrapOutboundOut(.end(nil)), promise: nil) | ||
} | ||
} | ||
} | ||
|
||
class SOCKSTestHandler: ChannelInboundHandler, RemovableChannelHandler { | ||
|
||
typealias InboundIn = ClientMessage | ||
|
||
let handshakeHandler: SOCKSServerHandshakeHandler | ||
|
||
init(handshakeHandler: SOCKSServerHandshakeHandler) { | ||
self.handshakeHandler = handshakeHandler | ||
} | ||
|
||
func channelRead(context: ChannelHandlerContext, data: NIOAny) { | ||
let message = self.unwrapInboundIn(data) | ||
switch message { | ||
case .greeting: | ||
context.writeAndFlush(.init( | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ServerMessage.selectedAuthenticationMethod(.init(method: .noneRequired))), promise: nil) | ||
context.writeAndFlush(.init( | ||
ServerMessage.authenticationData(context.channel.allocator.buffer(capacity: 0), complete: true)), promise: nil) | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case .authenticationData: | ||
break | ||
Davidde94 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case .request(let request): | ||
context.writeAndFlush(.init( | ||
ServerMessage.response(.init(reply: .succeeded, boundAddress: request.addressType))), promise: nil) | ||
context.channel.pipeline.addHandlers([ | ||
ByteToMessageHandler(HTTPRequestDecoder()), | ||
HTTPResponseEncoder(), | ||
], position: .after(self)).whenSuccess { | ||
context.channel.pipeline.removeHandler(self, promise: nil) | ||
context.channel.pipeline.removeHandler(self.handshakeHandler, promise: nil) | ||
} | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.