Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetupEndpoints issue in a special situation #142

Open
0x5e opened this issue Jul 29, 2023 · 1 comment
Open

SetupEndpoints issue in a special situation #142

0x5e opened this issue Jul 29, 2023 · 1 comment

Comments

@0x5e
Copy link

0x5e commented Jul 29, 2023

Hi @brutella ,
Thanks for this great project at first :)

I'm testing this project running on my MBP and found a small issue:
When iPhone connects the usb cable with MBP, the camera live stream usually not working in Home App.

And I found when usb cable connected, iPhone sometimes request hap server directly from the wired network, not via wifi.

hkcam/setup.go

Lines 89 to 98 in 959d664

iface, err := ifaceOfRequest(r)
if err != nil {
log.Debug.Println(err)
return
}
ip, err := ipAtInterface(*iface, req.ControllerAddr.IPVersion)
if err != nil {
log.Debug.Println(err)
return
}

The iface value is en14 instead of en0. then the server failed to get ip of en14 because it only have a ipv6 address.

Instead of get localIP from r.Context().Value(http.LocalAddrContextKey), my solution is like this:

  1. Obtain remoteIP from req.ControllerAddr.IPAddr
  2. Enumerate net.Interfaces(), find the proper subnet that contains remoteIP, return the ip of this interface.
func FindLocalIPForRemoteIP(remoteIP string) string {
	ifaces, _ := net.Interfaces()
	for _, iface := range ifaces {
		addrs, _ := iface.Addrs()
		for _, addr := range addrs {
			ip, subnet, _ := net.ParseCIDR(addr.String())
			if subnet.Contains(net.ParseIP(remoteIP)) {
				return ip.String()
			}
		}
	}
	return ""
}


localIP := util.FindLocalIPForRemoteIP(req.ControllerAddr.IPAddr)
if localIP == "" {
	log.Debug.Println("Failed to get localIP")
	return
}
@brutella
Copy link
Owner

brutella commented Aug 7, 2023

Sounds good to me. Happy to accept a pull request. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants