Exemple #1
0
        public async Task <Data.ObjectResult> Post([FromBody] MyModelGen modelGen)
        {
            using (var client = new SshClient("103.7.41.51", "root", "Gsoft@235"))
            {
                try
                {
                    client.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1200);
                    client.Connect();


                    if (client.IsConnected)
                    {
                        using (SftpClient sftpClient = new SftpClient("103.7.41.51", 22, "root", "Gsoft@235"))
                        {
                            sftpClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1200);
                            sftpClient.OperationTimeout       = TimeSpan.FromSeconds(1200);
                            sftpClient.Connect();
                            if (sftpClient.IsConnected)
                            {
                                bool isExist = false;
                                try
                                {
                                    sftpClient.ChangeDirectory("/home/gwebsite/public_html/" + modelGen.Subdomain);
                                    isExist = true;
                                }
                                catch (SftpPathNotFoundException)
                                {
                                    isExist = false;
                                }
                                finally
                                {
                                    sftpClient.Disconnect();
                                }
                                if (isExist)
                                {
                                    Data.ObjectResult checkResult = new Data.ObjectResult();
                                    checkResult.isSucceeded  = false;
                                    checkResult.ErrorMessage = "Website này đã được khởi tạo!";
                                    return(checkResult);
                                }
                            }
                        }
                        ////Copy source tu folder goc fromLocation sang folder moi toDestination

                        // client.RunCommand($"mkdir '{modelGen.Destination}' && cd '{modelGen.Source}' && cp * '{modelGen.Destination}'");
                        client.RunCommand($"mkdir '{modelGen.Destination}' && cp -a '{modelGen.Source + "/."}' '{modelGen.Destination}'");
                        // client.RunCommand($"cd /home/gwebsite/public_html");
                        //Fix permission cua tat ca folder ve 0755 va cac file ve 0644
                        string chmodCommand = "find " + "'/home/gwebsite/public_html/" + modelGen.Subdomain + @"' -type d -exec chmod 0755 {} \;";
                        client.RunCommand(chmodCommand);
                        string chmodAllSubFolder = "find " + "'/home/gwebsite/public_html/" + modelGen.Subdomain + "/.'" + @" -type d -exec chmod 0755 {} \;";
                        client.RunCommand(chmodAllSubFolder);
                        string chmodAllFile = "find " + "'/home/gwebsite/public_html/" + modelGen.Subdomain + "/.'" + @" -type f -exec chmod 0644 {} \;";
                        client.RunCommand(chmodAllFile);
                        //Fix permission chuyen owner cua tat ca cac file va folder source tu root -> gwebsite
                        string chownFolder = "chown gwebsite:gwebsite " + "'/home/gwebsite/public_html/" + modelGen.Subdomain + "'";
                        client.RunCommand(chownFolder);
                        string chownAllContent = "chown -R gwebsite:gwebsite " + "'/home/gwebsite/public_html/" + modelGen.Subdomain + "'";
                        client.RunCommand(chownAllContent);
                        /*------------*/
                        //Khoi tao ket noi vao MySqL
                        //(Khi deploy that tren Server thi PortFoward phai la "0" khi deploy Local thi la "3306")
                        var portForwarded = new ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 3306);
                        client.AddForwardedPort(portForwarded);
                        portForwarded.Start();

                        //  DatabaseConnect mySqlConnector = new DatabaseConnect("Server=127.0.0.1; Port=3306; Uid=root; Pwd=JOpaqGH7N7xz;");

                        DatabaseConnect mySqlConnector = new DatabaseConnect(modelGen.MySQlConnectionString);

                        // string createUserandDBScript = $"CREATE DATABASE IF NOT EXISTS {modelGen.DatabaseName}; CREATE USER '{modelGen.DatabaseUser}'@'localhost' IDENTIFIED BY '{modelGen.Password}'; GRANT ALL PRIVILEGES ON  {modelGen.DatabaseName}. * TO '{modelGen.DatabaseUser}'@'localhost';FLUSH PRIVILEGES;";
                        Data.ObjectResult result = new Data.ObjectResult();
                        result = mySqlConnector.OpenConnection();
                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            //result.ErrorMessage = "Lỗi không tạo được kết nối đến MySQL";
                            return(result);
                        }



                        string createDatabaseScript = $"CREATE DATABASE IF NOT EXISTS {modelGen.DatabaseName} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";
                        string createUserScript     = $"CREATE USER '{modelGen.DatabaseUser}'@'localhost' IDENTIFIED BY '{modelGen.Password}';";
                        string grantPermission      = $"GRANT ALL PRIVILEGES ON  {modelGen.DatabaseName}.* TO '{modelGen.DatabaseUser}'@'localhost' IDENTIFIED BY '{modelGen.Password}';";
                        string resetPermissionTable = $"FLUSH PRIVILEGES;";



                        result = await mySqlConnector.ExecuteCommand(createDatabaseScript);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }

                        result = await mySqlConnector.ExecuteCommand(createUserScript);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }

                        result = await mySqlConnector.ExecuteCommand(grantPermission);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }

                        result = await mySqlConnector.ExecuteCommand(resetPermissionTable);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        //isSucceeded = await mySqlConnector.ExecuteCommand(createUserandDBScript);
                        //if (isSucceeded == false)
                        //{
                        //    return "Lỗi xảy ra khi tạo DB và User";
                        //}
                        //Import du lieu vao DB vua tao
                        if (!String.IsNullOrEmpty(modelGen.ScriptLocation))
                        {
                            result = await mySqlConnector.ExecuteCommand($"USE {modelGen.DatabaseName};");

                            if (result.isSucceeded == false)
                            {
                                mySqlConnector.CloseConnection();
                                return(result);
                            }

                            //execute file script vao DB vua tao
                            SshCommand getScriptFileCommand = client.CreateCommand($"cat '{modelGen.ScriptLocation}'");
                            string     scriptContent        = getScriptFileCommand.Execute();
                            string     setCharacter         = "set names utf8mb4";
                            result = await mySqlConnector.ExecuteCommand(setCharacter);

                            if (result.isSucceeded == false)
                            {
                                mySqlConnector.CloseConnection();
                                return(result);
                            }
                            // scriptContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(scriptContent));
                            //scriptContent = Encoding.UTF8.GetString(Convert.FromBase64String(scriptContent));
                            //byte[] encodedFile = Encoding.Unicode.GetBytes(scriptContent);
                            //scriptContent = Encoding.Unicode.GetString(encodedFile);
                            if (!String.IsNullOrEmpty(scriptContent))
                            {
                                result = await mySqlConnector.ExecuteCommand(scriptContent);

                                if (result.isSucceeded == false)
                                {
                                    //mySqlConnector.CloseConnection();
                                    //  return result;
                                }
                            }
                        }
                        string changeDBQuery = $"USE {modelGen.DatabaseName};";
                        result = await mySqlConnector.ExecuteCommand(changeDBQuery);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        string siteURL            = "http://" + modelGen.Subdomain + "." + modelGen.Domain;
                        string updateSiteURLQuery = $"Update wp_options SET option_value = '{siteURL}' WHERE option_name = 'siteurl'";
                        result = await mySqlConnector.ExecuteCommand(updateSiteURLQuery);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        string updateHomeQuery = $"Update wp_options SET option_value = '{siteURL}' WHERE option_name = 'home'";
                        result = await mySqlConnector.ExecuteCommand(updateHomeQuery);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        string useCWP = "USE root_cwp;";
                        result = await mySqlConnector.ExecuteCommand(useCWP);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }

                        string insertSubdomain = $"Insert into subdomains (subdomain,domain,user,path,setup_time) values('{modelGen.Subdomain}','{modelGen.Domain}','gwebsite','{modelGen.Destination}','{DateTime.Now}')";
                        result = await mySqlConnector.ExecuteCommand(insertSubdomain);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        //string switchToCWPDatabase = "USE cwp_root;";

                        // string createSubdommainScript = "";
                        //mySqlConnector.ExecuteCommand("");
                        //Dong ket noi MySQL
                        mySqlConnector.CloseConnection();
                        //hieu chinh file wp-config

                        string     configFileLocation     = modelGen.Destination + "/wp-config.php";
                        SshCommand getWordpressConfigFile = client.CreateCommand($"cat '{configFileLocation}'");
                        string     wpconfigContent        = getWordpressConfigFile.Execute();
                        if (!String.IsNullOrEmpty(wpconfigContent))
                        {
                            wpconfigContent = wpconfigContent.Replace("define('DB_NAME','');", $"define('DB_NAME','{modelGen.DatabaseName}');");
                            wpconfigContent = wpconfigContent.Replace("define('DB_USER','');", $"define('DB_USER','{modelGen.DatabaseUser}');");
                            wpconfigContent = wpconfigContent.Replace("define('DB_PASSWORD','');", $"define('DB_PASSWORD','{modelGen.Password}');");

                            using (SftpClient sftpClient = new SftpClient("103.7.41.51", 22, "root", "Gsoft@235"))
                            {
                                sftpClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1200);
                                sftpClient.OperationTimeout       = TimeSpan.FromSeconds(1200);
                                sftpClient.Connect();
                                if (sftpClient.IsConnected)
                                {
                                    try
                                    {
                                        using (MemoryStream memStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(wpconfigContent)))
                                        {
                                            try
                                            {
                                                sftpClient.UploadFile(memStream, configFileLocation, true);
                                            }
                                            catch (Exception ex)
                                            {
                                                Data.ObjectResult objRS = new Data.ObjectResult();
                                                objRS.isSucceeded  = false;
                                                objRS.ErrorMessage = "Có lỗi xảy ra khi cập nhật lại file wp-config";
                                                return(objRS);
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Data.ObjectResult objRS = new Data.ObjectResult();
                                        objRS.isSucceeded  = false;
                                        objRS.ErrorMessage = "Có lỗi xảy ra khi tạo file wp-config - " + ex.Message;
                                        return(objRS);
                                    }
                                    finally
                                    {
                                        sftpClient.Disconnect();
                                    }
                                }
                            }
                        }
                        ///Sua File VHost
                        string     vHostFileLocation = "/usr/local/apache/conf.d/vhosts.conf";
                        SshCommand getvHostFile      = client.CreateCommand($"cat '{vHostFileLocation}'");
                        string     vHostFile         = getvHostFile.Execute();

                        string vHostContent = "";
                        //vHostContent += Environment.NewLine;
                        // vHostContent += $"# vhost_start {modelGen.Subdomain + "." + modelGen.Domain} ";
                        vHostContent += Environment.NewLine;
                        vHostContent += "<VirtualHost 103.7.41.51:8181> ";
                        vHostContent += Environment.NewLine;
                        vHostContent += $"ServerName {modelGen.Subdomain + "." + modelGen.Domain} ";
                        vHostContent += Environment.NewLine;
                        vHostContent += $"ServerAlias www.{modelGen.Subdomain + "." + modelGen.Domain}";
                        vHostContent += Environment.NewLine;
                        vHostContent += $"ServerAdmin webmaster@{modelGen.Subdomain + "." + modelGen.Domain}";
                        vHostContent += Environment.NewLine;
                        vHostContent += $"DocumentRoot /home/gwebsite/public_html/{modelGen.Subdomain}";
                        vHostContent += Environment.NewLine;
                        vHostContent += "UseCanonicalName Off";
                        vHostContent += Environment.NewLine;
                        vHostContent += $"ScriptAlias /cgi-bin/ /home/gwebsite/public_html/{modelGen.Subdomain}/cgi-bin/";
                        vHostContent += Environment.NewLine;
                        vHostContent += $"# Custom settings are loaded below this line (if any exist)";
                        vHostContent += Environment.NewLine;
                        vHostContent += "# Include " + '"' + $"/usr/local/apache/conf/userdata/gwebsite/{modelGen.Subdomain + "." + modelGen.Domain}/*.conf";
                        vHostContent += Environment.NewLine;
                        vHostContent += Environment.NewLine;
                        vHostContent += "<IfModule mod_userdir.c>";
                        vHostContent += Environment.NewLine;
                        vHostContent += "UserDir disabled";
                        vHostContent += Environment.NewLine;
                        vHostContent += "UserDir enabled gwebsite";
                        vHostContent += Environment.NewLine;
                        vHostContent += "</IfModule>";
                        vHostContent += Environment.NewLine;
                        vHostContent += Environment.NewLine;
                        vHostContent += "<IfModule mod_suexec.c>";
                        vHostContent += Environment.NewLine;
                        vHostContent += "SuexecUserGroup gwebsite gwebsite";
                        vHostContent += Environment.NewLine;
                        vHostContent += "</IfModule>";

                        vHostContent += Environment.NewLine;
                        vHostContent += Environment.NewLine;
                        vHostContent += "<IfModule mod_suphp.c>";
                        vHostContent += Environment.NewLine;
                        vHostContent += "suPHP_UserGroup gwebsite gwebsite";
                        vHostContent += Environment.NewLine;
                        vHostContent += "suPHP_ConfigPath /home/gwebsite>";
                        vHostContent += Environment.NewLine;
                        vHostContent += "</IfModule>";
                        vHostContent += Environment.NewLine;
                        vHostContent += Environment.NewLine;
                        vHostContent += "<Directory " + '"' + $"/home/gwebsite/public_html/{modelGen.Subdomain}" + '"' + '>';
                        vHostContent += Environment.NewLine;
                        vHostContent += "AllowOverride All";
                        vHostContent += Environment.NewLine;
                        vHostContent += "</Directory>";
                        vHostContent += Environment.NewLine;
                        vHostContent += Environment.NewLine;
                        vHostContent += "</VirtualHost>";
                        vHostContent += Environment.NewLine;
                        //vHostContent += $"# vhost_end {modelGen.Subdomain + "." + modelGen.Domain}";
                        //vHostFile += Environment.NewLine;
                        vHostFile += vHostContent;



                        using (SftpClient sftpClient = new SftpClient("103.7.41.51", 22, "root", "Gsoft@235"))
                        {
                            sftpClient.ConnectionInfo.Timeout = TimeSpan.FromSeconds(1200);
                            sftpClient.OperationTimeout       = TimeSpan.FromSeconds(1200);
                            sftpClient.Connect();
                            if (sftpClient.IsConnected)
                            {
                                try
                                {
                                    using (MemoryStream memStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(vHostFile)))
                                    {
                                        try
                                        {
                                            sftpClient.UploadFile(memStream, vHostFileLocation, true);
                                        }
                                        catch (Exception ex)
                                        {
                                            Data.ObjectResult objRS = new Data.ObjectResult();
                                            objRS.isSucceeded  = false;
                                            objRS.ErrorMessage = "Có lỗi xảy ra khi cập nhật lại file vHost của Apache";
                                            return(objRS);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    Data.ObjectResult objRS = new Data.ObjectResult();
                                    objRS.isSucceeded  = false;
                                    objRS.ErrorMessage = "Có lỗi xảy ra khi tạo file vHost - " + ex.Message;
                                    return(objRS);
                                }
                                finally
                                {
                                    sftpClient.Disconnect();
                                }
                            }
                        }
                        //Restart Apache de nhan Vhost moi
                        string restartApacheCommand = "systemctl restart httpd.service";
                        client.RunCommand(restartApacheCommand);

                        mySqlConnector.CloseConnection();
                        client.Disconnect();
                    }
                    else
                    {
                        Data.ObjectResult result = new Data.ObjectResult();
                        result.isSucceeded  = false;
                        result.ErrorMessage = "Kết nối SSH đến VPS thất bại";
                        return(result);
                    }
                    Data.ObjectResult rs = new Data.ObjectResult();
                    rs.isSucceeded  = true;
                    rs.ErrorMessage = "Website đã được khởi tạo!";
                    return(rs);
                }
                catch (Exception ex)
                {
                    Data.ObjectResult rs = new Data.ObjectResult();
                    rs.isSucceeded  = false;
                    rs.ErrorMessage = "Lỗi - " + ex.Message;
                    return(rs);
                }
                finally
                {
                    client.Disconnect();
                }
            }
        }
