-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new proxy feature based on http and https proxy agents. Proxy feature works like notifications, there is many proxy could be related one proxy entry. Supported features - Proxies can activate and disable in bulk - Proxies auto enabled by default for new monitors - Proxies could be applied in bulk to current monitors - Both authenticated and anonymous proxies supported - Export and import support for proxies
- Loading branch information
Showing
12 changed files
with
595 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- You should not modify if this have pushed to Github, unless it does serious wrong with the db. | ||
BEGIN TRANSACTION; | ||
|
||
CREATE TABLE proxy ( | ||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
user_id INT NOT NULL, | ||
protocol VARCHAR(10) NOT NULL, | ||
host VARCHAR(255) NOT NULL, | ||
port SMALLINT NOT NULL, | ||
auth BOOLEAN NOT NULL, | ||
username VARCHAR(255) NULL, | ||
password VARCHAR(255) NULL, | ||
active BOOLEAN NOT NULL DEFAULT 1, | ||
'default' BOOLEAN NOT NULL DEFAULT 0, | ||
created_date DATETIME DEFAULT (DATETIME('now')) NOT NULL | ||
); | ||
|
||
ALTER TABLE monitor ADD COLUMN proxy_id INTEGER REFERENCES proxy(id); | ||
|
||
CREATE INDEX proxy_id ON monitor (proxy_id); | ||
CREATE INDEX proxy_user_id ON proxy (user_id); | ||
|
||
COMMIT; |
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,21 @@ | ||
const { BeanModel } = require("redbean-node/dist/bean-model"); | ||
|
||
class Proxy extends BeanModel { | ||
toJSON() { | ||
return { | ||
id: this._id, | ||
userId: this._user_id, | ||
protocol: this._protocol, | ||
host: this._host, | ||
port: this._port, | ||
auth: !!this._auth, | ||
username: this._username, | ||
password: this._password, | ||
active: !!this._active, | ||
default: !!this._default, | ||
createdDate: this._created_date, | ||
}; | ||
} | ||
} | ||
|
||
module.exports = Proxy; |
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,99 @@ | ||
const { R } = require("redbean-node"); | ||
|
||
class Proxy { | ||
|
||
/** | ||
* Saves and updates given proxy entity | ||
* | ||
* @param proxy | ||
* @param proxyID | ||
* @param userID | ||
* @return {Promise<Bean>} | ||
*/ | ||
static async save(proxy, proxyID, userID) { | ||
let bean; | ||
|
||
if (proxyID) { | ||
bean = await R.findOne("proxy", " id = ? AND user_id = ? ", [proxyID, userID]); | ||
|
||
if (!bean) { | ||
throw new Error("proxy not found"); | ||
} | ||
|
||
} else { | ||
bean = R.dispense("proxy"); | ||
} | ||
|
||
// Make sure given proxy protocol is supported | ||
if (!["http", "https"].includes(proxy.protocol)) { | ||
throw new Error(`Unsupported proxy protocol "${proxy.protocol}. Supported protocols are http and https."`); | ||
} | ||
|
||
// When proxy is default update deactivate old default proxy | ||
if (proxy.default) { | ||
await R.exec("UPDATE proxy SET `default` = 0 WHERE `default` = 1"); | ||
} | ||
|
||
bean.user_id = userID; | ||
bean.protocol = proxy.protocol; | ||
bean.host = proxy.host; | ||
bean.port = proxy.port; | ||
bean.auth = proxy.auth; | ||
bean.username = proxy.username; | ||
bean.password = proxy.password; | ||
bean.active = proxy.active || true; | ||
bean.default = proxy.default || false; | ||
|
||
await R.store(bean); | ||
|
||
if (proxy.applyExisting) { | ||
await applyProxyEveryMonitor(bean.id, userID); | ||
} | ||
|
||
return bean; | ||
} | ||
|
||
/** | ||
* Deletes proxy with given id and removes it from monitors | ||
* | ||
* @param proxyID | ||
* @param userID | ||
* @return {Promise<void>} | ||
*/ | ||
static async delete(proxyID, userID) { | ||
const bean = await R.findOne("proxy", " id = ? AND user_id = ? ", [proxyID, userID]); | ||
|
||
if (!bean) { | ||
throw new Error("proxy not found"); | ||
} | ||
|
||
// Delete removed proxy from monitors if exists | ||
await R.exec("UPDATE monitor SET proxy_id = null WHERE proxy_id = ?", [proxyID]); | ||
|
||
// Delete proxy from list | ||
await R.trash(bean); | ||
} | ||
} | ||
|
||
/** | ||
* Applies given proxy id to monitors | ||
* | ||
* @param proxyID | ||
* @param userID | ||
* @return {Promise<void>} | ||
*/ | ||
async function applyProxyEveryMonitor(proxyID, userID) { | ||
// Find all monitors with id and proxy id | ||
const monitors = await R.getAll("SELECT id, proxy_id FROM monitor WHERE user_id = ?", [userID]); | ||
|
||
// Update proxy id not match with given proxy id | ||
for (const monitor of monitors) { | ||
if (monitor.proxy_id !== proxyID) { | ||
await R.exec("UPDATE monitor SET proxy_id = ? WHERE id = ?", [proxyID, monitor.id]); | ||
} | ||
} | ||
} | ||
|
||
module.exports = { | ||
Proxy, | ||
}; |
Oops, something went wrong.