"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SysinfoController = void 0;
const common_1 = require("@nestjs/common");
const swagger_1 = require("@nestjs/swagger");
const http = require("http");
const ssh2 = require("ssh2");
const svgMap = require("svg-world-map");
const domStringify = require("virtual-dom-stringify");
const config_service_1 = require("../config/config.service");
const sysinfo_service_1 = require("./sysinfo.service");
let SysinfoController = class SysinfoController {
    constructor(sysInfoService, configService) {
        this.sysInfoService = sysInfoService;
        this.configService = configService;
    }
    getNodeAgent(node) {
        const conn = this.configService.config.nodes.find(p => p.host === node);
        if (!conn)
            throw new common_1.BadRequestException('node not found');
        const agent = new ssh2.HTTPAgent(conn);
        agent.defaultPort = 3000;
        return agent;
    }
    async general(res, node) {
        if (node === 'localhost')
            res.json(await this.sysInfoService.general());
        else {
            const agent = this.getNodeAgent(node);
            http.get({
                path: '/api/v1/sysinfo/localhost/general',
                agent,
                headers: { Connection: 'close' }
            }, (proxyRes) => {
                proxyRes.pipe(res);
            });
        }
    }
    async ip() {
        return this.sysInfoService.ip();
    }
    async geoIp() {
        return this.sysInfoService.geoIp();
    }
    async geoMap(res) {
        const { lon, lat } = await this.sysInfoService.geoIp();
        res.setHeader('Content-Type', 'image/svg+xml');
        const map = svgMap(lon, lat, {
            ocean: 'transparent',
            land: '#ccc',
            mapWidth: 500,
            pinHeight: 4
        });
        res.end(domStringify(map));
    }
    async docker(res, node) {
        if (node === 'localhost')
            res.json(await this.sysInfoService.docker());
        else {
            const agent = this.getNodeAgent(node);
            http.get({
                path: '/api/v1/sysinfo/localhost/docker',
                agent,
                headers: { Connection: 'close' }
            }, (proxyRes) => {
                proxyRes.pipe(res);
            });
        }
    }
};
__decorate([
    common_1.Get('/api/v1/sysinfo/:node/general'),
    __param(0, common_1.Res()), __param(1, common_1.Param('node')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], SysinfoController.prototype, "general", null);
__decorate([
    common_1.Get('/api/v1/sysinfo/:node/ip'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], SysinfoController.prototype, "ip", null);
__decorate([
    common_1.Get('/api/v1/sysinfo/:node/geo-ip'),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", Promise)
], SysinfoController.prototype, "geoIp", null);
__decorate([
    common_1.Get('/api/v1/sysinfo/:node/location.svg'),
    __param(0, common_1.Res()),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], SysinfoController.prototype, "geoMap", null);
__decorate([
    common_1.Get('/api/v1/sysinfo/:node/docker'),
    __param(0, common_1.Res()), __param(1, common_1.Param('node')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Promise)
], SysinfoController.prototype, "docker", null);
SysinfoController = __decorate([
    common_1.Controller(),
    swagger_1.ApiTags('SysInfo'),
    __metadata("design:paramtypes", [sysinfo_service_1.SysinfoService,
        config_service_1.ConfigService])
], SysinfoController);
exports.SysinfoController = SysinfoController;
//# sourceMappingURL=sysinfo.controller.js.map