Exemple #2
0
        public async Task <Data.ObjectResult> CloneProduct(string fromLocation, string toDestination, string dbName, string mysqlUsername, string mysqlPassword, string scriptLocation)
        {
            using (var client = new SshClient("103.7.41.51", "root", "Gsoft@235"))
            {
                try
                {
                    client.Connect();



                    if (client.IsConnected)
                    {
                        ////Copy source tu folder goc fromLocation sang folder moi toDestination
                        //client.RunCommand($"mkdir '{toDestination}'");
                        client.RunCommand($"mkdir '{toDestination}' && cd '{fromLocation}' && cp * '{toDestination}'");
                        /*------------*/
                        //Tao Mysql User va DB, gan user quan ly DB vua tao
                        var portForwarded = new ForwardedPortLocal("127.0.0.1", 3306, "127.0.0.1", 3306);
                        client.AddForwardedPort(portForwarded);
                        portForwarded.Start();
                        DatabaseConnect mySqlConnector = new DatabaseConnect("Server=127.0.0.1; Port=3306; Uid=root; Pwd=JOpaqGH7N7xz;");
                        //string createUserandDBScript = $"CREATE DATABASE IF NOT EXISTS {dbName}; CREATE USER '{mysqlUsername}'@'localhost' IDENTIFIED BY '{mysqlPassword}'; GRANT ALL PRIVILEGES ON  {dbName}. * TO '{mysqlUsername}'@'localhost';FLUSH PRIVILEGES;";

                        string createDatabaseScript = $"CREATE DATABASE IF NOT EXISTS {dbName};";
                        string createUserScript     = $"CREATE USER '{mysqlUsername}'@'localhost' IDENTIFIED BY '{mysqlPassword}';";
                        string grantPermission      = $"GRANT ALL PRIVILEGES ON  {dbName}. * TO '{mysqlUsername}'@'localhost';";
                        string resetPermissionTable = $"FLUSH PRIVILEGES;";

                        Data.ObjectResult result = new Data.ObjectResult();
                        //khoi tao ket noi MySQL
                        result = mySqlConnector.OpenConnection();
                        if (result.isSucceeded == false)
                        {
                            return(result);
                        }

                        result = await mySqlConnector.ExecuteCommand(createDatabaseScript);

                        if (result.isSucceeded == false)
                        {
                            //  result.ErrorMessage = "Lỗi xảy ra khi tạo DB ";
                            return(result);
                        }
                        result = await mySqlConnector.ExecuteCommand(createUserScript);

                        if (result.isSucceeded == false)
                        {
                            // result.ErrorMessage= "Không thể tạo User Mysql";
                            return(result);
                        }
                        result = await mySqlConnector.ExecuteCommand(grantPermission);

                        if (result.isSucceeded == false)
                        {
                            // result.ErrorMessage = $"Không thể gán quyền user {mysqlUsername} cho Database {dbName}";
                            return(result);
                        }
                        result = await mySqlConnector.ExecuteCommand(resetPermissionTable);

                        if (result.isSucceeded == false)
                        {
                            return(result);
                        }
                        result = await mySqlConnector.ExecuteCommand($"USE {dbName};");

                        if (result.isSucceeded == false)
                        {
                            return(result);
                        }


                        //execute file script vao DB vua tao
                        SshCommand getScriptFileCommand = client.CreateCommand($"cat '{scriptLocation}'");

                        string scriptContent = getScriptFileCommand.Execute();
                        if (!String.IsNullOrEmpty(scriptContent))
                        {
                            result = await mySqlConnector.ExecuteCommand(scriptContent);

                            if (result.isSucceeded == false)
                            {
                                return(result);
                            }
                        }


                        //Tao Subdomain
                        string useCWP = "USE root_cwp;";
                        result = await mySqlConnector.ExecuteCommand(useCWP);

                        if (result.isSucceeded == false)
                        {
                            return(result);
                        }

                        string insertSubdomain = $"Insert into subdomains (subdomain,domain,user,path,setup_time) values('subdomain','gwebsite.net','gwebsite','home/gwebsite/public_html/subdomain','{DateTime.Now}')";
                        result = await mySqlConnector.ExecuteCommand(insertSubdomain);

                        if (result.isSucceeded == false)
                        {
                            return(result);
                        }

                        mySqlConnector.CloseConnection();
                        getScriptFileCommand.Dispose();
                        //hieu chinh file wp-config
                        string     configFileLocation     = toDestination + "/wp-config.php";
                        SshCommand getWordpressConfigFile = client.CreateCommand($"cat '{configFileLocation}'");
                        string     wpconfigContent        = getWordpressConfigFile.Execute();
                        if (!String.IsNullOrEmpty(wpconfigContent))
                        {
                            wpconfigContent = wpconfigContent.Replace("define('DB_NAME','');", $"define('DB_NAME','{dbName}');");
                            wpconfigContent = wpconfigContent.Replace("define('DB_USER','');", $"define('DB_USER','{mysqlUsername}');");
                            wpconfigContent = wpconfigContent.Replace("define('DB_PASSWORD','');", $"define('DB_PASSWORD','{mysqlPassword}');");
                            getWordpressConfigFile.Dispose();
                            using (SftpClient sftpClient = new SftpClient("103.7.41.51", 22, "root", "Gsoft@235"))
                            {
                                sftpClient.Connect();
                                if (sftpClient.IsConnected)
                                {
                                    try
                                    {
                                        using (MemoryStream memStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(wpconfigContent)))
                                        {
                                            try
                                            {
                                                sftpClient.UploadFile(memStream, configFileLocation, true);
                                            }
                                            catch (Exception ex)
                                            {
                                                Data.ObjectResult rs = new Data.ObjectResult();
                                                rs.isSucceeded  = false;
                                                rs.ErrorMessage = "Có lỗi xảy ra khi cập nhật lại file wp-config - " + ex.Message;
                                                return(rs);
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        Data.ObjectResult rs = new Data.ObjectResult();
                                        rs.isSucceeded  = false;
                                        rs.ErrorMessage = "Có lỗi xảy ra khi tạo file wp-config - " + ex.Message;
                                        return(rs);
                                    }
                                }
                            }
                        }



                        client.Disconnect();
                    }
                    else
                    {
                        Data.ObjectResult objrs = new Data.ObjectResult();
                        objrs.isSucceeded  = false;
                        objrs.ErrorMessage = "Kết nối SSH đến VPS thất bại";
                        return(objrs);
                    }
                    Data.ObjectResult objResult = new Data.ObjectResult();
                    objResult.isSucceeded  = true;
                    objResult.ErrorMessage = "Clone source thành công";

                    return(objResult);
                }
                catch (Exception ex)
                {
                    Data.ObjectResult objrs = new Data.ObjectResult();
                    objrs.isSucceeded  = false;
                    objrs.ErrorMessage = "Lỗi - " + ex.Message;
                    return(objrs);
                }
                finally
                {
                    client.Disconnect();
                }
            }
        }
Exemple #3
0
        public async Task <Data.ObjectResult> Post([FromBody] WebControlViewModel webControlViewModel)
        {
            HttpClient apiClient = new HttpClient();
            IEnumerable <CmAllcode> listMySqlConn;

            try
            {
                Uri    address    = new Uri("http://103.7.41.51:9823/api/AllCode/MYSQL_CONN");
                string jsonString = await apiClient.GetStringAsync(address);

                listMySqlConn = JsonConvert.DeserializeObject <IEnumerable <CmAllcode> >(jsonString);
            }
            catch (Exception ex)
            {
                listMySqlConn = null;
                Data.ObjectResult result = new Data.ObjectResult();
                result.isSucceeded  = false;
                result.ErrorMessage = "Lỗi trong quá trình tạo kết nối đến MySQL - " + ex.Message;
                return(result);
            }
            finally
            {
                apiClient.Dispose();
            }
            using (var client = new SshClient("103.7.41.51", "root", "Gsoft@235"))
            {
                try
                {
                    client.Connect();
                    if (client.IsConnected)
                    {
                        var portForwarded = new ForwardedPortLocal("127.0.0.1", 0, "127.0.0.1", 3306);
                        client.AddForwardedPort(portForwarded);
                        portForwarded.Start();
                        string mySQLConnectionString = "";
                        if (listMySqlConn != null)
                        {
                            mySQLConnectionString = listMySqlConn.ToList()[0].Content;
                        }
                        DatabaseConnect mySqlConnector = new DatabaseConnect(mySQLConnectionString);

                        Data.ObjectResult result = new Data.ObjectResult();
                        result = mySqlConnector.OpenConnection();
                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        string useDatabase = $"USE {webControlViewModel.PrjProjectMaster.DATABASE_NAME};";
                        result = await mySqlConnector.ExecuteCommand(useDatabase);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        string warningPage = "http://warning.gwebsite.net";
                        //HttpClient getWarningClient = new HttpClient();
                        //IEnumerable<CmAllcode> warningURL = null;
                        //try
                        //{
                        //    Uri address = new Uri("http://103.7.41.51:9823/api/AllCode/WARNING_URL");
                        //    string jsonString = await apiClient.GetStringAsync(address);
                        //    warningURL = JsonConvert.DeserializeObject<IEnumerable<CmAllcode>>(jsonString);
                        //    if (warningURL != null)
                        //    {
                        //        warningPage = warningURL.ToList()[0].Content;
                        //    }
                        //}
                        //catch (Exception ex)
                        //{

                        //    result = new Data.ObjectResult();
                        //    result.isSucceeded = false;
                        //    result.ErrorMessage = "Lỗi trong quá trình lấy dữ liệu - " + ex.Message;
                        //    return result;
                        //}
                        //finally
                        //{
                        //    getWarningClient.Dispose();
                        //    warningURL = null;
                        //}
                        string siteURL = "";
                        //OPERATION_STATE = 1 nghia la Website dang hoat dong (kiem tra bang CM_ALLCODE)
                        if (webControlViewModel.CwWebControl.OPERATION_STATE.Equals("1"))
                        {
                            siteURL = "http://" + webControlViewModel.PrjProjectMaster.SUB_DOMAIN + "." + webControlViewModel.PrjProjectMaster.DOMAIN;
                        }
                        //OPERATION_STATE = 0 nghia la Website tam dung hoat dong, redirect ve trang warning.gwebsite.net (kiem tra bang CM_ALLCODE)
                        else if (webControlViewModel.CwWebControl.OPERATION_STATE.Equals("0"))
                        {
                            siteURL = warningPage;
                        }
                        string updateSiteURLQuery = $"Update wp_options SET option_value = '{siteURL}' WHERE option_name = 'siteurl'";
                        result = await mySqlConnector.ExecuteCommand(updateSiteURLQuery);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }
                        string updateHomeQuery = $"Update wp_options SET option_value = '{siteURL}' WHERE option_name = 'home'";
                        result = await mySqlConnector.ExecuteCommand(updateHomeQuery);

                        if (result.isSucceeded == false)
                        {
                            mySqlConnector.CloseConnection();
                            return(result);
                        }

                        mySqlConnector.CloseConnection();
                        result.isSucceeded  = true;
                        result.ErrorMessage = $"Chuyển trạng thái hoạt động của website {webControlViewModel.PrjProjectMaster.SUB_DOMAIN + "." + webControlViewModel.PrjProjectMaster.DOMAIN} thành công";
                        return(result);
                    }
                    else
                    {
                        Data.ObjectResult result = new Data.ObjectResult();
                        result.isSucceeded  = false;
                        result.ErrorMessage = "Lỗi kết nối SSH";
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    Data.ObjectResult rs = new Data.ObjectResult();
                    rs.isSucceeded  = false;
                    rs.ErrorMessage = "Lỗi - " + ex.Message;
                    return(rs);
                }
            }
        }