Skip to content

Commit

Permalink
支持上传青龙
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanggaolei001 committed Oct 9, 2021
1 parent ac14f23 commit 195b80e
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 34 deletions.
20 changes: 13 additions & 7 deletions App.config
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
</startup>
<appSettings>
<!--如果你的面板没有相关接口,可以不管,如需开启改为true:-->
<!--青龙服务器及应用授权,如果不上传留空,关于青龙的ClientID和ClientSecret,参考青龙相关介绍,权限需要环境变量-->
<add key="qlUrl" value="http://127.0.0.1:5700"/>
<add key="qlClientID" value="aaaaaaaaaa"/>
<add key="qlClientSecret" value="aaaaaaaa-bbbbbbbbc-----ddd"/>


<!--如果你的面板没有相关接口,可以不管,如需开启改为true:-->
<add key="upload" value="false"/>
<!--向服务器:新增cookie时的接口地址,示例:-->
<!--向服务器:新增cookie时的接口地址,示例:-->
<add key="server" value="http://127.0.0.1:5678/addck?ck="/>
<!--方法,一般上传数据都是Post,代码也写了个支持Get,但后台没人这么写吧...-->
<add key="method" value="post"/>
</appSettings>
<add key="method" value="post"/>
</appSettings>
</configuration>
1 change: 1 addition & 0 deletions JdLoginTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<ItemGroup>
<PackageReference Include="CefSharp.Wpf" Version="85.3.130" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.19" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="RestSharp" Version="106.12.0" />
</ItemGroup>

Expand Down
163 changes: 136 additions & 27 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text.Json.Serialization;
using System.Windows;
using CefSharp;
using Newtonsoft.Json;
using RestSharp;

namespace JdLoginTool.Wpf
Expand All @@ -15,7 +18,7 @@ public MainWindow()
Browser.TitleChanged += Browser_TitleChanged;
this.Loaded += (o, e) =>
{
Browser.Address = "https://plogin.m.jd.com/login/login?appid=300&returnurl=https://wq.jd.com/passport/LoginRedirect";
Browser.Address = "m.jd.com";
};
}

Expand All @@ -28,38 +31,144 @@ private void Browser_TitleChanged(object sender, DependencyPropertyChangedEventA
var visitor = new TaskCookieVisitor();
cm.VisitAllCookies(visitor);
ck = visitor.Task.Result.Where(cookie => cookie.Name == "pt_key" || cookie.Name == "pt_pin").Aggregate(ck, (current, cookie) => current + $"{cookie.Name}={System.Web.HttpUtility.UrlEncode(cookie.Value)};");
if (ck.Contains("pt_key") && ck.Contains("pt_pin"))
{
Clipboard.SetText(ck);
var upload = ConfigurationManager.AppSettings["upload"] == "true";
var ckServer = ConfigurationManager.AppSettings["server"];
if (upload && !string.IsNullOrWhiteSpace(ckServer))
{
var method = ConfigurationManager.AppSettings["method"];
try
{
var client = new RestClient(ckServer + ck)
{
Timeout = -1
};
var request = new RestRequest(method == "post" ? Method.POST : Method.GET);
var response = client.Execute(request);
Console.WriteLine(response.Content);
MessageBox.Show(ck, "Cookie已上传服务器,且已复制到剪切板");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
else
{
MessageBox.Show(ck, "Cookie已复制到剪切板");
}
UploadToServer(ck);
UploadToQingLong(ck);
Application.Current.Shutdown();
}
}));
}

private string qlToken = "";
private void UploadToQingLong(string ck)
{
var qlUrl = ConfigurationManager.AppSettings["qlUrl"];
if (string.IsNullOrWhiteSpace(qlUrl))
{
return;
}
try
{
if (string.IsNullOrWhiteSpace(qlToken))
{
GetQingLongToken();
}
if (string.IsNullOrWhiteSpace(qlToken))
{
MessageBox.Show("登陆青龙失败:获取Token失败");
return;
}
//todo:检测是新ck还是老ck,即是否是更新.
//暂不实现,是否登陆重复先自己搞吧.


var client = new RestClient($"{qlUrl}/open/envs");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", $"Bearer {qlToken}");
request.AddHeader("Content-Type", "application/json");
var body = $"[{{\"name\":\"JD_COOKIE\",\"value\":\"{ck}\"}}]";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
MessageBox.Show(response.Content, "上传青龙成功(Cookie已复制到剪切板)");
}
catch (Exception e)
{
MessageBox.Show(e.Message, "上传青龙失败,Cookie已复制到剪切板,请自行添加处理");
}
}



private void GetQingLongToken()
{
var qlUrl = ConfigurationManager.AppSettings["qlUrl"];
var qlClientID = ConfigurationManager.AppSettings["qlClientID"];
var qlClientSecret = ConfigurationManager.AppSettings["qlClientSecret"];
var client = new RestClient($"{qlUrl}/open/auth/token?client_id={qlClientID}&client_secret={qlClientSecret}");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
if (response.StatusCode == HttpStatusCode.OK)
{
var result = JsonConvert.DeserializeObject<QLTokenResult>(response.Content);
qlToken = result.data.token;
//todo:保存 qlToken,下次运行先拿,并判断是否过期,过期删除,重新获取,后面再实现,目前应用场景影响不大.
}
}

private static void UploadToServer(string ck)
{
var upload = ConfigurationManager.AppSettings["upload"] == "true";
var ckServer = ConfigurationManager.AppSettings["server"];
if (upload && !string.IsNullOrWhiteSpace(ckServer))
{
var method = ConfigurationManager.AppSettings["method"];
try
{
var client = new RestClient(ckServer + ck)
{
Timeout = -1
};
var request = new RestRequest(method == "post" ? Method.POST : Method.GET);
var response = client.Execute(request);
Console.WriteLine(response.Content);
MessageBox.Show(ck, "Cookie已上传服务器,且已复制到剪切板");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}
}

public class QLTokenResult
{
public int code { get; set; }
public Data data { get; set; }
}

public class Data
{
public string token { get; set; }
public string token_type { get; set; }
public int expiration { get; set; }
}
public class JDCookie
{
public String ptPin { get; set; }
public String ptKey { get; set; }

public static JDCookie parse(String ck)
{
JDCookie jdCookie = new JDCookie();
String[] split = ck.Split(";");
foreach (var s in split)
{
if (s.StartsWith("pt_key"))
{
jdCookie.ptKey = (s.Split("=")[1]);
}
if (s.StartsWith("pt_pin"))
{
jdCookie.ptPin = (s.Split("=")[1]);
}
}

return jdCookie;
}


public override String ToString()
{
return "pt_key=" + ptKey + ";pt_pin=" + ptPin + ";";
}
}

}

0 comments on commit 195b80e

Please sign in to comment.