Refactor to move PortForwardingMacApp under /desktop

This commit is contained in:
Colin Bendell
2021-12-01 20:19:55 -05:00
committed by Lorenzo Blasa
parent 1e1cc37c3b
commit b26997af00
11 changed files with 59 additions and 29 deletions

View File

@@ -63,7 +63,9 @@
53915CC92152E31C0090EEA6 /* Plugins */ = {isa = PBXFileReference; lastKnownFileType = folder; path = Plugins; sourceTree = SOURCE_ROOT; };
53D19A0520A4BA3600A371E3 /* FlipperKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FlipperKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
53D4C51120A5B89900613A96 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
5B3C0104984F1A2F624E5394 /* Pods-SonarKit.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SonarKit.release.xcconfig"; path = "Pods/Target Support Files/Pods-SonarKit/Pods-SonarKit.release.xcconfig"; sourceTree = "<group>"; };
AF8587A796104EAC5B080F7D /* Pods-FlipperKit.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlipperKit.release.xcconfig"; path = "Pods/Target Support Files/Pods-FlipperKit/Pods-FlipperKit.release.xcconfig"; sourceTree = "<group>"; };
C6253FA5661121EDD200B8A9 /* Pods-SonarKit.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SonarKit.debug.xcconfig"; path = "Pods/Target Support Files/Pods-SonarKit/Pods-SonarKit.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -82,6 +84,8 @@
256B672257477FB71D3FAD20 /* Pods */ = {
isa = PBXGroup;
children = (
C6253FA5661121EDD200B8A9 /* Pods-SonarKit.debug.xcconfig */,
5B3C0104984F1A2F624E5394 /* Pods-SonarKit.release.xcconfig */,
22B8670D786EA50E30082023 /* Pods-FlipperKit.debug.xcconfig */,
AF8587A796104EAC5B080F7D /* Pods-FlipperKit.release.xcconfig */,
);
@@ -230,7 +234,7 @@
53D199FC20A4BA3600A371E3 /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1310;
LastUpgradeCheck = 0940;
ORGANIZATIONNAME = Facebook;
TargetAttributes = {
53D19A0420A4BA3600A371E3 = {
@@ -246,7 +250,6 @@
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 53D199FB20A4BA3600A371E3;
productRefGroup = 53D19A0620A4BA3600A371E3 /* Products */;
@@ -335,7 +338,6 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -362,7 +364,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
@@ -397,7 +399,6 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
@@ -418,7 +419,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.2;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
@@ -457,13 +458,12 @@
"${PODS_ROOT}/Headers/Public/glog",
"${PODS_ROOT}/Headers/Public/CocoaAsyncSocket",
/usr/local/Cellar/openssl/1.0.2o_1/include,
/opt/homebrew/Cellar/boost/1.76.0/include,
"${SRCROOT}",
"${SRCROOT}/Plugins/**",
);
INFOPLIST_FILE = "$(SRCROOT)/FlipperKit/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",
@@ -540,7 +540,7 @@
);
INFOPLIST_FILE = "$(SRCROOT)/FlipperKit/Info.plist";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
OTHER_CPLUSPLUSFLAGS = (
"$(OTHER_CFLAGS)",

View File

@@ -1,18 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
@interface FKPortForwardingClient : NSObject
- (instancetype)init;
- (void)forwardConnectionsToPort:(NSUInteger)port;
- (void)connectToMultiplexingChannelOnPort:(NSUInteger)port;
- (void)close;
@end

View File

@@ -1,340 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "FKPortForwardingClient.h"
#import <CocoaAsyncSocket/GCDAsyncSocket.h>
#import <peertalk/PTChannel.h>
#import <peertalk/PTUSBHub.h>
#import "FKPortForwardingCommon.h"
static const NSTimeInterval ReconnectDelay = 1.0;
@interface FKPortForwardingClient ()<
GCDAsyncSocketDelegate,
PTChannelDelegate> {
NSUInteger _destPort;
NSUInteger _channelPort;
NSNumber* _connectingToDeviceID;
NSNumber* _connectedDeviceID;
NSDictionary* _connectedDeviceProperties;
BOOL _notConnectedQueueSuspended;
PTChannel* _connectedChannel;
dispatch_queue_t _notConnectedQueue;
dispatch_queue_t _clientSocketsQueue;
NSMutableDictionary* _clientSockets;
}
@property(atomic, readonly) NSNumber* connectedDeviceID;
@property(atomic, assign) PTChannel* connectedChannel;
@end
@implementation FKPortForwardingClient
@synthesize connectedDeviceID = _connectedDeviceID;
- (instancetype)init {
if (self = [super init]) {
_notConnectedQueue = dispatch_queue_create(
"FKPortForwarding.notConnectedQueue", DISPATCH_QUEUE_SERIAL);
_clientSocketsQueue = dispatch_queue_create(
"FKPortForwarding.clients", DISPATCH_QUEUE_SERIAL);
_clientSockets = [NSMutableDictionary dictionary];
}
return self;
}
- (void)forwardConnectionsToPort:(NSUInteger)port {
_destPort = port;
}
- (void)connectToMultiplexingChannelOnPort:(NSUInteger)port {
_channelPort = port;
[self startListeningForDevices];
[self enqueueConnectToLocalIPv4Port];
}
- (void)close {
[self.connectedChannel close];
}
- (PTChannel*)connectedChannel {
return _connectedChannel;
}
- (void)setConnectedChannel:(PTChannel*)connectedChannel {
_connectedChannel = connectedChannel;
if (!_connectedChannel) {
for (GCDAsyncSocket* sock in [_clientSockets objectEnumerator]) {
[sock setDelegate:nil];
[sock disconnect];
}
[_clientSockets removeAllObjects];
}
// Toggle the notConnectedQueue_ depending on if we are connected or not
if (!_connectedChannel && _notConnectedQueueSuspended) {
dispatch_resume(_notConnectedQueue);
_notConnectedQueueSuspended = NO;
} else if (_connectedChannel && !_notConnectedQueueSuspended) {
dispatch_suspend(_notConnectedQueue);
_notConnectedQueueSuspended = YES;
}
if (!_connectedChannel && _connectingToDeviceID) {
[self enqueueConnectToUSBDevice];
}
}
#pragma mark - PTChannelDelegate
- (void)ioFrameChannel:(PTChannel*)channel
didReceiveFrameOfType:(uint32_t)type
tag:(uint32_t)tag
payload:(PTData*)payload {
// NSLog(@"received %@, %u, %u, %@", channel, type, tag, payload);
if (type == FKPortForwardingFrameTypeOpenPipe) {
GCDAsyncSocket* sock =
[[GCDAsyncSocket alloc] initWithDelegate:self
delegateQueue:_clientSocketsQueue];
sock.userData = @(tag);
_clientSockets[@(tag)] = sock;
NSError* connectError;
if (![sock connectToHost:@"localhost"
onPort:_destPort
error:&connectError]) {
FBPFLog(
@"Failed to connect to local %lu - %@",
(unsigned long)_destPort,
connectError);
}
FBPFTrace(@"open socket (%d)", tag);
}
if (type == FKPortForwardingFrameTypeWriteToPipe) {
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock writeData:[NSData dataWithBytes:payload.data length:payload.length]
withTimeout:-1
tag:0];
FBPFTrace(@"channel -> socket (%d) %zu bytes", tag, payload.length);
}
if (type == FKPortForwardingFrameTypeClosePipe) {
GCDAsyncSocket* sock = _clientSockets[@(tag)];
[sock disconnectAfterWriting];
FBPFTrace(@"close socket (%d)", tag);
}
}
- (void)ioFrameChannel:(PTChannel*)channel didEndWithError:(NSError*)error {
if (_connectedDeviceID &&
[_connectedDeviceID isEqualToNumber:channel.userInfo]) {
[self didDisconnectFromDevice:_connectedDeviceID];
}
if (_connectedChannel == channel) {
FBPFTrace(@"Disconnected from %@", channel.userInfo);
self.connectedChannel = nil;
}
}
#pragma mark - GCDAsyncSocketDelegate
- (void)socket:(GCDAsyncSocket*)sock
didConnectToHost:(NSString*)host
port:(uint16_t)port {
FBPFTrace(
@"socket (%ld) connected to %@",
(long)[sock.userData integerValue],
host);
[sock readDataWithTimeout:-1 tag:0];
}
- (void)socketDidDisconnect:(GCDAsyncSocket*)sock withError:(NSError*)err {
UInt32 tag = [sock.userData unsignedIntValue];
[_clientSockets removeObjectForKey:@(tag)];
FBPFTrace(@"socket (%d) disconnected", (unsigned int)tag);
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeClosePipe
tag:tag
withPayload:nil
callback:nil];
}
- (void)socket:(GCDAsyncSocket*)sock didReadData:(NSData*)data withTag:(long)_ {
UInt32 tag = [sock.userData unsignedIntValue];
[_connectedChannel sendFrameOfType:FKPortForwardingFrameTypeWriteToPipe
tag:tag
withPayload:NSDataToGCDData(data)
callback:^(NSError* error) {
FBPFTrace(
@"channel -> socket (%d), %lu bytes",
(unsigned int)tag,
(unsigned long)data.length);
[sock readDataWithTimeout:-1 tag:0];
}];
}
#pragma mark - Wired device connections
- (void)startListeningForDevices {
NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
__weak typeof(self) weakSelf = self;
[nc addObserverForName:PTUSBDeviceDidAttachNotification
object:PTUSBHub.sharedHub
queue:nil
usingBlock:^(NSNotification* note) {
NSNumber* deviceID = [note.userInfo objectForKey:@"DeviceID"];
// NSLog(@"PTUSBDeviceDidAttachNotification: %@",
// note.userInfo);
FBPFTrace(@"PTUSBDeviceDidAttachNotification: %@", deviceID);
typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
dispatch_async(strongSelf->_notConnectedQueue, ^{
[strongSelf didAttachToDevice:deviceID note:note];
});
}];
[nc addObserverForName:PTUSBDeviceDidDetachNotification
object:PTUSBHub.sharedHub
queue:nil
usingBlock:^(NSNotification* note) {
NSNumber* deviceID = [note.userInfo objectForKey:@"DeviceID"];
// NSLog(@"PTUSBDeviceDidDetachNotification: %@",
// note.userInfo);
FBPFTrace(@"PTUSBDeviceDidDetachNotification: %@", deviceID);
[weakSelf didDetachFromDevice:deviceID];
}];
}
- (void)didAttachToDevice:(NSNumber*)deviceID note:(NSNotification*)note {
if (!_connectingToDeviceID ||
![deviceID isEqualToNumber:_connectingToDeviceID]) {
[self disconnectFromCurrentChannel];
_connectingToDeviceID = deviceID;
_connectedDeviceProperties = [note.userInfo objectForKey:@"Properties"];
[self enqueueConnectToUSBDevice];
}
}
- (void)didDetachFromDevice:(NSNumber*)deviceID {
if ([_connectingToDeviceID isEqualToNumber:deviceID]) {
_connectedDeviceProperties = nil;
_connectingToDeviceID = nil;
if (_connectedChannel) {
[_connectedChannel close];
}
}
}
- (void)didDisconnectFromDevice:(NSNumber*)deviceID {
FBPFLog(@"Disconnected from device #%@", deviceID);
if ([_connectedDeviceID isEqualToNumber:deviceID]) {
[self willChangeValueForKey:@"connectedDeviceID"];
_connectedDeviceID = nil;
[self didChangeValueForKey:@"connectedDeviceID"];
}
}
- (void)disconnectFromCurrentChannel {
if (_connectedDeviceID && _connectedChannel) {
[_connectedChannel close];
self.connectedChannel = nil;
}
}
- (void)enqueueConnectToLocalIPv4Port {
dispatch_async(_notConnectedQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self connectToLocalIPv4Port];
});
});
}
- (void)connectToLocalIPv4Port {
PTChannel* channel = [PTChannel channelWithDelegate:self];
channel.userInfo =
[NSString stringWithFormat:@"127.0.0.1:%lu", (unsigned long)_channelPort];
[channel
connectToPort:_channelPort
IPv4Address:INADDR_LOOPBACK
callback:^(NSError* error, PTAddress* address) {
if (error) {
if (error.domain == NSPOSIXErrorDomain &&
(error.code == ECONNREFUSED || error.code == ETIMEDOUT)) {
// this is an expected state
} else {
FBPFTrace(
@"Failed to connect to 127.0.0.1:%lu: %@",
(unsigned long)_channelPort,
error);
}
} else {
[self disconnectFromCurrentChannel];
self.connectedChannel = channel;
channel.userInfo = address;
FBPFLog(@"Connected to %@", address);
}
[self performSelector:@selector(enqueueConnectToLocalIPv4Port)
withObject:nil
afterDelay:ReconnectDelay];
}];
}
- (void)enqueueConnectToUSBDevice {
dispatch_async(_notConnectedQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self connectToUSBDevice];
});
});
}
- (void)connectToUSBDevice {
PTChannel* channel = [PTChannel channelWithDelegate:self];
channel.userInfo = _connectingToDeviceID;
channel.delegate = self;
[channel connectToPort:(int)_channelPort
overUSBHub:PTUSBHub.sharedHub
deviceID:_connectingToDeviceID
callback:^(NSError* error) {
[self didConnectToChannel:channel withError:error];
}];
}
- (void)didConnectToChannel:(PTChannel*)channel withError:(NSError*)error {
if (error) {
FBPFTrace(@"Failed to connect to device #%@: %@", channel.userInfo, error);
if (channel.userInfo == _connectingToDeviceID) {
[self performSelector:@selector(enqueueConnectToUSBDevice)
withObject:nil
afterDelay:ReconnectDelay];
}
} else {
_connectedDeviceID = _connectingToDeviceID;
self.connectedChannel = channel;
FBPFLog(
@"Connected to device #%@\n%@",
_connectingToDeviceID,
_connectedDeviceProperties);
}
}
@end

