Skip to content

Commit

Permalink
Release 0.1.0 (#12)
Browse files Browse the repository at this point in the history
* use Android Proxy Selector instead or env vars.

* use minSDKVersion 31 in example

* ios: use min platform version 11.0 for example

* fix: use Foundation Lib for executing pac

* return error string instead of "Error"

* clean code

* release 0.1.0

* update: Homepage

* modify project details
  • Loading branch information
Rushabhshroff authored Aug 7, 2023
1 parent b618c89 commit 5875367
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import androidx.annotation.NonNull;
import android.text.TextUtils;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -31,12 +35,26 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getDeviceProxy")) {
Map map = new HashMap<String, String>();
map.put("http.proxyHost", System.getProperty("http.proxyHost"));
map.put("http.proxyPort", System.getProperty("http.proxyPort"));
map.put("https.proxyHost", System.getProperty("https.proxyHost"));
map.put("https.proxyPort", System.getProperty("https.proxyPort"));
result.success(map);
HashMap<String, String> _map = new HashMap<String, String>() {
{
put("host", null);
put("port", null);
}
};
String url = call.argument("url");
ProxySelector selector = ProxySelector.getDefault();
try {
for (Proxy proxy : selector.select(new URI(url))) {
if (proxy.type() == Proxy.Type.HTTP) {
InetSocketAddress addr = (InetSocketAddress) proxy.address();
_map.put("host", addr.getHostName());
_map.put("port", Integer.toString(addr.getPort()));
}
}
result.success(_map);
} catch (Exception ex) {
result.error("URL Error",ex.getMessage(),null);
}
} else {
result.notImplemented();
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>8.0</string>
<string>11.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Uncomment this line to define a global platform for your project
# platform :ios, '9.0'
platform :ios, '11.0'

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/flutter_system_proxy/ios"

SPEC CHECKSUMS:
Flutter: 434fef37c0980e73bb6479ef766c45957d4b510c
Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854
flutter_system_proxy: 96eb97e3857a1d1bc533a6f7387a1f0dcb63d782

PODFILE CHECKSUM: aafe91acc616949ddb318b77800a7f51bffa2a4c
PODFILE CHECKSUM: 7368163408c647b7eb699d0d788ba6718e18fb8d

COCOAPODS: 1.11.2
COCOAPODS: 1.11.3
34 changes: 21 additions & 13 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 51;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -68,7 +68,6 @@
3D695DD80D4DCA36C1B83028 /* Pods-Runner.release.xcconfig */,
2B955829EF2302761365E6DB /* Pods-Runner.profile.xcconfig */,
);
name = Pods;
path = Pods;
sourceTree = "<group>";
};
Expand Down Expand Up @@ -156,7 +155,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1300;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -263,7 +262,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build";
shellScript = "/bin/sh \"$FLUTTER_ROOT/packages/flutter_tools/bin/xcode_backend.sh\" build\n";
};
/* End PBXShellScriptBuildPhase section */

