Skip to content

Commit a46cf04

Browse files
committed
add command line flags
1 parent 84a4306 commit a46cf04

File tree

4 files changed

+64
-7
lines changed

4 files changed

+64
-7
lines changed

cmd/flags.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"muhammadn/avahi-proxy/pkg/proxy"
6+
)
7+
8+
var runCmd = &cobra.Command{
9+
Use: "run",
10+
Aliases: []string{"r"},
11+
Short: "Start running the proxy",
12+
RunE: func(cmd *cobra.Command, args []string) error {
13+
baseDomain, _ := cmd.Flags().GetString("basedomain")
14+
port, _ := cmd.Flags().GetString("port")
15+
16+
baseDomain = baseDomain + "."
17+
18+
proxy.RunProxy(baseDomain, port)
19+
return nil
20+
},
21+
}
22+
23+
func init() {
24+
rootCmd.AddCommand(runCmd)
25+
runCmd.Flags().StringP("baseDomain", "", "", "Your local LAN domain name, example: home.lan")
26+
runCmd.Flags().StringP("port", "p", "5354", "Port number to run avahi-proxy (default: 5354)")
27+
28+
runCmd.MarkFlagRequired("baseDomain")
29+
}

cmd/root.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var (
8+
rootCmd = &cobra.Command{
9+
Use: "avahi-proxy",
10+
Short: "Map Multicast DNS .local hostnames to your LAN domain name",
11+
Long: `Automatically map Multicast DNS .local hostnames to your LAN domain name (example, ".home.lan" or ".internal" and use your favourite DNS server to query avahi-proxy to get machine hostnames on .local multicast network.
12+
13+
Let your Windows and Android phones resolve your Multicast devices like iPhone/Mac and iPad, or maybe you want to expose .local hostnames over IPSec with your LAN domain name`,
14+
}
15+
)
16+
17+
func Execute() error {
18+
return rootCmd.Execute()
19+
}

main.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"muhammadn/avahi-proxy/cmd"
5+
)
6+
7+
func main() {
8+
cmd.Execute()
9+
}

cmd/avahi-proxy/main.go renamed to pkg/proxy/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package main
1+
package proxy
22

33
import (
44
"fmt"
55
"log"
6-
"strconv"
76
"strings"
7+
"strconv"
88

99
"github.com/godbus/dbus/v5"
1010
"github.com/holoplot/go-avahi"
@@ -73,14 +73,14 @@ func parseQuery(m *dns.Msg) {
7373
}
7474
}
7575

76-
func main() {
76+
func RunProxy(baseDomain string, port string) {
7777
// attach request handler func
78-
dns.HandleFunc("home.lan.", handleDnsRequest)
78+
dns.HandleFunc(baseDomain, handleDnsRequest)
7979

8080
// start server
81-
port := 5354
82-
server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"}
83-
log.Printf("Starting at %d\n", port)
81+
server := &dns.Server{Addr: ":" + port, Net: "udp"}
82+
nport, _ := strconv.Atoi(port)
83+
log.Printf("Starting at %d\n", nport)
8484
err := server.ListenAndServe()
8585
defer server.Shutdown()
8686
if err != nil {

0 commit comments

Comments
 (0)