Implemented auto updating lua script

Downloads latest script from server if outdated (10 seconds)
Server sends encrypted token to client to keep session new and rejects
..old tokens
This allows updating the script in this repository
This commit is contained in:
Michael Chen
2022-01-16 21:31:07 +01:00
parent 9406aaa050
commit 0b9cb03bae
7 changed files with 290 additions and 12 deletions

View File

@ -0,0 +1,32 @@
namespace MinecraftDiscordBot;
public class TimeoutTokenProvider : ITokenProvider {
public TimeoutTokenProvider(int timeoutSeconds, ICipher? cipher = null) {
_timeout = timeoutSeconds;
_cipher = cipher ?? new AesCipher();
}
private readonly ICipher _cipher;
private readonly int _timeout;
public bool VerifyToken(string token) {
byte[] data;
try {
data = _cipher.Decrypt(Convert.FromHexString(token));
} catch (Exception e) {
Program.LogError("TokenProvider", e);
return false;
}
var when = DateTime.FromBinary(BitConverter.ToInt64(data, 0));
return when >= DateTime.UtcNow.AddSeconds(-_timeout);
}
public string GenerateToken() {
var time = BitConverter.GetBytes(DateTime.UtcNow.ToBinary());
var key = Guid.NewGuid().ToByteArray();
var token = Convert.ToHexString(_cipher.Encrypt(time.Concat(key).ToArray()));
return token;
}
}
public interface ITokenProvider {
string GenerateToken();
bool VerifyToken(string token);
}