mirror of
https://github.com/kubenetworks/kubevpn.git
synced 2025-12-24 11:51:13 +08:00
refactor: add tun device name to hosts comment (#725)
This commit is contained in:
@@ -94,7 +94,8 @@ const (
|
|||||||
EnvSSHJump = "SSH_JUMP_BY_KUBEVPN"
|
EnvSSHJump = "SSH_JUMP_BY_KUBEVPN"
|
||||||
|
|
||||||
// hosts entry keyword
|
// hosts entry keyword
|
||||||
HostsKeyWord = "# Add by KubeVPN"
|
HostsKeyword = "Added by KubeVPN"
|
||||||
|
HostsDeviceKeyword = "# For dev %s " + HostsKeyword
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ func (c *Config) AddServiceNameToHosts(ctx context.Context, hosts ...Entry) erro
|
|||||||
err := c.appendHosts(appendHosts)
|
err := c.appendHosts(appendHosts)
|
||||||
c.Lock.Unlock()
|
c.Lock.Unlock()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
plog.G(ctx).Errorf("Failed to add hosts(%s): %v", entryList2String(appendHosts), err)
|
plog.G(ctx).Errorf("Failed to add hosts(%s): %v", c.entryList2String(appendHosts), err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ func (c *Config) watchServiceToAddHosts(ctx context.Context, hosts []Entry) {
|
|||||||
err = c.appendHosts(appendHosts)
|
err = c.appendHosts(appendHosts)
|
||||||
c.Lock.Unlock()
|
c.Lock.Unlock()
|
||||||
if err != nil && !errors.Is(err, context.Canceled) {
|
if err != nil && !errors.Is(err, context.Canceled) {
|
||||||
plog.G(ctx).Errorf("Failed to add hosts(%s) to hosts: %v", entryList2String(appendHosts), err)
|
plog.G(ctx).Errorf("Failed to add hosts(%s) to hosts: %v", c.entryList2String(appendHosts), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -137,46 +137,27 @@ func (c *Config) appendHosts(appendHosts []Entry) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
str := entryList2String(appendHosts)
|
str := c.entryList2String(appendHosts)
|
||||||
_, err = f.WriteString(str)
|
_, err = f.WriteString(str)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) removeHosts(hosts []Entry) error {
|
func (c *Config) removeHosts() error {
|
||||||
if len(hosts) == 0 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < len(c.Hosts); i++ {
|
|
||||||
if sets.New[Entry]().Insert(hosts...).Has(c.Hosts[i]) {
|
|
||||||
c.Hosts = append(c.Hosts[:i], c.Hosts[i+1:]...)
|
|
||||||
i--
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hostFile := GetHostFile()
|
hostFile := GetHostFile()
|
||||||
content, err2 := os.ReadFile(hostFile)
|
content, err2 := os.ReadFile(hostFile)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
if !strings.Contains(string(content), config.HostsKeyWord) {
|
if !strings.Contains(string(content), config.HostsKeyword) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
keyword := fmt.Sprintf(config.HostsDeviceKeyword, c.TunName)
|
||||||
var retain []string
|
var retain []string
|
||||||
reader := bufio.NewReader(bytes.NewReader(content))
|
reader := bufio.NewReader(bytes.NewReader(content))
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
var needsRemove bool
|
if !strings.Contains(line, keyword) {
|
||||||
if strings.Contains(line, config.HostsKeyWord) {
|
|
||||||
for _, host := range hosts {
|
|
||||||
if strings.Contains(line, host.IP) && strings.Contains(line, host.Domain) {
|
|
||||||
needsRemove = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !needsRemove {
|
|
||||||
retain = append(retain, line)
|
retain = append(retain, line)
|
||||||
}
|
}
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
@@ -205,11 +186,11 @@ type Entry struct {
|
|||||||
Domain string
|
Domain string
|
||||||
}
|
}
|
||||||
|
|
||||||
func entryList2String(entryList []Entry) string {
|
func (c *Config) entryList2String(entryList []Entry) string {
|
||||||
var sb = new(bytes.Buffer)
|
var sb = new(bytes.Buffer)
|
||||||
w := tabwriter.NewWriter(sb, 1, 1, 1, ' ', 0)
|
w := tabwriter.NewWriter(sb, 1, 1, 1, ' ', 0)
|
||||||
for _, e := range entryList {
|
for _, e := range entryList {
|
||||||
_, _ = fmt.Fprintf(w, "\n%s\t%s\t%s\t%s", e.IP, e.Domain, "", config.HostsKeyWord)
|
_, _ = fmt.Fprintf(w, "\n%s\t%s\t%s\t%s", e.IP, e.Domain, "", fmt.Sprintf(config.HostsDeviceKeyword, c.TunName))
|
||||||
}
|
}
|
||||||
_ = w.Flush()
|
_ = w.Flush()
|
||||||
return sb.String()
|
return sb.String()
|
||||||
@@ -250,7 +231,7 @@ func (c *Config) generateAppendHosts(serviceList []v12.Service, hosts []Entry) [
|
|||||||
for {
|
for {
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
for i := 0; i < len(entryList); i++ {
|
for i := 0; i < len(entryList); i++ {
|
||||||
if strings.Contains(line, config.HostsKeyWord) && strings.Contains(line, entryList[i].Domain) {
|
if strings.Contains(line, config.HostsKeyword) && strings.Contains(line, entryList[i].Domain) {
|
||||||
entryList = append(entryList[:i], entryList[i+1:]...)
|
entryList = append(entryList[:i], entryList[i+1:]...)
|
||||||
i--
|
i--
|
||||||
}
|
}
|
||||||
@@ -275,7 +256,7 @@ func CleanupHosts() error {
|
|||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
return err2
|
return err2
|
||||||
}
|
}
|
||||||
if !strings.Contains(string(content), config.HostsKeyWord) {
|
if !strings.Contains(string(content), config.HostsKeyword) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,7 +264,7 @@ func CleanupHosts() error {
|
|||||||
reader := bufio.NewReader(bytes.NewReader(content))
|
reader := bufio.NewReader(bytes.NewReader(content))
|
||||||
for {
|
for {
|
||||||
line, err := reader.ReadString('\n')
|
line, err := reader.ReadString('\n')
|
||||||
if !strings.Contains(line, config.HostsKeyWord) {
|
if !strings.Contains(line, config.HostsKeyword) {
|
||||||
retain = append(retain, line)
|
retain = append(retain, line)
|
||||||
}
|
}
|
||||||
if errors.Is(err, io.EOF) {
|
if errors.Is(err, io.EOF) {
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
|
|
||||||
"github.com/docker/docker/libnetwork/resolvconf"
|
"github.com/docker/docker/libnetwork/resolvconf"
|
||||||
miekgdns "github.com/miekg/dns"
|
miekgdns "github.com/miekg/dns"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
|
||||||
"tailscale.com/net/dns"
|
"tailscale.com/net/dns"
|
||||||
"tailscale.com/util/dnsname"
|
"tailscale.com/util/dnsname"
|
||||||
|
|
||||||
@@ -155,7 +154,7 @@ func (c *Config) UseLibraryDNS(tunName string, clientConfig *miekgdns.ClientConf
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) CancelDNS() {
|
func (c *Config) CancelDNS() {
|
||||||
c.removeHosts(sets.New[Entry]().Insert(c.Hosts...).UnsortedList())
|
c.removeHosts()
|
||||||
if c.OSConfigurator != nil {
|
if c.OSConfigurator != nil {
|
||||||
_ = c.OSConfigurator.Close()
|
_ = c.OSConfigurator.Close()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -210,7 +210,7 @@ func (c *Config) CancelDNS() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//networkCancel()
|
//networkCancel()
|
||||||
_ = c.removeHosts(sets.New[Entry]().Insert(c.Hosts...).UnsortedList())
|
_ = c.removeHosts()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetResolvers
|
// GetResolvers
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
|
|
||||||
"golang.org/x/sys/windows"
|
"golang.org/x/sys/windows"
|
||||||
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
"golang.zx2c4.com/wireguard/windows/tunnel/winipcfg"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
|
||||||
|
|
||||||
plog "github.com/wencaiwulue/kubevpn/v2/pkg/log"
|
plog "github.com/wencaiwulue/kubevpn/v2/pkg/log"
|
||||||
)
|
)
|
||||||
@@ -54,7 +53,7 @@ func (c *Config) SetupDNS(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) CancelDNS() {
|
func (c *Config) CancelDNS() {
|
||||||
c.removeHosts(sets.New[Entry]().Insert(c.Hosts...).UnsortedList())
|
c.removeHosts()
|
||||||
tun, err := net.InterfaceByName(c.TunName)
|
tun, err := net.InterfaceByName(c.TunName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user