From cf7cb56ff7d27eacf72778b0c3c4ae40e6b02cb2 Mon Sep 17 00:00:00 2001 From: andrurodr Date: Fri, 4 Mar 2022 09:12:17 -0300 Subject: [PATCH] proxy support --- README.md | 2 ++ src/input.ts | 26 +++++++++++++++++--------- src/main.ts | 11 ++++++----- src/utils.ts | 4 ++++ 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d7e63457..35083801 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Discord Notification Made Easy. color: 0x0000ff username: GitHub Actions avatar_url: ${{ secrets.AVATAR_URL }} + proxyHost: egress-proxy + proxyPort: 3129 ``` ![image](https://user-images.githubusercontent.com/33576079/102154036-ecf1ad00-3ebb-11eb-9af3-ff58982d9ecb.png) diff --git a/src/input.ts b/src/input.ts index a26f190a..d2b8d86f 100644 --- a/src/input.ts +++ b/src/input.ts @@ -1,5 +1,5 @@ import * as core from '@actions/core' -import { logWarning, stob } from './utils' +import { asNumber, logWarning, stob } from './utils' export interface Inputs { webhooks: string[] @@ -12,6 +12,8 @@ export interface Inputs { avatar_url: string nocontext: boolean noprefix: boolean + proxyHost?: string + proxyPort?: number } interface StatusOption { @@ -36,7 +38,7 @@ export const statusOpts: Record = { export function getInputs(): Inputs { // webhook - const webhook: string = core.getInput('webhook').trim() || process.env.DISCORD_WEBHOOK || '' + const webhook: string = core.getInput('webhook', { required: true, trimWhitespace: true}) || process.env.DISCORD_WEBHOOK || '' const webhooks: string[] = webhook.split('\n').filter(x => x || false) // prevent webhooks from leak webhooks.forEach((w, i) => { @@ -52,17 +54,23 @@ export function getInputs(): Inputs { const nocontext = nodetail || stob(core.getInput('nocontext')) const noprefix = nodetail || stob(core.getInput('noprefix')) + // retrieve proxy config + const proxyHost = core.getInput('proxyHost', { required: false, trimWhitespace: true }) + const proxyPort = asNumber(core.getInput('proxyPort', { required: false, trimWhitespace: true })) + const inputs: Inputs = { webhooks: webhooks, - status: core.getInput('status').trim().toLowerCase(), - description: core.getInput('description').trim(), - title: (core.getInput('title') || core.getInput('job')).trim(), - image: core.getInput('image').trim(), + status: core.getInput('status', { trimWhitespace: true }).toLowerCase(), + description: core.getInput('description', { trimWhitespace: true }), + title: (core.getInput('title') || core.getInput('job', { trimWhitespace: true })), + image: core.getInput('image', { trimWhitespace: true }), color: parseInt(core.getInput('color')), - username: core.getInput('username').trim(), - avatar_url: core.getInput('avatar_url').trim(), + username: core.getInput('username', { trimWhitespace: true }), + avatar_url: core.getInput('avatar_url', { trimWhitespace: true }), nocontext: nocontext, - noprefix: noprefix + noprefix: noprefix, + proxyHost, + proxyPort } // validate diff --git a/src/main.ts b/src/main.ts index d2a6cfc2..e3d4b48a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,17 +18,18 @@ async function run() { endGroup() logInfo(`Triggering ${inputs.webhooks.length} webhook${inputs.webhooks.length>1 ? 's' : ''}...`) - await Promise.all(inputs.webhooks.map(w => wrapWebhook(w.trim(), payload))) - } catch(e) { + await Promise.all(inputs.webhooks.map(w => wrapWebhook(w.trim(), payload, inputs.proxyHost, inputs.proxyPort))) + } catch(e: any) { logError(`Unexpected failure: ${e} (${e.message})`) } } -function wrapWebhook(webhook: string, payload: Object): Promise { +function wrapWebhook(webhook: string, payload: Object, proxyHost: string | undefined, proxyPort: number | undefined ): Promise { return async function() { try { - await axios.post(webhook, payload) - } catch(e) { + const config = proxyHost && proxyPort ? {proxy: { host: proxyHost, port: proxyPort }} : {} + await axios.post(webhook, payload, config) + } catch(e: any) { if (e.response) { logError(`Webhook response: ${e.response.status}: ${JSON.stringify(e.response.data)}`) } else { diff --git a/src/utils.ts b/src/utils.ts index 18235efd..bbddce45 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -21,3 +21,7 @@ export function logWarning(msg: string) { export function stob(s: string): boolean { return s.trim().toLowerCase() === 'true' } + +export function asNumber(s?: string): number | undefined { + return s ? parseInt(s) : undefined +}