123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- package nmcli
- import (
- "AudioPlayer/nmcli/model"
- "context"
- nmcli_go "github.com/KunMengcode/nmcli-go"
- "github.com/KunMengcode/nmcli-go/connection"
- "net/rpc"
- "strings"
- )
- type EthernetInterface interface {
- SetStaticIP(Request model.EthernetStaticIPRequest, Reply *model.EmptyReply) error
- Show(Request model.DeviceInfoRequest, Reply *model.EthernetInfo) error
- register() error
- }
- type Ethernet struct {
- nmcli *nmcli_go.NMCli
- }
- func NewEthernet(nmcli *nmcli_go.NMCli) EthernetInterface {
- return &Ethernet{nmcli: nmcli}
- }
- func (receiver *Ethernet) register() error {
- return rpc.RegisterName("NetworkManager.Ethernet", receiver)
- }
- func (receiver *Ethernet) SetStaticIP(Request model.EthernetStaticIPRequest, Reply *model.EmptyReply) error {
- _, err := receiver.nmcli.Connection.Modify(context.Background(), false, Request.Id, map[string]string{
- "ipv4.addresses": Request.Ipv4Addresses,
- "ipv4.method": Request.Ipv4Method,
- "ipv4.dns": Request.DNS,
- "ipv4.gateway": Request.Gateway,
- })
- if err != nil {
- return err
- }
- _, err = receiver.nmcli.Connection.Up(context.Background(), Request.Id, connection.UpOptions{})
- if err != nil {
- return err
- }
- return nil
- }
- func (receiver *Ethernet) Show(Request model.DeviceInfoRequest, Reply *model.EthernetInfo) (err error) {
- if Request.DeviceInterfaceName == "" {
- Request.DeviceInterfaceName = "enp4s0"
- }
- show, err := receiver.nmcli.Device.Show(context.Background(), Request.DeviceInterfaceName)
- if err != nil {
- return err
- }
- *Reply = model.EthernetInfo{
- 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], "|"),
- }
- return nil
- }
|