gofirewall/USAGE.md

103 lines
2.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GoFirewall 使用文档
## 目录
1. [基本概念](#基本概念)
2. [配置文件](#配置文件)
3. [规则管理](#规则管理)
4. [日志系统](#日志系统)
5. [流量转发](#流量转发)
6. [API参考](#api参考)
## 基本概念
GoFirewall 是一个基于规则的网络防火墙,主要功能包括:
- **流量过滤**:根据规则允许或阻止网络流量
- **日志记录**:记录匹配的流量和系统事件
- **流量转发**支持NAT规则转发数据包
## 配置文件
### 配置参数
| 参数 | 类型 | 默认值 | 描述 |
|------|------|--------|------|
| log_level | string | "info" | 日志级别 (debug/info/warn/error) |
| capture_interface | string | "" | 监听的网络接口 |
| forward_enabled | bool | false | 是否启用流量转发 |
| max_packet_size | int | 65536 | 最大数据包大小 |
## 规则管理
### 规则格式
```json
{
"id": "rule-1",
"name": "Allow SSH",
"protocol": "tcp",
"src_ip": "192.168.1.1",
"src_port": "",
"dst_ip": "",
"dst_port": "22",
"action": "allow",
"description": "Allow SSH access",
"enabled": true
}
```
### 规则字段
| 字段 | 必填 | 描述 |
|------|------|------|
| protocol | 是 | 协议类型 (tcp/udp/icmp/all) |
| src_ip | 否 | 源IP地址支持通配符* |
| dst_ip | 否 | 目标IP地址支持通配符* |
| src_port | 否 | 源端口,支持范围(如8000-9000) |
| dst_port | 否 | 目标端口,支持范围 |
| action | 是 | 动作 (allow/deny) |
| enabled | 是 | 是否启用规则 |
## 日志系统
日志文件默认输出到`firewall.log`,包含以下信息:
- 匹配的规则ID
- 数据包源/目标信息
- 执行动作
- 时间戳
## 流量转发
### 转发规则示例
```go
forwarder.AddForwardRule(ForwardRule{
SrcIP: "192.168.1.100",
SrcPort: 8080,
DstIP: "10.0.0.2",
DstPort: 80,
})
```
## API参考
### Firewall 接口
```go
type Firewall interface {
Start() error
Stop()
AddRule(rule *Rule)
RemoveRule(ruleID string) bool
}
```
### Logger 接口
```go
type Logger interface {
Info(v ...interface{})
Warn(v ...interface{})
Error(v ...interface{})
Debug(v ...interface{})
}
```