server.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. "time"
  9. "github.com/grandcat/zeroconf"
  10. )
  11. var (
  12. name = flag.String("name", "GoZeroconfGo", "The name for the service.")
  13. service = flag.String("service", "_workstation._tcp", "Set the service type of the new service.")
  14. domain = flag.String("domain", "local.", "Set the network domain. Default should be fine.")
  15. host = flag.String("host", "pc1", "Set host name for service.")
  16. ip = flag.String("ip", "::1", "Set IP a service should be reachable.")
  17. port = flag.Int("port", 42424, "Set the port the service is listening to.")
  18. waitTime = flag.Int("wait", 10, "Duration in [s] to publish service for.")
  19. )
  20. func main() {
  21. flag.Parse()
  22. server, err := zeroconf.RegisterProxy(*name, *service, *domain, *port, *host, []string{*ip}, []string{"txtv=0", "lo=1", "la=2"}, nil)
  23. if err != nil {
  24. panic(err)
  25. }
  26. defer server.Shutdown()
  27. log.Println("Published proxy service:")
  28. log.Println("- Name:", *name)
  29. log.Println("- Type:", *service)
  30. log.Println("- Domain:", *domain)
  31. log.Println("- Port:", *port)
  32. log.Println("- Host:", *host)
  33. log.Println("- IP:", *ip)
  34. // Clean exit.
  35. sig := make(chan os.Signal, 1)
  36. signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
  37. // Timeout timer.
  38. var tc <-chan time.Time
  39. if *waitTime > 0 {
  40. tc = time.After(time.Second * time.Duration(*waitTime))
  41. }
  42. select {
  43. case <-sig:
  44. // Exit by user
  45. case <-tc:
  46. // Exit by timeout
  47. }
  48. log.Println("Shutting down.")
  49. }