Beispiel #1
0
        public frmMain()
        {
            browser = new HttpBrowserEntitiy();
            myTcpServer.SocketException += SocketException;
            iconOpacityFlipper           = false;
            Dieing = false;
            isOpeningControllerBrowser = false;

            notifyIcon = new System.Windows.Forms.NotifyIcon();
            System.Windows.Forms.ContextMenu trayMenu = new System.Windows.Forms.ContextMenu();
            trayMenu.MenuItems.Add("Exit", OnExit);

            notifyIcon.Icon        = new System.Drawing.Icon(MobiController.Properties.Resources.favicon, 40, 40);
            notifyIcon.Visible     = true;
            notifyIcon.ContextMenu = trayMenu;

            showTrayMessage = () =>
            {
                if (App.Config.isShowMessageOnMinimize)
                {
                    notifyIcon.BalloonTipClicked += baloonClick;
                    notifyIcon.ShowBalloonTip(2000, "", "You can open Mobicontroller by double clicking this icon. " + lblServerStatus.Text, System.Windows.Forms.ToolTipIcon.Info);
                }
            };

            InitializeComponent(); // all preceeding commands must stay before this!!!
            lblTime.Text            = DateTime.Now.ToString("hh:mm tt");
            notifyIcon.DoubleClick += (sender, e) => { Show(); WinAPI.SetForegroundWindow(HWnd); };

            if (App.Config.username.Equals(""))
            {
                login();
            }
            else
            {
                procedureLoggedin();
            }

            try
            {
                var updateConnection = browser.openConnection(WEB_ROOT, HttpBrowserEntitiy.PORTS.HTTP);
                // the version.txt file on the server contains the current version of MobiController
                updateConnection.sendRequestAsync(new HttpRequest("/releases/MobiController/version.txt"), (response) =>
                {
                    string strVersion = response.Body.Trim();
                    if (!App.VERSION.Equals(strVersion))
                    {
                        Dispatcher.Invoke(() =>
                        {
                            var msg = new MessageBox("An update to MobiController is available. Would you like to download the the installer?", "Update");
                            msg.PrgBar.Visibility = System.Windows.Visibility.Visible;
                            msg.addButton("Download", () =>
                            {
                                Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = true);
                                var newConnection = Browser.openConnection(WEB_ROOT, HttpBrowserEntitiy.PORTS.HTTP);
                                newConnection.sendRequestAsync(new HttpRequest("/releases/MobiController/" + strVersion + "/MobiController_v" + strVersion + ".exe"), (dlResponse) =>
                                {
                                    try
                                    {
                                        using (var fs = File.Create(App.APP_DATA_FOLDER + "update.exe"))
                                        {
                                            newConnection.continueDownload(fs, dlResponse, (contentlen) =>
                                            {
                                                Dispatcher.Invoke(() =>
                                                {
                                                    Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = false);
                                                    msg.prgbar.Maximum = contentlen;
                                                });
                                            }, (inc) =>
                                            {
                                                Dispatcher.Invoke(() => msg.prgbar.Value += inc);
                                            });
                                        }
                                        Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = true);
                                        App.exec(App.APP_DATA_FOLDER + "update.exe");

                                        Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = false);
                                    }
                                    catch (System.ComponentModel.Win32Exception)
                                    {
                                        Dispatcher.Invoke(() =>
                                        {
                                            var errmsg = new MessageBox("The installer could not be started.");
                                            errmsg.addButton("Open Folder Location", () =>
                                            {
                                                App.exec("explorer.exe", '"' + App.APP_DATA_FOLDER + '"');
                                                //Process explorer = new Process();
                                                //explorer.StartInfo.FileName = "explorer.exe";
                                                //explorer.StartInfo.Arguments = '"' + App.APP_DATA_FOLDER;
                                                //explorer.Start();
                                            });
                                            errmsg.Show();
                                        });
                                    }
                                    catch (ConnectionException)
                                    {
                                        Dispatcher.Invoke(() => { new MessageBox("There was a problem with the server. Try again later or download it manually from the site.").Show(); });
                                    }
                                    Dispatcher.Invoke(() => msg.Close());
                                });
                            }, false);
                            msg.addButton("Go Online", () =>
                            {
                                //Process p = new Process();
                                //p.StartInfo.FileName = "http://" + WEB_ROOT;
                                //p.Start();
                                App.exec("http://" + WEB_ROOT);
                            });

                            msg.Show();
                        });
                    }
                });
            }
            catch (SocketException)
            {
            }
        }
