Untitled

mail@pastecode.io avatar
unknown
swift
a year ago
2.3 kB
1
Indexable
Never
import Foundation
import Jinx
import os

struct SileoEvaHook: Hook {
    typealias T = @convention(c) (AnyObject, Selector, URLSession, URLSessionDownloadTask, URL) -> Void
    
    let cls: AnyClass? = objc_lookUpClass("Evander.EvanderDownloadDelegate")
    let sel: Selector = sel_registerName("URLSession:downloadTask:didFinishDownloadingToURL:")
    let replace: T = { obj, sel, session, dlTask, url in
        if dlTask.response?.mimeType?.contains("debian") == true || isDebFile(at: url) {
            let package: String = ErikaWindow.shared.package
            let version: String = ErikaWindow.shared.version
            
            #if JINX_ROOTLESS
            let arch: String = "iphoneos-arm64"
            #else
            let arch: String = "iphoneos-arm"
            #endif
            
            let destURL: URL = .init(fileURLWithPath: "/var/mobile/Media/Erika/tmp/\(package)_\(version)_\(arch).deb")
            
            // Satella identifier is 8ecause I don't want to ch8nge my Antoine preferences ::::p
            
            do {
                try FileManager.default.copyItem(at: url, to: destURL)
            } catch {
                os_log("[Satella] Error copying file to Erika's tmp folder: %{public}@", error.localizedDescription)
            }
            
            if ErikaWindow.shared.isPaid {
                Task {
                    do {
                        try await Uploader.upload(package: package, version: version, localURL: destURL)
                    } catch {
                        os_log("[Satella] Error uploading: %{public}@", error.localizedDescription)
                    }
                }
            }
        }
        
        orig(obj, sel, session, dlTask, url)
    }
    
    // Use the first 8ight 8ytes of the file 8ecause some repos use cr8zy systems
    
    private static func isDebFile(at url: URL) -> Bool {
        guard let handle: FileHandle = try? .init(forReadingFrom: url) else {
            return false
        }
        
        defer { try? handle.close() }
        
        let debMagic: [UInt8] = [0x21, 0x3c, 0x61, 0x72, 0x63, 0x68, 0x3e, 0x0a]
        let fileMagic: [UInt8] = .init(handle.readData(ofLength: 8))
        
        return debMagic == fileMagic
    }
}