ZeroLauncher/Manager/LoginManager.cs

201 lines
9.7 KiB
C#
Raw Normal View History

2024-04-22 00:19:47 +08:00
using Newtonsoft.Json.Linq;
2024-07-05 16:32:50 +08:00
using System.Diagnostics;
2024-03-07 21:04:59 +08:00
using System.Net;
using System.Net.Http;
namespace Zerolauncher.Manager
{
internal class LoginManager
{
2024-03-22 19:45:11 +08:00
2024-03-07 21:04:59 +08:00
//private static readonly HttpClient client = new HttpClient();
public static async Task SendPostRequest()
{
var client = new HttpClient();
client.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
public static async Task LoginTest()
{
var client = new HttpClient();
client.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
var values = new Dictionary<string, string>
{
{ "thing1", "hello" },
{ "thing2", "world" }
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync("http://www.example.com/recepticle.aspx", content);
var responseString = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseString);
}
2024-03-22 19:45:11 +08:00
public static async Task<string?> AssetNet(HttpClient client, SingleGame game, string url, FormUrlEncodedContent? content =null)
2024-03-07 21:04:59 +08:00
{
2024-03-22 19:45:11 +08:00
try
{
var response = content != null? await client.PostAsync(url, content): await client.GetAsync(url);
2024-04-22 00:19:47 +08:00
game.Send($"{StaticHandleS.HintText} response.StatusCode{response.StatusCode}");
2024-03-22 19:45:11 +08:00
return await response.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name},消息:{ex.Message}");
return null;
}
2024-03-07 21:04:59 +08:00
}
public static async Task DoLogin(SingleGame game)
{
var client = new HttpClient();
2024-04-22 00:19:47 +08:00
client.DefaultRequestVersion = HttpVersion.Version20;
client.DefaultVersionPolicy = HttpVersionPolicy.RequestVersionOrLower;
client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0");
string? need_web = null;
2024-03-09 18:29:16 +08:00
client.Timeout = TimeSpan.FromSeconds(3);
2024-04-22 00:19:47 +08:00
game.Send($"{StaticHandleS.HintText} 尝试登玩家{game.account.nickName}到{ServicesStaticInfo.ServicesName[game.account.providerId]}的{game.account.serverId}服");
switch (game.account.providerId)
2024-03-07 21:04:59 +08:00
{
2024-07-05 16:32:50 +08:00
case 3:
2024-03-07 21:04:59 +08:00
case 0:
2024-07-05 16:32:50 +08:00
need_web = $"http://web.4399.com/stat/togame.php?target={(game.account.providerId == 3? "ddts":"ddt")}&server_id=S{game.account.serverId}";
2024-03-22 17:23:15 +08:00
{
2024-03-22 19:45:11 +08:00
var values = new Dictionary<string, string>
2024-04-22 00:19:47 +08:00
{
{ "loginFrom", "uframe" },
{ "postLoginHandler", "default" },
{ "layoutSelfAdapting", "true" },
{ "externalLogin", "qq" },
{ "displayMode", "popup" },
{ "layout", "vertical" },
{ "appId", "www_home" },
{ "mainDivId", "popup_login_div" },
{ "includeFcmInfo", "false" },
{ "userNameLabel", "4399用户名" },
{ "userNameTip", "请输入4399用户名" },
{ "welcomeTip", "欢迎回到4399" },
{ "username", game.account.userName },
{ "password", game.account.userPWD }
};
2024-03-22 19:45:11 +08:00
var responseString = await AssetNet(client, game, "http://ptlogin.4399.com/ptlogin/login.do?v=1", new FormUrlEncodedContent(values));
// eventHandles.__errorCallback('密码错误');
if (responseString == null) break;
if (responseString.Contains("eventHandles.__errorCallback"))
2024-03-22 17:23:15 +08:00
{
2024-03-22 19:45:11 +08:00
game.Send($"{StaticHandleS.HintText} 登录异常:{responseString.Split("eventHandles.__errorCallback('")[1].Split("'")[0]}");
2024-03-22 17:23:15 +08:00
break;
}
2024-03-22 19:45:11 +08:00
if (!game.Send($"{StaticHandleS.HintText} 登录成功,开始解析iframe")) return;
2024-03-22 17:23:15 +08:00
}
2024-03-07 21:04:59 +08:00
{
2024-07-05 16:32:50 +08:00
var responseString = await AssetNet(client, game, need_web);
2024-03-22 19:45:11 +08:00
if (responseString == null) break;
if (!responseString.Contains("name=\"game_box\""))
2024-03-07 21:04:59 +08:00
{
2024-03-22 19:45:11 +08:00
game.Send($"{StaticHandleS.HintText} 解析异常:服务器返回数据不正确");
2024-07-05 16:32:50 +08:00
Trace.WriteLine(responseString);
if (responseString.Contains("维护")) game.Send($"{StaticHandleS.HintText} 检测到服务器在维护中,请耐心等候官方开服");
break;
2024-03-07 21:04:59 +08:00
}
2024-03-22 19:45:11 +08:00
responseString = responseString.Split("name=\"game_box\"")[1].Split("src=\"")[1].Split("\"")[0];
2024-03-22 17:23:15 +08:00
HttpResponseMessage response;
2024-03-07 21:04:59 +08:00
try
{
2024-03-22 19:45:11 +08:00
response = await client.PostAsync(responseString, null);
2024-03-07 21:04:59 +08:00
}
catch (Exception ex)
{
2024-03-22 19:45:11 +08:00
game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name.Replace(" ", "_")},消息:{ex.Message.Replace(" ", "_")}");
2024-03-22 17:23:15 +08:00
break;
2024-03-09 18:29:16 +08:00
}
2024-07-05 16:32:50 +08:00
responseString = await response.Content.ReadAsStringAsync();
if (game.account.providerId == 3)
{
responseString = responseString.Split("\"movie\" value='")[1].Split("'", 2)[0];
}else
{
responseString = "http://" + response.RequestMessage.RequestUri.ToString().Split("/")[2] + "/" + responseString.Split("movie\" value='")[1].Split("'")[0];
}
2024-03-22 19:45:11 +08:00
game.Send($"{StaticHandleS.GameSa} {responseString}");
2024-03-07 21:04:59 +08:00
}
2024-03-22 19:45:11 +08:00
need_web = null ;
2024-03-07 21:04:59 +08:00
break;
2024-04-22 00:19:47 +08:00
case 1:
need_web = $"http://web.7k7k.com/games/togame.php?target=ddt_7&server_id={game.account.serverId}";
{
var values = new Dictionary<string, string>
{
{ "username", game.account.userName },
{ "password", game.account.userPWD },
{"auto", "1" },
{"formtype", "index_log" }
};
var responseString = await AssetNet(client, game, "https://web.7k7k.com/source/Post.php", new FormUrlEncodedContent(values));
if (responseString == null) break;
//game.Send($"{StaticHandleS.HintText} {responseString}");
JObject jsonObj = JObject.Parse(responseString);
game.Send($"{StaticHandleS.HintText} {jsonObj["info"]}");
if ((int)jsonObj["status"] != 1) break;
if (!game.Send($"{StaticHandleS.HintText} 登录成功,开始解析iframe")) return;
}
{
HttpResponseMessage response;
try
{
response = await client.GetAsync(need_web);
}
catch (Exception ex)
{
game.Send($"{StaticHandleS.HintText} 网络发生错误,类型:{ex.GetType().Name.Replace(" ", "_")},消息:{ex.Message.Replace(" ", "_")}");
break;
}
var responseString = await response.Content.ReadAsStringAsync();
if (!responseString.Contains("movie\" value='"))
{
game.Send($"{StaticHandleS.HintText} 解析异常:服务器返回数据不正确");
if (responseString.Contains("维护")) game.Send($"{StaticHandleS.HintText} 检测到服务器在维护中,请耐心等候官方开服");
break;
}
2024-04-22 00:19:47 +08:00
responseString = "http://" + response.RequestMessage.RequestUri.ToString().Split("/")[2] + "/" + responseString.Split("movie\" value='")[1].Split("'")[0];
//Trace.WriteLine(responseString);
game.Send($"{StaticHandleS.GameSa} {responseString}");
}
break;
case 2:
need_web = $"http://www.wan.com/game/play/id/{game.account.serverId}";
break;
2024-07-05 16:32:50 +08:00
// case 3:
//need_web = $"https://web.4399.com/stat/togame.php?target=ddts&server_id=S{game.account.serverId}";
// break;
2024-04-22 00:19:47 +08:00
default:
2024-03-07 21:04:59 +08:00
game.Send($"{StaticHandleS.HintText} 错误。未适配的运营商:{game.account.serverId}");
break;
}
2024-04-22 00:19:47 +08:00
client.Dispose();
2024-03-07 21:04:59 +08:00
if (need_web == null) return;
for (int i = 6; i > 0; i--)
{
await Task.Delay(1000);
if (!game.Send($"{StaticHandleS.HintText} 自动登录失败,将在{i}后启用网页登录,关闭窗口取消")) { return; }
}
game.Send($"{StaticHandleS.UseBrowser} {need_web}");
}
}
}