Skip to content

Commit

Permalink
added RemoveCIDRs
Browse files Browse the repository at this point in the history
  • Loading branch information
cjimti committed Jul 24, 2023
1 parent bec8513 commit f47bf69
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
40 changes: 38 additions & 2 deletions txeh.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (h *Hosts) Save() error {
// SaveAs saves rendered hosts file to the filename specified
func (h *Hosts) SaveAs(fileName string) error {
if h.RawText != nil {
return errors.New("can not call Save or SaveAs with RawText. Use RenderHostsFile to return a string")
return errors.New("cannot call Save or SaveAs with RawText. Use RenderHostsFile to return a string")
}
hfData := []byte(h.RenderHostsFile())

Expand All @@ -128,6 +128,9 @@ func (h *Hosts) SaveAs(fileName string) error {

// Reload hosts file
func (h *Hosts) Reload() error {
if h.RawText != nil {
return errors.New("cannot call Reload with RawText")
}
h.Lock()
defer h.Unlock()

Expand Down Expand Up @@ -157,7 +160,7 @@ func (h *Hosts) RemoveAddress(address string) {
}
}

// RemoveFirstAddress removed the first entry (line) found with the provided address.
// RemoveFirstAddress removes the first entry (line) found with the provided address.
func (h *Hosts) RemoveFirstAddress(address string) bool {
h.Lock()
defer h.Unlock()
Expand All @@ -172,6 +175,39 @@ func (h *Hosts) RemoveFirstAddress(address string) bool {
return false
}

// RemoveCIDRs Remove CIDR Range (Classless inter-domain routing)
// examples:
//
// 127.1.0.0/16 = 127.1.0.0 -> 127.1.255.255
// 127.1.27.0/24 = 127.1.27.0 -> 127.1.27.255
func (h *Hosts) RemoveCIDRs(cidrs []string) error {
addresses := make([]string, 0)

// loop through all the CIDR ranges (we probably have less ranges than IPs)
for _, cidr := range cidrs {

_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
return err
}

hfLines := h.GetHostFileLines()

for _, hfl := range *hfLines {
ip := net.ParseIP(hfl.Address)
if ip != nil {
if ipnet.Contains(ip) {
addresses = append(addresses, hfl.Address)
}
}
}
}

h.RemoveAddresses(addresses)

return nil
}

// RemoveHosts removes all hostname entries of the provided host slice
func (h *Hosts) RemoveHosts(hosts []string) {
for _, host := range hosts {
Expand Down
35 changes: 6 additions & 29 deletions util/cmd/remove_cidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package cmd
import (
"errors"
"fmt"
"net"
"github.com/spf13/cobra"
"os"
"strings"

"github.com/spf13/cobra"
)

func init() {
Expand Down Expand Up @@ -40,39 +38,18 @@ var removeCidrCmd = &cobra.Command{

func RemoveIPRanges(cidrs []string) {

addresses := make([]string, 0)

// loop through all the CIDR ranges
for _, cidr := range cidrs {

_, ipnet, err := net.ParseCIDR(cidr)
if err != nil {
fmt.Printf("Error: there was a problem with the IP range %s. Reason: %s\n", cidr, err.Error())
os.Exit(1)
}

hfLines := etcHosts.GetHostFileLines()

for _, hfl := range *hfLines {

ip := net.ParseIP(hfl.Address)
if ip != nil {
if ipnet.Contains(ip) {
addresses = append(addresses, hfl.Address)
}
}
}

err := etcHosts.RemoveCIDRs(cidrs)
if err != nil {
fmt.Printf("Error: there was a problem parsing a CIDR. Reason: %s\n", err.Error())
os.Exit(1)
}

etcHosts.RemoveAddresses(addresses)

if DryRun {
fmt.Print(etcHosts.RenderHostsFile())
return
}

err := etcHosts.Save()
err = etcHosts.Save()
if err != nil {
fmt.Printf("Error: could not save %s. Reason: %s\n", etcHosts.WriteFilePath, err.Error())
os.Exit(1)
Expand Down

0 comments on commit f47bf69

Please sign in to comment.