From 7ce815f2bee732aac983f11f538a123d53edb1d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cwanyongkang=E2=80=9D?= <“937888580@qq.com”> Date: Tue, 12 Jan 2021 16:20:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9F=9A=E5=AD=90IP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/AgentClient8Service.cs | 559 ++++++++++++++++++ .../Hncore.Pass.Vpn/Service/AgentService.cs | 6 + 2 files changed, 565 insertions(+) create mode 100644 Services/Hncore.Pass.Vpn/Service/AgentClient8Service.cs diff --git a/Services/Hncore.Pass.Vpn/Service/AgentClient8Service.cs b/Services/Hncore.Pass.Vpn/Service/AgentClient8Service.cs new file mode 100644 index 0000000..fcdd815 --- /dev/null +++ b/Services/Hncore.Pass.Vpn/Service/AgentClient8Service.cs @@ -0,0 +1,559 @@ +using AngleSharp.Html.Parser; +using Hncore.Infrastructure.Common; +using Hncore.Infrastructure.Extension; +using Hncore.Infrastructure.Serializer; +using Hncore.Infrastructure.WebApi; +using Hncore.Pass.Vpn.Model; +using Hncore.Pass.Vpn.Request.Product; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Net; +using System.Net.Http; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using System.Threading.Tasks; +namespace Hncore.Pass.Vpn.Service +{ + public class AgentClient8Service:AgentClientBaseService + { + string LoginUrl { get; set; } = "login/"; + string LoginCodeUrl { get; set; } = ""; + string RefrushTokenUrl { get; set; } = "user_main/"; + string SingleAddUrl { get; set; } = "/userRegisterAdd/"; + string SingleReAddUrl { get; set; } = "/user_buy_server/"; + string MuiltAddUrl { get; set; } = "agent/memberMuiltAdd.html"; + string RefundUrl { get; set; } = "agent/memberRefundAct.html "; + string UpdateUrl = "setuser/";//agent/memberUpdate/id/1155709.html + string OnlineUrl { get; set; } = "active_log/?user="; + string KIllUrl { get; set; } = "onlineOffline/"; + string searchAccountUrl = "/user_main/?sum=1000&page=1&search_data="; + string searchTestAccountUrl = "agent/memberList/type/0.html?search=1&username="; + string DeleteUrl { get; set; } = "agent/memberDel/id/";//1160862.html"; + IHttpClientFactory m_HttpClientFactory; + public AgentClient8Service(IHttpClientFactory httpClientFactory):base(httpClientFactory) + { + m_HttpClientFactory = httpClientFactory; + } + + public override async Task RefrushStatus() + { + int status = 0; + if (this.Token.Has()) + { + var client = CreateHttpClient(); + if (this.RefrushTokenUrl.Has()) + { + // client.DefaultRequestHeaders.Add("Cookie", this.Token); + var getResp = await client.GetAsync(this.RefrushTokenUrl); + var content = await getResp.Content.ReadAsStringAsync(); + if (getResp.StatusCode == HttpStatusCode.OK && content.IndexOf("agentLogin.html") == -1) + { + status = 1; + } + else + { + Debug.WriteLine("离线"); + Debug.WriteLine(content); + } + } + } + return status; + } + public override async Task<(byte[], string)> GetCode() + { + var client = CreateHttpClient(false); + if (this.LoginUrl.NotHas()) return (null, ""); + var getResp = await client.GetAsync(this.LoginUrl); + var cookie = this.GetCookie(getResp, "PHPSESSID"); + if (cookie.Has()) + { + client.DefaultRequestHeaders.Add("Cookie", cookie); + var ret = await client.GetByteArrayAsync(this.LoginCodeUrl + "?t=" + DateTime.Now.Millisecond); + return (ret, cookie); + } + return (null, ""); + } + public override async Task Login(AgentLoginRequest request) + { + var client = CreateHttpClient(false); + + var map = new Dictionary(){ + {"agentName",request.Account }, + {"password",request.Pwd }, + {"authnum",request.Code }, + }; + LogHelper.Info("Login", map.ToJson()); + AddCookie(client, request.Key); + //client.DefaultRequestHeaders.Add("Cookie", request.Key); + var resp = await client.PostAsForm(this.LoginUrl, map); + var content = await resp.Content.ReadAsStringAsync(); + if (content.IndexOf("alert alert-danger") != -1) + { + return new ApiResult(ResultCode.C_VISITOR_CHECKING, "登录失败"); + } + return new ApiResult(request.Key); + } + public override bool CheckAccount(int productId,List accounts) + { + return false; + } + + /// + /// 新开 + /// + /// + /// + /// + /// + public override async Task NewAccount(string packageKey, string account, string pwd, int connCount = 1, int accountType = 1, int payCount = 1) + { + var client = CreateHttpClient(); + var map = new Dictionary(){ + {"user",account }, + {"pass",pwd }, + {"logincount",connCount.ToString()}, + {"serverid",packageKey }, + {"comment","api" }, + {"csrfmiddlewaretoken",""}, + {"type","newuser"}, + {"username","admin1"}, + {"otherserver","no"}, + }; + var title = GetOpTitle("NewAccount", account); + LogHelper.Info(title, map.ToJson()); + try + { + var resp = await client.PostAsForm(this.SingleAddUrl, map); + var content = await resp.Content.ReadAsStringAsync(); + JObject jo = (JObject)JsonConvert.DeserializeObject(content); + var status = jo["code"].ToString(); + if (status == "1") + { + return new ApiResult(ResultCode.C_SUCCESS); + } + else + { + LogHelper.Error(title, content); + return new ApiResult(ResultCode.C_INVALID_ERROR, "开户失败"); + } + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message); + return new ApiResult(ResultCode.C_INVALID_ERROR, "开户失败"); + } + } + + /// + /// 新开 + /// + /// + /// + /// + /// + public override async Task NewMuiltAccount(string packageKey, string account, string pwd, int connCount = 1, int accountType = 1,int startNum=0, int endNum=1 ) + { + var client = CreateHttpClient(); + var map = new Dictionary(){ + {"taocanName",packageKey }, + {"usernameTpl",account }, + {"password",pwd }, + {"maxonline",connCount.ToString()}, + {"startNum",startNum.ToString()}, + {"endNum",endNum.ToString()}, + {"mode",accountType.ToString()}, + }; + var title = GetOpTitle("NewMuiltAccount", account); + LogHelper.Info(title, map.ToJson()); + try + { + var resp = await client.PostAsForm(this.MuiltAddUrl, map); + var content = await resp.Content.ReadAsStringAsync(); + if (content.Has() && content.IndexOf("成功") != -1) + { + return new ApiResult(1); + } + else + { + LogHelper.Error(title, content); + return new ApiResult(ResultCode.C_INVALID_ERROR, "开户失败"); + } + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message); + return new ApiResult(ResultCode.C_INVALID_ERROR, "开户失败"); + } + } + + /// + /// 续费 + /// + /// + /// + /// + /// + public override async Task NewReAccount(string packageKey, string account, int connCount, int payCount = 1) + { + var client = CreateHttpClient(); + + + //账号信息 + var resp_info = await client.GetAsync("/user_main/?sum=1000&page=1&search_data="+account); + var content_info = await resp_info.Content.ReadAsStringAsync(); + + var parser_info = new HtmlParser(); + var document_info = await parser_info.ParseDocumentAsync(content_info); + var trs_xufei = document_info.QuerySelectorAll("tr[id]"); + //账号信息 + var account_id = trs_xufei[0].Id; + + var map = new Dictionary(){ + {"csrfmiddlewaretoken","" }, + {"user",account }, + {"id",account_id }, + {"otherserver","no" }, + {"serverid",packageKey }, + }; + var title = GetOpTitle("NewReAccount", account); + LogHelper.Info(title, map.ToJson()); + try + { + var resp = await client.PostAsForm(this.SingleReAddUrl, map); + var content = await resp.Content.ReadAsStringAsync(); + if (content.Has() && content.Contains("新到期时间")) + { + return new ApiResult(1); + } + else + { + LogHelper.Error(title, content); + return new ApiResult(ResultCode.C_INVALID_ERROR, "续费失败"); + } + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message); + return new ApiResult(ResultCode.C_INVALID_ERROR, "续费失败"); + } + } + /// + /// 删除账号 + /// + /// + /// + /// + public override async Task DeleteAccount(string account) + { + var client = CreateHttpClient(); + var infoRet = await this.GetAccountInfo(account, true); + if (infoRet.Code != ResultCode.C_SUCCESS) + return false; + + var title = GetOpTitle("DeleteAccount", account); + LogHelper.Info(title, account); + try + { + var delete = this.DeleteUrl + infoRet.Data.Id + ".html"; + var resp = await client.GetAsync(delete); + var content = await resp.Content.ReadAsStringAsync(); + if (content.Has() && content.IndexOf("alert(\"删除成功\")") != -1) + { + return true; + } + else + { + LogHelper.Error(title, content); + return false; + } + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message); + return false; + } + } + /// + /// 得到账号信息 + /// + /// + /// + /// + public override async Task> GetAccountInfo(string account,bool isTest=false) + { + var client = CreateHttpClient(); + var title = GetOpTitle("GetAccountInfo", account); + + + + var info = ""; + try + { + //账号信息 + var resp_info = await client.GetAsync("/user_main/?sum=1000&page=1&search_data="+account); + var content_info = await resp_info.Content.ReadAsStringAsync(); + + var parser_info = new HtmlParser(); + var document_info = await parser_info.ParseDocumentAsync(content_info); + var trs_info = document_info.QuerySelector("tr[id]"); + + var tds = trs_info.QuerySelectorAll("td").ToList(); + //账号信息 + + var trData = new OriginAccountModel + { + Account = account, + Pwd = tds[2].TextContent, + AccountType = tds[3].TextContent, + Package = tds[3].TextContent, + RegistTime = tds[5].TextContent, + EndTime = tds[6].TextContent, + ConnectCount = tds[4].TextContent, + IsActive = tds[7].TextContent, + Remark = tds[8].TextContent, + }; + trData.RealEndTime = DateTime.Parse(trData.EndTime); + if (tds[7].TextContent.Contains("过期")) + { + trData.RestTime = "已过期"; + } + var href = trs_info.LastElementChild.QuerySelector("a")?.Attributes["href"]?.Value; + if (href.Has()) + { + var start = href.LastIndexOf('/'); + var end = href.IndexOf(".html"); + if (start != -1 && end != -1) + { + trData.Id = href.Substring(start + 1, end - start).TrimEnd('.'); + } + } + return new ApiResult(trData); + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message+"-->info:"+ info); + return new ApiResult(ResultCode.C_INVALID_ERROR, "查询失败"); + } + + return new ApiResult(ResultCode.C_INVALID_ERROR, "没有查询到信息"); + + } + /// + /// 修改账号密码 + /// + /// + /// + /// + /// + public override async Task UpdateAccountPwd(string account, string pwd) + { + var ret = await this.GetAccountInfo(account); + if (ret.Code != ResultCode.C_SUCCESS) + { + return false; + } + var accountModel = ret.Data; + if (accountModel == null) + { + return false; + } + var client = CreateHttpClient(); + + //账号信息 + var resp_info = await client.GetAsync("/user_main/?sum=1000&page=1&search_data="+account); + var content_info = await resp_info.Content.ReadAsStringAsync(); + + var parser_info = new HtmlParser(); + var document_info = await parser_info.ParseDocumentAsync(content_info); + var trs_xufei = document_info.QuerySelectorAll("tr[id]"); + //账号信息 + var account_id = trs_xufei[0].Id; + + var map = new Dictionary(){ + {"id",account_id }, + {"pass",pwd }, + {"comment","api" }, + {"csrfmiddlewaretoken","" }, + {"type","setuserpass" }, + {"user",account }, + }; + LogHelper.Info(GetOpTitle("UpdateAccountPwd", account), map.ToJson()); + try + { + var resp = await client.PostAsForm(this.UpdateUrl, map); + var content = await resp.Content.ReadAsStringAsync(); + return true; + } + catch (Exception ex) + { + LogHelper.Error(GetOpTitle("UpdateAccountPwd", account), ex); + } + return false; + } + /// + /// 退款 + /// + /// + /// + /// + public override async Task Refund(string account, string packageKey, int days) + { + var client = CreateHttpClient(); + var map = new Dictionary(){ + {"refundUser",account }, + {"refundReason","hl" }, + }; + var title = GetOpTitle("Refund", account); + LogHelper.Info(title, map.ToJson()); + try + { + var resp = await client.PostAsForm(this.RefundUrl, map); + var content = await resp.Content.ReadAsStringAsync(); + content = System.Text.RegularExpressions.Regex.Unescape(content); + if (content.Has() && content.IndexOf("退款成功") != -1) + { + return new ApiResult(1); + } + else + { + LogHelper.Error(title, content); + return new ApiResult(ResultCode.C_INVALID_ERROR, "退款失败"); + } + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message); + return new ApiResult(ResultCode.C_INVALID_ERROR, "退款失败"); + } + } + /// + /// 是否在线 + /// + /// + /// + /// + public override async Task>> OnLine(string account) + { + + var client = CreateHttpClient(); + var title = GetOpTitle("OnLine", account); + + var info = ""; + try + { + var url = this.OnlineUrl + account; + var resp = await client.GetAsync(url); + var content = await resp.Content.ReadAsStringAsync(); + + var parser = new HtmlParser(); + var document = await parser.ParseDocumentAsync(content); + var trs = document.QuerySelectorAll("table.table tr").ToList(); + var retData = new List(); + if (trs.Count() > 1) + { + foreach (var tr in trs.Skip(1)) + { + var tds = tr.QuerySelectorAll("td").ToList(); + var tdAccount = account; + info = string.Join("", tds.Select(m => m.OuterHtml)); + //序号 会员账号 服务器IP 登录时间 在线时长 会员IP 上行流量 下行流量 操作 + + var trData = new OriginAccountOnlineModel + { + Account = account, + ServerIP = tds[6].TextContent, + LoginTime = tds[1].TextContent, + OnlineTime = tds[3].TextContent, + LoginIP = tds[6].TextContent, + UpStream = tds[4].TextContent, + DownStream = tds[7].TextContent, + Id = tds[2].QuerySelector("button")?.Attributes["onclick"]?.Value + }; + retData.Add(trData); + } + } + return new ApiResult>(retData); + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message + "-->info:" + info); + return new ApiResult>(ResultCode.C_INVALID_ERROR, "查询失败"); + } + } + /// + /// 踢号 + /// + /// + /// + /// + public override async Task KillOut( string id) + { + var client = CreateHttpClient(); + var title = GetOpTitle("KillOut", id); + var info = ""; + + string[] arrStr = id.Split('\''); + //online_offline('gj3173','122.229.1.82','81852a60','10.20.0.12','hhjs','1') + + + var map = new Dictionary(){ + {"user",arrStr[1] }, + {"nasip",arrStr[3] }, + {"acctid",arrStr[5] }, + {"csrfmiddlewaretoken","" }, + {"acctip",arrStr[7] }, + {"nasid",arrStr[9] }, + {"page","1" }, + }; + try + { + var url = this.KIllUrl; + var resp = await client.PostAsForm(url, map); + var content = await resp.Content.ReadAsStringAsync(); + return true; + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message + "-->info:" + info); + } + return false; + } + + /// + /// 是否存在 + /// + /// + /// + public override async Task Exist(string account) + { + var client = CreateHttpClient(); + var title = GetOpTitle("Exist", account); + + + try + { + var resp = await client.GetAsync("/user_main/?sum=1000&page=1&search_data="+account); + var content = await resp.Content.ReadAsStringAsync(); + + var parser = new HtmlParser(); + var document = await parser.ParseDocumentAsync(content); + var trs = document.QuerySelectorAll("table.table tr").ToList(); + if (trs.Count() > 15) + { + return true; + } + else + { + return false; + } + } + catch (Exception ex) + { + LogHelper.Error(title, ex.Message); + return false; + } + } + } +} diff --git a/Services/Hncore.Pass.Vpn/Service/AgentService.cs b/Services/Hncore.Pass.Vpn/Service/AgentService.cs index 5d09454..04f14fa 100644 --- a/Services/Hncore.Pass.Vpn/Service/AgentService.cs +++ b/Services/Hncore.Pass.Vpn/Service/AgentService.cs @@ -57,6 +57,8 @@ namespace Hncore.Pass.Vpn.Service agent = new AgentClient6Service(m_HttpClientFactory) { ClientName = product.GroupNO }; if (product.GroupNO == "g7") agent = new AgentClient7Service(m_HttpClientFactory) { ClientName = product.GroupNO }; + if (product.GroupNO == "g8") + agent = new AgentClient8Service(m_HttpClientFactory) { ClientName = product.GroupNO }; agent.Product = product; return agent; } @@ -188,6 +190,10 @@ namespace Hncore.Pass.Vpn.Service var agent = GetAgent(product); agent.Init(product.BaseUrl, product.Token); + Console.WriteLine("========================================================================"); + Console.WriteLine(packageId); + Console.WriteLine(package.PackageType); + Console.WriteLine("========================================================================"); if (package.PackageType == PackageType.Base) { var ret = await agent.NewAccount(package.OriginKey, account, pwd, connCount, accountType,payCount);