diff --git a/config.json b/config.json index e07c7ce9..b83acf90 100644 --- a/config.json +++ b/config.json @@ -22,6 +22,13 @@ }, "connectors": { "discord": "Discord Login Token", + "awnAPI": { + "orgID": "YourOrgID", + "creds": { + "username": "AwnUsername", + "password": "AwnPassword" + } + }, "mysql": { "host": "host", "port": 3306, diff --git a/squad-server/factory.js b/squad-server/factory.js index 70427c72..df91aa1b 100644 --- a/squad-server/factory.js +++ b/squad-server/factory.js @@ -4,6 +4,7 @@ import { fileURLToPath } from 'url'; import Discord from 'discord.js'; import sequelize from 'sequelize'; +import AwnAPI from './utils/awn-api.js'; import Logger from 'core/logger'; @@ -129,6 +130,12 @@ export default class SquadServerFactory { return connector; } + if (type === 'awnAPI') { + const awn = new AwnAPI(connectorConfig); + await awn.auth(connectorConfig); + return awn; + } + throw new Error(`${type.connector} is an unsupported connector type.`); } diff --git a/squad-server/templates/config-template.json b/squad-server/templates/config-template.json index 32c8d520..03b19c43 100644 --- a/squad-server/templates/config-template.json +++ b/squad-server/templates/config-template.json @@ -22,6 +22,7 @@ }, "connectors": { "discord": "Discord Login Token", + "awnAPI": {"orgID":"YourOrgID", "creds": {"username":"AwnUsername", "password":"AwnPassword"}}, "mysql": { "host": "host", "port": 3306, diff --git a/squad-server/utils/awn-api.js b/squad-server/utils/awn-api.js new file mode 100644 index 00000000..20000364 --- /dev/null +++ b/squad-server/utils/awn-api.js @@ -0,0 +1,87 @@ +import axios from 'axios'; +import Logger from 'core/logger'; + +export default class AwnAPI { + constructor(options) { + this.orgID = options.orgID; + this.xAuthToken = null; + } + + async auth(options) { + const requestData = { + username: `${options.creds.username}`, + password: `${options.creds.password}` + }; + const res = await this.request('post', '/v5/auth/login', requestData); + + if (res.status === 200 && res.data.valid) { + this.xAuthToken = res.data.accessToken; + Logger.verbose('awnAPI', 1, `Authentication succeeded.`); + } + } + + async request(method, endpoint, data) { + let ret = null; + + if (!endpoint.startsWith('/')) endpoint = `/${endpoint}`; + if (!endpoint.startsWith('/v5/')) endpoint = `/v5/org/${this.orgID}${endpoint}`; + if (endpoint.includes(':orgId')) endpoint = endpoint.replace(':orgId', `${this.orgID}`); + + try { + const axiosRequest = { + method: method, + url: `https://api.awn.gg${endpoint}`, + data: data, + headers: { 'X-AWN-ACCESS-TOKEN': this.xAuthToken } + }; + + Logger.verbose('awnAPI', 1, `${axiosRequest.method.toUpperCase()}: ${axiosRequest.url}`); + Logger.verbose('awnAPI', 3, `Request Data: ${JSON.stringify(data)}`); + + const res = await axios(axiosRequest); + + ret = { status: res.status, statusText: res.statusText, data: res.data }; + Logger.verbose('awnAPI', 3, `${JSON.stringify(ret)}`); + return ret; + } catch (err) { + ret = { + status: err.response.status, + statusText: err.response.statusText, + error: err.response.data.error + }; + Logger.verbose('awnAPI', 1, `ERROR: ${JSON.stringify(err.response.status)}`); + Logger.verbose('awnAPI', 3, `ERROR: ${JSON.stringify(ret)}`); + return ret; + } + } + + async addAdmin(listID, steamID) { + const ret = await this.request('post', `game-servers/admin-lists/${listID}/admins`, { + type: 'steam64', + value: `${steamID}` + }); + ret.success = ret.status === 200; + return ret; + } + + async getAdmin(listID, adminID) { + const ret = await this.request('get', `game-servers/admin-lists/${listID}/admins/${adminID}`); + ret.success = ret.status === 200; + return ret; + } + + async removeAdmin(listID, adminID) { + const ret = await this.request( + 'delete', + `game-servers/admin-lists/${listID}/admins/${adminID}` + ); + ret.success = ret.status === 204; + return ret; + } + + async getAdminList(listID) { + const ret = await this.request('get', `game-servers/admin-lists/${listID}`); + ret.success = ret.status === 200; + return ret; + } +}