增加二进制文件包含必要资源文件功能
This commit is contained in:
66
embed.go
Normal file
66
embed.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed ip2region/*
|
||||||
|
var embeddedFS embed.FS
|
||||||
|
|
||||||
|
func releaseEmbeddedDir(efs embed.FS, embedPath string, targetDir string) error {
|
||||||
|
return fs.WalkDir(efs, embedPath, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath, _ := filepath.Rel(embedPath, path)
|
||||||
|
targetPath := filepath.Join(targetDir, relPath)
|
||||||
|
|
||||||
|
if d.IsDir() {
|
||||||
|
return os.MkdirAll(targetPath, 0755)
|
||||||
|
}
|
||||||
|
|
||||||
|
data, err := efs.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("读取嵌入文件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果文件已存在就跳过(可选)
|
||||||
|
if _, err := os.Stat(targetPath); err == nil {
|
||||||
|
fmt.Printf("文件已存在: %s,跳过写入\n", targetPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(targetPath, data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("写入文件失败: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("释放: %s → %s\n", path, targetPath)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func embed_ip2region() {
|
||||||
|
fmt.Println("正在释放 ip2region 数据...")
|
||||||
|
|
||||||
|
err := releaseEmbeddedDir(embeddedFS, "ip2region", "ip2region")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("释放失败:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("释放完成,程序继续执行...")
|
||||||
|
|
||||||
|
// 你可以在这里使用 ip2region.xdb 文件了,例如:
|
||||||
|
if _, err := os.Stat("ip2region/ip2region.xdb"); err == nil {
|
||||||
|
fmt.Println("✅ 确认 ip2region.xdb 已成功写出")
|
||||||
|
// 这里可以调用 ip2region 逻辑加载它
|
||||||
|
} else {
|
||||||
|
fmt.Println("❌ 找不到 ip2region.xdb")
|
||||||
|
}
|
||||||
|
}
|
||||||
22
install.sh
22
install.sh
@@ -17,20 +17,20 @@ main()
|
|||||||
|
|
||||||
if ! test -d $DENYIP_HOME; then
|
if ! test -d $DENYIP_HOME; then
|
||||||
mkdir -p $DENYIP_HOME
|
mkdir -p $DENYIP_HOME
|
||||||
mkdir -p $DENYIP_HOME/ip2region
|
#mkdir -p $DENYIP_HOME/ip2region
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ! test -f ~/DenyIp/ip2region/ip2region.xdb; then
|
# if ! test -f ~/DenyIp/ip2region/ip2region.xdb; then
|
||||||
wget -O $DENYIP_HOME/ip2region/ip2region.xdb https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/ip2region/ip2region.xdb
|
# wget -O $DENYIP_HOME/ip2region/ip2region.xdb https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/ip2region/ip2region.xdb
|
||||||
|
|
||||||
if test "$(md5sum $DENYIP_HOME/ip2region/ip2region.xdb | cut -d " " -f 1)" != "508c6b0257a02152b9d1b2b3792936e1"; then
|
# if test "$(md5sum $DENYIP_HOME/ip2region/ip2region.xdb | cut -d " " -f 1)" != "508c6b0257a02152b9d1b2b3792936e1"; then
|
||||||
echo "ip2region.xdb md5 check fail"
|
# echo "ip2region.xdb md5 check fail"
|
||||||
rm -rf $DENYIP_HOME
|
# rm -rf $DENYIP_HOME
|
||||||
exit 1
|
# exit 1
|
||||||
else
|
# else
|
||||||
echo "ip2region.xdb md5 check success"
|
# echo "ip2region.xdb md5 check success"
|
||||||
fi
|
# fi
|
||||||
fi
|
# fi
|
||||||
|
|
||||||
if ! test -f ~/DenyIp/denyip; then
|
if ! test -f ~/DenyIp/denyip; then
|
||||||
wget -O $DENYIP_HOME/denyip https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/denyip
|
wget -O $DENYIP_HOME/denyip https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/denyip
|
||||||
|
|||||||
12
main.go
12
main.go
@@ -30,7 +30,7 @@ var (
|
|||||||
Protocol *string // BPF 过滤器
|
Protocol *string // BPF 过滤器
|
||||||
|
|
||||||
IPSET_NUMBER int // 当前使用的 ipset 集合编号
|
IPSET_NUMBER int // 当前使用的 ipset 集合编号
|
||||||
MAX_IPSET_NAME = 12 // 最大 ipset 集合数量
|
MAX_IPSET_NAME = 100 // 最大 ipset 集合数量
|
||||||
IPSET_NAME string // 当前使用的 ipset 集合名称
|
IPSET_NAME string // 当前使用的 ipset 集合名称
|
||||||
|
|
||||||
IpList = list.New() // 存储 IPv4 地址的链表
|
IpList = list.New() // 存储 IPv4 地址的链表
|
||||||
@@ -350,7 +350,7 @@ func RunMainProcess() { // 主进程逻辑
|
|||||||
// 遍历 ProcessedIPMap,检查每个 IP 的记录是否已超过 24 小时
|
// 遍历 ProcessedIPMap,检查每个 IP 的记录是否已超过 24 小时
|
||||||
for ip, t := range ProcessedIPMap {
|
for ip, t := range ProcessedIPMap {
|
||||||
// 如果当前时间减去记录时间大于 1 小时,则删除该条目
|
// 如果当前时间减去记录时间大于 1 小时,则删除该条目
|
||||||
if now.Sub(t) > 10*time.Minute {
|
if now.Sub(t) > 30*time.Minute {
|
||||||
delete(ProcessedIPMap, ip)
|
delete(ProcessedIPMap, ip)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -366,7 +366,7 @@ func RunMainProcess() { // 主进程逻辑
|
|||||||
// 启动防火墙管理
|
// 启动防火墙管理
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
if ipset_len, _ := NumIPSet(IPSET_NAME); ipset_len >= 65534 {
|
if ipset_len, _ := NumIPSet(IPSET_NAME); ipset_len >= 65535 {
|
||||||
log.Printf("\033[31m ipset %s 列表已满 %d \033[0m\n", IPSET_NAME, ipset_len)
|
log.Printf("\033[31m ipset %s 列表已满 %d \033[0m\n", IPSET_NAME, ipset_len)
|
||||||
|
|
||||||
// 创建新的 ipset 集合
|
// 创建新的 ipset 集合
|
||||||
@@ -384,7 +384,7 @@ func RunMainProcess() { // 主进程逻辑
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(7 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -408,7 +408,7 @@ func HandleCmd() {
|
|||||||
if help {
|
if help {
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"\t\tDenyip firewall\n" +
|
"\t\tDenyip firewall\n" +
|
||||||
"\tVersion 0.1\n" +
|
"\tVersion 0.2\n" +
|
||||||
"\tE-mail: aixiao@aixiao.me\n" +
|
"\tE-mail: aixiao@aixiao.me\n" +
|
||||||
"\tDate: 20250102\n")
|
"\tDate: 20250102\n")
|
||||||
|
|
||||||
@@ -456,6 +456,8 @@ func main() {
|
|||||||
runtime.GOMAXPROCS(runtime.NumCPU()) // 设置最大CPU核数
|
runtime.GOMAXPROCS(runtime.NumCPU()) // 设置最大CPU核数
|
||||||
HandleCmd()
|
HandleCmd()
|
||||||
|
|
||||||
|
embed_ip2region()
|
||||||
|
|
||||||
// 守护进程模式
|
// 守护进程模式
|
||||||
if *daemon {
|
if *daemon {
|
||||||
StartDaemon()
|
StartDaemon()
|
||||||
|
|||||||
Reference in New Issue
Block a user