Exemplo n.º 1
0
        public void StartServer()
        {
            var mainList   = new List <ThreadWrapper>();
            var clientList = new List <ThreadWrapper>();

            // Create a listener for the proxy port
            var sockServer   = new TcpListener(IPAddress.Parse("192.168.1.114"), 7878);
            var serverThread = new ThreadWrapper();

            serverThread.Run((keep) =>
            {
                try
                {
                    sockServer.Start();
                    Console.WriteLine("启动监听 ");
                    while (keep())
                    {
                        // Accept connections on the proxy port.
                        var socket = sockServer.AcceptSocket();
                        //删除已经结束的任务
                        while (clientList.Count() > 10000)
                        {
                            Thread.Sleep(100);
                            clientList = clientList.Where(x => x != null && !x.ThreadIsStoped()).ToList();
                        }

                        var clientHandler = new ThreadWrapper();
                        clientHandler.Run((keep1) =>
                        {
                            new ProxyHandler(socket).Run();
                            return(true);
                        }, "客户端线程 ");
                        clientList.Add(clientHandler);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    Com.TryScope(() => { sockServer.Stop(); });
                }

                return(true);
            }, "服务器线程");

            mainList.Add(serverThread);

            while (!ConvertHelper.GetString(Console.ReadLine()).ToLower().Contains("stop"))
            {
                Thread.Sleep(500);
            }
            Console.WriteLine("正在关闭线程");

            //关闭监听
            Com.TryScope(() => { sockServer.Stop(); }, (e) => { Console.WriteLine(e.Message); });
            //关闭接收连接的线程
            Com.TryScope(() =>
            {
                ThreadWrapper.CloseAndRemove(ref mainList);
            }, (e) => { Console.WriteLine(e.Message); });
            //关闭所有客户端处理线程
            Com.TryScope(() =>
            {
                ThreadWrapper.CloseAndRemove(ref clientList);
            }, (e) => { Console.WriteLine(e.Message); });

            Console.WriteLine("安全关闭线程");
        }
Exemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var url = ConvertHelper.GetString(textBox1.Text);
                var sp  = url.Split('?').Where(x => ValidateHelper.IsPlumpString(x)).ToArray();
                if (sp.Length != 2)
                {
                    MessageBox.Show("URL格式不正确");
                    return;
                }
                var sign_key = "www_qipeilong_cn";
                var dict     = new SortedDictionary <string, string>(new MyStringComparer());
                foreach (var p in sp[1].Split('&'))
                {
                    var kv = ConvertHelper.GetString(p).Split('=');
                    if (!(kv.Length == 1 || kv.Length == 2))
                    {
                        continue;
                    }
                    if (!ValidateHelper.IsPlumpString(kv[0]))
                    {
                        continue;
                    }
                    if (kv.Length == 1)
                    {
                        kv = new string[] { kv[0], string.Empty };
                    }
                    if (kv[0].ToLower() == "sign" || kv[0].Length <= 0)
                    {
                        continue;
                    }
                    if (kv[0]?.Length > 32 || kv[1]?.Length > 32)
                    {
                        continue;
                    }
                    dict[kv[0]] = kv[1];
                }
                var strdata = string.Join("&", dict.Select(x => x.Key + "=" + x.Value));
                strdata += sign_key;
                strdata  = strdata.ToLower();

                var md5 = ConvertHelper.GetString(SecureHelper.GetMD5(strdata)).ToUpper();
                dict["sign"] = md5;
                strdata      = string.Join("&", dict.Select(x => x.Key + "=" + x.Value));

                list = list.Where(x => !x.ThreadIsStoped()).ToList();
                var tt = new ThreadWrapper();
                tt.OnStarted = () =>
                {
                    this.Invoke(new Action(() => { this.button1.Enabled = false; }));
                    return(true);
                };
                tt.OnFinished = () =>
                {
                    this.Invoke(new Action(() => { this.button1.Enabled = true; }));
                    return(true);
                };
                tt.Run((run) =>
                {
                    try
                    {
                        var start = DateTime.Now;
                        var p     = dict.ToDictionary(x => x.Key, x => x.Value);
                        var json  = HttpClientHelper.Post(sp[0], p);
                        this.Invoke(new Action(() =>
                        {
                            Clipboard.SetDataObject(json);
                            this.textBox2.Text = $"{sp[0]}?{Com.DictToUrlParams(p)}======结果已复制,耗时:{(DateTime.Now - start).TotalMilliseconds}毫秒";
                        }));
                    }
                    catch (Exception err)
                    {
                        this.Invoke(new Action(() =>
                        {
                            MessageBox.Show(err.Message);
                        }));
                    }
                    return(true);
                });
                list.Add(tt);
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }
        }