Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
04d70a4d12 | |||
8da43a79d3 | |||
3153021190 | |||
82701c19e7 | |||
d3a68b4fef | |||
c4c1367306 | |||
1f5352d9f5 | |||
f652cc72fe | |||
f510408fce | |||
e250e9b1a2 | |||
5a9b7bbeee | |||
1158b4ff99 | |||
8eb777dd45 | |||
acf5c242d3 | |||
9e9dd8d935 | |||
b7cc500f4d | |||
8a4126a49c |
22
.gitignore
vendored
22
.gitignore
vendored
@ -1,6 +1,22 @@
|
|||||||
|
.nogit/
|
||||||
|
|
||||||
|
# artifacts
|
||||||
coverage/
|
coverage/
|
||||||
docs/
|
public/
|
||||||
|
pages/
|
||||||
|
|
||||||
|
# installs
|
||||||
node_modules/
|
node_modules/
|
||||||
|
|
||||||
.nogit/
|
# caches
|
||||||
nogit/
|
.yarn/
|
||||||
|
.cache/
|
||||||
|
.rpt2_cache
|
||||||
|
|
||||||
|
# builds
|
||||||
|
dist/
|
||||||
|
dist_web/
|
||||||
|
dist_serve/
|
||||||
|
dist_ts_web/
|
||||||
|
|
||||||
|
# custom
|
@ -1,10 +1,10 @@
|
|||||||
# gitzone standard
|
# gitzone ci_default
|
||||||
image: hosttoday/ht-docker-node:npmci
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
paths:
|
paths:
|
||||||
- .npmci_cache/
|
- .npmci_cache/
|
||||||
key: "$CI_BUILD_STAGE"
|
key: '$CI_BUILD_STAGE'
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- security
|
- security
|
||||||
@ -20,53 +20,49 @@ mirror:
|
|||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci git mirror
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
snyk:
|
snyk:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g snyk
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command snyk test
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
# ====================
|
# ====================
|
||||||
testLEGACY:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install legacy
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
testLTS:
|
testStable:
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci node install lts
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
testSTABLE:
|
|
||||||
stage: test
|
stage: test
|
||||||
script:
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
- npmci node install stable
|
- npmci node install stable
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
|
- docker
|
||||||
|
- priv
|
||||||
|
|
||||||
|
testBuild:
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- npmci npm prepare
|
||||||
|
- npmci node install stable
|
||||||
|
- npmci npm install
|
||||||
|
- npmci command npm run build
|
||||||
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@ -78,6 +74,7 @@ release:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
@ -86,20 +83,14 @@ release:
|
|||||||
# ====================
|
# ====================
|
||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
image: docker:stable
|
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
services:
|
|
||||||
- docker:stable-dind
|
|
||||||
script:
|
script:
|
||||||
- export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
|
- npmci command npm install -g tslint typescript
|
||||||
- docker run
|
- npmci npm prepare
|
||||||
--env SOURCE_CODE="$PWD"
|
- npmci npm install
|
||||||
--volume "$PWD":/code
|
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
||||||
--volume /var/run/docker.sock:/var/run/docker.sock
|
|
||||||
"registry.gitlab.com/gitlab-org/security-products/codequality:$SP_VERSION" /code
|
|
||||||
artifacts:
|
|
||||||
paths: [codeclimate.json]
|
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- priv
|
- priv
|
||||||
|
|
||||||
@ -110,17 +101,20 @@ trigger:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
|
|
||||||
pages:
|
pages:
|
||||||
image: hosttoday/ht-docker-node:npmci
|
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g typedoc typescript
|
- npmci node install lts
|
||||||
|
- npmci command npm install -g @gitzone/tsdoc
|
||||||
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command typedoc --module "commonjs" --target "ES2016" --out public/ ts/
|
- npmci command tsdoc
|
||||||
tags:
|
tags:
|
||||||
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
- notpriv
|
||||||
only:
|
only:
|
||||||
@ -130,13 +124,3 @@ pages:
|
|||||||
paths:
|
paths:
|
||||||
- public
|
- public
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
|
||||||
windowsCompatibility:
|
|
||||||
image: stefanscherer/node-windows:10-build-tools
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npm install & npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- windows
|
|
||||||
allow_failure: true
|
|
||||||
|
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
26
.vscode/settings.json
vendored
Normal file
26
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
{
|
||||||
|
"json.schemas": [
|
||||||
|
{
|
||||||
|
"fileMatch": ["/npmextra.json"],
|
||||||
|
"schema": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"npmci": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for npmci"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "settings for gitzone",
|
||||||
|
"properties": {
|
||||||
|
"projectType": {
|
||||||
|
"type": "string",
|
||||||
|
"enum": ["website", "element", "service", "npm"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
54
README.md
54
README.md
@ -1,54 +0,0 @@
|
|||||||
# cflare
|
|
||||||
|
|
||||||
easy cloudflare management
|
|
||||||
|
|
||||||
## Availabililty
|
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/cflare)
|
|
||||||
[](https://GitLab.com/mojoio/cflare)
|
|
||||||
[](https://github.com/mojoio/cflare)
|
|
||||||
[](https://mojoio.gitlab.io/cflare/)
|
|
||||||
|
|
||||||
## Status for master
|
|
||||||
|
|
||||||
[](https://GitLab.com/mojoio/cflare/commits/master)
|
|
||||||
[](https://GitLab.com/mojoio/cflare/commits/master)
|
|
||||||
[](https://www.npmjs.com/package/cflare)
|
|
||||||
[](https://david-dm.org/mojoio/cflare)
|
|
||||||
[](https://www.bithound.io/github/mojoio/cflare/master/dependencies/npm)
|
|
||||||
[](https://www.bithound.io/github/mojoio/cflare)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](https://nodejs.org/dist/latest-v6.x/docs/api/)
|
|
||||||
[](http://standardjs.com/)
|
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
import * as cflare from 'cflare'
|
|
||||||
|
|
||||||
let myCflareAccount = new cflare.CflareAccount()
|
|
||||||
testCflareAccount.auth({
|
|
||||||
email: 'someuser@example.com',
|
|
||||||
key: 'someLongApiKey'
|
|
||||||
})
|
|
||||||
|
|
||||||
let myAsyncCflareManagement = async () => {
|
|
||||||
// get things
|
|
||||||
let myZones = await myCflareAccount.listZones() // zones are fully typed
|
|
||||||
let myIdForADomain = await myCflareAccount.getZoneId('example.com') // type number
|
|
||||||
let myRecordsForADomain = await myCflareAccount.listRecords('example.com') // records are fully typed
|
|
||||||
|
|
||||||
// set things
|
|
||||||
myCflareAccount.updateRecord(...)
|
|
||||||
myCflareAccount.createRecord(...)
|
|
||||||
myCflareAccount.deleteRecord(...)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
For further information read the linked docs at the top of this README.
|
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
|
||||||
|
|
||||||
[](https://mojo.io)
|
|
@ -2,5 +2,16 @@
|
|||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [],
|
"npmGlobalTools": [],
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
|
"module": {
|
||||||
|
"githost": "gitlab.com",
|
||||||
|
"gitscope": "mojoio",
|
||||||
|
"gitrepo": "cloudflare",
|
||||||
|
"shortDescription": "easy cloudflare management",
|
||||||
|
"npmPackagename": "@mojoio/cloudflare",
|
||||||
|
"license": "MIT"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
1731
package-lock.json
generated
1731
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
41
package.json
41
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/cloudflare",
|
"name": "@mojoio/cloudflare",
|
||||||
"version": "2.0.0",
|
"version": "3.0.6",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "easy cloudflare management",
|
"description": "easy cloudflare management",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -24,19 +24,32 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
"homepage": "https://gitlab.com/pushrocks/cflare#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.2",
|
"@pushrocks/smartdelay": "^2.0.6",
|
||||||
"@pushrocks/smartlog": "^2.0.1",
|
"@pushrocks/smartlog": "^2.0.21",
|
||||||
"@pushrocks/smartpromise": "^2.0.5",
|
"@pushrocks/smartpromise": "^3.0.6",
|
||||||
"@pushrocks/smartrequest": "^1.1.14",
|
"@pushrocks/smartrequest": "^1.1.47",
|
||||||
"@pushrocks/smartstring": "^3.0.4",
|
"@pushrocks/smartstring": "^3.0.18",
|
||||||
"@tsclass/tsclass": "^1.0.48"
|
"@tsclass/tsclass": "^3.0.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.17",
|
||||||
"@gitzone/tsrun": "^1.1.12",
|
"@gitzone/tsrun": "^1.2.8",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.28",
|
||||||
"@pushrocks/qenv": "^2.0.2",
|
"@pushrocks/qenv": "^4.0.6",
|
||||||
"@pushrocks/tapbundle": "^3.0.5",
|
"@pushrocks/tapbundle": "^3.2.0",
|
||||||
"@types/node": "^10.5.8"
|
"@types/node": "^13.7.0",
|
||||||
}
|
"tslint": "^6.0.0",
|
||||||
|
"tslint-config-prettier": "^1.18.0"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"ts/**/*",
|
||||||
|
"ts_web/**/*",
|
||||||
|
"dist/**/*",
|
||||||
|
"dist_web/**/*",
|
||||||
|
"dist_ts_web/**/*",
|
||||||
|
"assets/**/*",
|
||||||
|
"cli.js",
|
||||||
|
"npmextra.json",
|
||||||
|
"readme.md"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
50
test/test.ts
50
test/test.ts
@ -1,19 +1,25 @@
|
|||||||
|
// tslint:disable-next-line: no-implicit-dependencies
|
||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import cloudflare = require('../ts/index');
|
// tslint:disable-next-line: no-implicit-dependencies
|
||||||
import { Qenv } from '@pushrocks/qenv';
|
import { Qenv } from '@pushrocks/qenv';
|
||||||
let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
|
||||||
|
|
||||||
let testCloudflareAccount = new cloudflare.CloudflareAccount();
|
import cloudflare = require('../ts/index');
|
||||||
testCloudflareAccount.auth({
|
|
||||||
email: process.env.CF_EMAIL,
|
const testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
||||||
key: process.env.CF_KEY
|
|
||||||
|
const randomPrefix = Math.floor(Math.random() * 2000);
|
||||||
|
let testCloudflareAccount: cloudflare.CloudflareAccount;
|
||||||
|
|
||||||
|
tap.test('should create a valid instance of CloudflareAccount', async () => {
|
||||||
|
testCloudflareAccount = new cloudflare.CloudflareAccount({
|
||||||
|
email: testQenv.getEnvVarOnDemand('CF_EMAIL'),
|
||||||
|
key: testQenv.getEnvVarOnDemand('CF_KEY')
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
let randomPrefix = Math.floor(Math.random() * 2000);
|
tap.test('.listZones() -> should display an entire account', async tools => {
|
||||||
|
|
||||||
tap.skip.test('.listZones() -> should display an entire account', async tools => {
|
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
let result = await testCloudflareAccount.listZones();
|
const result = await testCloudflareAccount.listZones();
|
||||||
console.log(result);
|
console.log(result);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -42,7 +48,9 @@ tap.test('should create a valid record for a subdomain', async tools => {
|
|||||||
|
|
||||||
tap.test('should get a record from Cloudflare', async tools => {
|
tap.test('should get a record from Cloudflare', async tools => {
|
||||||
tools.timeout(600000);
|
tools.timeout(600000);
|
||||||
await testCloudflareAccount.getRecord('bleu.de', 'A').then(function(responseArg) {
|
await testCloudflareAccount
|
||||||
|
.getRecord(`${randomPrefix}subdomain.bleu.de`, 'A')
|
||||||
|
.then(responseArg => {
|
||||||
console.log(responseArg);
|
console.log(responseArg);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@ -60,4 +68,24 @@ tap.test('.purge(some.domain) -> should purge everything', async () => {
|
|||||||
await testCloudflareAccount.purgeZone('bleu.de');
|
await testCloudflareAccount.purgeZone('bleu.de');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// WORKERS
|
||||||
|
tap.test('should create a worker', async () => {
|
||||||
|
const worker = await testCloudflareAccount.workerManager.createWorker(
|
||||||
|
'myawesomescript',
|
||||||
|
`addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })`
|
||||||
|
);
|
||||||
|
await worker.setRoutes([
|
||||||
|
{
|
||||||
|
zoneName: 'bleu.de',
|
||||||
|
pattern: 'https://*bleu.de/hello'
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
console.log(worker);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should get workers', async () => {
|
||||||
|
const workerArray = await testCloudflareAccount.workerManager.listWorkers();
|
||||||
|
console.log(workerArray);
|
||||||
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,35 +1,54 @@
|
|||||||
import plugins = require('./cloudflare.plugins');
|
import plugins = require('./cloudflare.plugins');
|
||||||
import * as interfaces from './cloudflare.interfaces';
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
import { TDnsRecord } from '@tsclass/tsclass';
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
|
import { ZoneManager } from './cloudflare.classes.zonemanager';
|
||||||
|
|
||||||
export class CloudflareAccount {
|
export class CloudflareAccount {
|
||||||
private authEmail: string;
|
private authEmail: string;
|
||||||
private authKey: string;
|
private authKey: string;
|
||||||
constructor() {
|
private accountIdentifier: string;
|
||||||
// Nothing here
|
|
||||||
}
|
|
||||||
|
|
||||||
auth(optionsArg: { email: string; key: string }) {
|
public workerManager = new WorkerManager(this);
|
||||||
|
public zoneManager = new ZoneManager(this);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* constructor sets auth information on the CloudflareAccountInstance
|
||||||
|
* @param optionsArg
|
||||||
|
*/
|
||||||
|
constructor(optionsArg: { email: string; key: string }) {
|
||||||
this.authEmail = optionsArg.email;
|
this.authEmail = optionsArg.email;
|
||||||
this.authKey = optionsArg.key;
|
this.authKey = optionsArg.key;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getAccountIdentifier() {
|
||||||
|
if (!this.accountIdentifier) {
|
||||||
|
const route = `/accounts?page=1&per_page=20&direction=desc`;
|
||||||
|
const response: any = await this.request('GET', route);
|
||||||
|
this.accountIdentifier = response.result[0].id;
|
||||||
|
// console.log('Account identifier is: ' + this.accountIdentifier);
|
||||||
|
}
|
||||||
|
return this.accountIdentifier;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets a zone id of a domain from cloudflare
|
* gets a zone id of a domain from cloudflare
|
||||||
* @param domainName
|
* @param domainName
|
||||||
*/
|
*/
|
||||||
async getZoneId(domainName: string) {
|
public async getZoneId(domainName: string) {
|
||||||
let domain = new plugins.smartstring.Domain(domainName);
|
const domain = new plugins.smartstring.Domain(domainName);
|
||||||
let zoneArray = await this.listZones(domain.zoneName);
|
const zoneArray = await this.listZones(domain.zoneName);
|
||||||
let filteredResponse = zoneArray.filter(zoneArg => {
|
const filteredResponse = zoneArray.filter(zoneArg => {
|
||||||
return zoneArg.name === domainName;
|
return zoneArg.name === domainName;
|
||||||
});
|
});
|
||||||
if (filteredResponse.length >= 1) {
|
if (filteredResponse.length >= 1) {
|
||||||
return filteredResponse[0].id;
|
return filteredResponse[0].id;
|
||||||
} else {
|
} else {
|
||||||
plugins.smartlog.defaultLogger.error(`the domain ${domainName} does not appear to be in this account!`);
|
plugins.smartlog.defaultLogger.log(
|
||||||
|
'error',
|
||||||
|
`the domain ${domainName} does not appear to be in this account!`
|
||||||
|
);
|
||||||
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
|
throw new Error(`the domain ${domainName} does not appear to be in this account!`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,57 +58,67 @@ export class CloudflareAccount {
|
|||||||
* @param domainNameArg
|
* @param domainNameArg
|
||||||
* @param typeArg
|
* @param typeArg
|
||||||
*/
|
*/
|
||||||
async getRecord(domainNameArg: string, typeArg: TDnsRecord): Promise<interfaces.ICflareRecord> {
|
public async getRecord(
|
||||||
let done = plugins.smartpromise.defer<interfaces.ICflareRecord>();
|
domainNameArg: string,
|
||||||
let result: interfaces.ICflareRecord;
|
typeArg: plugins.tsclass.network.TDnsRecord
|
||||||
|
): Promise<interfaces.ICflareRecord> {
|
||||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
this.listRecords(domain.zoneName).then(recordArrayArg => {
|
const recordArrayArg = await this.listRecords(domain.zoneName);
|
||||||
let filteredResponse = recordArrayArg.filter(recordArg => {
|
const filteredResponse = recordArrayArg.filter(recordArg => {
|
||||||
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
return recordArg.type === typeArg && recordArg.name === domainNameArg;
|
||||||
});
|
});
|
||||||
done.resolve(filteredResponse[0]);
|
return filteredResponse[0];
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async createRecord(domainNameArg: string, typeArg: TDnsRecord, contentArg: string) {
|
public async createRecord(
|
||||||
let done = plugins.smartpromise.defer();
|
domainNameArg: string,
|
||||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
typeArg: plugins.tsclass.network.TDnsRecord,
|
||||||
let domainIdArg = await this.getZoneId(domain.zoneName);
|
contentArg: string
|
||||||
let dataObject = {
|
): Promise<any> {
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
const domainIdArg = await this.getZoneId(domain.zoneName);
|
||||||
|
const dataObject = {
|
||||||
name: domain.fullName,
|
name: domain.fullName,
|
||||||
type: typeArg,
|
type: typeArg,
|
||||||
content: contentArg
|
content: contentArg
|
||||||
};
|
};
|
||||||
this.request('POST', '/zones/' + domainIdArg + '/dns_records', dataObject).then(function(
|
const response = await this.request(
|
||||||
responseArg
|
'POST',
|
||||||
) {
|
'/zones/' + domainIdArg + '/dns_records',
|
||||||
done.resolve(responseArg);
|
dataObject
|
||||||
});
|
);
|
||||||
return done.promise;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
removeRecord(domainNameArg: string, typeArg: TDnsRecord) {
|
/**
|
||||||
let done = plugins.smartpromise.defer();
|
* removes a record from Cloudflare
|
||||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
* @param domainNameArg
|
||||||
this.getRecord(domain.fullName, typeArg).then(responseArg => {
|
* @param typeArg
|
||||||
if (responseArg) {
|
*/
|
||||||
let requestRoute: string =
|
public async removeRecord(
|
||||||
'/zones/' + responseArg.zone_id + '/dns_records/' + responseArg.id;
|
domainNameArg: string,
|
||||||
this.request('DELETE', requestRoute).then(responseArg => {
|
typeArg: plugins.tsclass.network.TDnsRecord
|
||||||
done.resolve(responseArg);
|
): Promise<any> {
|
||||||
});
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
|
const cflareRecord = await this.getRecord(domain.fullName, typeArg);
|
||||||
|
if (cflareRecord) {
|
||||||
|
const requestRoute: string = `/zones/${cflareRecord.zone_id}/dns_records/${cflareRecord.id}`;
|
||||||
|
return await this.request('DELETE', requestRoute);
|
||||||
} else {
|
} else {
|
||||||
done.reject();
|
throw new Error(`could not remove record for ${domainNameArg} with type ${typeArg}`);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
return done.promise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
/**
|
||||||
let done = plugins.smartpromise.defer();
|
* updates a record
|
||||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
* @param domainNameArg
|
||||||
|
* @param typeArg
|
||||||
|
* @param valueArg
|
||||||
|
*/
|
||||||
|
public updateRecord(domainNameArg: string, typeArg: string, valueArg) {
|
||||||
|
// TODO: implement
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
return done.promise;
|
return done.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,14 +126,14 @@ export class CloudflareAccount {
|
|||||||
* list all records of a specified domain name
|
* list all records of a specified domain name
|
||||||
* @param domainNameArg - the domain name that you want to get the records from
|
* @param domainNameArg - the domain name that you want to get the records from
|
||||||
*/
|
*/
|
||||||
async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
public async listRecords(domainNameArg: string): Promise<interfaces.ICflareRecord[]> {
|
||||||
let domain = new plugins.smartstring.Domain(domainNameArg);
|
const domain = new plugins.smartstring.Domain(domainNameArg);
|
||||||
let domainId = await this.getZoneId(domain.zoneName);
|
const domainId = await this.getZoneId(domain.zoneName);
|
||||||
let responseArg: any = await this.request(
|
const responseArg: any = await this.request(
|
||||||
'GET',
|
'GET',
|
||||||
'/zones/' + domainId + '/dns_records?per_page=100'
|
'/zones/' + domainId + '/dns_records?per_page=100'
|
||||||
);
|
);
|
||||||
let result: interfaces.ICflareRecord[] = responseArg.result;
|
const result: interfaces.ICflareRecord[] = responseArg.result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,7 +141,7 @@ export class CloudflareAccount {
|
|||||||
* list all zones in the associated authenticated account
|
* list all zones in the associated authenticated account
|
||||||
* @param domainName
|
* @param domainName
|
||||||
*/
|
*/
|
||||||
async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
|
public async listZones(domainName?: string): Promise<interfaces.ICflareZone[]> {
|
||||||
// TODO: handle pagination
|
// TODO: handle pagination
|
||||||
let requestRoute = `/zones?per_page=50`;
|
let requestRoute = `/zones?per_page=50`;
|
||||||
|
|
||||||
@ -121,38 +150,46 @@ export class CloudflareAccount {
|
|||||||
requestRoute = `${requestRoute}&name=${domainName}`;
|
requestRoute = `${requestRoute}&name=${domainName}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
let response: any = await this.request('GET', requestRoute);
|
const response: any = await this.request('GET', requestRoute);
|
||||||
let result = response.result;
|
const result = response.result;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async purgeZone(domainName: string) {
|
public async purgeZone(domainName: string) {
|
||||||
let domain = new plugins.smartstring.Domain(domainName);
|
const domain = new plugins.smartstring.Domain(domainName);
|
||||||
let domainId = await this.getZoneId(domain.zoneName);
|
const domainId = await this.getZoneId(domain.zoneName);
|
||||||
let requestUrl = `/zones/${domainId}/purge_cache`;
|
const requestUrl = `/zones/${domainId}/purge_cache`;
|
||||||
let payload = {
|
const payload = {
|
||||||
purge_everything: true
|
purge_everything: true
|
||||||
};
|
};
|
||||||
let respone = await this.request('DELETE', requestUrl, payload);
|
const respone = await this.request('DELETE', requestUrl, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
request(methodArg: string, routeArg: string, dataArg = {}) {
|
public request(
|
||||||
let done = plugins.smartpromise.defer();
|
methodArg: string,
|
||||||
let options: plugins.smartrequest.ISmartRequestOptions = {
|
routeArg: string,
|
||||||
|
dataArg: any = {},
|
||||||
|
requestHeadersArg = {}
|
||||||
|
): Promise<any> {
|
||||||
|
const done = plugins.smartpromise.defer();
|
||||||
|
const options: plugins.smartrequest.ISmartRequestOptions = {
|
||||||
method: methodArg,
|
method: methodArg,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-Auth-Email': this.authEmail,
|
'X-Auth-Email': this.authEmail,
|
||||||
'X-Auth-Key': this.authKey,
|
'X-Auth-Key': this.authKey,
|
||||||
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg))
|
'Content-Length': Buffer.byteLength(JSON.stringify(dataArg)),
|
||||||
|
...requestHeadersArg
|
||||||
},
|
},
|
||||||
requestBody: dataArg
|
requestBody: dataArg
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// console.log(options);
|
||||||
|
|
||||||
let retryCount = 0; // count the amount of retries
|
let retryCount = 0; // count the amount of retries
|
||||||
|
|
||||||
let makeRequest = async () => {
|
const makeRequest = async () => {
|
||||||
let response: any = await plugins.smartrequest.request(
|
const response: any = await plugins.smartrequest.request(
|
||||||
`https://api.cloudflare.com/client/v4${routeArg}`,
|
`https://api.cloudflare.com/client/v4${routeArg}`,
|
||||||
options
|
options
|
||||||
);
|
);
|
||||||
@ -163,13 +200,15 @@ export class CloudflareAccount {
|
|||||||
retryRequest();
|
retryRequest();
|
||||||
} else if (response.statusCode === 400) {
|
} else if (response.statusCode === 400) {
|
||||||
console.log(`bad request for route ${routeArg}! Going to retry!`);
|
console.log(`bad request for route ${routeArg}! Going to retry!`);
|
||||||
retryRequest();
|
console.log(response.body);
|
||||||
} else {
|
} else {
|
||||||
console.log(response.statusCode);
|
console.log(response.statusCode);
|
||||||
done.reject(new Error('request failed'));
|
done.reject(new Error('request failed'));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let retryRequest = async (delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)) => {
|
const retryRequest = async (
|
||||||
|
delayTimeArg = Math.floor(Math.random() * (60000 - 8000) + 8000)
|
||||||
|
) => {
|
||||||
console.log(`retry started and waiting for ${delayTimeArg} ms`);
|
console.log(`retry started and waiting for ${delayTimeArg} ms`);
|
||||||
await plugins.smartdelay.delayFor(delayTimeArg);
|
await plugins.smartdelay.delayFor(delayTimeArg);
|
||||||
if (retryCount < 10) {
|
if (retryCount < 10) {
|
||||||
@ -184,4 +223,6 @@ export class CloudflareAccount {
|
|||||||
private authCheck() {
|
private authCheck() {
|
||||||
return this.authEmail && this.authKey; // check if auth is available
|
return this.authEmail && this.authKey; // check if auth is available
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// acme convenience functions
|
||||||
}
|
}
|
||||||
|
92
ts/cloudflare.classes.worker.ts
Normal file
92
ts/cloudflare.classes.worker.ts
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
import { WorkerManager } from './cloudflare.classes.workermanager';
|
||||||
|
|
||||||
|
export interface IWorkerRoute extends interfaces.ICflareWorkerRoute {
|
||||||
|
zoneName: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CloudflareWorker {
|
||||||
|
// STATIC
|
||||||
|
public static async fromApiObject(
|
||||||
|
workerManager: WorkerManager,
|
||||||
|
apiObject
|
||||||
|
): Promise<CloudflareWorker> {
|
||||||
|
const newWorker = new CloudflareWorker(workerManager);
|
||||||
|
Object.assign(newWorker, apiObject.result);
|
||||||
|
await newWorker.getRoutes();
|
||||||
|
return newWorker;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
private workerManager: WorkerManager;
|
||||||
|
|
||||||
|
public script: string;
|
||||||
|
public id: string;
|
||||||
|
public etag: string;
|
||||||
|
// tslint:disable-next-line: variable-name
|
||||||
|
public created_on: string;
|
||||||
|
// tslint:disable-next-line: variable-name
|
||||||
|
public modified_on: string;
|
||||||
|
|
||||||
|
public routes: IWorkerRoute[] = [];
|
||||||
|
constructor(workerManagerArg: WorkerManager) {
|
||||||
|
this.workerManager = workerManagerArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all routes for a worker
|
||||||
|
*/
|
||||||
|
public async getRoutes() {
|
||||||
|
const zones = await this.workerManager.cfAccount.listZones();
|
||||||
|
for (const zone of zones) {
|
||||||
|
const requestRoute = `/zones/${zone.id}/workers/routes`;
|
||||||
|
const response: {
|
||||||
|
result: interfaces.ICflareWorkerRoute[];
|
||||||
|
} = await this.workerManager.cfAccount.request('GET', requestRoute);
|
||||||
|
for (const route of response.result) {
|
||||||
|
console.log('hey');
|
||||||
|
console.log(route);
|
||||||
|
console.log(this.id);
|
||||||
|
if (route.script === this.id) {
|
||||||
|
this.routes.push({ ...route, zoneName: zone.name });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setRoutes(routeArray: Array<{ zoneName: string; pattern: string }>) {
|
||||||
|
for (const newRoute of routeArray) {
|
||||||
|
// lets determine wether a route is new, needs an update or already up to date.
|
||||||
|
let routeStatus: 'new' | 'needsUpdate' | 'alreadyUpToDate' = 'new';
|
||||||
|
let routeIdForUpdate: string;
|
||||||
|
for (const existingRoute of this.routes) {
|
||||||
|
if (existingRoute.pattern === newRoute.pattern) {
|
||||||
|
routeStatus = 'needsUpdate';
|
||||||
|
routeIdForUpdate = existingRoute.id;
|
||||||
|
if (existingRoute.script === this.id) {
|
||||||
|
routeStatus = 'alreadyUpToDate';
|
||||||
|
plugins.smartlog.defaultLogger.log('info', `route already exists, no update needed`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lets care about actually setting routes
|
||||||
|
if (routeStatus === 'new') {
|
||||||
|
const zoneId = await this.workerManager.cfAccount.getZoneId(newRoute.zoneName);
|
||||||
|
const requestRoute = `/zones/${zoneId}/workers/routes`;
|
||||||
|
await this.workerManager.cfAccount.request('POST', requestRoute, {
|
||||||
|
pattern: newRoute.pattern,
|
||||||
|
script: this.id
|
||||||
|
});
|
||||||
|
} else if (routeStatus === 'needsUpdate') {
|
||||||
|
const zoneId = await this.workerManager.cfAccount.getZoneId(newRoute.zoneName);
|
||||||
|
const requestRoute = `/zones/${zoneId}/workers/routes/${routeIdForUpdate}`;
|
||||||
|
await this.workerManager.cfAccount.request('PUT', requestRoute, {
|
||||||
|
pattern: newRoute.pattern,
|
||||||
|
script: this.id
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
36
ts/cloudflare.classes.workermanager.ts
Normal file
36
ts/cloudflare.classes.workermanager.ts
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
import { CloudflareWorker } from './cloudflare.classes.worker';
|
||||||
|
|
||||||
|
export class WorkerManager {
|
||||||
|
public cfAccount: CloudflareAccount;
|
||||||
|
|
||||||
|
constructor(cfAccountArg: CloudflareAccount) {
|
||||||
|
this.cfAccount = cfAccountArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async createWorker(workerName: string, workerScript: string): Promise<CloudflareWorker> {
|
||||||
|
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
||||||
|
const route = `/accounts/${accountIdentifier}/workers/scripts/${workerName}`;
|
||||||
|
const responseBody = await this.cfAccount.request('PUT', route, workerScript, {
|
||||||
|
'Content-Type': 'application/javascript',
|
||||||
|
'Content-Length': Buffer.byteLength(workerScript)
|
||||||
|
});
|
||||||
|
return CloudflareWorker.fromApiObject(this, responseBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* lists workers
|
||||||
|
*/
|
||||||
|
public async listWorkers() {
|
||||||
|
const accountIdentifier = await this.cfAccount.getAccountIdentifier();
|
||||||
|
const route = `/accounts/${accountIdentifier}/workers/scripts`;
|
||||||
|
const response = await this.cfAccount.request('GET', route);
|
||||||
|
const results = response.result;
|
||||||
|
const workers: CloudflareWorker[] = [];
|
||||||
|
for (const apiObject of results) {
|
||||||
|
workers.push(await CloudflareWorker.fromApiObject(this, apiObject));
|
||||||
|
}
|
||||||
|
return workers;
|
||||||
|
}
|
||||||
|
}
|
6
ts/cloudflare.classes.zone.ts
Normal file
6
ts/cloudflare.classes.zone.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import * as interfaces from './interfaces';
|
||||||
|
|
||||||
|
export class CloudflareZone {
|
||||||
|
// TODO
|
||||||
|
}
|
11
ts/cloudflare.classes.zonemanager.ts
Normal file
11
ts/cloudflare.classes.zonemanager.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import * as plugins from './cloudflare.plugins';
|
||||||
|
import { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
|
||||||
|
export class ZoneManager {
|
||||||
|
public cfAccount: CloudflareAccount;
|
||||||
|
public zoneName: string;
|
||||||
|
|
||||||
|
constructor(cfAccountArg: CloudflareAccount) {
|
||||||
|
this.cfAccount = cfAccountArg;
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
export { CloudflareAccount } from './cloudflare.classes.account';
|
export { CloudflareAccount } from './cloudflare.classes.account';
|
||||||
|
export { CloudflareWorker } from './cloudflare.classes.worker';
|
||||||
|
15
ts/interfaces/cloudflare.api.record.ts
Normal file
15
ts/interfaces/cloudflare.api.record.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
export interface ICflareRecord {
|
||||||
|
id: string;
|
||||||
|
type: string;
|
||||||
|
name: string;
|
||||||
|
content: string;
|
||||||
|
proxiable: boolean;
|
||||||
|
proxied: boolean;
|
||||||
|
ttl: number;
|
||||||
|
locked: boolean;
|
||||||
|
zone_id: string;
|
||||||
|
zone_name: string;
|
||||||
|
created_on: string;
|
||||||
|
modified_on: string;
|
||||||
|
data: any;
|
||||||
|
}
|
5
ts/interfaces/cloudflare.api.workerroute.ts
Normal file
5
ts/interfaces/cloudflare.api.workerroute.ts
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
export interface ICflareWorkerRoute {
|
||||||
|
id: string;
|
||||||
|
pattern: string;
|
||||||
|
script: string;
|
||||||
|
}
|
@ -1,5 +1,3 @@
|
|||||||
import * as plugins from './cloudflare.plugins';
|
|
||||||
|
|
||||||
export interface ICflareZone {
|
export interface ICflareZone {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
@ -41,19 +39,3 @@ export interface ICflareZone {
|
|||||||
type: string;
|
type: string;
|
||||||
checked_on: string;
|
checked_on: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ICflareRecord {
|
|
||||||
id: string;
|
|
||||||
type: string;
|
|
||||||
name: string;
|
|
||||||
content: string;
|
|
||||||
proxiable: boolean;
|
|
||||||
proxied: boolean;
|
|
||||||
ttl: number;
|
|
||||||
locked: boolean;
|
|
||||||
zone_id: string;
|
|
||||||
zone_name: string;
|
|
||||||
created_on: string;
|
|
||||||
modified_on: string;
|
|
||||||
data: any;
|
|
||||||
}
|
|
3
ts/interfaces/index.ts
Normal file
3
ts/interfaces/index.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export * from './cloudflare.api.record';
|
||||||
|
export * from './cloudflare.api.zone';
|
||||||
|
export * from './cloudflare.api.workerroute';
|
16
tslint.json
16
tslint.json
@ -1,3 +1,17 @@
|
|||||||
{
|
{
|
||||||
"extends": "tslint-config-standard"
|
"extends": ["tslint:latest", "tslint-config-prettier"],
|
||||||
|
"rules": {
|
||||||
|
"semicolon": [true, "always"],
|
||||||
|
"no-console": false,
|
||||||
|
"ordered-imports": false,
|
||||||
|
"object-literal-sort-keys": false,
|
||||||
|
"member-ordering": {
|
||||||
|
"options":{
|
||||||
|
"order": [
|
||||||
|
"static-method"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultSeverity": "warning"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user