mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
fix(launcher): refine console host display
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/sipeed/picoclaw/pkg/config"
|
||||
"github.com/sipeed/picoclaw/pkg/logger"
|
||||
@@ -54,41 +55,88 @@ func FindPicoclawBinary() string {
|
||||
return "picoclaw"
|
||||
}
|
||||
|
||||
// GetLocalIPv4 returns a non-loopback local IPv4 address.
|
||||
func GetLocalIPv4() string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
func appendUniqueIP(addrs []string, seen map[string]struct{}, value string) []string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return addrs
|
||||
}
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() && ipnet.IP.To4() != nil {
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
if _, ok := seen[value]; ok {
|
||||
return addrs
|
||||
}
|
||||
return ""
|
||||
seen[value] = struct{}{}
|
||||
return append(addrs, value)
|
||||
}
|
||||
|
||||
// GetLocalIPv6 returns a non-loopback local IPv6 address.
|
||||
func GetLocalIPv6() string {
|
||||
// GetLocalIPv4s returns all non-loopback local IPv4 addresses.
|
||||
func GetLocalIPv4s() []string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
results := make([]string, 0, 4)
|
||||
seen := make(map[string]struct{}, 4)
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP == nil || ipnet.IP.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
if ip4 := ipnet.IP.To4(); ip4 != nil {
|
||||
results = appendUniqueIP(results, seen, ip4.String())
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func isDisplayGlobalIPv6(ip net.IP) bool {
|
||||
if ip == nil || ip.IsLoopback() || ip.To4() != nil {
|
||||
return false
|
||||
}
|
||||
ip = ip.To16()
|
||||
if ip == nil {
|
||||
return false
|
||||
}
|
||||
// Only show IPv6 global unicast addresses in 2000::/3.
|
||||
return ip[0]&0xe0 == 0x20
|
||||
}
|
||||
|
||||
// GetGlobalIPv6s returns all IPv6 global unicast addresses.
|
||||
func GetGlobalIPv6s() []string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
results := make([]string, 0, 4)
|
||||
seen := make(map[string]struct{}, 4)
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP == nil {
|
||||
continue
|
||||
}
|
||||
ip := ipnet.IP
|
||||
if ip.IsLoopback() || ip.To4() != nil {
|
||||
if !isDisplayGlobalIPv6(ip) {
|
||||
continue
|
||||
}
|
||||
if ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() {
|
||||
continue
|
||||
}
|
||||
return ip.String()
|
||||
results = appendUniqueIP(results, seen, ip.String())
|
||||
}
|
||||
return ""
|
||||
return results
|
||||
}
|
||||
|
||||
// GetLocalIPv4 returns the first non-loopback local IPv4 address.
|
||||
func GetLocalIPv4() string {
|
||||
addrs := GetLocalIPv4s()
|
||||
if len(addrs) == 0 {
|
||||
return ""
|
||||
}
|
||||
return addrs[0]
|
||||
}
|
||||
|
||||
// GetLocalIPv6 returns the first IPv6 global unicast address.
|
||||
func GetLocalIPv6() string {
|
||||
addrs := GetGlobalIPv6s()
|
||||
if len(addrs) == 0 {
|
||||
return ""
|
||||
}
|
||||
return addrs[0]
|
||||
}
|
||||
|
||||
// GetLocalIP returns a non-loopback local IPv4 address for backward compatibility.
|
||||
|
||||
Reference in New Issue
Block a user