Skip to content
This repository has been archived by the owner on Nov 28, 2020. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Qusic committed May 15, 2018
0 parents commit b7a5844
Show file tree
Hide file tree
Showing 48 changed files with 7,617 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.theos
.DS_Store
*.swp
*.deb
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2018 Qusic <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
TWEAK_NAME = skia
TOOL_NAME = skiad
BUNDLE_NAME = skiapref

skia_FILES = skia.cpp config.cpp posix.cpp netcore.cpp libc++/shared_mutex.cpp
skia_FRAMEWORKS = CoreFoundation CFNetwork JavaScriptCore
skia_LIBRARIES = substrate
skia_INSTALL_PATH = /Library/MobileSubstrate/DynamicLibraries

skiad_FILES = skiad.mm
skiad_PRIVATE_FRAMEWORKS = AppSupport
skiad_INSTALL_PATH = /usr/libexec

skiapref_FILES = skiapref.mm config.cpp
skiapref_RESOURCE_DIRS = res
skiapref_FRAMEWORKS = UIKit CoreGraphics CFNetwork JavaScriptCore
skiapref_PRIVATE_FRAMEWORKS = AppSupport Preferences
skiapref_LIBRARIES = substrate
skiapref_INSTALL_PATH = /Library/PreferenceBundles

export TARGET = iphone:clang
export ARCHS = armv7 armv7s arm64
export TARGET_IPHONEOS_DEPLOYMENT_VERSION = 7.0
export ADDITIONAL_CFLAGS = -fvisibility=hidden -isystem include
export ADDITIONAL_CCFLAGS = -std=c++1y
export ADDITIONAL_OBJCFLAGS = -fobjc-arc

include $(THEOS)/makefiles/common.mk
include $(THEOS_MAKE_PATH)/tweak.mk
include $(THEOS_MAKE_PATH)/tool.mk
include $(THEOS_MAKE_PATH)/bundle.mk

internal-stage::
$(ECHO_NOTHING)dir="$(THEOS_STAGING_DIR)/DEBIAN" && mkdir -p "$$dir" && cp scripts/postinst scripts/prerm "$$dir/"$(ECHO_END)
$(ECHO_NOTHING)dir="$(THEOS_STAGING_DIR)/Library/LaunchDaemons" && mkdir -p "$$dir" && cp $(TOOL_NAME).plist "$$dir/me.qusic.$(TOOL_NAME).plist"$(ECHO_END)
$(ECHO_NOTHING)dir="$(THEOS_STAGING_DIR)/Library/PreferenceLoader/Preferences" && mkdir -p "$$dir" && cp $(BUNDLE_NAME).plist "$$dir/$(TWEAK_NAME).plist"$(ECHO_END)
85 changes: 85 additions & 0 deletions config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "config.hpp"
#include <substrate.h>

void config::create_context() {
script_context = JSGlobalContextCreate(NULL);
for (const auto &native_function : native_functions) {
JSStringRef function_name = JSStringCreateWithUTF8CString(native_function.first.c_str());
JSObjectCallAsFunctionCallback function_callback = reinterpret_cast<JSObjectCallAsFunctionCallback>(MSFindSymbol(NULL, native_function.second.c_str()));
JSObjectRef function_object = JSObjectMakeFunctionWithCallback(script_context, function_name, function_callback);
JSObjectSetProperty(script_context, JSContextGetGlobalObject(script_context), function_name, function_object, 0, NULL);
JSStringRelease(function_name);
}
JSStringRef support_script = read_script("/Library/PreferenceBundles/skiapref.bundle/proxy.js");
JSEvaluateScript(script_context, support_script, NULL, NULL, 0, NULL);
JSStringRelease(support_script);
JSStringRef config_script = read_script("/User/Library/Preferences/me.qusic.skia.js");
JSEvaluateScript(script_context, config_script, NULL, NULL, 0, NULL);
JSStringRelease(config_script);
}

