Skip to content

Commit

Permalink
Merge pull request #8 from XKaguya/dev
Browse files Browse the repository at this point in the history
Version 1.0.9
  • Loading branch information
XKaguya authored Mar 3, 2024
2 parents de145cf + 173ae86 commit f2629fc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 39 deletions.
20 changes: 14 additions & 6 deletions RDPInterceptor/API/NetworkInterceptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class NetworkInterceptor
private static WinDivert? Divert { get; set; }

private static readonly SemaphoreSlim semaphore = new(1);

private static readonly SemaphoreSlim semaphoreLogFile = new(1);

private static WinDivertPacket? Packet { get; set; }

Expand Down Expand Up @@ -219,17 +221,19 @@ private static void LogConnections(bool isLogConnection, string content)
Logger.Debug(content);
}
}

private static async Task LogConnectionAsync(IPAddress srcIpAddr)
public static async Task LogConnectionAsync(IPAddress srcIpAddr)
{
try
{
await semaphoreLogFile.WaitAsync();

string logFilePath = "Connectionlist.log";

if (File.Exists(logFilePath))
{
string[] lines = await File.ReadAllLinesAsync(logFilePath);
if (lines.Contains(srcIpAddr.ToString()))
if (Array.Exists(lines, line => line.Equals(srcIpAddr.ToString())))
{
return;
}
Expand All @@ -240,16 +244,20 @@ private static async Task LogConnectionAsync(IPAddress srcIpAddr)
fs.Close();
}

using (StreamWriter writer = File.AppendText(logFilePath))
await using (StreamWriter writer = File.AppendText(logFilePath))
{
await writer.WriteLineAsync(srcIpAddr.ToString());
}
}
}
catch (Exception e)
{
Logger.Error(e.Message + e.StackTrace);
Console.WriteLine(e.Message);
throw;
}
finally
{
semaphoreLogFile.Release();
}
}

public static async void ReadLinesFromFileAsync()
Expand Down
23 changes: 19 additions & 4 deletions RDPInterceptor/API/WebService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public async Task<IActionResult> StartCapture()
catch (Exception ex)
{
Logger.Error(ex.Message + ex.StackTrace);

throw;
}
}
Expand Down Expand Up @@ -194,10 +195,24 @@ public async Task<IActionResult> PostNewIp()
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
string ipAddr = reader.ReadToEndAsync().Result;

TaskCompletionSource<IActionResult> tcs = new TaskCompletionSource<IActionResult>();

Application.Current.Dispatcher.Invoke(() => { NetworkInterceptor.AddIpIntoList(ipAddr); });
Application.Current.Dispatcher.InvokeAsync(() =>
{
try
{
NetworkInterceptor.AddIpIntoList(ipAddr);
tcs.SetResult(Ok("IP has been added."));
}
catch (Exception ex)
{
Logger.Error(ex.Message + ex.StackTrace);
throw;
}
});

return Ok();
return Ok("IP has been added.");
}
}
catch (Exception e)
Expand Down Expand Up @@ -329,11 +344,11 @@ await HttpContext.SignInAsync(

Logger.Log("Login success.");

return Ok();
return Ok("Sucess.");
}
else
{
return Unauthorized();
return Ok("Failed.");
}
}

Expand Down
20 changes: 10 additions & 10 deletions RDPInterceptor/Web/Data.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ <h5 class="modal-title" id="settingsModalLabel">Settings</h5>
if (response.ok) {
return response.text();
} else {
console.error('Failed to start.');
alert('Failed to start.');
}
}).then(data => {
console.log('Response:', data);
}).catch(error => {
console.error('Network error:', error);
alert('Network error:', error);
});
}

Expand All @@ -114,7 +114,7 @@ <h5 class="modal-title" id="settingsModalLabel">Settings</h5>
console.error('Failed on stop capture.');
}
}).catch(error => {
console.error('Network error:', error);
alert('Network error:', error);
});
}

