Skip to content

Commit 67bb532

Browse files
committed
Make remote rekey safer
1 parent a8668a7 commit 67bb532

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.15")]
36-
[assembly: AssemblyFileVersion("1.0.0.15")]
35+
[assembly: AssemblyVersion("1.0.0.16")]
36+
[assembly: AssemblyFileVersion("1.0.0.16")]

XRFAgent.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<UpdatePeriodically>false</UpdatePeriodically>
2626
<UpdateRequired>false</UpdateRequired>
2727
<MapFileExtensions>true</MapFileExtensions>
28-
<ApplicationRevision>15</ApplicationRevision>
28+
<ApplicationRevision>16</ApplicationRevision>
2929
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
3030
<UseApplicationTrust>false</UseApplicationTrust>
3131
<BootstrapperEnabled>true</BootstrapperEnabled>

modSync.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class modSync
1414
private static string Sync_ServerURL;
1515
private static string Sync_SandstormToken;
1616
private static string Sync_AccessKey;
17+
private static int Sync_LastSuccessful;
1718

1819
/// <summary>
1920
/// Loads the sync module: Schedules heartbeats
@@ -60,18 +61,28 @@ public static void Unload()
6061
public static string RemoteRekey(string NewServerURL, string NewSandstormToken)
6162
{
6263
int SuccessCount = 0;
64+
Sync_LastSuccessful = 0;
65+
string OldServerURL = Sync_ServerURL;
66+
string OldSandstormToken = Sync_SandstormToken;
6367
Sync_ServerURL = NewServerURL;
6468
Sync_SandstormToken = NewSandstormToken;
6569
SuccessCount += modDatabase.UpdateConfig(new modDatabase.Config { Key = "Sync_ServerURL", Value = NewServerURL });
6670
SuccessCount += modDatabase.UpdateConfig(new modDatabase.Config { Key = "Sync_SandstormToken", Value = NewSandstormToken });
67-
if (SuccessCount == 2)
71+
SendMessage("server", "fetch", "none");
72+
Thread.Sleep(2000);
73+
SuccessCount += Sync_LastSuccessful;
74+
if (SuccessCount == 3)
6875
{
6976
modLogging.LogEvent("Sync remotely rekeyed", EventLogEntryType.Warning, 6003);
7077
return "Remote rekey successful";
7178
}
7279
else
7380
{
74-
modLogging.LogEvent("Sync was remotely rekeyed, but it was unsuccessful", EventLogEntryType.Error, 6004);
81+
modLogging.LogEvent("Sync attempted a remote rekey, but it was unsuccessful", EventLogEntryType.Error, 6004);
82+
Sync_ServerURL = OldServerURL;
83+
Sync_SandstormToken = OldSandstormToken;
84+
modDatabase.UpdateConfig(new modDatabase.Config { Key = "Sync_ServerURL", Value = OldServerURL });
85+
modDatabase.UpdateConfig(new modDatabase.Config { Key = "Sync_SandstormToken", Value = OldSandstormToken });
7586
return "Remote rekey failed";
7687
}
7788
}
@@ -85,7 +96,7 @@ public static string RemoteRekey(string NewServerURL, string NewSandstormToken)
8596
/// <param name="ExtendedMessage">(string) Contents of extended message data</param>
8697
public static async void SendMessage(string Destination, string MessageType, string Message, string ExtendedMessage = "")
8798
{
88-
// TODO Only send messages if we are online
99+
// TODO: Only send messages if we are online
89100
try
90101
{
91102
HttpClient MessageClient = new HttpClient();
@@ -103,10 +114,12 @@ public static async void SendMessage(string Destination, string MessageType, str
103114
if (MessageResponse.IsSuccessStatusCode != true)
104115
{
105116
modLogging.LogEvent("Sync error:" + (int)MessageResponse.StatusCode + " " + MessageResponse.StatusCode, EventLogEntryType.Error, 6002);
117+
Sync_LastSuccessful = 0;
106118
}
107119
else
108120
{
109121
string ResponseContent = await MessageResponse.Content.ReadAsStringAsync();
122+
Sync_LastSuccessful = 1;
110123
// VERBOSE: Uncomment to show JSON response from server
111124
// modLogging.LogEvent((int)MessageResponse.StatusCode + " " + MessageResponse.StatusCode + " " + ResponseContent, EventLogEntryType.Information);
112125

@@ -127,6 +140,7 @@ public static async void SendMessage(string Destination, string MessageType, str
127140
}
128141
catch (Exception err)
129142
{
143+
Sync_LastSuccessful = 0;
130144
modLogging.LogEvent(err.Message + "\n\n" + err.StackTrace, EventLogEntryType.Error, 6002);
131145
}
132146
}

0 commit comments

Comments
 (0)