From 3bd9d1f3a4bd50db5ef1975e6a5152937912bc2d Mon Sep 17 00:00:00 2001 From: danielViolist Date: Sun, 23 Apr 2023 23:47:30 -0400 Subject: [PATCH 1/3] fixed IPv6 and hostname problems #130 #129 --- db/devices.go | 122 +++++++++++++++++++++++--------------------------- 1 file changed, 55 insertions(+), 67 deletions(-) diff --git a/db/devices.go b/db/devices.go index 89ff4aa..b43a9cc 100644 --- a/db/devices.go +++ b/db/devices.go @@ -5,8 +5,8 @@ import ( "errors" "fmt" "io/ioutil" + "net" "os" - "strconv" "strings" ) @@ -14,7 +14,8 @@ const devices = "devices.db" type device struct { name string - ip string // static IP of the device + //ip string // static IP of the device + ip net.IP // This is the NAME of the image file // NOT the actual image in memory image string @@ -43,7 +44,7 @@ func CreateDevice(name, ip, image, floorNm string) (dev device, err error) { return } // Check IP formatting and IP not already in database - if err = CheckIP(ip); err != nil { + if _, err = CheckIP(ip); err != nil { return } // Making sure the floor can be read or @@ -54,7 +55,7 @@ func CreateDevice(name, ip, image, floorNm string) (dev device, err error) { // All checks are good, creating the // floor and writing to db dev.name = name - dev.ip = ip + dev.ip = net.ParseIP(ip) dev.image = image dev.floorName = floorNm dev.positionT = "\"0\"" @@ -95,43 +96,30 @@ func writeDevice(d device) (err error) { /* This checks that the string fits -a valid IP format. Returns -nil if it's good. Error otherwise. +a valid IP/hostname format. Returns +nil error if no problems are found. + +Note: This will convert hostnames +to IP addresses. */ -func CheckIP(ip string) error { - // checking if IP already matches another device - ips, err := GetAllIPs() - for _, i := range ips { - if ip == i { - return fmt.Errorf("IP is already in use for a device.") +func CheckIP(ip string) (net.IP, error) { + // This function converts to a go IP type. + // will be nil if parsing failed. + check := net.ParseIP(ip) + if check == nil { + // Could be a hostname. + // Getting the IP from net function + hostIP, err := net.LookupHost(ip) + if err != nil { + return nil, fmt.Errorf("Failed to parse IP address or host.") } + // Just taking the first IP in the slice + // Could potentially cause problems but + // should work in most cases + check = net.ParseIP(hostIP[0]) } - // checking IP format valid - checkIP := strings.Split(ip, ".") - if len(checkIP) != 4 { - return fmt.Errorf("IP format is invalid.") - } - // Getting each octet to verify that it is in - // a valid IP range of 0-255 - first, err := strconv.Atoi(checkIP[0]) - if err != nil || first < 0 || first > 255 { - return fmt.Errorf("IP format is invalid.") - } - second, err := strconv.Atoi(checkIP[1]) - if err != nil || second < 0 || second > 255 { - return fmt.Errorf("IP format is invalid.") - } - third, err := strconv.Atoi(checkIP[2]) - if err != nil || third < 0 || third > 255 { - return fmt.Errorf("IP format is invalid.") - } - fourth, err := strconv.Atoi(checkIP[3]) - if err != nil || fourth < 0 || fourth > 255 { - return fmt.Errorf("IP format is invalid.") - } - - // All should be good - return nil + // IP is not nil so everything is good + return check, nil } /* @@ -160,7 +148,7 @@ func CheckDevice(name, floorNm string) error { if len(line) > 1 { d := device{ name: line[0], - ip: line[1], + ip: net.ParseIP(line[1]), image: line[2], floorName: line[3], positionT: line[4], @@ -193,7 +181,7 @@ func EditDevice(name, newName, newIP, newImage, floorNm string) { if len(splitLine) > 1 { d := device{ name: splitLine[0], - ip: splitLine[1], + ip: net.ParseIP(splitLine[1]), image: splitLine[2], floorName: splitLine[3], positionT: splitLine[4], @@ -260,42 +248,42 @@ func DeleteDevice(name, floorNm string) error { } func GetIP(name, floorName string) string { - devices, _ := GetAllDevicesForFloor(floorName) - for _, device := range devices { - if device.name == name { - return device.ip - } + devices, _ := GetAllDevicesForFloor(floorName) + for _, device := range devices { + if device.name == name { + return device.ip.String() } + } return "" } func GetImage(name, floorName string) string { - devices, _ := GetAllDevicesForFloor(floorName) - for _, device := range devices { - if device.name == name { - return device.image - } + devices, _ := GetAllDevicesForFloor(floorName) + for _, device := range devices { + if device.name == name { + return device.image } + } return "" } -func GetPositionsT(name, floorName string) (string) { - devices, _ := GetAllDevicesForFloor(floorName) - for _, device := range devices { - if device.name == name { - return device.positionT - } +func GetPositionsT(name, floorName string) string { + devices, _ := GetAllDevicesForFloor(floorName) + for _, device := range devices { + if device.name == name { + return device.positionT } + } return "0" } -func GetPositionsL(name, floorName string) (string) { - devices, _ := GetAllDevicesForFloor(floorName) - for _, device := range devices { - if device.name == name { - return device.positionL - } +func GetPositionsL(name, floorName string) string { + devices, _ := GetAllDevicesForFloor(floorName) + for _, device := range devices { + if device.name == name { + return device.positionL } + } return "0" } @@ -328,7 +316,7 @@ func GetAllDevicesForFloor(floorNm string) (devs []device, err error) { if len(line) > 1 { d := device{ name: line[0], - ip: line[1], + ip: net.ParseIP(line[1]), image: line[2], floorName: line[3], positionT: line[4], @@ -350,7 +338,7 @@ func GetAllIPs() (myDevices []string, err error) { for _, floor := range floors { devices, _ := GetAllDevicesForFloor(floor.name) for _, device := range devices { - deviceIPs = append(deviceIPs, device.ip) + deviceIPs = append(deviceIPs, device.ip.String()) } } return deviceIPs, nil @@ -374,10 +362,10 @@ func EditDeviceCoordinates(name, floorNm, top, left string) { if len(splitLine) > 1 { d := device{ name: splitLine[0], - ip: splitLine[1], + ip: net.ParseIP(splitLine[1]), image: splitLine[2], floorName: splitLine[3], - positionT: splitLine[4], + positionT: splitLine[4], positionL: splitLine[5], } if d.name == name { @@ -391,4 +379,4 @@ func EditDeviceCoordinates(name, floorNm, top, left string) { if err != nil { fmt.Println(err) } -} \ No newline at end of file +} From 3f52a1da53893bc6e7323e2412f04abe03b1c481 Mon Sep 17 00:00:00 2001 From: danielViolist Date: Mon, 24 Apr 2023 16:05:14 -0400 Subject: [PATCH 2/3] fixed line 594 #176 --- application.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/application.go b/application.go index 258320c..1731d97 100644 --- a/application.go +++ b/application.go @@ -591,7 +591,7 @@ func editDevice(c *gin.Context) { } // checking IP is valid if (len(newIP) > 0) && (newIP != db.GetIP(name, getCurrentFloor())) { - if err := db.CheckIP(newIP); err != nil { + if _, err := db.CheckIP(newIP); err != nil { c.HTML(http.StatusBadRequest, "index.html", gin.H{ "ViewDeviceModal": "ViewDeviceModal", "DeviceName": name, @@ -763,4 +763,3 @@ func setCurrentDevice(deviceName string) { func getCurrentDevice() (deviceName string) { return currentDevice } - From 4e657ab73586a67896ed47cfe71983d49b70ef1c Mon Sep 17 00:00:00 2001 From: danielViolist Date: Mon, 24 Apr 2023 16:26:01 -0400 Subject: [PATCH 3/3] not sure --- application.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/application.go b/application.go index 2b40ff8..5c4b362 100644 --- a/application.go +++ b/application.go @@ -772,8 +772,6 @@ func setCurrentDevice(deviceName string) { func getCurrentDevice() (deviceName string) { return currentDevice } -<<<<<<< HEAD -======= /* Renders forgot password page @@ -866,4 +864,3 @@ func performResetPassword(c *gin.Context) { showLoginPage(c) } } ->>>>>>> 4c43a5d1ff9789411ab1b52877e0a4b8c397b830