Beispiel #2
0
        private void startUPnP()
        {
            Dispatcher.Invoke(() =>
            {
                BitmapImage bi3 = new BitmapImage();
                bi3.BeginInit();
                bi3.UriSource = new Uri("/MobiController;component/Resources/x.ico", UriKind.Relative);
                bi3.EndInit();
                imgUPnPstat.Source = bi3;
                prgbarUPnP.Value   = 0;
                lblUPnPStat.Text   = "";
            });

            bool Discovered = false;

            try
            {
                Discovered = UPnP.Discover();
            }
            catch (SocketException)
            {
                Dispatcher.Invoke(() =>
                {
                    lblUPnPStat.Foreground = Brushes.Red;
                    lblUPnPStat.Text       = "No UPnP enabled router was found.";
                });
            }
            catch (WebException)
            {
                Dispatcher.Invoke(() =>
                {
                    lblUPnPStat.Foreground = Brushes.Red;
                    lblUPnPStat.Text       = "No UPnP enabled router was found.";
                });
            }
            int serverPort = -1;
            int UPnPPort   = -1;

            try{
                Dispatcher.Invoke(() =>
                {
                    serverPort = Convert.ToInt32(txtPort.Text);
                    UPnPPort   = Convert.ToInt32(txtUPnPPort.Text);
                });
            }catch (FormatException) {
                Dispatcher.Invoke(() =>
                {
                    new MessageBox("Error converting ports. Choose numeric port numbers.").Show();
                });
                return;
            }
            if (serverPort < 1 || UPnPPort < 1)
            {
                Dispatcher.Invoke(() => new MessageBox("Port error. Please specify positive port values.").Show());
                return;
            }
            if (Discovered)
            {
                Dispatcher.Invoke(() => prgbarUPnP.Value += 20);
                try
                {
                    UPnP.deletePortMapping(UPnPPort, "TCP");
                }
                catch (SocketException)
                { }
                catch (WebException) { }
                Dispatcher.Invoke(() => prgbarUPnP.Value += 20);
                try
                {
                    UPnP.addPortMapping(UPnPPort, serverPort, myTcpServer.getLocalIP().ToString(), "TCP", "MobiController");
                }
                catch (SocketException)
                {
                    Dispatcher.Invoke(() =>
                    {
                        lblUPnPStat.Foreground = Brushes.Red;
                        lblUPnPStat.Text       = "Error mapping port. Try a port number greater than 6000.";
                    });
                    return;
                }
                catch (WebException)
                {
                    Dispatcher.Invoke(() =>
                    {
                        lblUPnPStat.Foreground = Brushes.Red;
                        lblUPnPStat.Text       = "Error mapping port. Try a port number greater than 6000.";
                    });
                    return;
                }
                Dispatcher.Invoke(() => prgbarUPnP.Value += 20);
                string ExternalIp = "";
                try
                {
                    ExternalIp = UPnP.getExternalIPAddress();
                }
                catch (SocketException)
                {
                    Dispatcher.Invoke(() =>
                    {
                        lblUPnPStat.Foreground = Brushes.Red;
                        lblUPnPStat.Text       = "Unexpected error getting external IP. The port mapping may still work.";
                    });
                    return;
                }
                catch (WebException)
                {
                    Dispatcher.Invoke(() =>
                    {
                        lblUPnPStat.Foreground = Brushes.Red;
                        lblUPnPStat.Text       = "Unexpected error getting external IP. The port mapping may still work.";
                    });
                    return;
                }
                Dispatcher.Invoke(() =>
                {
                    txtExternalIP.Text = ExternalIp;
                    if (UPnPPort != 80)
                    {
                        txtExternalIP.Text += ":" + UPnPPort.ToString();
                    }
                    prgbarUPnP.Value += 20;
                });
                try
                {
                    HttpBrowserEntitiy browser = new HttpBrowserEntitiy();
                    var conn    = browser.openConnection(ExternalIp, UPnPPort);
                    var request = new HttpRequest("/UPnP");
                    request.addHeader("Connection", "Close");
                    request.addHeader("upnptestnum", myHttpEngine.UPNP_TEST_NUMBER.ToString());
                    request.Method = HttpEngine.META_HEAD;
                    conn.sendRequest(request);
                }
                catch (SocketException)
                {
                    Dispatcher.Invoke(() =>
                    {
                        lblUPnPStat.Foreground = Brushes.Red;
                        lblUPnPStat.Text       = "Error testing connection, make sure there are no other port mappings to your local port.";
                    });
                }
            }
        }
