-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (128 loc) · 3.55 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"strings"
"syscall"
"github.com/hashicorp/mdns"
)
func main() {
var interfaceName string
var address string
var serviceType string
var hostname string
var port int
var err error
var ip net.IP
flag.StringVar(&hostname, "hostName", "", "The hostname that uniquely identifies this instance")
flag.StringVar(&interfaceName, "interfaceName", "", "The network interface to expose")
flag.StringVar(&address, "address", "", "The IP address to advertise")
flag.StringVar(&serviceType, "serviceType", "", "The type to advertise over mdns (e.g. \"_kcrypt._tcp\")")
flag.IntVar(&port, "port", 0, "The port to expose")
flag.Parse()
if port == 0 {
log.Println("port should be specified with --port")
os.Exit(1)
}
if interfaceName == "" && address == "" {
log.Println("interfaceName or address should be specified (--interfaceName|-address)")
os.Exit(1)
}
if serviceType == "" {
log.Println("serviceType should be specified with --serviceType")
os.Exit(1)
}
if hostname == "" {
log.Println("hostName should be specified with --hostName")
os.Exit(1)
}
// Create a valid FQDN from the hostname
if !strings.HasSuffix(hostname, ".") {
hostname += "."
}
if address != "" {
ip = net.ParseIP(address)
if ip == nil {
log.Println("invalid IPv4 address specified")
os.Exit(1)
}
} else {
ip, err = findIPAddress(interfaceName)
if err != nil {
log.Println(err.Error())
os.Exit(1)
}
if ip == nil {
log.Printf("Could not find an IP address (v4) for interface %s", interfaceName)
os.Exit(1)
}
}
// Setup our service export
host, _ := os.Hostname()
info := []string{"An instance of " + serviceType}
service, err := mdns.NewMDNSService(host, serviceType, "", hostname, port, []net.IP{ip}, info)
if err != nil {
panic(err.Error())
}
// Create the mDNS server, defer shutdown
server, _ := mdns.NewServer(&mdns.Config{Zone: service})
defer server.Shutdown()
log.Printf("Server created. Advertising %s:%d as %s of type %s", ip, port, hostname, serviceType)
sitAndWait()
}
func sitAndWait() {
// Create a channel to receive signals
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
// Create a channel to communicate with the goroutine
exitChan := make(chan struct{})
// Start a goroutine to listen for user input
go listenForInput(exitChan)
// Wait for a signal or user input to exit the program
select {
case sig := <-signalChan:
fmt.Printf("Received signal: %v\n", sig)
case <-exitChan:
fmt.Println("User pressed a key. Exiting...")
}
// Perform cleanup or additional actions before exiting, if necessary
fmt.Println("Program has exited.")
}
func listenForInput(exitChan chan<- struct{}) {
fmt.Print("Press Enter to exit: ")
// Create a scanner to read a line of input
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
// Send a signal to the main goroutine to exit
exitChan <- struct{}{}
}
func findIPAddress(iName string) (net.IP, error) {
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range interfaces {
if iface.Name == iName {
addrs, err := iface.Addrs()
if err != nil {
return nil, fmt.Errorf("error getting addresses: %w", err)
}
for _, addr := range addrs {
ip, _, err := net.ParseCIDR(addr.String())
if err != nil {
return nil, fmt.Errorf("parsing address: %w", err)
}
// Check if it's an IPv4 address
if ipv4 := ip.To4(); ipv4 != nil {
return ipv4, nil
}
}
}
}
return nil, nil
}