减少刷新频率

This commit is contained in:
wanghongen
2023-07-07 22:36:42 +08:00
parent c523080e05
commit 75534d9004
12 changed files with 268 additions and 46 deletions

View File

@@ -18,9 +18,10 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
exit(EXIT_FAILURE)
}
let networkSettings = NEPacketTunnelNetworkSettings(tunnelRemoteAddress: "127.0.0.1")
networkSettings.mtu = 1500
NSLog(conf.debugDescription)
//http
let host = conf["proxtHost"] as! String
let host = conf["proxyHost"] as! String
let proxyPort = conf["proxyPort"] as! Int
let proxySettings = NEProxySettings()
proxySettings.httpEnabled = true
@@ -28,8 +29,9 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
proxySettings.httpsEnabled = true
proxySettings.httpsServer = NEProxyServer(address: host, port: proxyPort)
proxySettings.matchDomains = [""]
networkSettings.proxySettings = proxySettings
setTunnelNetworkSettings(networkSettings) {
error in
guard error == nil else {
@@ -43,23 +45,24 @@ class PacketTunnelProvider: NEPacketTunnelProvider {
}
NSLog("startTunnelend")
}
override func stopTunnel(with reason: NEProviderStopReason, completionHandler: @escaping () -> Void) {
completionHandler()
}
override func handleAppMessage(_ messageData: Data, completionHandler: ((Data?) -> Void)?) {
// Add code here to handle the message.
if let handler = completionHandler {
NSLog("handleAppMessage ", messageData.debugDescription)
handler(messageData)
}
}
override func sleep(completionHandler: @escaping () -> Void) {
// Add code here to get ready to sleep.
completionHandler()
}
override func wake() {
// Add code here to wake up.
}

View File

@@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 54;
objectVersion = 55;
objects = {
/* Begin PBXBuildFile section */
@@ -19,6 +19,8 @@
9B0912222A54593A001108B7 /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B0912212A54593A001108B7 /* NetworkExtension.framework */; };
9B0912252A54593A001108B7 /* PacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B0912242A54593A001108B7 /* PacketTunnelProvider.swift */; };
9B09122A2A54593A001108B7 /* ProxyPin.appex in Embed Foundation Extensions */ = {isa = PBXBuildFile; fileRef = 9B0912202A54593A001108B7 /* ProxyPin.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
9B70772D2A5718FB00F184A9 /* AudioManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B70772C2A5718FB00F184A9 /* AudioManager.swift */; };
9B7077362A5728B900F184A9 /* silence.mp3 in Resources */ = {isa = PBXBuildFile; fileRef = 9B7077352A5728B900F184A9 /* silence.mp3 */; };
B375908E625E0AED772FA2C0 /* Pods_RunnerTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D37E307095F2B3E689A68827 /* Pods_RunnerTests.framework */; };
/* End PBXBuildFile section */
@@ -92,6 +94,8 @@
9B0912242A54593A001108B7 /* PacketTunnelProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PacketTunnelProvider.swift; sourceTree = "<group>"; };
9B0912262A54593A001108B7 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9B0912272A54593A001108B7 /* ProxyPin.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = ProxyPin.entitlements; sourceTree = "<group>"; };
9B70772C2A5718FB00F184A9 /* AudioManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioManager.swift; sourceTree = "<group>"; };
9B7077352A5728B900F184A9 /* silence.mp3 */ = {isa = PBXFileReference; lastKnownFileType = audio.mp3; path = silence.mp3; sourceTree = "<group>"; };
D37E307095F2B3E689A68827 /* Pods_RunnerTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RunnerTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
E328C7F89A365CDC0EAD15C6 /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -192,6 +196,7 @@
97C146F01CF9000F007C117D /* Runner */ = {
isa = PBXGroup;
children = (
9B7077352A5728B900F184A9 /* silence.mp3 */,
9B09121A2A5457B3001108B7 /* VpnManager.swift */,
9B0912192A545757001108B7 /* Runner.entitlements */,
97C146FA1CF9000F007C117D /* Main.storyboard */,
@@ -202,6 +207,7 @@
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */,
74858FAE1ED2DC5600515810 /* AppDelegate.swift */,
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */,
9B70772C2A5718FB00F184A9 /* AudioManager.swift */,
);
path = Runner;
sourceTree = "<group>";
@@ -303,7 +309,7 @@
};
};
buildConfigurationList = 97C146E91CF9000F007C117D /* Build configuration list for PBXProject "Runner" */;
compatibilityVersion = "Xcode 9.3";
compatibilityVersion = "Xcode 13.0";
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
@@ -334,6 +340,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9B7077362A5728B900F184A9 /* silence.mp3 in Resources */,
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */,
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
@@ -460,6 +467,7 @@
files = (
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */,
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
9B70772D2A5718FB00F184A9 /* AudioManager.swift in Sources */,
9B09121B2A5457B3001108B7 /* VpnManager.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
@@ -805,7 +813,7 @@
INFOPLIST_FILE = ProxyPin/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ProxyPin;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 16.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
@@ -843,7 +851,7 @@
INFOPLIST_FILE = ProxyPin/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ProxyPin;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 16.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
@@ -878,7 +886,7 @@
INFOPLIST_FILE = ProxyPin/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = ProxyPin;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
IPHONEOS_DEPLOYMENT_TARGET = 16.2;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",

View File

@@ -29,15 +29,47 @@ import NetworkExtension
}
})
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func applicationWillTerminate(_ application: UIApplication) {
VpnManager.shared.disconnect()
}
override func applicationWillTerminate(_ application: UIApplication) {
VpnManager.shared.disconnect()
}
var timer: Timer?
var bgTask: UIBackgroundTaskIdentifier?
override func applicationDidEnterBackground(_ application: UIApplication) {
// timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerAction), userInfo: nil, repeats: true)
// RunLoop.current.add(timer!, forMode: RunLoop.Mode.common)
// bgTask = application.beginBackgroundTask(expirationHandler: nil)
}
@objc func timerAction() {
print(UIApplication.shared.backgroundTimeRemaining )
if UIApplication.shared.backgroundTimeRemaining < 60.0 {
let application = UIApplication.shared
bgTask = application.beginBackgroundTask(expirationHandler: nil)
}
}
var backgroundUpdateTask: UIBackgroundTaskIdentifier = UIBackgroundTaskIdentifier(rawValue: 0)
func endBackgroundUpdateTask() {
AudioManager.shared.openBackgroundAudioAutoplay = false
UIApplication.shared.endBackgroundTask(self.backgroundUpdateTask)
self.backgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
}
override func applicationWillResignActive(_ application: UIApplication) {
AudioManager.shared.openBackgroundAudioAutoplay = true
self.backgroundUpdateTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
self.endBackgroundUpdateTask()
})
}
override func applicationDidBecomeActive(_ application: UIApplication) {
self.endBackgroundUpdateTask()
}
}

View File

@@ -0,0 +1,157 @@
import Foundation
import AVFoundation
import UIKit
class AudioManager: NSObject {
static let shared = AudioManager()
fileprivate let audioSession = AVAudioSession.sharedInstance()
fileprivate var backgroundAudioPlayer: AVAudioPlayer?
fileprivate var backgroundTimeLength = 0
fileprivate var timer: Timer?
static let audioName = ""
//
var openBackgroundAudioAutoplay = false {
didSet {
if self.openBackgroundAudioAutoplay {
self.setupAudioSession()
self.setupBackgroundAudioPlayer()
} else {
if let player = self.backgroundAudioPlayer {
if player.isPlaying {
player.stop()
}
}
self.backgroundAudioPlayer = nil
try? self.audioSession.setActive(false, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
}
}
}
override init() {
super.init()
self.setupListener()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
private func setupAudioSession() {
do {
try self.audioSession.setCategory(AVAudioSession.Category.playback, options: AVAudioSession.CategoryOptions.mixWithOthers)
try self.audioSession.setActive(false)
} catch let error {
debugPrint("\(type(of:self)):\(error)")
}
}
private func setupBackgroundAudioPlayer() {
do {
self.backgroundAudioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "silence", ofType: "mp3")!))
} catch let error {
debugPrint("\(type(of:self)):\(error)")
}
self.backgroundAudioPlayer?.numberOfLoops = -1
self.backgroundAudioPlayer?.volume = 1
self.backgroundAudioPlayer?.delegate = self
}
//
private func setupListener() {
NotificationCenter.default.addObserver(self, selector: #selector(didEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(audioSessionInterruption(notification:)), name: AVAudioSession.interruptionNotification, object: nil)
}
}
// MARK: -
extension AudioManager {
///
@objc public func didEnterBackground() {
self.setupTimer()
guard self.openBackgroundAudioAutoplay else {return}
do {
try self.audioSession.setActive(true)
} catch let error {
debugPrint("\(type(of:self)):\(error))")
}
self.backgroundAudioPlayer?.prepareToPlay()
self.backgroundAudioPlayer?.play()
}
///
@objc public func didBecomeActive() {
self.removeTimer()
self.hintBackgroundTimeLength()
self.backgroundTimeLength = 0
guard self.openBackgroundAudioAutoplay else {return}
self.backgroundAudioPlayer?.pause()
do {
try self.audioSession.setActive(false, options: AVAudioSession.SetActiveOptions.notifyOthersOnDeactivation)
} catch let error {
debugPrint("\(type(of:self)):\(error))")
}
}
///
@objc fileprivate func audioSessionInterruption(notification: NSNotification) {
guard self.openBackgroundAudioAutoplay else {return}
guard let userinfo = notification.userInfo else {return}
guard let interruptionType: UInt = userinfo[AVAudioSessionInterruptionTypeKey] as! UInt? else {return}
if interruptionType == AVAudioSession.InterruptionType.began.rawValue {
//
debugPrint("\(type(of:self)): 中断开始 userinfo:\(userinfo)")
} else if interruptionType == AVAudioSession.InterruptionType.ended.rawValue {
//
debugPrint("\(type(of:self)): 中断结束 userinfo:\(userinfo)")
guard let player = self.backgroundAudioPlayer else {return}
if player.isPlaying == false {
debugPrint("\(type(of:self)): 音乐未播放,准备开始播放")
do {
try self.audioSession.setActive(true)
} catch let error {
debugPrint("\(type(of:self)):\(error)")
}
player.prepareToPlay()
player.play()
} else {
debugPrint("\(type(of:self)): 音乐正在播放")
}
}
}
}
// MARK: -
extension AudioManager {
fileprivate func setupTimer() {
self.removeTimer()
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(timerTask), userInfo: nil, repeats: true)
RunLoop.main.add(self.timer!, forMode: RunLoop.Mode.common)
// RunLoop.main.add(self.timer!, forMode: RunLoop.Mode.init(rawValue: ""))
}
fileprivate func removeTimer() {
self.timer?.invalidate()
self.timer = nil;
}
@objc func timerTask() {
self.backgroundTimeLength += 1
}
fileprivate func hintBackgroundTimeLength() {
let message = "本次后台持续时间:\(self.backgroundTimeLength)s"
print(message)
}
}
// MARK: -
extension AudioManager: AVAudioPlayerDelegate {
func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
}
func audioPlayerDecodeErrorDidOccur(_ player: AVAudioPlayer, error: Error?) {
debugPrint("\(type(of:self))" + error.debugDescription)
}
}

View File

@@ -2,6 +2,10 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
</array>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
@@ -26,8 +30,18 @@
<string>$(FLUTTER_BUILD_NUMBER)</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>processing</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
@@ -45,11 +59,6 @@
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
</dict>

View File

@@ -10,7 +10,6 @@
<array>
<string>allow-vpn</string>
</array>
<key>com.apple.security.application-groups</key>
<array>
<string>group.com.proxy.pin</string>

View File

@@ -14,7 +14,7 @@ enum VPNStatus {
class VpnManager{
var activeVPN: NETunnelProviderManager?;
public var proxtHost: String = "127.0.0.01"
public var proxyHost: String = "127.0.0.01"
public var proxyPort: Int = 8888
static let shared = VpnManager()
@@ -94,7 +94,7 @@ extension VpnManager{
}
var conf = [String:AnyObject]()
conf["proxtHost"] = self.proxtHost as AnyObject
conf["proxyHost"] = self.proxyHost as AnyObject
conf["proxyPort"] = self.proxyPort as AnyObject
let orignConf = manager.protocolConfiguration as! NETunnelProviderProtocol
@@ -141,7 +141,7 @@ extension VpnManager{
extension VpnManager{
func connect(host: String?, port: Int?){
self.proxtHost = host ?? self.proxtHost
self.proxyHost = host ?? self.proxyHost
self.proxyPort = port ?? self.proxyPort
self.loadAndCreatePrividerManager { (manager) in

BIN
ios/Runner/silence.mp3 Normal file

Binary file not shown.