https://github.com/keinos/go-hostpital
A simple go library to maintain and manage hosts files for DNS sinkhole applications.
https://github.com/keinos/go-hostpital
go golang golang-library hosts hostsfile parser
Last synced: about 1 year ago
JSON representation
A simple go library to maintain and manage hosts files for DNS sinkhole applications.
- Host: GitHub
- URL: https://github.com/keinos/go-hostpital
- Owner: KEINOS
- License: mit
- Created: 2022-12-18T05:33:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-13T19:37:33.000Z (about 1 year ago)
- Last Synced: 2025-04-13T20:34:39.039Z (about 1 year ago)
- Topics: go, golang, golang-library, hosts, hostsfile, parser
- Language: Go
- Homepage:
- Size: 10.2 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://github.com/KEINOS/go-hostpital/blob/main/.github/workflows/unit-tests.yml "Supported versions")
[](https://pkg.go.dev/github.com/KEINOS/go-hostpital/hostpital#section-documentation "Read generated documentation of the package")
# go-hostpital
A simple library written in `go` (golang) to maintain and manage `hosts` files.
## Usage
```go
go get "github.com/KEINOS/go-hostpital"
```
```go
import "github.com/KEINOS/go-hostpital/hostpital"
func ExampleValidator() {
// Validator with default settings
validator := hostpital.NewValidator()
validator.AllowComment = true // Allow comment lines in the hostfile.
validator.IDNACompatible = false // Want RFC 6125 2.2 compatibility. If true, IDNA2008 compatible.
// Validate a file
pathFile := filepath.Join("testdata", "hosts.txt")
if validator.ValidateFile(pathFile) {
fmt.Println("The hostfile is valid.")
}
// Output: The hostfile is valid.
}
```
```go
import "github.com/KEINOS/go-hostpital/hostpital"
func ExampleParser() {
// For the default settings, see the NewValidator() example.
parser := hostpital.NewParser()
// Set the IP address to use for all the hosts. Suitable for DNS sinkhole.
parser.UseIPAddress = "0.0.0.0"
// Parse a file to clean up
pathFile := filepath.Join("testdata", "hosts.txt")
parsed, err := parser.ParseFile(pathFile)
if err != nil {
log.Fatal(err)
}
fmt.Println(parsed)
// Output:
// 0.0.0.0 dummy1.example.com
// 0.0.0.0 dummy2.example.com
// 0.0.0.0 dummy3.example.com
// 0.0.0.0 dummy4.example.com
// 0.0.0.0 dummy5.example.com dummy6.example.com
}
```
```go
import "github.com/KEINOS/go-hostpital/hostpital"
// Various functions
func Example() {
// Detects IPV4 and IPV6
fmt.Println("IsIPAddress(\"0.0.0.0\") -->", hostpital.IsIPAddress("0.0.0.0"))
fmt.Println("IsIPAddress(\"::\") -->", hostpital.IsIPAddress("::"))
fmt.Println("IsIPAddress(\"0.0.0.0.0\") -->", hostpital.IsIPAddress("0.0.0.0.0"))
// True if host name is ready for registration. False if it is a raw punycode or not IDNA2008 compatible.
fmt.Println("IsIDNAComatible(\"xn--gpher-jua.com\") -->", hostpital.IsIDNAComatible("xn--gpher-jua.com"))
fmt.Println("IsIDNAComatible(\"göpher.com\") -->", hostpital.IsIDNAComatible("göpher.com"))
// ASCII/Punycode <---> Unicode conversion
hostASCII, err := hostpital.TransformToASCII("göpher.com")
fmt.Println("TransformToASCII(\"göpher.com\") -->", hostASCII, err)
hostUnicode, err := hostpital.TransformToUnicode("xn--gpher-jua.com")
fmt.Println("TransformToUnicode(\"xn--gpher-jua.com\") -->", hostUnicode, err)
// Trim a comment from a line
hostTrimmed, err := hostpital.TrimComment("127.0.0.0 localhost # this is a line comment")
fmt.Println("TrimComments(\"127.0.0.0 localhost # this is a line comment\") --->", hostTrimmed, err)
/* And more ... */
// Output:
// IsIPAddress("0.0.0.0") --> true
// IsIPAddress("::") --> true
// IsIPAddress("0.0.0.0.0") --> false
// IsIDNAComatible("xn--gpher-jua.com") --> true
// IsIDNAComatible("göpher.com") --> false
// TransformToASCII("göpher.com") --> xn--gpher-jua.com
// TransformToPunycode("xn--gpher-jua.com") --> göpher.com
// TrimComments("127.0.0.0 localhost # this is a line comment") ---> 127.0.0.0 localhost
}
```
- [View more examples](https://pkg.go.dev/github.com/KEINOS/go-hostpital/hostpital#pkg-examples) @ pkg.go.dev
- For command line example usage, see [./cmd/hostpital](./cmd/hostpital)
## Statuses
[](https://github.com/KEINOS/go-hostpital/actions/workflows/unit-tests.yml)
[](https://github.com/KEINOS/go-hostpital/actions/workflows/golangci-lint.yml)
[](https://github.com/KEINOS/go-hostpital/actions/workflows/codeQL-analysis.yml)
[](https://github.com/KEINOS/go-hostpital/actions/workflows/platform-tests.yml)
[](https://codecov.io/gh/KEINOS/go-hostpital)
[](https://goreportcard.com/report/github.com/KEINOS/go-hostpital)
## Contributing
[](https://github.com/KEINOS/go-hostpital/blob/main/.github/workflows/unit-tests.yml "Supported versions")
[](https://pkg.go.dev/github.com/KEINOS/go-hostpital#section-documentation "Read generated documentation of the app")
- Branch to PR: `main`
- [CONTRIBUTING.md](https://github.com/KEINOS/go-hostpital/blob/main/.github/CONTRIBUTING.md)
- [CIs](https://github.com/KEINOS/go-hostpital/actions) on PR/Push: `unit-tests` `golangci-lint` `codeQL-analysis` `platform-tests`
- [Security policy](https://github.com/KEINOS/go-hostpital/blob/main/.github/SECURITY.md)
## License/Copyright
- [MIT License](https://github.com/KEINOS/go-hostpital/blob/main/LICENSE)
- Copyright [KEINOS and the Hostpital contributors](https://github.com/KEINOS/go-hostpital/graphs/contributors)
- [BSD-3-Clause license](https://github.com/golang/go/blob/master/LICENSE)
- Copyright of [`Is_compatible_rfc6125.go` by The Go Authors](https://github.com/KEINOS/go-hostpital/blob/main/hostpital/Is_compatible_rfc6125.go)