Beispiel #3
0
        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Return || e.Key == Key.Enter)
            {
                if (txtPassword.Password.Length < 1)
                {
                    lblPass.Foreground = Brushes.Red;
                    return;
                }
                lblPass.Foreground = Brushes.White;
                if (txtUsername.Text.Length < 1)
                {
                    lblUser.Foreground = Brushes.Red;
                    return;
                }
                lblUser.Foreground = Brushes.White;

                string hostname = "secure144.inmotionhosting.com";
                Browser = new HttpBrowserEntitiy();
                var connection = Browser.openSecureConnection(hostname, HttpBrowserEntitiy.PORTS.SSL, hostname);
                var request    = new HttpRequest("POST", PHP_LOGIN_SCRIPT);

                txtUsername.IsEnabled = false;
                txtPassword.IsEnabled = false;

                request.addPost("username", txtUsername.Text);
                request.addPost("password", txtPassword.Password.ToString());
                request.addHeader("Content-Type", "application/x-www-form-urlencoded");
                loginThread = connection.sendRequestAsync(request, (HttpResponse r) =>
                {
                    if (r.headers.ContainsKey("location") && r.headers["location"].Contains("home.php"))
                    {
                        Browser.processResponse(r);
                        request            = new HttpRequest("POST", PHP_REFLECT_SCRIPT);
                        request.cookies    = Browser.SessionCookies;
                        var response       = connection.sendRequest(request);
                        string[] bodyparts = response.Body.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                        string name        = bodyparts[1]; // should be the reflected user name (in case email address was used to log in)

                        loginThread = null;
                        Dispatcher.Invoke(() =>
                        {
                            SHA256Managed hasher = new SHA256Managed();
                            byte[] password      = Encoding.UTF8.GetBytes(txtPassword.Password.ToString());

                            AccountContent.Visibility = Visibility.Hidden;
                            Sheet.Visibility          = Visibility.Hidden;
                            MainContentPane.Effect    = null;

                            App.Config.username = name;
                            App.Config.password = hasher.ComputeHash(password);

                            txtUsername.IsEnabled = true;
                            txtPassword.IsEnabled = true;
                            txtPassword.Clear();

                            procedureLoggedin();

                            App.serialize(App.Config);
                        });
                        //Bouncer.addAccount(txtUsername.Text, txtPassword.Password);
                        //AccountContent.Visibility = Visibility.Hidden;
                        //Sheet.Visibility = Visibility.Hidden;
                        //MainContentPane.Effect = null;
                        //client authenticated
                    }
                    else
                    {
                        Dispatcher.Invoke(() => {
                            lblLoginFail.Foreground = Brushes.Red;
                            lblLoginFail.Text       = "Login failed.";
                            txtUsername.IsEnabled   = true;
                            txtPassword.IsEnabled   = true;
                            txtPassword.Focus();
                        });
                        // not authenticated
                    }
                });

                this.loginTimeout = new System.Threading.Timer((stateinfo) =>
                {
                    if (loginThread != null)
                    {
                        loginThread.Suspend();
                        //loginThread.Abort(); // handle exception first (IOException)
                        loginThread = null;
                        Dispatcher.Invoke(() =>
                        {
                            lblLoginFail.Foreground = Brushes.Red;
                            lblLoginFail.Text       = "Login timed out.";
                            txtUsername.IsEnabled   = true;
                            txtPassword.IsEnabled   = true;
                            txtPassword.Focus();
                        });
                    }
                    loginTimeout.Dispose();
                }, null, 20000, 0);
            }
        }