跳转至

自 sing-box 1.14.0 起

ClickHouse

ClickHouse 服务把每条 TCP/UDP/TUN 会话写成一行插入 ClickHouse 表。

转发路径只入队,后台用 PrepareBatch + Append + Send 批量写入。送达不可靠:插入失败丢该批,队列满丢新事件。DNS hijack 不记录。log 块里的进程日志不会写入。

默认 native 协议(host:9000)。可选 HTTP(host:8123)。

结构

{
  "type": "clickhouse",
  "tag": "gw-01",
  "server": "127.0.0.1",
  "server_port": 9000,
  "database": "logs",
  "table": "sessions",
  "username": "default",
  "password": "",
  "protocol": "native",
  "tls": {},
  "detour": "",
  "batch": {
    "max_entries": 100,
    "max_wait": "1s"
  }
}

字段

tag

写入 node 列的节点名,用来区分实例。

server

必填

ClickHouse 主机。未设 server_port 时也可以写 host:port

server_port

ClickHouse 端口。

默认:

  • native: 9000
  • native + TLS: 9440
  • http: 8123
  • http + TLS: 8443

不要和 server 里的端口同时写。

database

数据库。为空则用服务器默认库。

table

必填

目标表。表必须事先建好,sing-box 不会建表。

username / password

ClickHouse 认证。

protocol

传输协议。

取值:

  • native(默认)
  • http

tls

TLS 配置,见 TLS

HTTPS(protocol: httptls.enabled)时需要。

detour

访问 ClickHouse 的出站 tag。为空则走默认出站。

batch.max_entries

队列达到该条数时刷新。默认 100

batch.max_wait

空闲超过该时间后刷新。默认 1s

CREATE TABLE logs.sessions
(
    node                LowCardinality(String),
    id                  String,
    start               DateTime64(3),
    end                 DateTime64(3),
    duration_ms         Int64,
    action              LowCardinality(String),
    network             LowCardinality(String),
    protocol            LowCardinality(String),
    user                String,
    source_ip           String,
    source_port         UInt16,
    source_mac          String,
    destination_domain  String,
    destination_ip      String,
    destination_port    UInt16,
    inbound             LowCardinality(String),
    inbound_type        LowCardinality(String),
    outbound            LowCardinality(String),
    outbound_type       LowCardinality(String),
    chain               Array(String),
    rule                String,
    upload              Int64,
    download            Int64,
    close               LowCardinality(String),
    process             String
)
ENGINE = MergeTree
ORDER BY (node, start, id)

actionallowrejectclose 在能判断时为 finrsttimeoutrejectdrop。失败记 clickhouse.push_failed。队列满记 clickhouse.dropped