123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- package SystemInfo
- import (
- pb "AudioPlayer/gRPC_Model"
- "context"
- nmcli_go "github.com/KunMengcode/nmcli-go"
- "github.com/KunMengcode/nmcli-go/connection"
- "github.com/KunMengcode/nmcli-go/device"
- "strings"
- )
- type NMCliServer struct {
- pb.UnimplementedNetworkManagerServer
- nmcli *nmcli_go.NMCli
- }
- func (s *NMCliServer) Open() {
- cli := nmcli_go.NewNMCli()
- s.nmcli = &cli
- }
- func (s *NMCliServer) CreateHotspot(c context.Context, in *pb.Hotspot) (*pb.Empty, error) {
- _, err := s.nmcli.Device.WiFiHotspotCreate(context.Background(), device.WiFiHotspotCreateOptions{
- Ifname: in.IfName,
- Con_name: "hotspot",
- SSID: in.SSID,
- Password: in.Password,
- })
- if err != nil {
- return nil, err
- }
- return &pb.Empty{}, nil
- }
- func (s *NMCliServer) ChangeHotspot(c context.Context, in *pb.Hotspot) (*pb.Empty, error) {
- _, err := s.nmcli.Connection.Modify(context.Background(), false, "hotspot", map[string]string{
- "802-11-wireless.ssid": in.SSID,
- "802-11-wireless-security.psk": in.Password,
- })
- if err != nil {
- return &pb.Empty{}, err
- }
- _, err = s.OpenHotspot(c, &pb.Empty{})
- if err != nil {
- return &pb.Empty{}, err
- }
- return &pb.Empty{}, nil
- }
- func (s *NMCliServer) OpenHotspot(c context.Context, in *pb.Empty) (*pb.Empty, error) {
- _, err := s.nmcli.Connection.Up(context.Background(), "hotspot", connection.UpOptions{})
- if err != nil {
- return &pb.Empty{}, err
- }
- return &pb.Empty{}, nil
- }
- func (s *NMCliServer) HotspotInfo(c context.Context, in *pb.Empty) (*pb.Hotspot, error) {
- show, err := s.nmcli.Connection.Show(context.Background(), "hotspot")
- if err != nil {
- return &pb.Hotspot{}, err
- }
- return &pb.Hotspot{
- SSID: show["802-11-wireless"][0][0],
- Password: show["802-11-wireless-security"][0][14],
- }, nil
- }
- func (s *NMCliServer) DeviceStatus(c context.Context, in *pb.Empty) (FiltrationState *pb.NetworkManagerDeviceStatusList, err error) {
- // 初始化返回值
- filtrationState := &pb.NetworkManagerDeviceStatusList{}
- // 获取设备状态
- state, err := s.nmcli.Device.Status(c)
- if err != nil {
- return filtrationState, err
- }
- // 遍历设备状态,过滤掉 loopback 设备
- for _, v := range state {
- if v.Device == "lo" {
- continue
- }
- // 添加设备信息到返回值列表
- filtrationState.Value = append(filtrationState.Value, &pb.NetworkManagerDeviceStatus{
- Device: v.Device,
- Type: v.Type,
- State: v.State,
- IP4Connectivity: v.IP4Connectivity,
- IP6Connectivity: v.IP6Connectivity,
- Connection: v.Connection,
- ConUUID: v.ConUUID,
- })
- }
- return filtrationState, nil
- }
- func (s *NMCliServer) EthernetSetStaticIP(c context.Context, in *pb.Ethernet) (*pb.Empty, error) {
- _, err := s.nmcli.Connection.Modify(context.Background(), false, *in.Id, map[string]string{
- "ipv4.addresses": in.Ipv4Addresses,
- "ipv4.method": *in.Ipv4Method,
- "ipv4.dns": strings.Join(in.DNS, ","),
- "ipv4.gateway": in.Gateway,
- })
- if err != nil {
- return &pb.Empty{}, err
- }
- _, err = s.nmcli.Connection.Up(context.Background(), *in.Id, connection.UpOptions{})
- if err != nil {
- return &pb.Empty{}, err
- }
- return &pb.Empty{}, nil
- }
- func (s *NMCliServer) EthernetInfo(c context.Context, in *pb.DeviceInterfaceName) (*pb.Ethernet, error) {
- if in.Value == "" {
- in.Value = "enp4s0"
- }
- show, err := s.nmcli.Device.Show(context.Background(), in.Value)
- if err != nil {
- return &pb.Ethernet{}, err
- }
- return &pb.Ethernet{
- MacAddress: &show[0]["GENERAL"][0][9],
- Ipv4Addresses: show[0]["IP4"][0][0],
- Gateway: show[0]["IP4"][0][1],
- DNS: strings.Split(show[0]["IP4"][0][3], " | "),
- }, nil
- }
|