Compare commits
2 Commits
dc5039d08d
...
851b643374
Author | SHA1 | Date | |
---|---|---|---|
851b643374 | |||
28aae5cec3 |
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
|
||||
mkdir -p $DENYIP_HOME
|
||||
mkdir -p $DENYIP_HOME/ip2region
|
||||
#mkdir -p $DENYIP_HOME/ip2region
|
||||
fi
|
||||
|
||||
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
|
||||
# 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
|
||||
|
||||
if test "$(md5sum $DENYIP_HOME/ip2region/ip2region.xdb | cut -d " " -f 1)" != "508c6b0257a02152b9d1b2b3792936e1"; then
|
||||
echo "ip2region.xdb md5 check fail"
|
||||
rm -rf $DENYIP_HOME
|
||||
exit 1
|
||||
else
|
||||
echo "ip2region.xdb md5 check success"
|
||||
fi
|
||||
fi
|
||||
# if test "$(md5sum $DENYIP_HOME/ip2region/ip2region.xdb | cut -d " " -f 1)" != "508c6b0257a02152b9d1b2b3792936e1"; then
|
||||
# echo "ip2region.xdb md5 check fail"
|
||||
# rm -rf $DENYIP_HOME
|
||||
# exit 1
|
||||
# else
|
||||
# echo "ip2region.xdb md5 check success"
|
||||
# fi
|
||||
# fi
|
||||
|
||||
if ! test -f ~/DenyIp/denyip; then
|
||||
wget -O $DENYIP_HOME/denyip https://git.aixiao.me/aixiao/DenyIP-go/raw/branch/main/denyip
|
||||
|
@@ -3,31 +3,32 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetLocalIpv4Addr() string {
|
||||
// 发送 HTTP GET 请求到外部服务
|
||||
resp, err := http.Get("https://inet-ip.aixiao.me/")
|
||||
if err != nil {
|
||||
fmt.Printf("请求失败: %s\n", err)
|
||||
return "NULL"
|
||||
}
|
||||
defer resp.Body.Close() // 确保响应体在函数结束时关闭
|
||||
defer resp.Body.Close()
|
||||
|
||||
// 读取响应体内容
|
||||
ip, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
fmt.Printf("读取响应失败: %s\n", err)
|
||||
return "NULL"
|
||||
}
|
||||
|
||||
// 去除字符串末尾的换行符
|
||||
ipStr := strings.TrimRight(string(ip), "\n")
|
||||
|
||||
// 输出获取到的公网 IP
|
||||
fmt.Printf("%s %d\n", ipStr, len(ipStr))
|
||||
ipStr := strings.TrimSpace(string(ip))
|
||||
// ✅ 验证是否是合法 IPv4
|
||||
if net.ParseIP(ipStr) == nil || strings.Contains(ipStr, "<") {
|
||||
fmt.Printf("无效的IP响应: %q\n", ipStr)
|
||||
return "NULL"
|
||||
}
|
||||
|
||||
fmt.Printf("公网IP: %s\n", ipStr)
|
||||
return ipStr
|
||||
}
|
||||
|
19
main.go
19
main.go
@@ -17,6 +17,13 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// 强制使用 Go 的纯用户态 DNS 解析器
|
||||
net.DefaultResolver = &net.Resolver{
|
||||
PreferGo: true,
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
daemon = flag.Bool("d", false, "守护进程模式")
|
||||
child = flag.Bool("child", false, "子进程模式, (不要使用!!!)")
|
||||
@@ -30,7 +37,7 @@ var (
|
||||
Protocol *string // BPF 过滤器
|
||||
|
||||
IPSET_NUMBER int // 当前使用的 ipset 集合编号
|
||||
MAX_IPSET_NAME = 12 // 最大 ipset 集合数量
|
||||
MAX_IPSET_NAME = 100 // 最大 ipset 集合数量
|
||||
IPSET_NAME string // 当前使用的 ipset 集合名称
|
||||
|
||||
IpList = list.New() // 存储 IPv4 地址的链表
|
||||
@@ -350,7 +357,7 @@ func RunMainProcess() { // 主进程逻辑
|
||||
// 遍历 ProcessedIPMap,检查每个 IP 的记录是否已超过 24 小时
|
||||
for ip, t := range ProcessedIPMap {
|
||||
// 如果当前时间减去记录时间大于 1 小时,则删除该条目
|
||||
if now.Sub(t) > 10*time.Minute {
|
||||
if now.Sub(t) > 30*time.Minute {
|
||||
delete(ProcessedIPMap, ip)
|
||||
}
|
||||
}
|
||||
@@ -366,7 +373,7 @@ func RunMainProcess() { // 主进程逻辑
|
||||
// 启动防火墙管理
|
||||
go func() {
|
||||
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)
|
||||
|
||||
// 创建新的 ipset 集合
|
||||
@@ -384,7 +391,7 @@ func RunMainProcess() { // 主进程逻辑
|
||||
|
||||
}
|
||||
|
||||
time.Sleep(7 * time.Second)
|
||||
time.Sleep(3 * time.Second)
|
||||
}
|
||||
}()
|
||||
|
||||
@@ -408,7 +415,7 @@ func HandleCmd() {
|
||||
if help {
|
||||
fmt.Printf(
|
||||
"\t\tDenyip firewall\n" +
|
||||
"\tVersion 0.1\n" +
|
||||
"\tVersion 0.2\n" +
|
||||
"\tE-mail: aixiao@aixiao.me\n" +
|
||||
"\tDate: 20250102\n")
|
||||
|
||||
@@ -456,6 +463,8 @@ func main() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU()) // 设置最大CPU核数
|
||||
HandleCmd()
|
||||
|
||||
embed_ip2region()
|
||||
|
||||
// 守护进程模式
|
||||
if *daemon {
|
||||
StartDaemon()
|
||||
|
Reference in New Issue
Block a user