View File

@@ -1,25 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import <Foundation/Foundation.h>
#define FBPFTrace(...) /*NSLog(__VA_ARGS__)*/
#define FBPFLog(...) NSLog(__VA_ARGS__)
enum {
FKPortForwardingFrameTypeOpenPipe = 201,
FKPortForwardingFrameTypeWriteToPipe = 202,
FKPortForwardingFrameTypeClosePipe = 203,
};
static dispatch_data_t NSDataToGCDData(NSData* data) {
__block NSData* retainedData = data;
return dispatch_data_create(data.bytes, data.length, nil, ^{
retainedData = nil;
});
(void)retainedData;
}

View File

@@ -7,7 +7,6 @@ target 'FlipperKit' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
workspace 'FlipperKit.xcworkspace'
project 'FlipperKit.xcodeproj'
pod 'Flipper', :path => '../Flipper.podspec'
# Pods for SonarKit
@@ -19,11 +18,3 @@ target 'FlipperKit' do
pod 'SocketRocket', '~> 0.6.0'
end
target 'PortForwardingMacApp' do
#use_frameworks!
platform :osx, '11'
workspace 'FlipperKit.xcworkspace'
project '../PortForwardingMacApp.xcodeproj'
pod 'Flipper-PeerTalk', '~>0.0'
pod 'CocoaAsyncSocket', '~>7.6'
end

View File

@@ -65,4 +65,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: fca1274f05facf86edf8f6963fe77c29120d949c
COCOAPODS: 1.11.2
COCOAPODS: 1.10.2