public void UpdateLogThread()
        {
            while (workerThread != null)
            {
                Configuration config = controller.GetCurrentConfiguration();

                ServerSpeedLogShow[] _ServerSpeedLogList = new ServerSpeedLogShow[1];

                _ServerSpeedLogList[0] = config.configs[config.index].ServerSpeedLog().Translate();
                ServerSpeedLogList     = _ServerSpeedLogList;
                //MessageBox.Show(FormatBytes(ServerSpeedLogList[0].avgDownloadBytes));
                workerEvent.WaitOne();
            }
        }
        public void UpdateLogThread()
        {
            while (workerThread != null)
            {
                Configuration        config = controller.GetCurrentConfiguration();
                ServerSpeedLogShow[] _ServerSpeedLogList = new ServerSpeedLogShow[config.configs.Count];
                for (int i = 0; i < config.configs.Count; ++i)
                {
                    _ServerSpeedLogList[i] = config.configs[i].ServerSpeedLog().Translate();
                }
                ServerSpeedLogList = _ServerSpeedLogList;

                workerEvent.WaitOne();
            }
        }
        public void RefreshLog()
        {
            if (ServerSpeedLogList == null)
            {
                return;
            }

            int           last_rowcount = ServerDataGrid.RowCount;
            Configuration config        = controller.GetCurrentConfiguration();

            if (listOrder.Count > config.configs.Count)
            {
                listOrder.RemoveRange(config.configs.Count, listOrder.Count - config.configs.Count);
            }
            while (listOrder.Count < config.configs.Count)
            {
                listOrder.Add(0);
            }
            while (ServerDataGrid.RowCount < config.configs.Count)
            {
                ServerDataGrid.Rows.Add();
                int id = ServerDataGrid.RowCount - 1;
                ServerDataGrid[0, id].Value = id;
            }
            if (ServerDataGrid.RowCount > config.configs.Count)
            {
                for (int list_index = 0; list_index < ServerDataGrid.RowCount; ++list_index)
                {
                    DataGridViewCell id_cell = ServerDataGrid[0, list_index];
                    int id = (int)id_cell.Value;
                    if (id >= config.configs.Count)
                    {
                        ServerDataGrid.Rows.RemoveAt(list_index);
                        --list_index;
                    }
                }
            }
            try
            {
                for (int list_index = (lastRefreshIndex >= ServerDataGrid.RowCount) ? 0 : lastRefreshIndex, rowChangeCnt = 0;
                     list_index < ServerDataGrid.RowCount && rowChangeCnt <= 200;
                     ++list_index, ++rowChangeCnt)
                {
                    lastRefreshIndex = list_index + 1;
                    DataGridViewCell id_cell          = ServerDataGrid[0, list_index];
                    int                id             = (int)id_cell.Value;
                    Server             server         = config.configs[id];
                    ServerSpeedLogShow serverSpeedLog = ServerSpeedLogList[id];
                    listOrder[id] = list_index;
                    rowChange     = false;
                    for (int curcol = 0; curcol < ServerDataGrid.Columns.Count; ++curcol)
                    {
                        DataGridViewCell cell       = ServerDataGrid[curcol, list_index];
                        string           columnName = ServerDataGrid.Columns[curcol].Name;
                        // Server
                        if (columnName == "Server")
                        {
                            if (config.index == id)
                            {
                                SetBackColor(cell, Color.Cyan);
                            }
                            else
                            {
                                SetBackColor(cell, Color.White);
                            }
                            SetCellText(cell, server.FriendlyName());
                        }
                        if (columnName == "Group")
                        {
                            SetCellText(cell, server.group);
                        }
                        // Enable
                        if (columnName == "Enable")
                        {
                            if (server.isEnable())
                            {
                                SetBackColor(cell, Color.White);
                            }
                            else
                            {
                                SetBackColor(cell, Color.Red);
                            }
                        }
                        // TotalConnectTimes
                        else if (columnName == "TotalConnect")
                        {
                            SetCellText(cell, serverSpeedLog.totalConnectTimes);
                        }
                        // TotalConnecting
                        else if (columnName == "Connecting")
                        {
                            long connections = serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes;
                            //long ref_connections = server.GetConnections().Count;
                            //if (ref_connections < connections)
                            //{
                            //    connections = ref_connections;
                            //}
                            Color[] colList = new Color[5] {
                                Color.White, Color.LightGreen, Color.Yellow, Color.Red, Color.Red
                            };
                            long[] bytesList = new long[5] {
                                0, 16, 32, 64, 65536
                            };
                            for (int i = 1; i < colList.Length; ++i)
                            {
                                if (connections < bytesList[i])
                                {
                                    SetBackColor(cell,
                                                 ColorMix(colList[i - 1],
                                                          colList[i],
                                                          (double)(connections - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1])
                                                          )
                                                 );
                                    break;
                                }
                            }
                            SetCellText(cell, serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes);
                        }
                        // AvgConnectTime
                        else if (columnName == "AvgLatency")
                        {
                            if (serverSpeedLog.avgConnectTime >= 0)
                            {
                                SetCellText(cell, serverSpeedLog.avgConnectTime);
                            }
                            else
                            {
                                SetCellText(cell, "-");
                            }
                        }
                        // AvgDownSpeed
                        else if (columnName == "AvgDownSpeed")
                        {
                            long    avgBytes = serverSpeedLog.avgDownloadBytes;
                            string  valStr   = FormatBytes(avgBytes);
                            Color[] colList  = new Color[6] {
                                Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red
                            };
                            long[] bytesList = new long[6] {
                                0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024L * 1024 * 1024 * 1024
                            };
                            for (int i = 1; i < colList.Length; ++i)
                            {
                                if (avgBytes < bytesList[i])
                                {
                                    SetBackColor(cell,
                                                 ColorMix(colList[i - 1],
                                                          colList[i],
                                                          (double)(avgBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1])
                                                          )
                                                 );
                                    break;
                                }
                            }
                            SetCellText(cell, valStr);
                        }
                        // MaxDownSpeed
                        else if (columnName == "MaxDownSpeed")
                        {
                            long    maxBytes = serverSpeedLog.maxDownloadBytes;
                            string  valStr   = FormatBytes(maxBytes);
                            Color[] colList  = new Color[6] {
                                Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red
                            };
                            long[] bytesList = new long[6] {
                                0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024 * 1024 * 1024
                            };
                            for (int i = 1; i < colList.Length; ++i)
                            {
                                if (maxBytes < bytesList[i])
                                {
                                    SetBackColor(cell,
                                                 ColorMix(colList[i - 1],
                                                          colList[i],
                                                          (double)(maxBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1])
                                                          )
                                                 );
                                    break;
                                }
                            }
                            SetCellText(cell, valStr);
                        }
                        // AvgUpSpeed
                        else if (columnName == "AvgUpSpeed")
                        {
                            long    avgBytes = serverSpeedLog.avgUploadBytes;
                            string  valStr   = FormatBytes(avgBytes);
                            Color[] colList  = new Color[6] {
                                Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red
                            };
                            long[] bytesList = new long[6] {
                                0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024L * 1024 * 1024 * 1024
                            };
                            for (int i = 1; i < colList.Length; ++i)
                            {
                                if (avgBytes < bytesList[i])
                                {
                                    SetBackColor(cell,
                                                 ColorMix(colList[i - 1],
                                                          colList[i],
                                                          (double)(avgBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1])
                                                          )
                                                 );
                                    break;
                                }
                            }
                            SetCellText(cell, valStr);
                        }
                        // MaxUpSpeed
                        else if (columnName == "MaxUpSpeed")
                        {
                            long    maxBytes = serverSpeedLog.maxUploadBytes;
                            string  valStr   = FormatBytes(maxBytes);
                            Color[] colList  = new Color[6] {
                                Color.White, Color.LightGreen, Color.Yellow, Color.Pink, Color.Red, Color.Red
                            };
                            long[] bytesList = new long[6] {
                                0, 1024 * 64, 1024 * 512, 1024 * 1024 * 4, 1024 * 1024 * 16, 1024 * 1024 * 1024
                            };
                            for (int i = 1; i < colList.Length; ++i)
                            {
                                if (maxBytes < bytesList[i])
                                {
                                    SetBackColor(cell,
                                                 ColorMix(colList[i - 1],
                                                          colList[i],
                                                          (double)(maxBytes - bytesList[i - 1]) / (bytesList[i] - bytesList[i - 1])
                                                          )
                                                 );
                                    break;
                                }
                            }
                            SetCellText(cell, valStr);
                        }
                        // TotalUploadBytes
                        else if (columnName == "Upload")
                        {
                            string valStr  = FormatBytes(serverSpeedLog.totalUploadBytes);
                            string fullVal = serverSpeedLog.totalUploadBytes.ToString();
                            if (cell.ToolTipText != fullVal)
                            {
                                if (fullVal == "0")
                                {
                                    SetBackColor(cell, Color.FromArgb(0xf4, 0xff, 0xf4));
                                }
                                else
                                {
                                    SetBackColor(cell, Color.LightGreen);
                                    cell.Tag = 8;
                                }
                            }
                            else if (cell.Tag != null)
                            {
                                cell.Tag = (int)cell.Tag - 1;
                                if ((int)cell.Tag == 0)
                                {
                                    SetBackColor(cell, Color.FromArgb(0xf4, 0xff, 0xf4));
                                }
                                //Color col = cell.Style.BackColor;
                                //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd)));
                            }
                            SetCellToolTipText(cell, fullVal);
                            SetCellText(cell, valStr);
                        }
                        // TotalDownloadBytes
                        else if (columnName == "Download")
                        {
                            string valStr  = FormatBytes(serverSpeedLog.totalDownloadBytes);
                            string fullVal = serverSpeedLog.totalDownloadBytes.ToString();
                            if (cell.ToolTipText != fullVal)
                            {
                                if (fullVal == "0")
                                {
                                    SetBackColor(cell, Color.FromArgb(0xff, 0xf0, 0xf0));
                                }
                                else
                                {
                                    SetBackColor(cell, Color.LightGreen);
                                    cell.Tag = 8;
                                }
                            }
                            else if (cell.Tag != null)
                            {
                                cell.Tag = (int)cell.Tag - 1;
                                if ((int)cell.Tag == 0)
                                {
                                    SetBackColor(cell, Color.FromArgb(0xff, 0xf0, 0xf0));
                                }
                                //Color col = cell.Style.BackColor;
                                //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd)));
                            }
                            SetCellToolTipText(cell, fullVal);
                            SetCellText(cell, valStr);
                        }
                        else if (columnName == "DownloadRaw")
                        {
                            string valStr  = FormatBytes(serverSpeedLog.totalDownloadRawBytes);
                            string fullVal = serverSpeedLog.totalDownloadRawBytes.ToString();
                            if (cell.ToolTipText != fullVal)
                            {
                                if (fullVal == "0")
                                {
                                    SetBackColor(cell, Color.FromArgb(0xff, 0x80, 0x80));
                                }
                                else
                                {
                                    SetBackColor(cell, Color.LightGreen);
                                    cell.Tag = 8;
                                }
                            }
                            else if (cell.Tag != null)
                            {
                                cell.Tag = (int)cell.Tag - 1;
                                if ((int)cell.Tag == 0)
                                {
                                    SetBackColor(cell, Color.FromArgb(0xf0, 0xf0, 0xff));
                                }
                                //Color col = cell.Style.BackColor;
                                //SetBackColor(cell, Color.FromArgb(Math.Min(255, col.R + colAdd), Math.Min(255, col.G + colAdd), Math.Min(255, col.B + colAdd)));
                            }
                            SetCellToolTipText(cell, fullVal);
                            SetCellText(cell, valStr);
                        }
                        // ErrorConnectTimes
                        else if (columnName == "ConnectError")
                        {
                            long  val = serverSpeedLog.errorConnectTimes + serverSpeedLog.errorDecodeTimes;
                            Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 2.5), (byte)Math.Max(0, 255 - val * 2.5));
                            SetBackColor(cell, col);
                            SetCellText(cell, val);
                        }
                        // ErrorTimeoutTimes
                        else if (columnName == "ConnectTimeout")
                        {
                            SetCellText(cell, serverSpeedLog.errorTimeoutTimes);
                        }
                        // ErrorTimeoutTimes
                        else if (columnName == "ConnectEmpty")
                        {
                            long  val = serverSpeedLog.errorEmptyTimes;
                            Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 8), (byte)Math.Max(0, 255 - val * 8));
                            SetBackColor(cell, col);
                            SetCellText(cell, val);
                        }
                        // ErrorContinurousTimes
                        else if (columnName == "Continuous")
                        {
                            long  val = serverSpeedLog.errorContinurousTimes;
                            Color col = Color.FromArgb(255, (byte)Math.Max(0, 255 - val * 8), (byte)Math.Max(0, 255 - val * 8));
                            SetBackColor(cell, col);
                            SetCellText(cell, val);
                        }
                        // ErrorPersent
                        else if (columnName == "ErrorPercent")
                        {
                            if (serverSpeedLog.errorLogTimes + serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes > 0)
                            {
                                double percent = (serverSpeedLog.errorConnectTimes
                                                  + serverSpeedLog.errorTimeoutTimes
                                                  + serverSpeedLog.errorDecodeTimes)
                                                 * 100.00
                                                 / (serverSpeedLog.errorLogTimes + serverSpeedLog.totalConnectTimes - serverSpeedLog.totalDisconnectTimes);
                                SetBackColor(cell, Color.FromArgb(255, (byte)(255 - percent * 2), (byte)(255 - percent * 2)));
                                SetCellText(cell, percent.ToString("F0") + "%");
                            }
                            else
                            {
                                SetBackColor(cell, Color.White);
                                SetCellText(cell, "-");
                            }
                        }
                        if (columnName == "Server")
                        {
                            if (cell.Style.Alignment != DataGridViewContentAlignment.MiddleLeft)
                            {
                                cell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
                            }
                        }
                        else
                        {
                            if (cell.Style.Alignment != DataGridViewContentAlignment.MiddleRight)
                            {
                                cell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
                            }
                        }
                    }
                    if (rowChange)
                    {
                        rowChangeCnt++;
                    }
                }
            }
            catch
            {
            }
            if (ServerDataGrid.SortedColumn != null)
            {
                ServerDataGrid.Sort(ServerDataGrid.SortedColumn, (ListSortDirection)((int)ServerDataGrid.SortOrder - 1));
            }
            if (last_rowcount == 0 && config.index >= 0 && config.index < ServerDataGrid.RowCount)
            {
                ServerDataGrid[0, config.index].Selected = true;
            }
        }
