Ejemplo n.º 1
0
 private void WifiWrapper_WifiStateChanged(WifiState state)
 {
     InterfaceName = wifiWrapper.CurrentState.InterfaceName;
     Ssid          = wifiWrapper.CurrentState.SSID;
     InterfaceId   = wifiWrapper.CurrentState.InterfaceIndex;
     IsInsecure    = wifiWrapper.CurrentState.IsInsecure;
 }
Ejemplo n.º 2
0
        private void Inte_WlanNotification(WlanNotificationData notifyData)
        {
            var listofPoints = GetAccessPoints();

            currentState = new WifiState();
            foreach (var network in listofPoints)
            {
                var networkInterface = _client.Interfaces.Where(i => i.CurrentConnection.profileName == network.profileName).FirstOrDefault();
                if (networkInterface != null)
                {
                    currentState.SSID       = Encoding.ASCII.GetString(network.dot11Ssid.SSID, 0, (int)network.dot11Ssid.SSIDLength);
                    currentState.IsInsecure = !network.securityEnabled;
                    for (int i = 0; i < _client.Interfaces.Length; i++)
                    {
                        var interf = _client.Interfaces[i];
                        if (interf.InterfaceGuid == notifyData.interfaceGuid)
                        {
                            currentState.InterfaceName  = interf.InterfaceName;
                            currentState.InterfaceIndex = i;
                        }
                    }
                }
            }

            WifiStateChanged.Invoke(this.CurrentState);
        }
Ejemplo n.º 3
0
        private void UpdateWiFiState()
        {
            lock (__InsecureLock)
            {
                try
                {
                    var  network           = new WiFiNetwork(__Interface.Ssid);
                    bool isInsecureNetwork = (int)__Interface.InterfaceState == 4 /* kCWInterfaceStateRunning */ &&
                                             (int)__Interface.SecurityMode == 0 /* kCWSecurityModeOpen */;

                    __CurrentState = new WifiState(network, isInsecureNetwork);

                    WiFiStateChanged(__CurrentState);
                }
                catch (Exception ex)
                {
                    Logging.Info($"Inrernal ERROR parsing info about connected WiFi network '{ex.Message}' : {ex}");
                }
            }
        }
