nmcli.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package SystemInfo
  2. import (
  3. pb "AudioPlayer/gRPC_Model"
  4. "context"
  5. nmcli_go "github.com/KunMengcode/nmcli-go"
  6. "github.com/KunMengcode/nmcli-go/connection"
  7. "github.com/KunMengcode/nmcli-go/device"
  8. "strings"
  9. )
  10. type NMCliServer struct {
  11. pb.UnimplementedNetworkManagerServer
  12. nmcli *nmcli_go.NMCli
  13. }
  14. func (s *NMCliServer) Open() {
  15. cli := nmcli_go.NewNMCli()
  16. s.nmcli = &cli
  17. }
  18. func (s *NMCliServer) CreateHotspot(c context.Context, in *pb.Hotspot) (*pb.Empty, error) {
  19. _, err := s.nmcli.Device.WiFiHotspotCreate(context.Background(), device.WiFiHotspotCreateOptions{
  20. Ifname: in.IfName,
  21. Con_name: "hotspot",
  22. SSID: in.SSID,
  23. Password: in.Password,
  24. })
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &pb.Empty{}, nil
  29. }
  30. func (s *NMCliServer) ChangeHotspot(c context.Context, in *pb.Hotspot) (*pb.Empty, error) {
  31. _, err := s.nmcli.Connection.Modify(context.Background(), false, "hotspot", map[string]string{
  32. "802-11-wireless.ssid": in.SSID,
  33. "802-11-wireless-security.psk": in.Password,
  34. })
  35. if err != nil {
  36. return &pb.Empty{}, err
  37. }
  38. _, err = s.OpenHotspot(c, &pb.Empty{})
  39. if err != nil {
  40. return &pb.Empty{}, err
  41. }
  42. return &pb.Empty{}, nil
  43. }
  44. func (s *NMCliServer) OpenHotspot(c context.Context, in *pb.Empty) (*pb.Empty, error) {
  45. _, err := s.nmcli.Connection.Up(context.Background(), "hotspot", connection.UpOptions{})
  46. if err != nil {
  47. return &pb.Empty{}, err
  48. }
  49. return &pb.Empty{}, nil
  50. }
  51. func (s *NMCliServer) HotspotInfo(c context.Context, in *pb.Empty) (*pb.Hotspot, error) {
  52. show, err := s.nmcli.Connection.Show(context.Background(), "hotspot")
  53. if err != nil {
  54. return &pb.Hotspot{}, err
  55. }
  56. return &pb.Hotspot{
  57. SSID: show["802-11-wireless"][0][0],
  58. Password: show["802-11-wireless-security"][0][14],
  59. }, nil
  60. }
  61. func (s *NMCliServer) DeviceStatus(c context.Context, in *pb.Empty) (FiltrationState *pb.NetworkManagerDeviceStatusList, err error) {
  62. // 初始化返回值
  63. filtrationState := &pb.NetworkManagerDeviceStatusList{}
  64. // 获取设备状态
  65. state, err := s.nmcli.Device.Status(c)
  66. if err != nil {
  67. return filtrationState, err
  68. }
  69. // 遍历设备状态,过滤掉 loopback 设备
  70. for _, v := range state {
  71. if v.Device == "lo" {
  72. continue
  73. }
  74. // 添加设备信息到返回值列表
  75. filtrationState.Value = append(filtrationState.Value, &pb.NetworkManagerDeviceStatus{
  76. Device: v.Device,
  77. Type: v.Type,
  78. State: v.State,
  79. IP4Connectivity: v.IP4Connectivity,
  80. IP6Connectivity: v.IP6Connectivity,
  81. Connection: v.Connection,
  82. ConUUID: v.ConUUID,
  83. })
  84. }
  85. return filtrationState, nil
  86. }
  87. func (s *NMCliServer) EthernetSetStaticIP(c context.Context, in *pb.Ethernet) (*pb.Empty, error) {
  88. _, err := s.nmcli.Connection.Modify(context.Background(), false, *in.Id, map[string]string{
  89. "ipv4.addresses": in.Ipv4Addresses,
  90. "ipv4.method": *in.Ipv4Method,
  91. "ipv4.dns": strings.Join(in.DNS, ","),
  92. "ipv4.gateway": in.Gateway,
  93. })
  94. if err != nil {
  95. return &pb.Empty{}, err
  96. }
  97. _, err = s.nmcli.Connection.Up(context.Background(), *in.Id, connection.UpOptions{})
  98. if err != nil {
  99. return &pb.Empty{}, err
  100. }
  101. return &pb.Empty{}, nil
  102. }
  103. func (s *NMCliServer) EthernetInfo(c context.Context, in *pb.DeviceInterfaceName) (*pb.Ethernet, error) {
  104. if in.Value == "" {
  105. in.Value = "enp4s0"
  106. }
  107. show, err := s.nmcli.Device.Show(context.Background(), in.Value)
  108. if err != nil {
  109. return &pb.Ethernet{}, err
  110. }
  111. return &pb.Ethernet{
  112. MacAddress: &show[0]["GENERAL"][0][9],
  113. Ipv4Addresses: show[0]["IP4"][0][0],
  114. Gateway: show[0]["IP4"][0][1],
  115. DNS: strings.Split(show[0]["IP4"][0][3], " | "),
  116. }, nil
  117. }