Expand Down Expand Up @@ -147,12 +147,12 @@ <h5 class="modal-title" id="settingsModalLabel">Settings</h5>
body: JSON.stringify(settingsObj)
}).then(response => {
if (response.ok) {
console.log('Settings applied successfully.');
alert('Settings applied successfully.');
} else {
console.error('Failed to apply settings.');
alert('Failed to apply settings.');
}
}).catch(error => {
console.error('Network error:', error);
alert('Network error:', error);
});

$('#settingsModal').modal('hide');
Expand All @@ -179,10 +179,10 @@ <h5 class="modal-title" id="settingsModalLabel">Settings</h5>
currentIpAddrList.splice(selectedIndex, 1);
return response.text();
} else {
console.error('Failed to delete IP.');
alert('Failed to delete IP. Possibly a null item.');
}
}).catch(error => {
console.error('Network error:', error);
alert('Network error:', error);
});
}
}
Expand All @@ -202,10 +202,10 @@ <h5 class="modal-title" id="settingsModalLabel">Settings</h5>
inputBox.value = "";
return response.text();
} else {
console.error('Failed to add IP.');
alert('Invalid IP/Domain address or IP has already exist.');
}
}).catch(error => {
console.error('Network error:', error);
alert('Network error:', error);
});
}

Expand Down
41 changes: 22 additions & 19 deletions RDPInterceptor/Web/Login.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,33 @@ <h2 class="text-center">Login</h2>
<label for="password">Password:</label>
<input type="password" id="password" class="form-control" required>
</div>
<button onclick="LoginIn()" type="submit" class="btn btn-primary">Login</button>
<button type="submit" class="btn btn-primary">Login</button>
</form>
<button onclick="ChangePasswd()" id="change-password" class="btn btn-link">Change Password</button>
<button id="change-password" class="btn btn-link">Change Password</button>
</div>
</div>
</div>

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script>
document.getElementById('login-form').addEventListener('submit', function (event) {
document.getElementById('login-form').addEventListener('submit', async function(event) {
event.preventDefault();

var username = document.getElementById('username').value;
var password = document.getElementById('password').value;

LoginIn(username, password);
await LoginIn(username, password);
});

document.getElementById('change-password').addEventListener('click', function () {
var username = prompt('Please enter your username');
var newPassword = prompt('Please enter your new password');

ChangePasswd(username, newPassword)
ChangePasswd(username, newPassword);
});

// Function

async function LoginIn(username, passwd) {
const authInfo = {username: username, password: passwd};
const authInfo = { username: username, password: passwd };
try {
const response = await fetch('/Process/LoginIn', {
method: 'POST',
Expand All @@ -55,18 +53,23 @@ <h2 class="text-center">Login</h2>
},
body: JSON.stringify(authInfo)
});
const result = await response.text();
if (response.ok) {
console.log('Login successful.');
var currentUrl = window.location.href;
var urlParts = currentUrl.split('/');
urlParts[urlParts.length - 1] = 'Data';
var newUrl = urlParts.join('/');
window.location.href = newUrl;
if (result === "Sucess.") {
console.log('Login successful.');
var currentUrl = window.location.href;
var urlParts = currentUrl.split('/');
urlParts[urlParts.length - 1] = 'Data';
var newUrl = urlParts.join('/');
window.location.href = newUrl;
} else {
alert('Failed to login. Wrong username or password.');
}
} else {
console.error('Failed to login.');
alert('Failed to login. Unknown problem.');
}
} catch (error) {
console.error('Network error:', error);
alert('Network error:', error);
}
}

Expand All @@ -81,12 +84,12 @@ <h2 class="text-center">Login</h2>
body: JSON.stringify(authInfo)
}).then(response => {
if (response.ok) {
console.log('Password changed successfully.');
alert('Password changed successfully.');
} else {
console.error('Failed to change password.');
alert('Failed to change password.');
}
}).catch(error => {
console.error('Network error:', error);
alert('Network error:', error);
});
}

Expand Down

0 comments on commit f2629fc

Please sign in to comment.