Browse Source

initial commit

main v0.0.1
Nicolas Massé 4 years ago
commit
b6252b7e1b
  1. 21
      LICENSE
  2. 76
      cmd/reset.go
  3. 46
      cmd/root.go
  4. 63
      cmd/setDns.go
  5. 8
      go.mod
  6. 12
      go.sum
  7. 28
      main.go

21
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.

76
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)
}

46
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)
}
}

63
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")
}

8
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
)

12
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=

28
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()
}
Loading…
Cancel
Save