"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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const es6_promisify_1 = require("es6-promisify");
const events_1 = __importDefault(require("events"));
const fs_1 = __importDefault(require("fs"));
const lodash_1 = require("lodash");
const path_1 = __importDefault(require("path"));
const rimraf_1 = __importDefault(require("rimraf"));
const typedi_1 = require("typedi");
const appInfo_1 = require("./appInfo");
const fsWriteFile = es6_promisify_1.promisify(fs_1.default.writeFile);
const fsReadFile = es6_promisify_1.promisify(fs_1.default.readFile);
const logCountPerUser = 500;
let HttpTrafficService = class HttpTrafficService extends events_1.default {
    constructor(appInfoService) {
        super();
        // http请求缓存数据 userId - > [{record}，{record}，{record}]
        this.cache = {};
        // 用户的请求id  一个用户可以关联多个请求设备，用户的请求分配唯一个一个请求id
        this.userRequestPointer = {};
        // 记录用户的监视窗数量
        this.userMonitorCount = {};
        const proxyDataDir = appInfoService.getProxyDataDir();
        // 监控数据缓存目录
        this.trafficDir = path_1.default.join(proxyDataDir, 'traffic');
        this.filterMap = {};
        this.stopRecord = {};
        // 创建定时任务，推送日志记录
        setInterval(() => {
            this.sendCachedData();
        }, 2000);
        try {
            rimraf_1.default.sync(this.trafficDir);
            fs_1.default.mkdirSync(this.trafficDir);
        }
        catch (e) {
            console.error(e);
        }
    }
    getFilter(userId) {
        const filters = this.filterMap[userId] || '';
        return filters;
    }
    setFilter(userId, filter) {
        this.filterMap[userId] = filter;
        this.emit('filter', userId, filter);
    }
    getStatus(userId) {
        return {
            overflow: this.userRequestPointer[userId] > logCountPerUser,
            stopRecord: this.stopRecord[userId] || false,
        };
    }
    setStopRecord(userId, stop) {
        this.stopRecord[userId] = stop;
        // 发送通知
        this.emit('state-change', userId, this.getStatus(userId));
    }
    clear(userId) {
        this.userRequestPointer[userId] = 0;
        // 发送通知
        this.emit('clear', userId);
    }
    // 将缓存数据发送给用户
    sendCachedData() {
        lodash_1.forEach(this.cache, (rows, userId) => {
            this.emit('traffic', userId, rows);
        });
        this.cache = {};
    }
    // 为请求分配id
    getRequestId(userId, urlObj) {
        // 处于停止记录状态 则不返回id
        if (this.stopRecord[userId]) {
            return -1;
        }
        // 获取当前ip
        let id = this.userRequestPointer[userId] || 0;
        // 超过500个请求则不再记录
        if (id > logCountPerUser) {
            return -1;
        }
        const filter = this.getFilter(userId);
        const { href } = urlObj;
        if (href.includes(filter)) {
            id++;
            this.userRequestPointer[userId] = id;
            if (id > logCountPerUser) {
                const state = this.getStatus(userId);
                // 向监控窗推送通知
                this.emit('state-change', userId, state);
            }
            return id;
        }
        return -1;
    }
    resetRequestId(userId) {
        this.userRequestPointer[userId] = 0;
    }
    // 获取监控窗口的数量，没有监控窗口 则不做记录
    hasMonitor(userId) {
        const cnt = this.userMonitorCount[userId] || 0;
        return cnt > 0;
    }
    // 用户监控窗数加1
    incMonitor(userId) {
        let cnt = this.userMonitorCount[userId] || 0;
        if (cnt === 0) {
            this.resetRequestId(userId);
        }
        cnt++;
        this.userMonitorCount[userId] = cnt;
    }
    // 用户监控窗数减一
    decMonitor(userId) {
        let cnt = this.userMonitorCount[userId] || 0;
        cnt--;
        this.userMonitorCount[userId] = cnt;
    }
    // 记录请求
    requestBegin({ id, userId, clientIp, method, httpVersion, urlObj, headers, }) {
        return __awaiter(this, void 0, void 0, function* () {
            const queue = this.cache[userId] || [];
            // 原始请求信息
            queue.push({
                id,
                originRequest: Object.assign({
                    clientIp,
                    headers,
                    httpVersion,
                    method,
                }, urlObj),
            });
            this.cache[userId] = queue;
        });
    }
    // 记录请求body
    actualRequest({ userId, id, requestData, originBody }) {
        return __awaiter(this, void 0, void 0, function* () {
            // 将body写文件
            const body = requestData.body;
            delete requestData.body;
            const queue = this.cache[userId] || [];
            queue.push({
                id,
                requestData,
            });
            this.cache[userId] = queue;
            if (body) {
                const bodyPath = this.getRequestBodyPath(userId, id);
                yield fsWriteFile(bodyPath, body, { encoding: 'utf-8' });
            }
            if (originBody) {
                const bodyPath = this.getOriginRequestBodyPath(userId, id);
                yield fsWriteFile(bodyPath, originBody, { encoding: 'utf-8' });
            }
        });
    }
    // 记录响应
    serverReturn({ userId, id, toClientResponse }) {
        return __awaiter(this, void 0, void 0, function* () {
            const queue = this.cache[userId] || [];
            const { statusCode, headers, receiveRequestTime, dnsResolveBeginTime, remoteRequestBeginTime, remoteResponseStartTime, remoteResponseEndTime, requestEndTime, remoteIp, body, } = toClientResponse;
            queue.push({
                id,
                response: {
                    dnsResolveBeginTime,
                    headers,
                    receiveRequestTime,
                    remoteIp,
                    remoteRequestBeginTime,
                    remoteResponseEndTime,
                    remoteResponseStartTime,
                    requestEndTime,
                    statusCode,
                },
            });
            this.cache[userId] = queue;
            if (body) {
                const bodyPath = this.getResponseBodyPath(userId, id);
                yield fsWriteFile(bodyPath, body, { encoding: 'utf-8' });
            }
        });
    }
    /**
     * 获取请求的请求内容
     * @param userId
     * @param requestId
     */
    getRequestBody(userId, requestId) {
        return __awaiter(this, void 0, void 0, function* () {
            const saveRequestPath = this.getRequestBodyPath(userId, requestId);
            return yield fsReadFile(saveRequestPath, { encoding: 'utf-8' });
        });
    }
    /**
     * 获取请求的请求内容
     * @param userId
     * @param requestId
     */
    getResponseBody(userId, requestId) {
        return __awaiter(this, void 0, void 0, function* () {
            const saveResponsePath = this.getResponseBodyPath(userId, requestId);
            return yield fsReadFile(saveResponsePath, { encoding: 'utf-8' });
        });
    }
    // 获取请求记录path
    getRequestBodyPath(userId, requestId) {
        return path_1.default.join(this.trafficDir, userId + '_' + requestId + '_req_body');
    }
    getOriginRequestBodyPath(userId, requestId) {
        return path_1.default.join(this.trafficDir, userId + '_' + requestId + '_req_body_origin');
    }
    // 获取响应记录path
    getResponseBodyPath(userId, requestId) {
        return path_1.default.join(this.trafficDir, userId + '_' + requestId + '_res_body');
    }
};
HttpTrafficService = __decorate([
    typedi_1.Service(),
    __metadata("design:paramtypes", [appInfo_1.AppInfoService])
], HttpTrafficService);
exports.HttpTrafficService = HttpTrafficService;
//# sourceMappingURL=httpTraffic.js.map