From b6252b7e1bf0ebd004e4de06e337ccfa6866d0a5 Mon Sep 17 00:00:00 2001 From: Nicolas MASSE Date: Fri, 13 May 2022 18:25:26 +0200 Subject: [PATCH] initial commit --- LICENSE | 21 ++++++++++++++ cmd/reset.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 46 +++++++++++++++++++++++++++++++ cmd/setDns.go | 63 ++++++++++++++++++++++++++++++++++++++++++ go.mod | 8 ++++++ go.sum | 12 ++++++++ main.go | 28 +++++++++++++++++++ 7 files changed, 254 insertions(+) create mode 100644 LICENSE create mode 100644 cmd/reset.go create mode 100644 cmd/root.go create mode 100644 cmd/setDns.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..171ca96 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright © 2022 Nicolas MASSE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/cmd/reset.go b/cmd/reset.go new file mode 100644 index 0000000..026898a --- /dev/null +++ b/cmd/reset.go @@ -0,0 +1,76 @@ +/* +Copyright © 2022 Nicolas MASSE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + "os" + "strings" + + "github.com/godbus/dbus/v5" + "github.com/spf13/cobra" +) + +// revertCmd represents the revert command +var revertCmd = &cobra.Command{ + Use: "revert", + Short: "Reverts upstream DNS of dnsmasq", + Long: `Reverts upstream DNS servers to their original values when the wireguard +connection is closed.`, + Run: func(cmd *cobra.Command, args []string) { + conn, err := dbus.ConnectSystemBus() + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to connect to system bus:", err) + os.Exit(1) + } + defer conn.Close() + + var entries []map[string]dbus.Variant + obj := conn.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/DnsManager") + err = obj.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.NetworkManager.DnsManager", "Configuration").Store(&entries) + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to fetch org.freedesktop.NetworkManager.DnsManager.Configuration property on org.freedesktop.NetworkManager:", err) + os.Exit(1) + } + + for _, entry := range entries { + for k, v := range entry { + if k == "nameservers" { + if nameservers, ok := v.Value().([]string); ok { + servers = append(servers, nameservers...) + } + } + } + } + fmt.Printf("Setting the dnsmasq upstream servers to: %s...\n", strings.Join(servers, ", ")) + obj = conn.Object("org.freedesktop.NetworkManager.dnsmasq", "/uk/org/thekelleys/dnsmasq") + err = obj.Call("uk.org.thekelleys.dnsmasq.SetDomainServers", 0, servers).Store() + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to call uk.org.thekelleys.dnsmasq.SetDomainServers on org.freedesktop.NetworkManager.dnsmasq", err) + os.Exit(1) + } + }, +} + +func init() { + rootCmd.AddCommand(revertCmd) +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..f25ba13 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,46 @@ +/* +Copyright © 2022 Nicolas MASSE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + "os" + + "github.com/spf13/cobra" +) + +// rootCmd represents the base command when called without any subcommands +var rootCmd = &cobra.Command{ + Use: "nm-dns-toggle", + Short: "Temporarily switches DNS used by dnsmasq on a NetworkManager", + Long: `While initializing a Wireguard connection, temporarily sets the upstream +servers used by dnsmasq and reverts those changes when the wireguard connection is closed`, +} + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + if err := rootCmd.Execute(); err != nil { + fmt.Println(err) + os.Exit(1) + } +} diff --git a/cmd/setDns.go b/cmd/setDns.go new file mode 100644 index 0000000..8afb581 --- /dev/null +++ b/cmd/setDns.go @@ -0,0 +1,63 @@ +/* +Copyright © 2022 Nicolas MASSE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package cmd + +import ( + "fmt" + "os" + "strings" + + "github.com/godbus/dbus/v5" + "github.com/spf13/cobra" +) + +var servers []string + +// setDnsCmd represents the setDns command +var setDnsCmd = &cobra.Command{ + Use: "set-dns", + Short: "Sets upstream DNS of dnsmasq", + Long: `While initializing a Wireguard connection, temporarily sets the upstream +servers used by dnsmasq.`, + Run: func(cmd *cobra.Command, args []string) { + conn, err := dbus.ConnectSystemBus() + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to connect to system bus:", err) + os.Exit(1) + } + defer conn.Close() + + fmt.Printf("Setting the dnsmasq upstream servers to: %s...\n", strings.Join(servers, ", ")) + + obj := conn.Object("org.freedesktop.NetworkManager.dnsmasq", "/uk/org/thekelleys/dnsmasq") + err = obj.Call("uk.org.thekelleys.dnsmasq.SetDomainServers", 0, servers).Store() + if err != nil { + fmt.Fprintln(os.Stderr, "Failed to call uk.org.thekelleys.dnsmasq.SetDomainServers on org.freedesktop.NetworkManager.dnsmasq", err) + os.Exit(1) + } + }, +} + +func init() { + rootCmd.AddCommand(setDnsCmd) + setDnsCmd.Flags().StringSliceVarP(&servers, "server", "s", []string{}, "Upstream servers to set") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e5cfa3f --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/nmasse-itix/nm-dns-toggle + +go 1.16 + +require ( + github.com/godbus/dbus/v5 v5.1.0 + github.com/spf13/cobra v1.4.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..896b6b0 --- /dev/null +++ b/go.sum @@ -0,0 +1,12 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= +github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/main.go b/main.go new file mode 100644 index 0000000..768f2b8 --- /dev/null +++ b/main.go @@ -0,0 +1,28 @@ +/* +Copyright © 2022 Nicolas MASSE + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ +package main + +import "github.com/nmasse-itix/nm-dns-toggle/cmd" + +func main() { + cmd.Execute() +}