{"id":26648921,"url":"https://github.com/limitcool/hostsparser","last_synced_at":"2025-03-25T00:47:22.310Z","repository":{"id":284077559,"uuid":"953748769","full_name":"limitcool/hostsparser","owner":"limitcool","description":"这是一个用Go语言编写的hosts文件解析器，它使用词法分析和语法分析技术来解析和修改hosts文件。作为一个库项目，它提供了简单易用的API来操作和管理hosts文件。","archived":false,"fork":false,"pushed_at":"2025-03-24T02:34:29.000Z","size":0,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-24T03:27:11.093Z","etag":null,"topics":["go","hosts","hosts-manage","hostseditor","hostsfile","lexer","parser"],"latest_commit_sha":null,"homepage":"https://github.com/limitcool/hostsparser","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/limitcool.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-24T02:31:53.000Z","updated_at":"2025-03-24T02:36:08.000Z","dependencies_parsed_at":"2025-03-24T03:27:21.511Z","dependency_job_id":"12824c37-4c1e-4d5b-9c96-58aab086e1a4","html_url":"https://github.com/limitcool/hostsparser","commit_stats":null,"previous_names":["limitcool/hostsparser"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fhostsparser","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fhostsparser/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fhostsparser/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/limitcool%2Fhostsparser/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/limitcool","download_url":"https://codeload.github.com/limitcool/hostsparser/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245377955,"owners_count":20605375,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","hosts","hosts-manage","hostseditor","hostsfile","lexer","parser"],"created_at":"2025-03-25T00:47:20.799Z","updated_at":"2025-03-25T00:47:22.287Z","avatar_url":"https://github.com/limitcool.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hosts文件解析器\n\n[English Version](README_EN.md)\n\n这是一个用Go语言编写的hosts文件解析器，它使用词法分析和语法分析技术来解析和修改hosts文件。作为一个库项目，它提供了简单易用的API来操作和管理hosts文件。\n\n## 功能特点\n\n- 使用词法分析和语法分析技术解析hosts文件\n- 支持添加、删除和查找hosts条目\n- 保留注释和空行\n- 支持IPv4和IPv6地址格式验证\n- 支持IP地址对应多个域名的映射关系\n- 支持按主机名操作hosts条目\n- 支持加载和保存hosts文件\n- 灵活的分级日志系统，支持自定义日志接口\n\n## 安装\n\n```bash\ngo get github.com/limitcool/hostsparser\n```\n\n## 库使用方法\n\n### 导入包\n\n```go\nimport (\n    \"github.com/limitcool/hostsparser/hosts\"\n    \"github.com/limitcool/hostsparser/logger\"\n)\n```\n\n### 加载hosts文件\n\n```go\n// 加载系统hosts文件\nhostsPath := hosts.GetSystemHostsPath()\nhostsFile, err := hosts.LoadHostsFile(hostsPath)\nif err != nil {\n    // 处理错误\n}\n\n// 或者创建新的hosts文件\nhostsFile := hosts.NewHostsFile()\n```\n\n### 修改hosts条目\n\n```go\n// 设置主机名对应的IP\nerr := hostsFile.SetHostIP(\"example.com\", \"127.0.0.1\")\n\n// 批量设置多个主机名映射到同一IP\nerr = hostsFile.SetMultipleHostIPs([]string{\"example.com\", \"www.example.com\"}, \"127.0.0.1\")\n\n// 删除主机名映射\nmodified, err := hostsFile.RemoveHost(\"example.com\")\n```\n\n### 查询hosts条目\n\n```go\n// 获取主机名对应的IP\nip, err := hostsFile.GetHostIP(\"example.com\")\n\n// 获取IP对应的所有主机名\ndomains, err := hostsFile.GetHostsByIP(\"127.0.0.1\")\n\n// 获取所有IP-域名对\npairs := hostsFile.GetAllIPDomainPairs()\n\n// 按IP或域名筛选\nfilteredByIP := hostsFile.FilterIPDomainPairs(\"127.0.0.1\", \"\")\nfilteredByDomain := hostsFile.FilterIPDomainPairs(\"\", \"example.com\")\n```\n\n### 保存hosts文件\n\n```go\n// 保存到原文件\nerr := hostsFile.SaveHostsFile(\"\")\n\n// 保存到新文件\nerr = hostsFile.SaveHostsFile(\"./hosts.new\")\n```\n\n### 日志系统\n\n本库提供了灵活的分级日志系统，支持Debug、Info、Warn和Error四个日志级别。\n\n#### 基本日志使用\n\n```go\n// 直接使用包级函数记录日志\nlogger.Debug(\"这是调试信息\")\nlogger.Info(\"这是普通信息\")\nlogger.Warn(\"这是警告信息\")\nlogger.Error(\"这是错误信息\")\n\n// 使用格式化函数\nlogger.Debugf(\"调试: %s\", \"详细信息\")\nlogger.Infof(\"信息: %s\", \"详细信息\")\nlogger.Warnf(\"警告: %s\", \"详细信息\")\nlogger.Errorf(\"错误: %s\", \"详细信息\")\n```\n\n#### 禁用日志\n\n```go\n// 完全禁用所有日志输出\nlogger.DisableLogging()\n```\n\n#### 自定义日志接口\n\n您可以实现自己的日志接口，例如集成zap、logrus等高性能日志库：\n\n```go\n// 1. 实现Logger接口\ntype MyCustomLogger struct {\n    // 自定义字段\n}\n\nfunc (l *MyCustomLogger) Debug(args ...interface{}) {\n    // 自定义实现\n}\n\nfunc (l *MyCustomLogger) Debugf(format string, args ...interface{}) {\n    // 自定义实现\n}\n\n// 实现其他方法...\n\n// 2. 设置为全局日志记录器\nlogger.SetLogger(\u0026MyCustomLogger{})\n```\n\n#### Zap日志库适配示例\n\n```go\nimport (\n    \"github.com/limitcool/hostsparser/logger\"\n    \"go.uber.org/zap\"\n)\n\n// 在主程序中使用\nfunc main() {\n    // 创建zap日志实例\n    zapLogger, _ := zap.NewProduction()\n    defer zapLogger.Sync()\n\n    // 直接使用zap的Sugar接口适配Logger接口\n    logger.SetLogger(zapLogger.Sugar())\n\n    // 现在所有日志都会通过zap记录\n}\n```\n\n## 项目结构\n\n- `lexer/` - 词法分析器，将hosts文件内容分解为词法单元\n- `parser/` - 语法分析器，解析词法单元并构建hosts条目结构\n- `hosts/` - hosts文件操作的核心功能\n- `logger/` - 分级日志系统，支持自定义日志接口\n\n## 实现细节\n\n### 词法分析和语法分析\n\n本项目使用经典的编译器前端技术来解析hosts文件：\n\n1. **词法分析（Lexical Analysis）**：将输入文本分解成有意义的标记（token），如IP地址、域名、注释等。\n\n2. **语法分析（Syntax Analysis）**：分析标记序列，构建结构化的hosts条目。\n\n这种方法使得解析过程更加健壮，能够处理各种格式的hosts文件，包括注释、空行和格式不正确的行。\n\n### Hosts文件格式\n\n标准hosts文件格式如下：\n\n```\n# 这是注释行\n127.0.0.1 localhost\n::1 localhost ipv6-localhost ipv6-loopback\n192.168.1.1 example.com www.example.com # 这是行末注释\n```\n\n## 许可证\n\nMIT\n\n## 贡献\n\n欢迎提交 Pull Requests 和 Issues。","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flimitcool%2Fhostsparser","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flimitcool%2Fhostsparser","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flimitcool%2Fhostsparser/lists"}