基本信息
源码名称:宽带跑流量工具源码
源码大小:10.32M
文件格式:.zip
开发语言:C#
更新时间:2020-07-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

宽带账号跑流量工具,下载

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myTools
{

    public partial class main : Form
    {
        private SynchronizationContext _synccontext;

        public static List<Tuple<string, string, string>> NetworkList;
        public static string newwork = "";
        public static bool IsRun = false;
        public static int runIndex = 0;
        public static int startIndex = 0;
        public static bool IsDebug = false;
        public static int OnlineDuration = 0;

        CancellationTokenSource tsource = new CancellationTokenSource();
        List<Task> lt = new List<Task>();

        public static int DownloadNum = 1;

        public static List<string> ImgUrls = new List<string>(){
           "https://sm.myapp.com/original/Office/xmind-8-update8-windows-3.7.8.201807240049.exe",
            "https://dl.softmgr.qq.com/original/Audio/iTunes64Setup-12.9.5.7.exe",
            "https://sm.myapp.com/original/Development/TArch8.5Try110627.exe",
            "https://sm.myapp.com/original/net_app/bluestacks_xiazai-3.1.3.401.exe",
            "https://dl.softmgr.qq.com/original/Office/foowwsoft6.13.exe",
            "https://dl.softmgr.qq.com/original/Development/jdk-8u191-windows-x64-8.0.1910.12.exe"
        };

        private string m_networkIndex = "";

        public main()
        {
            InitializeComponent();
            _synccontext = SynchronizationContext.Current;
            Init();
        }

        public void Init()
        {
            this.GetNetworkList();
            this.SetDataInit();
            CheckForIllegalCrossThreadCalls = false;
            this.GetConfig();
            if (IsDebug)
                this.btn_MacTest.Visible = true;
            else
                this.btn_MacTest.Visible = false;
        }

        public void SetDataInit()
        {
            this.btnReStart.Hide();
            this.btnStart.Show();
            IsRun = false;
            runIndex = 0;
            startIndex = 0;
            this.lbError.Text = "";
        }

        public void GetConfig()
        {
            string _url = System.Configuration.ConfigurationManager.AppSettings["downloadUrl"] ?? "";
            if (!string.IsNullOrWhiteSpace(_url))
            {
                string[] _urls = _url.Split(',');
                ImgUrls = _urls.ToList();
            }

            String _downloadNum = System.Configuration.ConfigurationManager.AppSettings["downloadNum"] ?? "";
            if (!string.IsNullOrWhiteSpace(_downloadNum))
            {
                DownloadNum = Convert.ToInt32(_downloadNum);
            }

            String _sDebug = System.Configuration.ConfigurationManager.AppSettings["IsDebug"] ?? "";
            if (!string.IsNullOrWhiteSpace(_sDebug) && _sDebug == "1")
            {
                IsDebug = true;
            }
            String _OnlineDuration = System.Configuration.ConfigurationManager.AppSettings["OnlineDuration"] ?? "";
            if (!string.IsNullOrWhiteSpace(_OnlineDuration))
            {
                OnlineDuration = Convert.ToInt32(_OnlineDuration);
            }

        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog path = new OpenFileDialog();
                path.Title = "请选择Excel文件";
                path.Filter = "excel|*.xls;*.xlsx";
                path.ShowDialog();
                this.txtFileName.Text = path.FileName;
                if (!string.IsNullOrWhiteSpace(this.txtFileName.Text))
                {
                    this.ReadExcel();
                    this.SetDataInit();
                }
            }
            catch (Exception ex)
            {
                this.lbError.Text = ex.Message;
            }
        }

        private void ReadExcel()
        {
            DataTable _data = ExcelHelper.ParseAndCheckExcelFile(this.txtFileName.Text);
            this.dgvExcel.DataSource = _data;
            this.lbTotal.Text = _data.Rows.Count.ToString();
        }

        private void GetNetworkList()
        {
            NetworkList = MACHelper.GetAliveNetworks();
            List<string> netWorkList = new List<string>();
            foreach (var obj in NetworkList)
            {
                netWorkList.Add(obj.Item3);
            }

            netWorkList.Insert(0, "请选择网络...");
            this.cbNetwork.DataSource = netWorkList;
        }

        private void cbNetwork_TextChanged(object sender, EventArgs e)
        {
            if (this.cbNetwork.SelectedValue.ToString() != "请选择网络...")
            {
                //this.GetMac(this.cbNetwork.SelectedValue.ToString());
                string macAddress = "";
                newwork = this.cbNetwork.SelectedValue.ToString();
                string index = MACHelper.GetAdapterIndex(out macAddress);
                foreach (Tuple<string, string, string> obj in NetworkList)
                {
                    if (obj.Item3 == newwork) {
                        m_networkIndex = obj.Item1;
                        macAddress = obj.Item2;
                        break;
                    }
                }
                
                this.txtLocationMac.Text = CorrectMacText(macAddress);
            }
        }

        private string CorrectMacText(string mac)
        {
            string _mac = mac;
            if (_mac == null || _mac.Length != 12)
                return "";
            int[] _indexs = new int[] { 2, 5, 8, 11, 14 };
            for (int i = 0; i < _indexs.Length; i  )
            {
                _mac = _mac.Insert(_indexs[i], "-");
            }
            return _mac;
        }

        #region run
        private void start()
        {
            if (string.IsNullOrWhiteSpace(txtFileName.Text))
            {
                lbError.Text = "请选择Excel文件";
                return;
            }
            string _newwork = this.cbNetwork.SelectedValue.ToString();
            if (this.cbNetwork.SelectedValue.ToString() == "请选择网络...")
            {
                lbError.Text = "请选择网络";
                return;
            }
            IsRun = true;
            SetControlDisable();
            lbError.Text = "数据正在执行中";

            foreach (var item in lt)
            {
                try
                {
                    item.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void run()
        {
            try
            {
                DataTable _data = (DataTable)dgvExcel.DataSource;
                for (int i = startIndex; i < _data.Rows.Count; i  )
                {
                    string _account = _data.Rows[i][0].ToString();
                    string _password = _data.Rows[i][1].ToString();

                    _synccontext.Post(_ => this.txtAccount.Text = _account, null);
                    _synccontext.Post(_ => this.txtPassword.Text = _password, null);
                    _synccontext.Post(_ => this.dgvExcel.BindingContext[this.dgvExcel.DataSource].Position = i, null);
                    //Thread.Sleep(6000);

                    DoWork(_account, _password);
                    runIndex  ;
                    _synccontext.Post(_ => this.lbDone.Text = runIndex.ToString(), null);

                    if (runIndex == _data.Rows.Count)
                    {
                        _synccontext.Post(_ => this.lbError.Text = "执行完毕", null);
                        this.Finish();
                    }
                }
            }
            catch (Exception ex)
            {
                _synccontext.Post(_ => this.lbError.Text = ex.Message, null);
            }
        }

        private void DoWork(string account, string password)
        {
            try
            {
                List<string> _urls = new List<string>();
                int code = -1;
                DoNewMac();
                Thread.Sleep(3000);
                if (MACHelper.NetWorkState(this.cbNetwork.SelectedValue.ToString()))
                {
                    _synccontext.Post(_ => this.lbError.Text = "mac已修改,网络连接中...", null);
                    SinASDL sinASDL = new SinASDL("宽带连接", account, password);
                    ////拨号上网
                    code = sinASDL.Connect();
                    if (code == 0)
                    {
                        _synccontext.Post(_ => this.lbError.Text = "ADSL连接成功", null);
                        //下载点东西吧
                        _urls = this.DownloadSomething();
                        //在线时间
                        if (OnlineDuration != 0)
                        {
                            _synccontext.Post(_ => this.lbError.Text = "下载完成,在线等待"   OnlineDuration.ToString()   "分钟", null);
                            Thread.Sleep(60000 * OnlineDuration);
                        }

                        Thread.Sleep(1000);
                        sinASDL.Disconnect();
                        _synccontext.Post(_ => this.lbError.Text = "已断开连接", null);
                        Thread.Sleep(1000);

                    }
                    else
                    {
                        _synccontext.Post(_ => this.lbError.Text = "ADSL连接失败"   code, null);
                        Thread.Sleep(3000);
                    }
                    LogHelper.SaveLog("info", string.Format("{0};{1};{2}\r\n{3}", account, password, (code == 0 ? "成功" : "失败")   code, string.Join(",", _urls)));
                }
                else
                {
                    LogHelper.SaveLog("info", string.Format("{0};{1};{2};{3}", account, password, "错误", "网卡不可用"));
                }

            }
            catch (Exception ex)
            {
                LogHelper.SaveLog("info", string.Format("{0};{1};{2};{3}", account, password, "错误", ex.Message));
            }
        }

        private void DoNewMac()
        {
            string networkName = this.cbNetwork.SelectedValue.ToString();
            string newMac = "";
            int ii = 1;
            while (true)
            {
                //设置新的mac
                ManagementObject mo = MACHelper.NetWork(networkName);
                newMac = SetNewMac();
                _synccontext.Post(_ => this.lbError.Text = "尝试设置mac:"   newMac   "----"   ii.ToString(), null);
                if (MACHelper.NetWorkState(networkName))
                {
                    MACHelper.DisableNetWork(mo);
                    _synccontext.Post(_ => this.lbError.Text = "网卡已禁用", null);
                    MACHelper.EnableNetWork(mo);
                    _synccontext.Post(_ => this.lbError.Text = "网卡正在启用", null);
                }
                ii  ;
                int _checkStateCount = 4;
                bool isSuccess = false;
                while (true)
                {
                    if (_checkStateCount <= 0)
                        break;
                    _synccontext.Post(_ => this.lbError.Text = "校验mac地址---"   _checkStateCount, null);
                    if (MACHelper.NetWorkState(networkName))
                    {
                        isSuccess = CheckMac(networkName, newMac);
                        if (isSuccess)
                            break;
                    }
                    Thread.Sleep(500);
                    _checkStateCount--;
                }
                if (isSuccess)
                {
                    _synccontext.Post(_ => this.lbError.Text = "设置成功:"   newMac, null);
                    break;
                }
                else
                {
                    _synccontext.Post(_ => this.lbError.Text = "设置失败,等待重试", null);
                    continue;
                }
            }
        }

        private bool CheckMac(string networkName, string newMac)
        {

            string nowMac = MACHelper.GetNetworkMac(networkName);
            if (string.IsNullOrWhiteSpace(nowMac))
            {
                return false;
            }
            if (nowMac == newMac)
            {
                return true;
            }
            return false;
        }

        private List<string> DownloadSomething()
        {
            List<string> _urls = new List<string>();
            for (int i = 0; i < DownloadNum; i  )
            {
                try
                {
                    Random rd = new Random();
                    int _index = rd.Next(0, ImgUrls.Count - 1);
                    _urls.Add(ImgUrls[_index]);
                    WebClient webClient = new WebClient();
                    _synccontext.Post(_ => this.lbError.Text = "开始下载"   (i   1), null);
                    Byte[] imgData = webClient.DownloadData(ImgUrls[_index]);
                    Stream ms = new MemoryStream(imgData);
                    ms.Position = 0;
                    _synccontext.Post(_ => this.lbError.Text = "结束下载"   (i   1), null);
                }
                catch (Exception ex)
                {
                    _synccontext.Post(_ => this.lbError.Text = ex.Message, null);
                }
            }
            return _urls;
        }

        private string SetNewMac()
        {
            String _mac = MACHelper.CreateNewMacAddress();
            this.txtRbMac.Text = this.CorrectMacText(_mac);
            MACHelper.SetMACAddress(_mac, m_networkIndex);
            return _mac;
        }
        #endregion

        private void SetControlDisable()
        {
            this.btnOpen.Enabled = !IsRun;
            if (runIndex > 0)
            {
                btnStart.Hide();
                btnReStart.Show();
            }
            else
            {
                btnStart.Show();
                btnReStart.Hide();
            }
            this.btnStart.Enabled = !IsRun;
            this.btnReStart.Enabled = !IsRun;
            this.btnStop.Enabled = IsRun;
            this.cbNetwork.Enabled = !IsRun;
        }

        private void Finish()
        {
            this.SetDataInit();
            stop();
        }

        private void stop()
        {
            //取消线程
            tsource.Cancel();
            IsRun = false;
            SetControlDisable();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            runIndex = 0;
            startIndex = 0;
            lt = new List<Task>();
            tsource = new CancellationTokenSource();
            //创建线程并 加到取消线程对象
            Task task = new Task(() =>
            {
                try
                {
                    if (tsource.IsCancellationRequested == true)
                    {
                        tsource.Token.ThrowIfCancellationRequested();
                    }
                    run();
                }
                catch (OperationCanceledException ex)
                {
                    _synccontext.Post(_ => this.lbError.Text = ex.Message, null);
                }
            }, tsource.Token);
            lt.Add(task);
            start();


        }

        private void btnReStart_Click(object sender, EventArgs e)
        {
            startIndex = runIndex;
            lt = new List<Task>();
            tsource = new CancellationTokenSource();
            //创建线程并 加到取消线程对象
            Task task = new Task(() =>
            {
                try
                {
                    if (tsource.IsCancellationRequested == true)
                    {
                        tsource.Token.ThrowIfCancellationRequested();
                    }
                    run();
                }
                catch (OperationCanceledException ex)
                {
                    _synccontext.Post(_ => this.lbError.Text = ex.Message, null);
                }
            }, tsource.Token);
            lt.Add(task);
            start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            stop();
        }

        private void btn_MacTest_Click(object sender, EventArgs e)
        {
            ManagementObject mo = MACHelper.NetWork(this.cbNetwork.SelectedValue.ToString());
            SetNewMac();
            if (MACHelper.NetWorkState(this.cbNetwork.SelectedValue.ToString()))
            {
                if (!MACHelper.DisableNetWork(mo))
                {
                    MessageBox.Show("禁用网卡失败!");
                }
                else
                {
                    MessageBox.Show("禁用网卡成功!");
                }
            }
            else
            {
                MessageBox.Show("网卡己禁用!");
            }
            Thread.Sleep(2000);
            if (MACHelper.NetWorkState(this.cbNetwork.SelectedValue.ToString()))
            {
                if (!MACHelper.EnableNetWork(mo))
                {
                    MessageBox.Show("开启网卡失败!");
                }
                else
                {
                    MessageBox.Show("开启网卡成功!");
                }
            }
            else
            {
                MessageBox.Show("网卡己开启!");
            }

            _synccontext.Post(_ => this.lbError.Text = "mac已修改,网络连接中...", null);
        }
    }
}