Exemple #4
0
        protected string process(string request)
        {
            string req;

            req = request.Substring(0, request.IndexOf("\r\n"));
            req = req.Substring(req.IndexOf("api?") + 4);
            req = req.Substring(0, req.IndexOf(" "));

            string[] get_params = req.Split('&');
            Dictionary <string, string> params_dict = new Dictionary <string, string>();

            foreach (string p in get_params)
            {
                if (p.IndexOf('=') > 0)
                {
                    int    index = p.IndexOf('=');
                    string key, val;
                    key = p.Substring(0, index);
                    val = p.Substring(index + 1);
                    params_dict[key] = val;
                }
            }
            if (request.IndexOf("POST ") == 0)
            {
                string post_params = request.Substring(request.IndexOf("\r\n\r\n") + 4);
                get_params = post_params.Split('&');
                foreach (string p in get_params)
                {
                    if (p.IndexOf('=') > 0)
                    {
                        int    index = p.IndexOf('=');
                        string key, val;
                        key = p.Substring(0, index);
                        val = p.Substring(index + 1);
                        params_dict[key] = Util.Utils.urlDecode(val);
                    }
                }
            }
            if (params_dict.ContainsKey("token") && params_dict.ContainsKey("app") &&
                _config.token.ContainsKey(params_dict["app"]) && _config.token[params_dict["app"]] == params_dict["token"])
            {
                if (params_dict.ContainsKey("action"))
                {
                    if (params_dict["action"] == "statistics")
                    {
                        Configuration               config = _config;
                        ServerSpeedLogShow[]        _ServerSpeedLogList = new ServerSpeedLogShow[config.configs.Count];
                        Dictionary <string, object> servers             = new Dictionary <string, object>();
                        for (int i = 0; i < config.configs.Count; ++i)
                        {
                            _ServerSpeedLogList[i]        = config.configs[i].ServerSpeedLog().Translate();
                            servers[config.configs[i].id] = _ServerSpeedLogList[i];
                        }
                        string content = JsonConvert.SerializeObject(servers, Formatting.Indented);

                        string text     = String.Format(@"HTTP/1.1 200 OK
Server: ShadowsocksR
Content-Type: text/plain
Content-Length: {0}
Connection: Close

", System.Text.Encoding.UTF8.GetBytes(content).Length) + content;
                        byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
                        _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local);
                        return("");
                    }
                    else if (params_dict["action"] == "config")
                    {
                        if (params_dict.ContainsKey("config"))
                        {
                            string content  = "";
                            string ret_code = "200 OK";
                            if (!_controller.SaveServersConfig(params_dict["config"]))
                            {
                                ret_code = "403 Forbid";
                            }
                            string text     = String.Format(@"HTTP/1.1 {0}
Server: ShadowsocksR
Content-Type: text/plain
Content-Length: {1}
Connection: Close

", ret_code, System.Text.Encoding.UTF8.GetBytes(content).Length) + content;
                            byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
                            _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local);
                            return("");
                        }
                        else
                        {
                            Dictionary <string, string> token = _config.token;
                            _config.token = new Dictionary <string, string>();
                            string content = JsonConvert.SerializeObject(_config, Formatting.Indented);
                            _config.token = token;

                            string text     = String.Format(@"HTTP/1.1 200 OK
Server: ShadowsocksR
Content-Type: text/plain
Content-Length: {0}
Connection: Close

", System.Text.Encoding.UTF8.GetBytes(content).Length) + content;
                            byte[] response = System.Text.Encoding.UTF8.GetBytes(text);
                            _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local);
                            return("");
                        }
                    }
                }
            }
            {
                byte[] response = System.Text.Encoding.UTF8.GetBytes("");
                _local.BeginSend(response, 0, response.Length, 0, new AsyncCallback(SendCallback), _local);
            }
            return("");
        }