void config::release_context() {
JSGlobalContextRelease(script_context);
script_context = NULL;
}

JSStringRef config::read_script(const std::string &file) {
CFStringRef path = CFStringCreateWithCString(kCFAllocatorDefault, file.c_str(), CFStringGetSystemEncoding());
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path, kCFURLPOSIXPathStyle, FALSE);
CFRelease(path);
CFReadStreamRef stream = CFReadStreamCreateWithFile(kCFAllocatorDefault, url);
CFRelease(url);
if (stream == NULL) {
return JSStringCreateWithUTF8CString("");
}
if (CFReadStreamOpen(stream) == FALSE) {
CFRelease(stream);
return JSStringCreateWithUTF8CString("");
}
CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0);
UInt8 buffer[1024 * 8];
CFIndex read = 0;
while ((read = CFReadStreamRead(stream, buffer, sizeof(buffer))) > 0) {
CFDataAppendBytes(data, buffer, read);
}
CFReadStreamClose(stream);
CFRelease(stream);
if (read == -1) {
CFRelease(data);
return JSStringCreateWithUTF8CString("");
}
CFStringRef string = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, CFDataGetBytePtr(data), CFDataGetLength(data), kCFStringEncodingUTF8, TRUE, kCFAllocatorNull);
JSStringRef script = JSStringCreateWithCFString(string);
CFRelease(string);
CFRelease(data);
return script;
}

void config::execute(const std::function<void(JSGlobalContextRef)> &code) {
code(script_context);
}

std::string config::evaluate(const std::string &code) {
std::string result;
execute([&](JSGlobalContextRef context) {
JSStringRef script = JSStringCreateWithUTF8CString(code.c_str());
JSValueRef exception = NULL;
JSValueRef value = JSEvaluateScript(context, script, NULL, NULL, 0, &exception);
JSStringRelease(script);
JSStringRef value_string = NULL;
if (exception) {
value_string = JSValueToStringCopy(context, exception, NULL);
} else {
if (JSValueIsObject(context, value)) {
value_string = JSValueCreateJSONString(context, value, 2, NULL) ?: JSValueToStringCopy(context, value, NULL);
} else {
value_string = JSValueToStringCopy(context, value, NULL);
}
}
char buffer[1024 * 8];
JSStringGetUTF8CString(value_string, buffer, sizeof(buffer));
JSStringRelease(value_string);
result = buffer;
});
return result;
}
22 changes: 22 additions & 0 deletions config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <string>
#include <unordered_map>
#include <functional>
#include <CoreFoundation/CoreFoundation.h>
#include <JavaScriptCore/JavaScriptCore.h>

class config {
private:
JSGlobalContextRef script_context;
const std::unordered_map<std::string, std::string> native_functions = {
{"__skia_primaryAddresses", "__ZL39_JSPrimaryIpv4AddressesFunctionCallbackPK15OpaqueJSContextP13OpaqueJSValueS3_mPKPKS2_PS5_"},
{"__skia_dnsResolve", "__ZL29_JSDnsResolveFunctionCallbackPK15OpaqueJSContextP13OpaqueJSValueS3_mPKPKS2_PS5_"},
};
void create_context();
void release_context();
JSStringRef read_script(const std::string &file);
public:
config() { create_context(); }
~config() { release_context(); }
void execute(const std::function<void(JSGlobalContextRef)> &code);
std::string evaluate(const std::string &code);
};
12 changes: 12 additions & 0 deletions control
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Package: me.qusic.skia
Name: skia
Version: 0.1
Description: Transparent SOCKS Redirector
Section: Tweaks
Priority: optional
Depends: mobilesubstrate, preferenceloader, me.qusic.shadowsocks
Architecture: iphoneos-arm
Author: Qusic <[email protected]>
Maintainer: Qusic <[email protected]>
Sponsor: Qusic <[email protected]>
Tag: purpose::extension
Loading

0 comments on commit b7a5844

Please sign in to comment.