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

Commit

Permalink
move all ObjC.classes out of global context
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiChou committed Nov 22, 2020
1 parent f2159d8 commit 4736ada
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 33 deletions.
11 changes: 5 additions & 6 deletions agent/app/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import uuidv4 from './lib/uuid'
import { open } from './lib/libc'
import { getDataAttrForPath } from './lib/foundation'

const { NSFileManager, NSProcessInfo, NSDictionary, NSBundle } = ObjC.classes

const fileManager = NSFileManager.defaultManager()


export function ls(path, root) {
const { NSFileManager, NSProcessInfo, NSBundle } = ObjC.classes

const prefix = root === 'bundle'
? NSBundle.mainBundle().bundlePath().toString()
: NSProcessInfo.processInfo().environment().objectForKey_('HOME').toString()

const pErr = Memory.alloc(Process.pointerSize)
Memory.writePointer(pErr, NULL)
const cwd = [prefix, path].join('/')
const nsArray = fileManager.contentsOfDirectoryAtPath_error_(cwd, pErr)
const nsArray = NSFileManager.defaultManager().contentsOfDirectoryAtPath_error_(cwd, pErr)
const err = Memory.readPointer(pErr)

if (!err.isNull()) {
Expand All @@ -30,7 +28,7 @@ export function ls(path, root) {
const isDir = Memory.alloc(Process.pointerSize)
const list = arrayFromNSArray(nsArray, 100).map((filename) => {
const fullPath = [prefix, path, filename].join('/')
fileManager.fileExistsAtPath_isDirectory_(fullPath, isDir)
NSFileManager.defaultManager().fileExistsAtPath_isDirectory_(fullPath, isDir)

return {
/* eslint eqeqeq:0 */
Expand All @@ -46,6 +44,7 @@ export function ls(path, root) {


export function plist(path) {
const { NSDictionary } = ObjC.classes
const info = NSDictionary.dictionaryWithContentsOfFile_(path)
if (info === null)
throw new Error(`malformed plist file: ${path}`)
Expand Down
5 changes: 2 additions & 3 deletions agent/app/info.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { toJSON } from './lib/nsdict'
import { NSTemporaryDirectory } from './lib/foundation'

const { NSBundle, NSProcessInfo, NSUserDefaults } = ObjC.classes


export function info() {
const { NSBundle, NSProcessInfo } = ObjC.classes
const mainBundle = NSBundle.mainBundle()
const json = toJSON(mainBundle.infoDictionary())
const data = NSProcessInfo.processInfo()
Expand Down Expand Up @@ -46,5 +45,5 @@ export function info() {


export function userDefaults() {
return toJSON(NSUserDefaults.alloc().init().dictionaryRepresentation())
return toJSON(ObjC.classes.NSUserDefaults.alloc().init().dictionaryRepresentation())
}
10 changes: 4 additions & 6 deletions agent/app/keychain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { NSMutableDictionary } = ObjC.classes


const SecItemCopyMatching = new NativeFunction(ptr(Module.findExportByName('Security', 'SecItemCopyMatching')), 'pointer', ['pointer', 'pointer'])
const SecItemDelete = new NativeFunction(ptr(Module.findExportByName('Security', 'SecItemDelete')), 'pointer', ['pointer'])
Expand All @@ -9,8 +7,6 @@ const SecAccessControlGetConstraints = new NativeFunction(
)


const kCFBooleanTrue = ObjC.classes.__NSCFBoolean.numberWithBool_(true)

/* eslint no-unused-vars: 0 */
const kSecReturnAttributes = 'r_Attributes',
kSecReturnData = 'r_Data',
Expand Down Expand Up @@ -155,7 +151,7 @@ function decodeAcl(entry) {
if (constraints.isNull())
return []

const accessControls = ObjC.Object(constraints)
const accessControls = new ObjC.Object(constraints)
const flags = []
const enumerator = accessControls.keyEnumerator()
for (let key = enumerator.nextObject(); key !== null; key = enumerator.nextObject()) {
Expand All @@ -181,9 +177,10 @@ function decodeAcl(entry) {


export function list() {
const kCFBooleanTrue = ObjC.classes.__NSCFBoolean.numberWithBool_(true)
const result = []

const query = NSMutableDictionary.alloc().init()
const query = ObjC.classes.NSMutableDictionary.alloc().init()
query.setObject_forKey_(kCFBooleanTrue, kSecReturnAttributes)
query.setObject_forKey_(kCFBooleanTrue, kSecReturnData)
query.setObject_forKey_(kCFBooleanTrue, kSecReturnRef)
Expand Down Expand Up @@ -231,6 +228,7 @@ export function list() {
}

export function clear() {
const { NSMutableDictionary } = ObjC.classes
// keychain item times to query for
kSecClasses.forEach((clazz) => {
const query = NSMutableDictionary.alloc().init()
Expand Down
20 changes: 6 additions & 14 deletions agent/app/lib/nsdict.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,12 @@ import { hasOwnProperty } from './utils'
// eslint-disable-next-line
null;


const {
NSMutableDictionary,
NSArray,
NSData,
NSDictionary,
NSMutableArray,
NSNumber,
NSString,
NSNull,
NSPropertyListSerialization,
__NSCFBoolean
} = ObjC.classes

const NSPropertyListImmutable = 0


export function toJSON(value) {
const { NSArray, NSDictionary, NSNumber } = ObjC.classes

if (value === null || typeof value !== 'object')
return value
if (value.isKindOfClass_(NSArray))
Expand All @@ -48,6 +36,8 @@ export function dictFromNSDictionary(nsDict) {
}

export function dictFromPlistCharArray(address, size) {
const { NSData, NSPropertyListSerialization } = ObjC.classes

const format = Memory.alloc(Process.pointerSize)
const err = Memory.alloc(Process.pointerSize)
const data = NSData.dataWithBytesNoCopy_length_(address, size)
Expand Down Expand Up @@ -79,6 +69,8 @@ export function arrayFromNSArray(nsArray, max) {
}

export function toNSObject(obj) {
const { NSMutableDictionary, NSMutableArray, NSString, NSNull, __NSCFBoolean } = ObjC.classes

// not tested, may be buggy
if ('isKindOfClass_' in obj)
return obj
Expand Down
3 changes: 1 addition & 2 deletions agent/app/pasteboard.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const { UIPasteboard } = ObjC.classes
const subject = 'pasteboard'

let current = null
let timer = null

export function start() {
const pasteboard = UIPasteboard.generalPasteboard()
const pasteboard = ObjC.classes.UIPasteboard.generalPasteboard()
timer = setInterval(() => {
let str = pasteboard.string()
if (!str)
Expand Down
4 changes: 2 additions & 2 deletions agent/app/screenshot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { NSThread, UIScreen, UIApplication } = ObjC.classes

const CGFloat = (Process.pointerSize === 4) ? 'float' : 'double'
const CGSize = [CGFloat, CGFloat]

Expand All @@ -24,6 +22,7 @@ const UIImagePNGRepresentation = new NativeFunction(
)

function performOnMainThread(action) {
const { NSThread } = ObjC.classes
return new Promise((resolve, reject) => {
function performAction() {
try {
Expand All @@ -43,6 +42,7 @@ function performOnMainThread(action) {


export default function screenshot() {
const { UIScreen, UIApplication } = ObjC.classes
return performOnMainThread(() => {
const bounds = UIScreen.mainScreen().bounds()
const cgsize = bounds[1]
Expand Down

0 comments on commit 4736ada

Please sign in to comment.