Skip to content

Commit 82cbc52

Browse files
committed
fix bot crash when proxy is unusable
1 parent 9dd368a commit 82cbc52

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

TbsCore/Core/WebBrowserInfo.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,21 @@ public WebBrowserInfo()
3535

3636
public string CurrentUrl { get => Driver?.Url; }
3737

38-
public async Task Init(Account acc, bool newAccess = true)
38+
public async Task<bool> Init(Account acc, bool newAccess = true)
3939
{
4040
this.acc = acc;
4141
Access.Access access = newAccess ? acc.Access.GetNewAccess() : acc.Access.GetCurrentAccess();
4242

43-
SetupChromeDriver(access, acc.AccInfo.Nickname, acc.AccInfo.ServerUrl);
44-
4543
if (!string.IsNullOrEmpty(access.Proxy))
4644
{
47-
await CheckProxy(acc);
45+
var checkResult = await CheckProxy(acc);
46+
if (!checkResult) return false;
4847
}
4948

49+
SetupChromeDriver(access, acc.AccInfo.Nickname, acc.AccInfo.ServerUrl);
50+
5051
await Navigate($"{acc.AccInfo.ServerUrl}/dorf1.php");
52+
return true;
5153
}
5254

5355
private void SetupChromeDriver(Access.Access access, string username, string server)
@@ -229,6 +231,18 @@ public async Task<bool> CheckProxy(Account acc)
229231
do
230232
{
231233
var currentAccess = acc.Access.GetCurrentAccess();
234+
if (string.IsNullOrEmpty(currentAccess.Proxy))
235+
{
236+
return true;
237+
}
238+
239+
if (!currentAccess.Ok)
240+
{
241+
acc.Logger.Warning($"All proxies in your account is unusable! Please check your proxy status.");
242+
243+
return false;
244+
}
245+
232246
var currentProxy = currentAccess.Proxy;
233247
acc.Logger.Information("Checking proxy " + currentProxy);
234248

TbsCore/Helpers/IoHelperCore.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static void SaveAccounts(List<Account> accounts, bool logout)
171171
/// Login into account and initialize everything
172172
/// </summary>
173173
/// <param name="acc">Account</param>
174-
public static async Task LoginAccount(Account acc)
174+
public static async Task<bool> LoginAccount(Account acc)
175175
{
176176
if (acc.Wb == null)
177177
{
@@ -182,7 +182,12 @@ public static async Task LoginAccount(Account acc)
182182
acc.Villages.ForEach(vill => vill.UnfinishedTasks = new List<VillUnfinishedTask>());
183183

184184
acc.Wb = new WebBrowserInfo();
185-
await acc.Wb.Init(acc);
185+
var opened = await acc.Wb.Init(acc);
186+
if (!opened)
187+
{
188+
acc.Logger.Warning("Cannot open browser. Check warning below");
189+
return false;
190+
}
186191
AccountHelper.StartAccountTasks(acc);
187192
acc.TaskTimer.Start();
188193
}
@@ -195,6 +200,7 @@ public static async Task LoginAccount(Account acc)
195200
DiscordHelper.SendMessage(acc, "TravianBotSharp is online now");
196201
}
197202
}
203+
return true;
198204
}
199205

200206
/// <summary>

TbsCore/Helpers/ProxyHelper.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ public static async Task<bool> TestProxy(RestClient client, string proxyIp)
3131
{
3232
Method = Method.Get,
3333
};
34-
var response = await client.ExecuteAsync(request);
35-
return response.Content.Equals(proxyIp);
34+
try
35+
{
36+
var response = await client.ExecuteAsync(request);
37+
return response.Content.Equals(proxyIp);
38+
}
39+
catch
40+
{
41+
return false;
42+
}
3643
}
3744
}
3845
}

TravBotSharp/ControlPanel.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ private void LoadAccounts()
7777
ObjectHelper.FixAccObj(x, x);
7878
x.Tasks = new TaskList(x);
7979
x.TaskTimer = new TaskTimer(x);
80-
80+
// we will check again before we login
81+
x.Access.AllAccess.ForEach(a => a.Ok = true);
8182
// x.Tasks.Load();
8283
});
8384

@@ -135,19 +136,30 @@ private void InsertAccIntoListView(string nick, string url, string proxy, int po
135136
accListView.Items.Add(item);
136137
}
137138

138-
private void button2_Click(object sender, EventArgs e) //login button
139+
private async void button2_Click(object sender, EventArgs e) //login button
139140
{
140141
var acc = GetSelectedAcc();
141142
if (0 < acc.Access.AllAccess.Count)
142143
{
143-
new Thread(async () =>
144+
var task = await Task.Run(async () =>
144145
{
145-
await IoHelperCore.LoginAccount(acc);
146+
var success = await IoHelperCore.LoginAccount(acc);
147+
if (!success) return false;
146148
acc.Tasks.OnUpdateTask = debugUc1.UpdateTaskTable;
147149
debugUc1.UpdateTaskTable();
148-
}).Start();
149-
generalUc1.UpdateBotRunning("true");
150-
return;
150+
151+
return true;
152+
});
153+
154+
if (task)
155+
{
156+
generalUc1.UpdateBotRunning("true");
157+
}
158+
else
159+
{
160+
_ = MessageBox.Show("Check debug log to more info", "Error in account", MessageBoxButtons.OK);
161+
return;
162+
}
151163
}
152164

153165
// Alert user that account has no access defined
@@ -185,7 +197,7 @@ private void accListView_SelectedIndexChanged(object sender, EventArgs e) // Dif
185197
}
186198
var acc = GetSelectedAcc();
187199
// If account has no Wb object, it's not logged in at the moment
188-
button2.Enabled = acc != null && acc.Wb == null;
200+
button2.Enabled = !(acc?.TaskTimer?.IsBotRunning ?? false);
189201

190202
UpdateFrontEnd();
191203

0 commit comments

Comments
 (0)