网卡信息获取

This commit is contained in:
shikong 2023-11-03 00:01:00 +08:00
parent 0112e66551
commit 67c3ad7795
6 changed files with 245 additions and 0 deletions

View File

@ -23,6 +23,50 @@ const docTemplate = `{
"host": "{{.Host}}", "host": "{{.Host}}",
"basePath": "{{.BasePath}}", "basePath": "{{.BasePath}}",
"paths": { "paths": {
"/if/info": {
"get": {
"description": "获取 网卡 信息",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Info"
],
"summary": "获取 网卡 信息",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/resp.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/net.InterfaceStat"
}
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/info": { "/info": {
"get": { "get": {
"description": "获取 iptables 规则 信息", "description": "获取 iptables 规则 信息",
@ -198,6 +242,47 @@ const docTemplate = `{
} }
} }
}, },
"net.InterfaceAddr": {
"type": "object",
"properties": {
"addr": {
"type": "string"
}
}
},
"net.InterfaceStat": {
"type": "object",
"properties": {
"addrs": {
"type": "array",
"items": {
"$ref": "#/definitions/net.InterfaceAddr"
}
},
"flags": {
"description": "e.g., FlagUp, FlagLoopback, FlagMulticast",
"type": "array",
"items": {
"type": "string"
}
},
"hardwareaddr": {
"description": "IEEE MAC-48, EUI-48 and EUI-64 form",
"type": "string"
},
"index": {
"type": "integer"
},
"mtu": {
"description": "maximum transmission unit",
"type": "integer"
},
"name": {
"description": "e.g., \"en0\", \"lo0\", \"eth0.100\"",
"type": "string"
}
}
},
"resp.Response": { "resp.Response": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -16,6 +16,50 @@
}, },
"basePath": "/", "basePath": "/",
"paths": { "paths": {
"/if/info": {
"get": {
"description": "获取 网卡 信息",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Info"
],
"summary": "获取 网卡 信息",
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/resp.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/net.InterfaceStat"
}
}
}
}
]
}
},
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
}
},
"/info": { "/info": {
"get": { "get": {
"description": "获取 iptables 规则 信息", "description": "获取 iptables 规则 信息",
@ -191,6 +235,47 @@
} }
} }
}, },
"net.InterfaceAddr": {
"type": "object",
"properties": {
"addr": {
"type": "string"
}
}
},
"net.InterfaceStat": {
"type": "object",
"properties": {
"addrs": {
"type": "array",
"items": {
"$ref": "#/definitions/net.InterfaceAddr"
}
},
"flags": {
"description": "e.g., FlagUp, FlagLoopback, FlagMulticast",
"type": "array",
"items": {
"type": "string"
}
},
"hardwareaddr": {
"description": "IEEE MAC-48, EUI-48 and EUI-64 form",
"type": "string"
},
"index": {
"type": "integer"
},
"mtu": {
"description": "maximum transmission unit",
"type": "integer"
},
"name": {
"description": "e.g., \"en0\", \"lo0\", \"eth0.100\"",
"type": "string"
}
}
},
"resp.Response": { "resp.Response": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -94,6 +94,34 @@ definitions:
description: '--sports example: 20000:40000' description: '--sports example: 20000:40000'
type: string type: string
type: object type: object
net.InterfaceAddr:
properties:
addr:
type: string
type: object
net.InterfaceStat:
properties:
addrs:
items:
$ref: '#/definitions/net.InterfaceAddr'
type: array
flags:
description: e.g., FlagUp, FlagLoopback, FlagMulticast
items:
type: string
type: array
hardwareaddr:
description: IEEE MAC-48, EUI-48 and EUI-64 form
type: string
index:
type: integer
mtu:
description: maximum transmission unit
type: integer
name:
description: e.g., "en0", "lo0", "eth0.100"
type: string
type: object
resp.Response: resp.Response:
properties: properties:
code: code:
@ -119,6 +147,32 @@ info:
title: iptables-helper API title: iptables-helper API
version: "1.0" version: "1.0"
paths: paths:
/if/info:
get:
consumes:
- application/json
description: 获取 网卡 信息
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/resp.Response'
- properties:
data:
items:
$ref: '#/definitions/net.InterfaceStat'
type: array
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 获取 网卡 信息
tags:
- Info
/info: /info:
get: get:
consumes: consumes:

1
go.mod
View File

@ -37,6 +37,7 @@ require (
github.com/rivo/uniseg v0.2.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/shirou/gopsutil v2.21.11+incompatible // indirect
github.com/sourcegraph/conc v0.3.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect github.com/spf13/cast v1.5.1 // indirect

2
go.sum
View File

@ -200,6 +200,8 @@ github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9c
github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U= github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ig1mpRjqV+Bu1U=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/shirou/gopsutil v2.21.11+incompatible h1:lOGOyCG67a5dv2hq5Z1BLDUqqKp3HkbjPcz5j6XMS0U=
github.com/shirou/gopsutil v2.21.11+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=

View File

@ -2,6 +2,7 @@ package controller
import ( import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/shirou/gopsutil/net"
response "iptables-helper/pkg/resp" response "iptables-helper/pkg/resp"
"iptables-helper/pkg/utils/command" "iptables-helper/pkg/utils/command"
"iptables-helper/pkg/utils/iptables" "iptables-helper/pkg/utils/iptables"
@ -10,6 +11,7 @@ import (
func SetupController(r fiber.Router) { func SetupController(r fiber.Router) {
api := r.Group("/") api := r.Group("/")
getRuleInfo(api) getRuleInfo(api)
getIfInfo(api)
} }
// getRuleInfo // getRuleInfo
@ -28,3 +30,19 @@ func getRuleInfo(api fiber.Router) {
return ctx.JSON(response.NewResponse(iptables.Parse(result))) return ctx.JSON(response.NewResponse(iptables.Parse(result)))
}) })
} }
// getIfInfo
// @Summary 获取 网卡 信息
// @Description 获取 网卡 信息
// @Tags Info
// @Accept json
// @Produce json
// @Success 200 {object} response.Response{data=[]net.InterfaceStat}
// @Failure default {object} errorx.CodeErrorResponse
// @Router /if/info [get]
func getIfInfo(api fiber.Router) {
api.Get("/if/info", func(ctx *fiber.Ctx) error {
stat, _ := net.Interfaces()
return ctx.JSON(response.NewResponse(stat))
})
}