自动更新管控端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

73 lines
1.4 KiB

package config
import (
"fmt"
"net"
"os"
)
type Config struct {
DeviceName string
Port string
LocalIP string
MulticastAddress string
WildcardAddress string
FilePath string
Version string
}
var G Config
func (c *Config) SetConf(port string) error {
Hostname, err := os.Hostname()
if err != nil {
Hostname = "unknow device"
}
// 设备名称
c.DeviceName = Hostname
c.Port = port
c.LocalIP, err = getLocalIP()
if err != nil {
return err
}
// 多播地址
c.MulticastAddress = fmt.Sprintf("224.0.0.169:%s", port)
// 广播地址
c.WildcardAddress = fmt.Sprintf("0.0.0.0:%s", port)
c.FilePath = "./"
c.Version = "0.3.0"
return nil
}
// 本地ip
func getLocalIP() (string, error) {
addrs, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
var ips []string
for _, address := range addrs {
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
ips = append(ips, ipnet.IP.String())
}
}
}
if len(ips) == 0 {
return "", fmt.Errorf("get local ip failed")
} else if len(ips) == 1 {
return ips[0], nil
} else {
// Select the one connected to the network
// when there are multiple network interfaces
// Is there a better way?
c, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return ips[0], nil
}
defer c.Close()
return c.LocalAddr().(*net.UDPAddr).IP.String(), nil
}
}