github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/dnsutil/hostsstore/hosts.go (about)

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  /*
    18    Portions from https://github.com/jaytaylor/go-hostsfile/blob/59e7508e09b9e08c57183ae15eabf1b757328ebf/hosts.go
    19    Copyright (c) 2016 Jay Taylor [@jtaylor]
    20  
    21    Permission is hereby granted, free of charge, to any person obtaining a copy
    22    of this software and associated documentation files (the "Software"), to deal
    23    in the Software without restriction, including without limitation the rights
    24    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    25    copies of the Software, and to permit persons to whom the Software is
    26    furnished to do so, subject to the following conditions:
    27  
    28    The above copyright notice and this permission notice shall be included in all
    29    copies or substantial portions of the Software.
    30  */
    31  
    32  package hostsstore
    33  
    34  import (
    35  	"bufio"
    36  	"fmt"
    37  	"io"
    38  	"strings"
    39  )
    40  
    41  const (
    42  	MarkerBegin = "<nerdctl>"
    43  	MarkerEnd   = "</nerdctl>"
    44  )
    45  
    46  // parseHostsButSkipMarkedRegion parses hosts file content but skips the <nerdctl> </nerdctl> region
    47  // mimics  https://github.com/jaytaylor/go-hostsfile/blob/59e7508e09b9e08c57183ae15eabf1b757328ebf/hosts.go#L18
    48  // mimics  https://github.com/norouter/norouter/blob/v0.6.2/pkg/agent/etchosts/etchosts.go#L128-L152
    49  func parseHostsButSkipMarkedRegion(w io.Writer, r io.Reader) error {
    50  	scanner := bufio.NewScanner(r)
    51  	skip := false
    52  LINE:
    53  	for scanner.Scan() {
    54  		line := scanner.Text()
    55  		line = strings.Replace(strings.Trim(line, " \t"), "\t", " ", -1)
    56  		sawMarkerEnd := false
    57  		if strings.HasPrefix(line, "#") {
    58  			com := strings.TrimSpace(line[1:])
    59  			switch com {
    60  			case MarkerBegin:
    61  				skip = true
    62  			case MarkerEnd:
    63  				sawMarkerEnd = true
    64  			}
    65  		}
    66  		if !skip {
    67  			if len(line) == 0 || line[0] == ';' || line[0] == '#' {
    68  				continue
    69  			}
    70  			pieces := strings.SplitN(line, " ", 2)
    71  			if len(pieces) > 1 && len(pieces[0]) > 0 {
    72  				if pieces[0] == "127.0.0.1" || pieces[0] == "::1" {
    73  					continue LINE
    74  				}
    75  				if names := strings.Fields(pieces[1]); len(names) > 0 {
    76  					for _, name := range names {
    77  						if strings.HasPrefix(name, "#") {
    78  							continue LINE
    79  						}
    80  					}
    81  					fmt.Fprintln(w, line)
    82  				}
    83  			}
    84  		}
    85  		if sawMarkerEnd {
    86  			skip = false
    87  		}
    88  	}
    89  	return scanner.Err()
    90  }