Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

c# - Ignore VPN/Proxy profiles in Xamarin Forms app

Is it possible to ignore any vpn/proxy profiles (such as those created by 1.1.1.1 app etc) and use pure WiFi connection (if it's available) in xamarin forms app ?

I am working on an api which is not accessible outside the network, any sort of proxy or vpn can interfere with the request.

My HttpClient and HttpClientHandler code

protected HttpClientHandler ClientHandler {
        get {
            if(_clientHandler == null) {
                _clientHandler = new HttpClientHandler() {
                    AllowAutoRedirect = true,
                    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
                    ClientCertificateOptions = ClientCertificateOption.Automatic,
                    CookieContainer = Cookies,
                    UseCookies = true,
                    UseProxy = false,
                    SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11
                };

                _clientHandler.ServerCertificateCustomValidationCallback = CertificateValidationCallback;                   
            }

            return _clientHandler;
        }
    }

    protected HttpClient Client {
        get {
            if(_client == null) {
                _client = new HttpClient(ClientHandler, false);
                _client.Timeout = TimeSpan.FromSeconds(TIMEOUT);                    
                _client.DefaultRequestHeaders.UserAgent.ParseAdd($"{SharedInfo.ProjectName}/{SharedInfo.Version}");
            }

            return _client;
        } 
    }

i currently have code to force either wifi or mobile data connection (searched a lot to find this) but it isn't what i require at the moment.

public class ConnectionManager : IConnectionManager {
    Context _context => Platform.AppContext;

    ConnectivityManager _manager => (ConnectivityManager) _context.GetSystemService(Context.ConnectivityService);

    public void ForceMobileData() {
        NetworkRequest.Builder request = new NetworkRequest.Builder();
        request.AddTransportType(TransportType.Cellular);
        _manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
    }

    public void ForceWifi() {
        NetworkRequest.Builder request = new NetworkRequest.Builder();
        request.AddTransportType(TransportType.Wifi);
        request.AddCapability(NetCapability.NotVpn);
        _manager.RegisterNetworkCallback(request.Build(), new CustomNetworkAvailableCallBack());
    }

    /// <summary>
    /// Custom network available call back.
    /// </summary>
    public class CustomNetworkAvailableCallBack : ConnectivityManager.NetworkCallback {
        Context _context => Platform.AppContext;

        ConnectivityManager _manager => (ConnectivityManager) _context.GetSystemService(Context.ConnectivityService);

        public override void OnAvailable(Network network) {
            //ConnectivityManager.SetProcessDefaultNetwork(network);    //deprecated (but works even in Android P)
            _manager.BindProcessToNetwork(network);           //this works in Android P
        }
    }
}
question from:https://stackoverflow.com/questions/65651474/ignore-vpn-proxy-profiles-in-xamarin-forms-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...