-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from CyberLions/C-VPN-API
VPN Request API
- Loading branch information
Showing
13 changed files
with
270 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using CCSODiscordBot.Services.Database.DataTables; | ||
using CCSODiscordBot.Services.Database.Repository; | ||
using Discord; | ||
using Discord.Commands; | ||
using Discord.Interactions; | ||
|
||
namespace CCSODiscordBot.Modules.VPNRequest | ||
{ | ||
public class AdminSlashCommands : InteractionModuleBase<ShardedInteractionContext> | ||
{ | ||
private readonly IGuildRepository _iGuildRepository; | ||
public AdminSlashCommands(IGuildRepository iGuildRepository) | ||
{ | ||
_iGuildRepository = iGuildRepository; | ||
} | ||
[SlashCommand("setvpnrequest", "Sets the VPN request URL and API key.")] | ||
[EnabledInDm(false)] | ||
[DefaultMemberPermissions(GuildPermission.Administrator)] | ||
public async Task SetVPNApiKey(string url, string key) | ||
{ | ||
await Context.Interaction.DeferAsync(true); | ||
Uri urlvalid; | ||
// validate URL: | ||
bool result = Uri.TryCreate(url, UriKind.Absolute, out urlvalid) && urlvalid != null && (urlvalid.Scheme == Uri.UriSchemeHttp || urlvalid.Scheme == Uri.UriSchemeHttps); | ||
if (!result) | ||
{ | ||
await Context.Interaction.FollowupAsync("The URL is invalid.", ephemeral: true); | ||
return; | ||
} | ||
// Ensure guild is configured: | ||
var guild = await _iGuildRepository.GetByDiscordIdAsync(Context.Guild.Id); | ||
if (guild == null) | ||
{ | ||
await Context.Interaction.FollowupAsync("The guild must be set up and verification must be enabled.", ephemeral: true); | ||
return; | ||
} | ||
// Add values to guild: | ||
guild.VPNAPIURL = urlvalid?.ToString(); | ||
guild.VPNAPIKey = key; | ||
|
||
// Update the DB: | ||
await _iGuildRepository.UpdateGuildAsync(guild); | ||
// Inform user: | ||
await Context.Interaction.FollowupAsync("Enabled VPN requests.", ephemeral: true); | ||
Console.WriteLine("VPN Request enabled. User, Guild: " + Context.User.Id + ", " + Context.Guild.Name); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using CCSODiscordBot.Services.Database.Repository; | ||
using CCSODiscordBot.Services.VPNAPI; | ||
using Discord; | ||
using Discord.Interactions; | ||
|
||
namespace CCSODiscordBot.Modules.VPNRequest | ||
{ | ||
public class UserSlashCommands : InteractionModuleBase<ShardedInteractionContext> | ||
{ | ||
private readonly IGuildRepository _iGuildRepository; | ||
private readonly IUserRepository _iUserRepository; | ||
public UserSlashCommands(IGuildRepository guildRepository, IUserRepository userRepository) | ||
{ | ||
_iGuildRepository = guildRepository; | ||
_iUserRepository = userRepository; | ||
} | ||
[SlashCommand("requestvpn", "Requests a VPN account.")] | ||
[DefaultMemberPermissions(GuildPermission.SendMessages)] | ||
[EnabledInDm(false)] | ||
public async Task RequestVPNAccount([MaxLength(100)][Summary(description: "Please enter a breif reason for why you are requesting the VPN.")]string reason) | ||
{ | ||
await Context.Interaction.DeferAsync(true); | ||
// get and check guild for configuration: | ||
var guild = await _iGuildRepository.GetByDiscordIdAsync(Context.Guild.Id); | ||
if (guild == null) | ||
{ | ||
await Context.Interaction.FollowupAsync("The guild has not been configured to support this command. Please ask an admin for further help.", ephemeral: true); | ||
return; | ||
} | ||
else if(string.IsNullOrEmpty(guild.VPNAPIKey) || string.IsNullOrEmpty(guild.VPNAPIURL)) | ||
{ | ||
await Context.Interaction.FollowupAsync("The guild has not been configured to support this command. Please ask an admin for further help. Error: empty url/key", ephemeral: true); | ||
return; | ||
} | ||
|
||
// Get user: | ||
var user = await _iUserRepository.GetByDiscordIdAsync(Context.User.Id, Context.Guild.Id); | ||
if(user is null || !user.Verified) | ||
{ | ||
ButtonBuilder getStartedButton = new ButtonBuilder(); | ||
getStartedButton.WithLabel("Get Started"); | ||
getStartedButton.Style = ButtonStyle.Success; | ||
getStartedButton.WithCustomId("get-started-" + Context.User.Id); | ||
ComponentBuilder component = new ComponentBuilder(); | ||
component.WithButton(getStartedButton); | ||
|
||
await Context.Interaction.FollowupAsync("You must have a verified PSU email registered with the bot to request VPN access.", ephemeral: true, components: component.Build()); | ||
return; | ||
} | ||
// Check to see if the request was already sent: | ||
if (user.VpnRequestSent) | ||
{ | ||
await Context.Interaction.FollowupAsync("You have already requested VPN access. Please contact an admin if you are having issues.", ephemeral: true); | ||
return; | ||
} | ||
// Log event: | ||
Console.WriteLine("Making VPN request for " + Context.User.Id + ", guild: " + Context.Guild.Name); | ||
// Make the request and ensure it was successful: | ||
bool requestStatus = await RequestHandler.MakeVPNRequest(guild, user, Context.User.ToString(), reason); | ||
if (requestStatus) | ||
{ | ||
user.VpnRequestSent = true; | ||
// Update the DB: | ||
await _iUserRepository.UpdateUserAsync(user); | ||
await Context.Interaction.FollowupAsync("Your request was sent. Once your request has been accepted, you will receive an email with your login credentials. The email will be sent to "+user.Email+".", ephemeral: true); | ||
} | ||
else | ||
{ | ||
await Context.Interaction.FollowupAsync("Request failed. Please try again later.", ephemeral: true); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# VPN Request Module | ||
This module makes an API request to an API that issues user credentials to log into a VPN for a lab. | ||
## Slash Commands | ||
- /setvpnrequest | ||
- By default, only admins can use this command. | ||
- This command should be provided a URL endpoint to post data to as well as an API token to authenticate to the server with. | ||
- Set by guild | ||
- /requestvpn | ||
- This command can be used by anyone with send message permission (by default). | ||
- Will send the user's information to the VPN API to create an account. | ||
- Will only work if the user is verified and the server has the VPN request API set up. | ||
- Use `/setvpnrequest` to set the VPN API up |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace CCSODiscordBot.Services.VPNAPI | ||
{ | ||
public class RequestData | ||
{ | ||
public RequestData(string discord, string first, string last, string psuemail, string reason, string username) | ||
{ | ||
this.DiscordName = discord; | ||
this.FirstName = first; | ||
this.LastName = last; | ||
this.PSUEmail = psuemail; | ||
this.Reason = reason; | ||
this.DiscordUsername = username; | ||
} | ||
[JsonPropertyName("discord")] | ||
public string DiscordName { get; set; } | ||
[JsonPropertyName("firstname")] | ||
public string FirstName { get; set; } | ||
[JsonPropertyName("lastname")] | ||
public string LastName { get; set; } | ||
[JsonPropertyName("psuemail")] | ||
public string PSUEmail { get; set; } | ||
[JsonPropertyName("reason")] | ||
public string Reason { get; set; } | ||
[JsonPropertyName("discordname")] | ||
public string DiscordUsername { get; set; } | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Net.Http.Headers; | ||
using System.Net.Sockets; | ||
using System.Text.Json; | ||
using CCSODiscordBot.Services.Database.DataTables; | ||
|
||
namespace CCSODiscordBot.Services.VPNAPI | ||
{ | ||
public class RequestHandler | ||
{ | ||
/// <summary> | ||
/// Make a request to the vpn api | ||
/// </summary> | ||
/// <param name="guild">The guild to use for configuration details</param> | ||
/// <param name="user">The user to request the VPN for</param> | ||
/// <param name="username">The discord username for the user to send the data for</param> | ||
/// <param name="reason">The reason the user needs access to the VPN</param> | ||
/// <returns></returns> | ||
public static async Task<bool> MakeVPNRequest(Guild guild, User user, string username, string reason) | ||
{ | ||
// Check for null values: | ||
if (guild.VPNAPIKey == null || guild.VPNAPIURL == null || user.FirstName == null || user.LastName == null || user.Email == null || !user.Verified) | ||
{ | ||
return false; | ||
} | ||
|
||
RequestData data = new RequestData(user.DiscordID.ToString(), user.FirstName, user.LastName, user.Email, reason, username); | ||
using (HttpClient client = new HttpClient()) | ||
{ | ||
|
||
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
client.DefaultRequestHeaders.Add("St2-Api-Key", guild.VPNAPIKey); | ||
using (HttpContent content = new StringContent(JsonSerializer.Serialize(data), System.Text.Encoding.UTF8, "application/json")) | ||
{ | ||
HttpResponseMessage result = await client.PostAsync(new Uri(guild.VPNAPIURL), content); | ||
|
||
if (result.IsSuccessStatusCode) | ||
{ | ||
Console.WriteLine("VPN Request success: " + user.DiscordID); | ||
return true; | ||
} | ||
Console.WriteLine("VPN Request failed: " + user.DiscordID + " " + result.StatusCode + " " + result.ReasonPhrase); | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|