mirror of
https://github.com/tobychui/zoraxy.git
synced 2025-08-05 20:58:28 +02:00
v2 init commit
This commit is contained in:
55
src/mod/netstat/nic.go
Normal file
55
src/mod/netstat/nic.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package netstat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"imuslab.com/zoraxy/mod/utils"
|
||||
)
|
||||
|
||||
type NetworkInterface struct {
|
||||
Name string
|
||||
ID int
|
||||
IPs []string
|
||||
}
|
||||
|
||||
func HandleListNetworkInterfaces(w http.ResponseWriter, r *http.Request) {
|
||||
nic, err := ListNetworkInterfaces()
|
||||
if err != nil {
|
||||
utils.SendErrorResponse(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
js, _ := json.Marshal(nic)
|
||||
utils.SendJSONResponse(w, string(js))
|
||||
}
|
||||
|
||||
func ListNetworkInterfaces() ([]NetworkInterface, error) {
|
||||
var interfaces []NetworkInterface
|
||||
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, iface := range ifaces {
|
||||
var ips []string
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
ips = append(ips, addr.String())
|
||||
}
|
||||
|
||||
interfaces = append(interfaces, NetworkInterface{
|
||||
Name: iface.Name,
|
||||
ID: iface.Index,
|
||||
IPs: ips,
|
||||
})
|
||||
}
|
||||
|
||||
return interfaces, nil
|
||||
}
|
Reference in New Issue
Block a user