Expand Down Expand Up @@ -340,7 +339,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand All @@ -358,8 +357,11 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.browserstack.fluttersystemproxy.flutterSystemProxyExample;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.browserstack.fluttersystemproxy.flutterSystemProxy;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -414,7 +416,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
Expand Down Expand Up @@ -463,7 +465,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = iphoneos;
Expand All @@ -482,8 +484,11 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.browserstack.fluttersystemproxy.flutterSystemProxyExample;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.browserstack.fluttersystemproxy.flutterSystemProxy;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -501,8 +506,11 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.browserstack.fluttersystemproxy.flutterSystemProxyExample;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.browserstack.fluttersystemproxy.flutterSystemProxy;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
SWIFT_VERSION = 5.0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ Future<String> fetchLocalHost() async {
return response.toString();
} catch (e) {
print(e);
return "Error";
return e.toString();
}
}
87 changes: 68 additions & 19 deletions ios/Classes/SwiftFlutterSystemProxyPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,77 @@ public class SwiftFlutterSystemProxyPlugin: NSObject, FlutterPlugin {
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getDeviceProxy":
let systemProxySettings = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() ?? [:] as CFDictionary
result(systemProxySettings as NSDictionary)
break
case "executePAC":
let systemProxySettings = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() ?? [:] as CFDictionary
let proxyDict = systemProxySettings as NSDictionary
if(proxyDict.value(forKey:"ProxyAutoConfigEnable")as! Bool){
let args = call.arguments as! NSDictionary
let url = args.value(forKey:"url") as! String
let host = args.value(forKey:"host") as! String
let js = args.value(forKey:"js") as! String
let jsEngine:JSContext = JSContext()
jsEngine.evaluateScript(js)
let fn = "FindProxyForURL(\"" + url + "\",\""+host+"\")"
let proxy = jsEngine.evaluateScript(fn)
result(proxy?.toString())
}else{
result("DIRECT")
}
let args = call.arguments as! NSDictionary
let url = args.value(forKey:"url") as! String
var dict:[String:Any] = [:]
findProxyFromEnvironment(url: url,callback: { host, port in
dict["host"] = host
dict["port"] = port
result(dict)
})
break
default:
result(FlutterMethodNotImplemented)
}
}

func findProxyFromEnvironment(url: String,callback: @escaping (_ host:String?,_ port:Int?)->Void) {
let proxConfigDict = CFNetworkCopySystemProxySettings()?.takeUnretainedValue() as NSDictionary?
if(proxConfigDict != nil){
if(proxConfigDict!["ProxyAutoConfigEnable"] as? Int == 1){
let pacUrl = proxConfigDict!["ProxyAutoConfigURLString"] as? String
let pacContent = proxConfigDict!["ProxyAutoConfigJavaScript"] as? String
if(pacContent != nil){
self.handlePacContent(pacContent: pacContent! as String, url: url, callback: callback)
}
downloadPac(pacUrl: pacUrl!, callback: { pacContent,error in

if(error != nil){
callback(nil,nil)
}else{
self.handlePacContent(pacContent: pacContent!, url: url, callback: callback)
}
})
} else if (proxConfigDict!["HTTPEnable"] as? Int == 1){
callback((proxConfigDict!["HTTPProxy"] as? String),(proxConfigDict!["HTTPPort"] as? Int))
} else if ( proxConfigDict!["HTTPSEnable"] as? Int == 1){
callback((proxConfigDict!["HTTPSProxy"] as? String),(proxConfigDict!["HTTPSPort"] as? Int))
} else {
callback(nil,nil)
}
}
}

func handlePacContent(pacContent: String,url: String, callback:(_ host:String?,_ port:Int?)->Void){
let proxies = CFNetworkCopyProxiesForAutoConfigurationScript(pacContent as CFString, CFURLCreateWithString(kCFAllocatorDefault, url as CFString, nil), nil)!.takeUnretainedValue() as? [[CFString: Any]] ?? [];
if(proxies.count > 0){
let proxy = proxies.first{$0[kCFProxyTypeKey] as! CFString == kCFProxyTypeHTTP || $0[kCFProxyTypeKey] as! CFString == kCFProxyTypeHTTPS}
if(proxy != nil){
let host = proxy?[kCFProxyHostNameKey] ?? nil
let port = proxy?[kCFProxyPortNumberKey] ?? nil
callback(host as? String,port as? Int)
}else{
callback(nil,nil)
}
}else{
callback(nil,nil)
}
}


func downloadPac(pacUrl:String, callback:@escaping (_ pacContent:String?,_ error: Error?)->Void) {
var pacContent:String = ""
let config = URLSessionConfiguration.default
config.connectionProxyDictionary = [AnyHashable: Any]()
let session = URLSession.init(configuration: config,delegate: nil,delegateQueue: OperationQueue.current)
session.dataTask(with: URL(string: pacUrl)!, completionHandler: { data, response, error in
if(error != nil){
callback(nil,error)
}
pacContent = String(bytes: data!,encoding: String.Encoding.utf8)!
callback(pacContent,nil)
}).resume()

}

}
Loading

0 comments on commit 5875367

Please sign in to comment.