forked from Blendman974/faceclaw
EvenHub compatibility for iOS
This commit is contained in:
1 parent
617cdc39eb
commit
7e58c7c05f
40 files changed
+629
-176
No files matched your search
@@ -44,7 +44,7 @@
|
||||
</dict>
|
||||
</dict>
|
||||
<key>NSLocalNetworkUsageDescription</key>
|
||||
<string>Faceclaw connects to the terminal servers you configure on your local network.</string>
|
||||
<string>Faceclaw connects to the terminal servers and developer app URLs you configure on your local network.</string>
|
||||
<key>UILaunchScreen</key>
|
||||
<dict/>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
@interface FaceclawCrypto : NSObject
|
||||
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message;
|
||||
@end
|
||||
@@ -0,0 +1,12 @@
|
||||
#import "FaceclawCrypto.h"
|
||||
#import <CommonCrypto/CommonHMAC.h>
|
||||
#import <CommonCrypto/CommonDigest.h>
|
||||
@implementation FaceclawCrypto
|
||||
+ (NSString *)hmacSha256:(NSString *)secret message:(NSString *)message {
|
||||
NSData *key = [secret dataUsingEncoding:NSUTF8StringEncoding];
|
||||
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
|
||||
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
|
||||
CCHmac(kCCHmacAlgSHA256, key.bytes, key.length, data.bytes, data.length, digest);
|
||||
return [[NSData dataWithBytes:digest length:sizeof(digest)] base64EncodedStringWithOptions:0];
|
||||
}
|
||||
@end
|
||||
@@ -1,11 +1,12 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <WebKit/WebKit.h>
|
||||
|
||||
/** One local EHPK runtime, retained by its TS session until its window closes. */
|
||||
/** One local or URL EvenHub runtime, retained by its TS session until its window closes. */
|
||||
@interface FaceclawEvenHubWebView : NSObject <WKScriptMessageHandler, WKNavigationDelegate, WKURLSchemeHandler>
|
||||
@property(nonatomic, copy) void (^eventHandler)(NSString *json);
|
||||
@property(nonatomic, copy) NSString *packageIdentifier;
|
||||
- (void)start:(NSString *)directory entrypoint:(NSString *)entrypoint script:(NSString *)script;
|
||||
- (void)startURL:(NSString *)url script:(NSString *)script;
|
||||
- (void)evaluate:(NSString *)script;
|
||||
- (void)showOnPhone;
|
||||
- (void)hideOnPhone;
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
@property(nonatomic) UIButton *back;
|
||||
@property(nonatomic) NSTimer *timer;
|
||||
@property(nonatomic) NSString *directory;
|
||||
@property(nonatomic) NSURL *remoteURL;
|
||||
@property(nonatomic) BOOL ticking;
|
||||
@property(nonatomic) BOOL destroyed;
|
||||
@end
|
||||
@@ -21,6 +22,20 @@
|
||||
}
|
||||
- (void)start:(NSString *)directory entrypoint:(NSString *)entrypoint script:(NSString *)script {
|
||||
_directory = directory.stringByStandardizingPath.stringByResolvingSymlinksInPath;
|
||||
NSURLComponents *url = [NSURLComponents new];
|
||||
url.scheme = @"faceclaw-ehpk"; url.host = @"app"; url.path = [@"/" stringByAppendingString:entrypoint];
|
||||
[self startRequest:url.URL script:script];
|
||||
}
|
||||
- (void)startURL:(NSString *)url script:(NSString *)script {
|
||||
NSURL *target = [NSURL URLWithString:url];
|
||||
if (!target.host.length || ![@[@"http", @"https"] containsObject:target.scheme.lowercaseString] || target.user || target.password) {
|
||||
[self emit:@{@"kind": @"error", @"message": @"Invalid app URL."}]; return;
|
||||
}
|
||||
_remoteURL = target;
|
||||
[self startRequest:target script:script];
|
||||
}
|
||||
- (void)startRequest:(NSURL *)url script:(NSString *)script {
|
||||
if (_destroyed) return;
|
||||
WKWebViewConfiguration *config = [WKWebViewConfiguration new];
|
||||
// Isolate browser storage by package, retaining it across closing/reopening.
|
||||
// SDK storage also persists independently through the shared TS session.
|
||||
@@ -81,9 +96,7 @@
|
||||
}];
|
||||
}];
|
||||
[NSRunLoop.mainRunLoop addTimer:_timer forMode:NSRunLoopCommonModes];
|
||||
NSURLComponents *url = [NSURLComponents new];
|
||||
url.scheme = @"faceclaw-ehpk"; url.host = @"app"; url.path = [@"/" stringByAppendingString:entrypoint];
|
||||
[_webView loadRequest:[NSURLRequest requestWithURL:url.URL]];
|
||||
[_webView loadRequest:[NSURLRequest requestWithURL:url]];
|
||||
}
|
||||
- (void)evaluate:(NSString *)script {
|
||||
if (!_destroyed) [_webView evaluateJavaScript:script completionHandler:nil];
|
||||
@@ -104,16 +117,25 @@
|
||||
[_webView.configuration.userContentController removeAllUserScripts];
|
||||
[_host removeFromSuperview]; _host = nil; _back = nil; _webView = nil; _eventHandler = nil;
|
||||
}
|
||||
- (BOOL)allowsURL:(NSURL *)url {
|
||||
if (!_remoteURL) return [url.scheme isEqual:@"faceclaw-ehpk"] && [url.host isEqual:@"app"];
|
||||
NSNumber *port = url.port ?: @([url.scheme.lowercaseString isEqual:@"https"] ? 443 : 80);
|
||||
NSNumber *remotePort = _remoteURL.port ?: @([_remoteURL.scheme.lowercaseString isEqual:@"https"] ? 443 : 80);
|
||||
return [url.scheme.lowercaseString isEqual:_remoteURL.scheme.lowercaseString] &&
|
||||
[url.host.lowercaseString isEqual:_remoteURL.host.lowercaseString] && [port isEqual:remotePort];
|
||||
}
|
||||
- (void)userContentController:(WKUserContentController *)controller didReceiveScriptMessage:(WKScriptMessage *)message {
|
||||
// Subframes and navigated third-party documents never receive host access.
|
||||
NSURL *url = message.frameInfo.request.URL;
|
||||
if (!message.frameInfo.isMainFrame || ![url.scheme isEqual:@"faceclaw-ehpk"] || ![url.host isEqual:@"app"]) return;
|
||||
if (!message.frameInfo.isMainFrame || ![self allowsURL:url]) return;
|
||||
if ([message.body isKindOfClass:NSDictionary.class]) [self emit:message.body];
|
||||
}
|
||||
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)action decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
|
||||
NSURL *url = action.request.URL;
|
||||
BOOL local = [url.scheme isEqual:@"faceclaw-ehpk"] && [url.host isEqual:@"app"];
|
||||
decisionHandler(local && action.targetFrame.isMainFrame ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel);
|
||||
BOOL allowed = [self allowsURL:url] && action.targetFrame.isMainFrame;
|
||||
if (!allowed && action.targetFrame.isMainFrame)
|
||||
[self emit:@{@"kind": @"error", @"message": @"Navigation outside the app origin is blocked. Open the destination as a separate app URL."}];
|
||||
decisionHandler(allowed ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel);
|
||||
}
|
||||
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
|
||||
[self emit:@{@"kind": @"loaded"}];
|
||||
@@ -131,7 +153,7 @@
|
||||
NSURL *url = task.request.URL;
|
||||
NSString *relative = [url.path stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
|
||||
NSString *file = [[_directory stringByAppendingPathComponent:relative] stringByStandardizingPath].stringByResolvingSymlinksInPath;
|
||||
BOOL allowed = !_destroyed && [url.host isEqual:@"app"] &&
|
||||
BOOL allowed = !_destroyed && _directory.length && !_remoteURL && [url.host isEqual:@"app"] &&
|
||||
[file hasPrefix:[_directory stringByAppendingString:@"/"]];
|
||||
NSData *data = allowed ? [NSData dataWithContentsOfFile:file options:NSDataReadingMappedIfSafe error:nil] : nil;
|
||||
NSString *ext = file.pathExtension.lowercaseString;
|
||||
|
||||
@@ -4,4 +4,5 @@
|
||||
+ (UIImage *)preview:(NSData *)gray width:(NSInteger)width height:(NSInteger)height green:(BOOL)green;
|
||||
+ (NSData *)renderSVG:(NSString *)svg size:(NSInteger)size;
|
||||
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height;
|
||||
+ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight;
|
||||
@end
|
||||
@@ -3,6 +3,29 @@
|
||||
#import <ImageIO/ImageIO.h>
|
||||
|
||||
@implementation FaceclawGraphics
|
||||
+ (NSData *)decodeImageFile:(NSString *)path maxWidth:(NSInteger)maxWidth maxHeight:(NSInteger)maxHeight {
|
||||
if (maxWidth <= 0 || maxHeight <= 0 || maxWidth > 2048 || maxHeight > 2048) return nil;
|
||||
NSURL *url = [NSURL fileURLWithPath:path];
|
||||
CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
|
||||
if (!source) return nil;
|
||||
NSDictionary *properties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, 0, NULL));
|
||||
CFRelease(source);
|
||||
double width = [properties[(id)kCGImagePropertyPixelWidth] doubleValue];
|
||||
double height = [properties[(id)kCGImagePropertyPixelHeight] doubleValue];
|
||||
if (width <= 0 || height <= 0) return nil;
|
||||
NSInteger orientation = [properties[(id)kCGImagePropertyOrientation] integerValue];
|
||||
if (orientation >= 5 && orientation <= 8) { double swap = width; width = height; height = swap; }
|
||||
double scale = MIN(1.0, MIN(maxWidth / width, maxHeight / height));
|
||||
NSInteger w = MAX(1, lround(width * scale)), h = MAX(1, lround(height * scale));
|
||||
NSData *bytes = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:nil];
|
||||
NSData *gray = [self decodeImage:bytes width:w height:h];
|
||||
if (!gray) return nil;
|
||||
// Same dimensions + pixels packet used by the shared image adapter.
|
||||
uint8_t dimensions[] = { w & 255, (w >> 8) & 255, h & 255, (h >> 8) & 255 };
|
||||
NSMutableData *packet = [NSMutableData dataWithBytes:dimensions length:4];
|
||||
[packet appendData:gray];
|
||||
return packet;
|
||||
}
|
||||
+ (NSData *)decodeImage:(NSData *)data width:(NSInteger)width height:(NSInteger)height {
|
||||
if (!data.length || width <= 0 || height <= 0 || width > 2048 || height > 2048) return nil;
|
||||
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
||||
|
||||
@@ -9,5 +9,6 @@ module FaceclawGraphics {
|
||||
header "FaceclawLocation.h"
|
||||
header "FaceclawConfigPort.h"
|
||||
header "FaceclawEvenHubWebView.h"
|
||||
header "FaceclawCrypto.h"
|
||||
export *
|
||||
}
|
||||
Reference in new issue
Block a user