Skip to content

Commit 35c6ebd

Browse files
committed
Initial value
1 parent 5b6ca5b commit 35c6ebd

File tree

2 files changed

+92
-81
lines changed

2 files changed

+92
-81
lines changed

Sources/AndroidInjection.swift

+90-80
Original file line numberDiff line numberDiff line change
@@ -29,91 +29,25 @@ protocol Kinded {
2929
open class AndroidInjection {
3030

3131
open class func connectAndRun(forMainThread: @escaping (@escaping () -> ()) -> ()) {
32+
if androidInjectionHost == "NNN.NNN.NNN.NNN" {
33+
NSLog("Injection: androidInjectionHost has not been updated, try build again")
34+
return
35+
}
36+
3237
DispatchQueue.global(qos: .background).async {
3338
var serverSocket: Int32 = -1
3439
while true {
35-
serverSocket = connectTo(ipAddress: androidInjectionHost, INJECTION_APPNAME: "Injection")
36-
if serverSocket >= 0 {
37-
break
38-
}
39-
Thread.sleep(forTimeInterval: 10)
40-
}
41-
42-
let serverWrite = fdopen(serverSocket, "w")
43-
NSLog("Injection: Connected to \(androidInjectionHost)")
44-
45-
var value: Int32 = Int32(INJECTION_PORT)
46-
let valueLength = MemoryLayout.size(ofValue: value)
47-
if serverWrite == nil || fwrite(&value, 1, valueLength, serverWrite) != valueLength {
48-
NSLog("Injection: Could not write magic to %d %p: %s", serverSocket, serverWrite!, strerror(errno))
49-
return
50-
}
51-
52-
if !#file.withCString( {
53-
filepath in
54-
value = Int32(strlen(filepath))
55-
return fwrite(&value, 1, valueLength, serverWrite) == valueLength &&
56-
fwrite(filepath, 1, Int(value), serverWrite) == value
57-
} ) {
58-
NSLog("Injection: Could not write filepath to %d %p: %s", serverSocket, serverWrite!, strerror(errno))
59-
return
60-
}
61-
fflush(serverWrite)
62-
63-
var injectionNumber = 0
64-
let serverRead = fdopen(serverSocket, "r")
65-
var compressedLength: Int32 = 0, uncompressedLength: Int32 = 0
66-
67-
while fread(&compressedLength, 1, valueLength, serverRead) == valueLength &&
68-
fread(&uncompressedLength, 1, valueLength, serverRead) == valueLength,
69-
var compressedBuffer = malloc(Int(compressedLength)),
70-
var uncompressedBuffer = malloc(Int(uncompressedLength)),
71-
fread(compressedBuffer, 1, Int(compressedLength), serverRead) == compressedLength {
72-
defer {
73-
free(uncompressedBuffer)
74-
free(compressedBuffer)
75-
}
76-
if compressedLength == valueLength && uncompressedLength == valueLength {
77-
continue
78-
}
79-
80-
NSLog("Injection: received %d/%d bytes", compressedLength, uncompressedLength)
81-
82-
var destLen = uLongf(uncompressedLength)
83-
if uncompress(uncompressedBuffer.assumingMemoryBound(to: Bytef.self), &destLen,
84-
compressedBuffer.assumingMemoryBound(to: Bytef.self),
85-
uLong(compressedLength)) != Z_OK || destLen != uLongf(uncompressedLength) {
86-
NSLog("Injection: uncompression failure")
40+
while true {
41+
serverSocket = connectTo(ipAddress: androidInjectionHost, INJECTION_APPNAME: "Injection")
42+
if serverSocket >= 0 {
8743
break
8844
}
45+
Thread.sleep(forTimeInterval: 10)
46+
}
8947

90-
injectionNumber += 1
91-
let libraryPath = NSTemporaryDirectory()+"injection\(injectionNumber).so"
92-
let libraryFILE = fopen(libraryPath, "w")
93-
if libraryFILE == nil || fwrite(uncompressedBuffer, 1, Int(uncompressedLength), libraryFILE) != uncompressedLength {
94-
NSLog("Injection: Could not write library file")
95-
break
96-
}
97-
fclose(libraryFILE)
98-
99-
forMainThread( {
100-
NSLog("Injection: Wrote to \(libraryPath), injecting...")
101-
let error = loadAndInject(library: libraryPath)
102-
var status = Int32(error == nil ? 0 : strlen(error))
103-
if fwrite(&status, 1, valueLength, serverWrite) != valueLength {
104-
NSLog("Injection: Could not write status")
105-
}
106-
if error != nil && fwrite(error, 1, Int(status), serverWrite) != status {
107-
NSLog("Injection: Could not write error string")
108-
}
109-
fflush(serverWrite)
110-
NSLog("Injection complete.")
111-
} )
48+
service(serverSocket: serverSocket, forMainThread: forMainThread)
49+
Thread.sleep(forTimeInterval: 10)
11250
}
113-
114-
NSLog("Injection loop exits")
115-
fclose(serverWrite)
116-
fclose(serverRead)
11751
}
11852
}
11953

@@ -140,11 +74,88 @@ open class AndroidInjection {
14074
else {
14175
return loaderSocket
14276
}
143-
14477
close(loaderSocket)
14578
return -1
14679
}
14780

81+
open class func service(serverSocket: Int32, forMainThread: @escaping (@escaping () -> ()) -> ()) {
82+
NSLog("Injection: Connected to \(androidInjectionHost)")
83+
84+
let serverWrite = fdopen(serverSocket, "w")
85+
var value: Int32 = Int32(INJECTION_PORT)
86+
let valueLength = MemoryLayout.size(ofValue: value)
87+
if serverWrite == nil || fwrite(&value, 1, valueLength, serverWrite) != valueLength {
88+
NSLog("Injection: Could not write magic to %d %p: %s", serverSocket, serverWrite!, strerror(errno))
89+
return
90+
}
91+
92+
if !#file.withCString( {
93+
filepath in
94+
value = Int32(strlen(filepath))
95+
return fwrite(&value, 1, valueLength, serverWrite) == valueLength &&
96+
fwrite(filepath, 1, Int(value), serverWrite) == value
97+
} ) {
98+
NSLog("Injection: Could not write filepath to %d %p: %s", serverSocket, serverWrite!, strerror(errno))
99+
return
100+
}
101+
fflush(serverWrite)
102+
103+
let serverRead = fdopen(serverSocket, "r")
104+
var compressedLength: Int32 = 0, uncompressedLength: Int32 = 0, injectionNumber = 0
105+
106+
while fread(&compressedLength, 1, valueLength, serverRead) == valueLength &&
107+
fread(&uncompressedLength, 1, valueLength, serverRead) == valueLength,
108+
var compressedBuffer = malloc(Int(compressedLength)),
109+
var uncompressedBuffer = malloc(Int(uncompressedLength)),
110+
fread(compressedBuffer, 1, Int(compressedLength), serverRead) == compressedLength {
111+
defer {
112+
free(compressedBuffer)
113+
free(uncompressedBuffer)
114+
}
115+
if compressedLength == valueLength && uncompressedLength == valueLength {
116+
continue
117+
}
118+
119+
NSLog("Injection: received %d/%d bytes", compressedLength, uncompressedLength)
120+
121+
var destLen = uLongf(uncompressedLength)
122+
if uncompress(uncompressedBuffer.assumingMemoryBound(to: Bytef.self), &destLen,
123+
compressedBuffer.assumingMemoryBound(to: Bytef.self),
124+
uLong(compressedLength)) != Z_OK || destLen != uLongf(uncompressedLength) {
125+
NSLog("Injection: uncompression failure")
126+
break
127+
}
128+
129+
injectionNumber += 1
130+
let libraryPath = NSTemporaryDirectory()+"injection\(injectionNumber).so"
131+
let libraryFILE = fopen(libraryPath, "w")
132+
if libraryFILE == nil ||
133+
fwrite(uncompressedBuffer, 1, Int(uncompressedLength), libraryFILE) != uncompressedLength {
134+
NSLog("Injection: Could not write library file")
135+
break
136+
}
137+
fclose(libraryFILE)
138+
139+
forMainThread( {
140+
NSLog("Injection: Wrote to \(libraryPath), injecting...")
141+
let error = loadAndInject(library: libraryPath)
142+
var status = Int32(error == nil ? 0 : strlen(error))
143+
if fwrite(&status, 1, valueLength, serverWrite) != valueLength {
144+
NSLog("Injection: Could not write status")
145+
}
146+
if error != nil && fwrite(error, 1, Int(status), serverWrite) != status {
147+
NSLog("Injection: Could not write error string")
148+
}
149+
fflush(serverWrite)
150+
NSLog("Injection complete.")
151+
} )
152+
}
153+
154+
NSLog("Injection loop exits")
155+
fclose(serverWrite)
156+
fclose(serverRead)
157+
}
158+
148159
open class func loadAndInject(library: String) -> UnsafePointer<Int8>? {
149160
guard let libHandle = dlopen(library, Int32(RTLD_LAZY)) else {
150161
let error = dlerror()
@@ -247,4 +258,3 @@ open class AndroidInjection {
247258
memcpy(byteAddr(existingClass) + vtableOffset, byteAddr(classMetadata) + vtableOffset, vtableLength)
248259
}
249260
}
250-

Sources/AndroidInjectionHost.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
let androidInjectionHost = "192.168.1.12"
1+
// Updated by ~/.gradle/scripts/swift-build.sh
2+
let androidInjectionHost = "NNN.NNN.NNN.NNN"

0 commit comments

Comments
 (0)