mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-11-20 23:02:25 +03:00
Squashed from: - c3e8b07f feat: add GET /api/networkInterfaces controller - 10a5dc38 try to fix null pointer - 4fa3e6c7 Merge branch 'v2rayA:main' into main - 5e0457e7 tun: use tinytun v0.0.1-beta.4 - ce4bfa34 shadowsocks and trojan backend choosen Co-authored-by: Markson Pigeonzilla <marksonpigeonzilla@bugdows.cn> Co-authored-by: Markson Hon <50002150+MarksonHon@users.noreply.github.com>
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"net"
|
|
"runtime"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/v2rayA/v2rayA/common"
|
|
)
|
|
|
|
type networkInterfaceInfo struct {
|
|
Name string `json:"name"`
|
|
Addrs []string `json:"addrs"`
|
|
IsLoopback bool `json:"isLoopback"`
|
|
}
|
|
|
|
// GetNetworkInterfaces returns all active network interfaces with their addresses.
|
|
// On Windows the interface names are GUIDs / friendly descriptions, so the Name
|
|
// field carries the display-friendly value from net.Interface.Name.
|
|
func GetNetworkInterfaces(ctx *gin.Context) {
|
|
ifaces, err := net.Interfaces()
|
|
if err != nil {
|
|
common.ResponseError(ctx, logError(err))
|
|
return
|
|
}
|
|
|
|
var result []networkInterfaceInfo
|
|
for _, iface := range ifaces {
|
|
if iface.Flags&net.FlagUp == 0 {
|
|
continue
|
|
}
|
|
addrs, err := iface.Addrs()
|
|
if err != nil {
|
|
continue
|
|
}
|
|
var addrStrings []string
|
|
for _, addr := range addrs {
|
|
addrStrings = append(addrStrings, addr.String())
|
|
}
|
|
_ = runtime.GOOS // imported for potential future platform-specific logic
|
|
result = append(result, networkInterfaceInfo{
|
|
Name: iface.Name,
|
|
Addrs: addrStrings,
|
|
IsLoopback: iface.Flags&net.FlagLoopback != 0,
|
|
})
|
|
}
|
|
common.ResponseSuccess(ctx, gin.H{"interfaces": result})
|
|
}
|