optimize
This commit is contained in:
parent
de0925ef48
commit
02cc3c3d9e
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
denyip
|
100
README.md
Normal file
100
README.md
Normal file
@ -0,0 +1,100 @@
|
||||
# DenyIP
|
||||
|
||||
## 概述
|
||||
|
||||
`DenyIP` 是一个用于保护大陆服务器免受非本地业务访问的防火墙工具。该工具能够高效地捕获网络数据包、提取源IP地址、进行地理位置判断,并将非大陆IP地址添加到IP黑名单中,从而严格限制非大陆来源的访问。
|
||||
|
||||
### 构建项目
|
||||
|
||||
克隆项目仓库:
|
||||
|
||||
```bash
|
||||
git clone https://git.aixiao.me/DenyIP-go.git
|
||||
cd DenyIP-go
|
||||
```
|
||||
|
||||
### 构建
|
||||
|
||||
编译项目:
|
||||
|
||||
```bash
|
||||
go build -o denyip
|
||||
```
|
||||
|
||||
### 命令行选项
|
||||
|
||||
```bash
|
||||
root@NIUYULING:/mnt/c/Users/root/Desktop/git.aixiao.me/DenyIP-go# ./denyip -h
|
||||
Denyip firewall
|
||||
Version 0.1
|
||||
E-mail: aixiao@aixiao.me
|
||||
Date: 20250102
|
||||
Usage of ./denyip:
|
||||
-child
|
||||
子进程模式
|
||||
-d 守护进程模式
|
||||
-f string
|
||||
指定 BPF 过滤器 (default "tcp")
|
||||
-h
|
||||
-help
|
||||
display this message
|
||||
-i string
|
||||
指定要使用的网络接口
|
||||
-l 列出可用的网络接口
|
||||
-o string
|
||||
保存捕获数据的输出文件(可选)
|
||||
-s string
|
||||
-s start 启动 Iptables 规则
|
||||
-s stop 停止 Iptables 规则
|
||||
root@NIUYULING:/mnt/c/Users/root/Desktop/git.aixiao.me/DenyIP-go#
|
||||
```
|
||||
|
||||
### 示例命令
|
||||
|
||||
- **启动守护进程**:
|
||||
|
||||
```bash
|
||||
./denyip -i eth0 -f tcp
|
||||
./denyip -d -i eth0 -f "tcp"
|
||||
```
|
||||
|
||||
- **启用Iptables规则**:
|
||||
|
||||
```bash
|
||||
./denyip -s start
|
||||
```
|
||||
|
||||
- **禁用Iptables规则**:
|
||||
|
||||
```bash
|
||||
./denyip -s stop
|
||||
```
|
||||
|
||||
- **查看帮助信息**:
|
||||
|
||||
```bash
|
||||
./denyip -h
|
||||
```
|
||||
|
||||
- **关闭守护进程**:
|
||||
|
||||
```bash
|
||||
killall -15 denyip
|
||||
```
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎贡献代码和提出改进建议!请通过 Pull Request 或 Issue 的方式提交。
|
||||
|
||||
## 联系信息
|
||||
|
||||
- **邮箱**:<aixiao@aixiao.me>
|
||||
- **日期**:20241024
|
||||
|
||||
## 许可证
|
||||
|
||||
本项目遵循 GNU 许可证,详情参见 [LICENSE](LICENSE) 文件。
|
||||
|
||||
---
|
||||
|
||||
希望这个 `README.md` 文件能够帮助您更好地介绍和使用 `DenyIP` 项目。如果有任何其他需求或修改,请随时告知。
|
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.23.4
|
||||
require github.com/google/gopacket v1.1.19
|
||||
|
||||
require (
|
||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230 // indirect
|
||||
github.com/lionsoul2014/ip2region/binding/golang v0.0.0-20241220152942-06eb5c6e8230
|
||||
golang.org/x/net v0.33.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
)
|
||||
|
23
ipset.go
23
ipset.go
@ -80,7 +80,7 @@ func IsIpset(setName string) int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func iptables(setName string) error {
|
||||
func iptables_add(setName string) error {
|
||||
|
||||
cmd := exec.Command("sh", "-c", fmt.Sprintf("iptables -A INPUT -p tcp -m set --match-set %s src -j DROP", setName))
|
||||
|
||||
@ -90,8 +90,25 @@ func iptables(setName string) error {
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
log.Printf("cmd.Run() failed with %v, stderr: %s\n", err, stderr.String())
|
||||
err = fmt.Errorf("failed to execute command: %w, stderr: %s", err, stderr.String())
|
||||
//log.Printf("cmd.Run() failed with %v, stderr: %s\n", err, stderr.String())
|
||||
//err = fmt.Errorf("failed to execute command: %w, stderr: %s", err, stderr.String())
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func iptables_del(setName string) error {
|
||||
|
||||
cmd := exec.Command("sh", "-c", fmt.Sprintf("iptables -D INPUT -p tcp -m set --match-set %s src -j DROP", setName))
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
cmd.Stderr = &stderr
|
||||
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
//log.Printf("cmd.Run() failed with %v, stderr: %s\n", err, stderr.String())
|
||||
//err = fmt.Errorf("failed to execute command: %w, stderr: %s", err, stderr.String())
|
||||
}
|
||||
|
||||
return err
|
||||
|
47
main.go
47
main.go
@ -155,7 +155,14 @@ func runMainProcess() { // 主进程逻辑
|
||||
//fmt.Println(e1.Value) // 输出第一个元素
|
||||
|
||||
region, _ := ip2region(e1.Value.(net.IP).String())
|
||||
log.Printf("当前 Ipset 链 %s\n", SIG_NAME)
|
||||
|
||||
log.Printf("当前 Ipset 链 %s %d\n", SIG_NAME, func() int {
|
||||
_len, _err := NumIPSet(SIG_NAME)
|
||||
if _err == nil {
|
||||
return _len
|
||||
}
|
||||
return 0
|
||||
}())
|
||||
|
||||
if !strings.Contains(region, "中国") && !strings.Contains(region, "内网") {
|
||||
if position, err := curl_(e1.Value.(net.IP).String()); err != nil { //判断地域
|
||||
@ -184,14 +191,14 @@ func runMainProcess() { // 主进程逻辑
|
||||
|
||||
for {
|
||||
|
||||
if ipset_len, _ := NumIPSet(SIG_NAME); ipset_len >= 100 {
|
||||
if ipset_len, _ := NumIPSet(SIG_NAME); ipset_len >= 65534 {
|
||||
log.Printf("ipset %s 列表已满 %dd\n", SIG_NAME, ipset_len)
|
||||
|
||||
// 创建新的 ipset 集合
|
||||
SIG++
|
||||
|
||||
if SIG >= MAX_SIG {
|
||||
log.Printf("已创建 %s 个集合!!!", MAX_SIG)
|
||||
log.Printf("已创建 %d 个集合!!!", MAX_SIG)
|
||||
}
|
||||
|
||||
SIG_NAME = fmt.Sprintf("root%d", SIG)
|
||||
@ -219,7 +226,7 @@ func handleCmd() {
|
||||
outputFile = flag.String("o", "", "保存捕获数据的输出文件(可选)")
|
||||
flag.StringVar(&instruction, "s", "", "-s start 启动 Iptables 规则\n-s stop 停止 Iptables 规则")
|
||||
flag.BoolVar(&help, "h", false, "")
|
||||
flag.BoolVar(&help, "help", false, "display this message")
|
||||
flag.BoolVar(&help, "help", false, "帮助信息")
|
||||
flag.Parse()
|
||||
|
||||
if help {
|
||||
@ -233,20 +240,26 @@ func handleCmd() {
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
switch instruction {
|
||||
case "start":
|
||||
fmt.Println("启动 Iptables 规则")
|
||||
for i := 0; i < MAX_SIG; i++ {
|
||||
_name := fmt.Sprintf("root%d", i)
|
||||
iptables(_name)
|
||||
}
|
||||
if instruction != "" {
|
||||
switch instruction {
|
||||
case "start":
|
||||
fmt.Println("启动 Iptables 规则")
|
||||
for i := 0; i < MAX_SIG; i++ {
|
||||
_name := fmt.Sprintf("root%d", i)
|
||||
iptables_add(_name)
|
||||
}
|
||||
|
||||
os.Exit(0)
|
||||
case "stop":
|
||||
fmt.Println("停止 Iptables 规则")
|
||||
os.Exit(0)
|
||||
default:
|
||||
log.Fatalf("未知的操作: %s. 请使用 'start' 或 'stop'.", instruction)
|
||||
os.Exit(0)
|
||||
case "stop":
|
||||
fmt.Println("停止 Iptables 规则")
|
||||
for i := 0; i < MAX_SIG; i++ {
|
||||
_name := fmt.Sprintf("root%d", i)
|
||||
iptables_del(_name)
|
||||
}
|
||||
os.Exit(0)
|
||||
default:
|
||||
log.Fatalf("未知的操作: %s. 请使用 'start' 或 'stop'.", instruction)
|
||||
}
|
||||
}
|
||||
|
||||
if listInterfaces {
|
||||
|
Loading…
x
Reference in New Issue
Block a user