buddy-tunnel
Advanced tools
Comparing version 1.0.12-dev to 1.0.13-dev
{ | ||
"name": "buddy-tunnel", | ||
"preferGlobal": false, | ||
"version": "1.0.12-dev", | ||
"version": "1.0.13-dev", | ||
"type": "module", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
import EventEmitter from 'events'; | ||
import logger from './logger.js'; | ||
import { LOG_ERROR_WHILE_REFRESHING_AGENT } from './texts.js'; | ||
import { TUNNEL_OPEN } from './utils.js'; | ||
import { | ||
AGENT_ACTION_RESTART, | ||
AGENT_ACTION_START, | ||
AGENT_ACTION_STOP, | ||
TUNNEL_OPEN | ||
} from './utils.js'; | ||
@@ -15,2 +20,3 @@ class Agent extends EventEmitter { | ||
this.api = api; | ||
this.manager = null; | ||
} | ||
@@ -22,3 +28,3 @@ | ||
start() { | ||
async start() { | ||
this.started = true; | ||
@@ -28,5 +34,6 @@ this.tunnels.forEach((t) => { | ||
}); | ||
return this.refresh(''); | ||
} | ||
stop() { | ||
async stop() { | ||
this.started = false; | ||
@@ -36,19 +43,41 @@ this.tunnels.forEach((t) => { | ||
}); | ||
return this.refresh(''); | ||
} | ||
async restart() { | ||
await this.stop(); | ||
await this.start(); | ||
} | ||
refreshIn5() { | ||
setTimeout(() => { | ||
this.refresh().then(); | ||
setTimeout(async () => { | ||
await this.refresh(); | ||
this.refreshIn5(); | ||
}, 5000); | ||
} | ||
async refresh() { | ||
async makeAction(action){ | ||
if (action === AGENT_ACTION_STOP) { | ||
if (this.manager) return this.manager.agentStop(); | ||
else return this.stop(); | ||
} | ||
if (action === AGENT_ACTION_START) { | ||
if (this.manager) return this.manager.agentStart(); | ||
else return this.start(); | ||
} | ||
if (action === AGENT_ACTION_RESTART) { | ||
if (this.manager) return this.manager.agentRestart(); | ||
else return this.restart(); | ||
} | ||
} | ||
async refresh(action = null) { | ||
try { | ||
const newTunnels = await this.api.refreshAgent(this.id, this.token, this.getTunnelsUpdate()); | ||
const data = await this.api.refreshAgent(this.id, this.token, this.getTunnelsUpdate(), this.started, action); | ||
if (data.agent.action) await this.makeAction(data.agent.action); | ||
// todo update tunnels configuration | ||
logger.info(newTunnels); | ||
logger.info(data); | ||
} catch (err) { | ||
logger.error(LOG_ERROR_WHILE_REFRESHING_AGENT); | ||
} | ||
this.refreshIn5(); | ||
} | ||
@@ -55,0 +84,0 @@ |
@@ -85,3 +85,4 @@ import { | ||
try { | ||
this.agent = await ApiBuddy.fetchAgent(this.id, this.token); | ||
this.agent = await ApiBuddy.fetchAgent(this.id, this.token, this.shouldStart); | ||
this.agent.manager = this; | ||
this.agent.startRefreshingConfiguration(); | ||
@@ -195,3 +196,17 @@ if (this.shouldStart) this.agent.start(); | ||
async processAgentStop(req, res) { | ||
async agentRestart() { | ||
let saved = await this.agentStop(); | ||
if (!saved) return false; | ||
return await this.agentStart(); | ||
} | ||
async agentStop(){ | ||
this.shouldStart = false; | ||
const saved = this.saveConfig(this.id, this.token, this.port, this.shouldStart); | ||
if (!saved) return false; | ||
await this.agent.stop(); | ||
return true; | ||
} | ||
async processAgentRestart(req, res) { | ||
if (!this.agent) { | ||
@@ -201,4 +216,3 @@ this.serverError(res, 'Agent not enabled'); | ||
} | ||
this.shouldStart = false; | ||
const saved = this.saveConfig(this.id, this.token, this.port, this.shouldStart); | ||
const saved = await this.agentRestart(); | ||
if (!saved) { | ||
@@ -208,3 +222,2 @@ this.serverError(res, 'Something went wrong'); | ||
} | ||
this.agent.stop(); | ||
this.serverOutput(res, { | ||
@@ -215,3 +228,3 @@ success: true | ||
async processAgentStart(req, res) { | ||
async processAgentStop(req, res) { | ||
if (!this.agent) { | ||
@@ -221,9 +234,32 @@ this.serverError(res, 'Agent not enabled'); | ||
} | ||
const saved = await this.agentStop(); | ||
if (!saved) { | ||
this.serverError(res, 'Something went wrong'); | ||
return; | ||
} | ||
this.serverOutput(res, { | ||
success: true | ||
}); | ||
} | ||
async agentStart(){ | ||
this.shouldStart = true; | ||
const saved = this.saveConfig(this.id, this.token, this.port, this.shouldStart); | ||
if (!saved) { | ||
return false; | ||
} | ||
await this.agent.start(); | ||
return true; | ||
} | ||
async processAgentStart(req, res) { | ||
if (!this.agent) { | ||
this.serverError(res, 'Agent not enabled'); | ||
return; | ||
} | ||
const saved = await this.agentStart(); | ||
if (!saved) { | ||
this.serverError(res, 'Something went wrong'); | ||
return; | ||
} | ||
this.agent.start(); | ||
this.serverOutput(res, { | ||
@@ -269,2 +305,3 @@ success: true | ||
if (req.url === '/agent/stop') return this.processAgentStop(req, res); | ||
if (req.url === '/agent/restart') return this.processAgentRestart(req, res); | ||
this.server404(res); | ||
@@ -271,0 +308,0 @@ } |
@@ -89,2 +89,6 @@ import logger from '../logger.js'; | ||
async restartAgent() { | ||
return makeRequest('/agent/restart'); | ||
} | ||
async stopAgent() { | ||
@@ -91,0 +95,0 @@ return makeRequest('/agent/stop'); |
@@ -82,7 +82,7 @@ import fetch from 'node-fetch'; | ||
async refreshAgent(id, token, tunnels) { | ||
async refreshAgent(id, token, tunnels, activate, action = null) { | ||
const hostname = getHostname(); | ||
const version = getVersion(); | ||
const host = Cfg.getTokenHost(); | ||
const r = await makeRequest(host, '/tunnel/agent/refresh', { | ||
return await makeRequest(host, '/tunnel/agent/refresh', { | ||
id, | ||
@@ -92,8 +92,9 @@ token, | ||
version, | ||
activate, | ||
action, | ||
tunnels | ||
}); | ||
return r.tunnels; | ||
} | ||
async fetchAgent(id, token) { | ||
async fetchAgent(id, token, activate) { | ||
const host = Cfg.getTokenHost(); | ||
@@ -103,2 +104,3 @@ logger.info(LOG_GETTING_AGENT); | ||
token, | ||
activate, | ||
id | ||
@@ -105,0 +107,0 @@ }); |
@@ -11,3 +11,2 @@ import { Command } from 'commander'; | ||
} from '../../texts.js'; | ||
import logger from '../../logger.js'; | ||
@@ -23,3 +22,2 @@ const commandAgentStatus = new Command('status'); | ||
} | ||
logger.info(commandAgentStatus.agentStatus); | ||
if (!commandAgentStatus.agentStatus.enabled) { | ||
@@ -26,0 +24,0 @@ Output.exitError(SUC_AGENT_IS_DISABLED); |
@@ -51,3 +51,3 @@ import { exec } from 'child_process'; | ||
} | ||
await this.docker(`run -d --env="BUDDY_HOSTNAME=${os.hostname()}" --name="${this.getContainerName(id)}" ${pull} --add-host="host.docker.internal:host-gateway" --restart="unless-stopped" -p "${port}:${port}" --volume="${Cfg.dir}:/root/.buddy-tunnel" "${IMAGE}:${tag}"`); | ||
await this.docker(`run -d --env="BUDDY_TUNNEL_DOCKER=1" --env="BUDDY_HOSTNAME=${os.hostname()}" --name="${this.getContainerName(id)}" ${pull} --add-host="host.docker.internal:host-gateway" --restart="unless-stopped" -p "${port}:${port}" --volume="${Cfg.dir}:/root/.buddy-tunnel" "${IMAGE}:${tag}"`); | ||
return true; | ||
@@ -54,0 +54,0 @@ } catch (err) { |
@@ -48,2 +48,3 @@ const ERR_AGENT_NOT_REGISTERED = 'Agent not registered. Exiting...'; | ||
const SUC_AGENT_STARTED = 'Agent started'; | ||
const SUC_AGENT_RESTARTED = 'Agent restarted'; | ||
const SUC_AGENT_IS_ENABLED_AND_STARTED = 'Agent is enabled & started'; | ||
@@ -96,2 +97,3 @@ const SUC_AGENT_IS_ENABLED_AND_STOPPED = 'Agent is enabled & stopped'; | ||
const DESC_COMMAND_AGENT_START = 'start agent'; | ||
const DESC_COMMAND_AGENT_RESTART = 'restart agent'; | ||
const DESC_COMMAND_AGENT_STATUS = 'agent status'; | ||
@@ -266,2 +268,3 @@ const DESC_COMMAND_AGENT_STOP = 'stop agent'; | ||
DESC_COMMAND_AGENT_START, | ||
DESC_COMMAND_AGENT_RESTART, | ||
DESC_COMMAND_AGENT_ENABLE, | ||
@@ -321,2 +324,3 @@ DESC_COMMAND_AGENT_DISABLE, | ||
SUC_AGENT_STARTED, | ||
SUC_AGENT_RESTARTED, | ||
SUC_AGENT_IS_DISABLED, | ||
@@ -323,0 +327,0 @@ SUC_AGENT_IS_ENABLED_AND_STARTED, |
@@ -163,2 +163,3 @@ import basicAuth from 'basic-auth'; | ||
this.ssh = null; | ||
this.started = false; | ||
} | ||
@@ -413,2 +414,4 @@ | ||
start() { | ||
if (this.started) return; | ||
this.started = true; | ||
logger.info(LOG_STARTING_TUNNEL(this.id)); | ||
@@ -459,2 +462,4 @@ // region latency | ||
stop() { | ||
if (!this.started) return; | ||
this.started = false; | ||
logger.info(LOG_STOPPING_TUNNEL(this.id)); | ||
@@ -461,0 +466,0 @@ this.emit(TUNNEL_EVENT_STOPPED, this); |
@@ -27,2 +27,6 @@ import path, { resolve } from 'path'; | ||
const AGENT_ACTION_STOP = 'STOP'; | ||
const AGENT_ACTION_START = 'START'; | ||
const AGENT_ACTION_RESTART = 'RESTART'; | ||
let cachedVersion; | ||
@@ -109,2 +113,5 @@ let cachedRoot; | ||
export { | ||
AGENT_ACTION_STOP, | ||
AGENT_ACTION_RESTART, | ||
AGENT_ACTION_START, | ||
getRootDir, | ||
@@ -111,0 +118,0 @@ getHostname, |
Sorry, the diff of this file is not supported yet
266480
87
5714