Ejemplo n.º 4
0
        private void TryConnectWirelessPKU()
        {
            try {
                WifiManager wifiMgr   = (WifiManager)this.Application.ApplicationContext.GetSystemService(Context.WifiService);
                WifiState   wifiState = wifiMgr.WifiState;
                if (wifiState != WifiState.Enabled)
                {
                    return;
                }

                WifiInfo info = wifiMgr.ConnectionInfo;
                if (info == null)
                {
                    return;
                }

                string wifiId = info.SSID;
                if (!MainService.IsTargetWifi(wifiId) || MainService.IsTooOften())
                {
                    return;
                }

                IPAddress ip = new IPAddress(BitConverter.GetBytes(info.IpAddress));
                new Thread(() => {
                    try {
                        const string USER_AGENT   = "IPGWAndroid1.4_Android6.0_8aeee18a-f4a8-4cab-b6fc-e7fb557b8b2f";
                        const string CONTENT_TYPE = "application/x-www-form-urlencoded; charset=utf-8";

                        Dictionary <string, string> dict = new Dictionary <string, string>();
                        dict["cmd"]      = "open";
                        dict["username"] = "******";       // TODO
                        dict["password"] = "******"; // TODO
                        dict["iprange"]  = "fee";
                        dict["ip"]       = ip.ToString();
                        dict["lang"]     = "en";
                        dict["app"]      = USER_AGENT;

                        string @params = string.Join("&",
                                                     dict.Select(pair => $"{pair.Key}={WebUtility.UrlEncode(pair.Value)}"));
                        string url = $"https://its.pku.edu.cn/cas/ITSClient?{@params}";

                        HttpWebRequest req = new HttpWebRequest(new System.Uri(url));
                        req.Method         = HttpMethod.Post.Method;
                        req.ContentType    = CONTENT_TYPE;
                        req.UserAgent      = USER_AGENT;
                        req.Proxy          = null;

                        Stream strm         = req.GetResponse().GetResponseStream();
                        StreamReader reader = new StreamReader(strm);
                        string respJson     = reader.ReadToEnd();
                        reader.Dispose();
                        strm.Dispose();

                        JObject root = JObject.Parse(respJson);
                        if (((string)root["succ"]).Length != 0)
                        {
                            MainService.ShowToast($"Connect to {wifiId} failed: {(string)root["succ"]}");
                            return;
                        }


                        string connections = (string)root["CONNECTIONS"];
                        string respIp      = (string)root["IP"];
                        if (((string)root["succ"]).Length != 0)
                        {
                            MainService.ShowToast($"Connect to {wifiId} failed: {(string) root["succ"]}");
                            return;
                        }

                        MainService.ShowToast($"Connected to {wifiId}.\nIP: {ip}\nConnections: {connections}");

                        //
                        // Update __lastTimeSpan
                        //
                        MainService.__lastTimeSpan = MainService.__watch.Elapsed;
                    }
                    catch (Exception ex) {
                        ShowToast(ex.ToString());
                    }
                }).Start();
            }
            catch (Exception ex) {
                MainService.ShowToast(ex.Message);
            }
        }
        private void UpdateWiFiInfoGuiData()
        {
            if (!NSThread.IsMain)
            {
                InvokeOnMainThread(() => UpdateWiFiInfoGuiData());
                return;
            }

            try
            {
                UpdateToDoLabelHiddenStatus();

                WifiState state = __MainViewModel.WiFiState;
                if (__MainViewModel.Settings.IsNetworkActionsEnabled == false)
                {
                    GuiWiFiButton.Hidden            = true;
                    GuiNetworkActionPopUpBtn.Hidden = true;
                    return;
                }

                NSFont wifiLabelFont = UIUtils.GetSystemFontOfSize(14, NSFontWeight.Thin);

                if (state == null || string.IsNullOrEmpty(state.Network.SSID))
                {
                    GuiWiFiButton.AttributedTitle = AttributedString.Create(LocalizedStrings.Instance.LocalizedString("Label_NoWiFiConnection"), NSColor.SystemGrayColor, NSTextAlignment.Center, wifiLabelFont);
                    GuiWiFiButton.Image           = null;

                    GuiWiFiButton.Enabled           = false;
                    GuiWiFiButton.Hidden            = false;
                    GuiNetworkActionPopUpBtn.Hidden = true;
                }
                else
                {
                    if (state.ConnectedToInsecureNetwork)
                    {
                        GuiWiFiButton.Image = NSImage.ImageNamed("iconWiFiSmallRed");

                        string networkName = " " + state.Network.SSID + " ";
                        string fullText    = networkName + "(" + LocalizedStrings.Instance.LocalizedString("Label_InsecureWiFiConnection") + ") ";

                        NSMutableAttributedString attrTitle = new NSMutableAttributedString(fullText);

                        NSStringAttributes stringAttributes0 = new NSStringAttributes();
                        stringAttributes0.ForegroundColor = __ToDoDescriptionTextColor;
                        stringAttributes0.Font            = wifiLabelFont;

                        NSStringAttributes stringAttributes1 = new NSStringAttributes();
                        stringAttributes1.ForegroundColor = NSColor.SystemRedColor;
                        stringAttributes1.Font            = wifiLabelFont;

                        attrTitle.AddAttributes(stringAttributes0, new NSRange(0, networkName.Length));
                        attrTitle.AddAttributes(stringAttributes1, new NSRange(networkName.Length, fullText.Length - networkName.Length));
                        attrTitle.SetAlignment(NSTextAlignment.Center, new NSRange(0, fullText.Length));

                        GuiWiFiButton.AttributedTitle = attrTitle;
                    }
                    else
                    {
                        GuiWiFiButton.Image           = NSImage.ImageNamed("iconWiFiSmallBlue");
                        GuiWiFiButton.AttributedTitle = AttributedString.Create(" " + state.Network.SSID, __ToDoDescriptionTextColor, NSTextAlignment.Center, wifiLabelFont);
                    }

                    RecreateNetworkActionsButtonItems();

                    GuiWiFiButton.Enabled           = true;
                    GuiWiFiButton.Hidden            = false;
                    GuiNetworkActionPopUpBtn.Hidden = false;
                }
            }
            catch (Exception ex)
            {
                GuiWiFiButton.Hidden            = true;
                GuiNetworkActionPopUpBtn.Hidden = true;

                Logging.Info($"{ex}");
            }
        }