Skip to content

Commit

Permalink
feat: add support for streams
Browse files Browse the repository at this point in the history
  • Loading branch information
jvvan committed Jan 25, 2022
1 parent f273b57 commit 0171b73
Show file tree
Hide file tree
Showing 4 changed files with 293 additions and 8 deletions.
154 changes: 148 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"dotenv": "^14.2.0"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.11",
"typescript": "^4.5.4"
}
}
31 changes: 31 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,35 @@ export const commands = [
new SlashCommandBuilder()
.setName("proxylist")
.setDescription("List all reverse proxies"),

new SlashCommandBuilder()
.setName("stream")
.setDescription("Make a stream")
.addStringOption((option) =>
option.setName("name").setDescription("Name").setRequired(true)
)
.addStringOption((option) =>
option
.setName("listen")
.setDescription("Listen (Example: 0.0.0.0:8080)")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("target")
.setDescription("Target (Example: 127.0.0.1:8181)")
.setRequired(true)
),
new SlashCommandBuilder()
.setName("streamdelete")
.setDescription("Delete a stream")
.addStringOption((option) =>
option
.setName("name")
.setDescription("Name of the stream to delete")
.setRequired(true)
),
new SlashCommandBuilder()
.setName("streamlist")
.setDescription("List all streams"),
];
114 changes: 112 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ client.on("interactionCreate", async (interaction): Promise<any> => {
});
} else if (interaction.commandName === "proxylist") {
const response = await proxy
.get<IMetadata[]>(`/proxies`)
.get<ProxyMetadata[]>(`/proxies`)
.then((res) => res.data);

if (!response.length) {
Expand All @@ -148,6 +148,110 @@ client.on("interactionCreate", async (interaction): Promise<any> => {
),
ephemeral: true,
});
} else if (interaction.commandName === "streamlist") {
const response = await proxy
.get<StreamMetadata[]>(`/streams`)
.then((res) => res.data);

if (!response.length) {
return interaction.reply({
embeds: [createEmbed("Stream List", "No streams found")],
});
}

const text = response
.map((stream) => `${stream.name}: ${stream.listen} -> ${stream.target}`)
.join("\n");
return interaction.reply({
embeds: Util.splitMessage(text, { maxLength: 2048 }).map((t) =>
createEmbed("Stream List", t)
),
ephemeral: true,
});
} else if (interaction.commandName === "stream") {
const name = interaction.options.getString("name");
const listen = interaction.options.getString("listen");
const target = interaction.options.getString("target");

await interaction.reply({
embeds: [
createEmbed(
"Please wait...",
"Creating a stream can take up to 30 seconds."
),
],
});
const response = await proxy
.post<{
message?: string;
error?: string;
statusCode: number;
}>(`/streams`, {
name,
target,
listen,
})
.then((r) => r.data)
.catch(console.error);
if (!response)
return interaction.editReply({
embeds: [createEmbed("Stream", "Something went wrong.")],
});

if (response?.statusCode !== 200) {
return interaction
.editReply({
embeds: [
createEmbed(
"Error while creating a stream",
response?.error ?? response?.message ?? "Unknown error"
),
],
})
.catch(() => {});
}
return interaction
.editReply({
embeds: [
createEmbed("Stream created", "Successfully created a stream"),
],
})
.catch(() => {});
} else if (interaction.commandName === "streamdelete") {
const name = interaction.options.getString("name");

await interaction.reply({
embeds: [
createEmbed(
"Please wait...",
"Deleting a stream can take up to 30 seconds."
),
],
});

const response = await proxy
.delete<{
error?: string;
message?: string;
statusCode: number;
}>(`/streams/${name}`)
.then((r) => r.data)
.catch(console.error);

if (response?.statusCode !== 200) {
return interaction.editReply({
embeds: [
createEmbed(
"Error while deleting a stream",
response?.error ?? response?.message ?? "Unknown error"
),
],
});
}

return interaction.editReply({
embeds: [createEmbed("Stream deleted", "Successfully deleted a stream")],
});
}
});

Expand All @@ -160,8 +264,14 @@ function createEmbed(title: string, description: string) {
.setColor(EMBED_COLOR);
}

interface IMetadata {
interface ProxyMetadata {
domain: string;
target: string;
ssl: boolean;
}

interface StreamMetadata {
name: string;
listen: string;
target: string;
}

0 comments on commit 0171b73

Please sign in to comment.