func (p *program) runHttp(s service.Service) { var cmds []string = []string{"shut", "reboot"} for index, command := range cmds { http.HandleFunc(fmt.Sprintf("/%s", command), handleFunc(index, command)) } logger.Info("http server start ...") err := http.ListenAndServe(":7070", nil) if err != nil { logger.Error(err) s.Stop() return } }
func (p *program) runUDP(s service.Service) { localAddr := getLocalAddr() if localAddr == nil { s.Stop() return } socket, err := net.ListenUDP("udp4", &net.UDPAddr{ IP: net.IPv4(0, 0, 0, 0), Port: 6060, }) if err != nil { logger.Error(err) s.Stop() return } logger.Info("udp server has start ...") defer socket.Close() out := make([]byte, 1024) for { select { case <-exit: return default: _, addr, err := socket.ReadFromUDP(out) if err != nil { logger.Error(err) continue } //logger.Infof("receive [%v] data : %s \n", addr.IP, string(out)) hostname, err := os.Hostname() if err != nil { logger.Error(err) hostname = "unknown" } resp := map[string]string{ "ip": localAddr["IP"], "hostname": hostname, } r, err := json.Marshal(resp) if err != nil { logger.Error(err) continue } socket.WriteToUDP(r, &net.UDPAddr{ IP: addr.IP, Port: 6060, }) } } }