-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (67 loc) · 1.32 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"errors"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "mdns-subdomain"
app.Usage = "Local mDNS announcer for subdomain"
app.Flags = flags
app.Action = action
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
var flags = []cli.Flag{
cli.StringFlag{
EnvVar: "LNAME_IFACE",
Name: "iface",
Usage: "specify network interface to listen, default listen to all",
},
cli.StringFlag{
EnvVar: "LNAME_HOSTNAME",
Name: "hostname",
Usage: "specify fixed hostname to broadcast, default the machine hostname",
},
}
func action(c *cli.Context) error {
var (
iface *net.Interface
hostname *string
err error
)
if c.IsSet("iface") {
iface, err = net.InterfaceByName(c.String("iface"))
if err != nil {
return err
}
}
if c.IsSet("hostname") {
value := c.String("hostname")
if !strings.HasSuffix(value, ".local") {
return errors.New("optional hostname must end with .local")
}
hostname = &value
}
conn, err := listen(iface, hostname)
if err != nil {
return err
}
gracefulStop := make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
<-gracefulStop
conn.stop()
}()
conn.serve()
return nil
}