@cliz/inlets
Version:
Cloud Native Tunnel
59 lines (58 loc) • 2.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.server = void 0;
const cli_1 = require("@cliz/cli");
const config_1 = require("../../config");
const utils_1 = require("./utils");
const monitor_1 = require("./monitor");
const tunnel_1 = require("./tunnel");
const debug = require('debug')('inlets:server');
async function server(options) {
const logger = cli_1.doreamon.logger.getLogger('server');
const notification = new utils_1.Notification(options === null || options === void 0 ? void 0 : options.notification);
const port = +(options === null || options === void 0 ? void 0 : options.port) || await cli_1.api.network.getPort({ start: 8080 });
const tcpPort = +(options === null || options === void 0 ? void 0 : options.tcpPort) || await cli_1.api.network.getPort({ start: 8081 });
const ctx = {
config: config_1.default,
domainMappings: (0, utils_1.createHostContainer)(),
callbackContainer: (0, utils_1.createCallbackCtainer)(),
container: (0, utils_1.createContainer)(),
};
const monitor = (0, monitor_1.createMonitor)(ctx, {
...options,
port,
tcpPort,
notification,
});
monitor.tunnel(({ type, containerId }) => {
if (type === 'http') {
return;
}
if (type === 'tcp') {
return tunnel_1.default.tcp(ctx, {
domain: options.domain,
containerId,
});
}
throw new Error(`unsupport type: ${type}`);
});
const http = tunnel_1.default.http(ctx, {
domain: options.domain,
});
http.attach(monitor.server);
const ip = await getMachineIP();
await notification.notify(`[服务端][启动] 成功 - ${ip}`, [
`服务端版本:${config_1.default.version}`,
`服务端IP: ${ip}`,
`当前时间:${cli_1.doreamon.date().format('YYYY-MM-DD HH:mm:ss')}`,
]);
}
exports.server = server;
async function getMachineIP() {
try {
return await cli_1.doreamon.request.get('https://httpbin.zcorky.com/ip/plain', { timeout: 3000 }).text();
}
catch (error) {
return '未知';
}
}