package com.influx;


import com.config.AppConfig;
import com.influx.InfluxDBClient;
import com.influxdb.client.*;
import com.influxdb.client.domain.WritePrecision;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.influxdb.client.write.Point;
import java.util.concurrent.CompletableFuture;
import com.influxdb.query.FluxTable;
import java.util.List;

public class SyslogToInfluxApp {
    private static final Logger logger = LoggerFactory.getLogger(SyslogToInfluxApp.class);

    private com.influx.InfluxDBClient influxClient;


    public static void main(String[] args) {
        SyslogToInfluxApp app = new SyslogToInfluxApp();
        app.start("<15>2025-09-26T11:47:18Z 5f46d3be75e1 supermario 128 honeypot_event - ﻿{\"source\":\"honeypot\",\"id\":\"f6a13c35-bf9d-4da6-a181-50ce23e7ef6a\",\"start_time\":\"2023-09-03T11:07:02.50167643Z\",\"time\":\"2023-09-03T11:16:18.883885281Z\",\"risk_level\":4,\"connection\":\"b18f3fbe-3fbf-4495-815f-ff26f6fb0bdf\",\"file_info\":null,\"extra\":{\"payload\":{\"format\":\"line\",\"name\":{\"cn\":\"攻击载荷\",\"en\":\"payload\"},\"value\":\"\"},\"uid\":{\"format\":\"line\",\"name\":{\"cn\":\"\",\"en\":\"\"},\"uid\":\"b4cbc73c-25d0-4429-ae1b-a856cdf1a651\",\"value\":\"\"}},\"type\":\"WEB_ATTACK_SCANNER\",\"agent_sn\":\"caa7da42-0cca-4cb1-b501-1f1eb2b588d5\",\"agent_name\":\" 教育局蜜罐探针\",\"honeypot_id\":\"11a9ac6bdf38ae2aaa49ec4f1b4a921bff71952cb9f175bdd8ee1f0497057bc6\",\"honeypot_name\":\"茂名市中小学管理平台管理后台\",\"src_ip\":\"117.50.189.7\",\"src_port\":58512,\"src_mac\":\"\",\"dest_ip\":\"192.168.222.2\",\"dest_port\":9200,\"proxy_ip\":null,\"node\":\"WRx3\"}" );
        // 添加关闭钩子
       // Runtime.getRuntime().addShutdownHook(new Thread(app::stop));
    }

    public void start(String syslog) {
        try {
            logger.info("Starting Syslog to InfluxDB application...");

            // 初始化 InfluxDB 客户端
            influxClient = new InfluxDBClient();
            influxClient.writeRecord(syslog);
            Point point = Point.measurement("syslog_normal")
                    .addTag("ip", "localhost")       // 添加标签
                    .addField("info",syslog)          // 添加字段
                    .addField("clientid","CC-202500110092")          // 添加字段
                    .addField("clientname","CC-honeypot92")          // 添加字段
                    .addField("source","honeypot")          // 添加字段
                    .time(System.currentTimeMillis(), WritePrecision.MS) ;// 毫秒级时间戳

            //influxClient.writePointBlocking(point);
            List<FluxTable>  logTables =influxClient.queryAllData("syslog_normal");
            System.out.println("influxdb wirte syslog ok");
            // 初始化 Syslog 解析器
           // SyslogParser parser = new SyslogParser();

            // 启动 Syslog 服务器
            //syslogServer = new SyslogServer(parser, influxClient);
            //CompletableFuture<Void> serverFuture = syslogServer.start();
         /*
            serverFuture.whenComplete((result, error) -> {
                if (error != null) {
                    logger.error("Failed to start syslog server", error);
                    System.exit(1);
                }
            });
          */
            // 等待服务器启动完成
          //  serverFuture.join();

        } catch (Exception e) {
            logger.error("Failed to write log function error", e);
        }
    }

}