github.com/iDigitalFlame/xmt@v0.5.4/device/machine.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package device
    18  
    19  import (
    20  	"github.com/iDigitalFlame/xmt/data"
    21  	"github.com/iDigitalFlame/xmt/device/arch"
    22  )
    23  
    24  // Machine is a struct that contains information about a specific device.
    25  // This struct contains generic Operating System Information such as Version,
    26  // Arch and network information.
    27  type Machine struct {
    28  	User     string
    29  	Version  string
    30  	Hostname string
    31  
    32  	Network      Network
    33  	PID, PPID    uint32
    34  	Capabilities uint32
    35  
    36  	ID       ID
    37  	System   uint8
    38  	Elevated uint8
    39  }
    40  
    41  // OS returns the Machine's OSType value.
    42  // This value is gained by shifting the 'System' value by bits 4 to the right.
    43  func (m Machine) OS() OSType {
    44  	return OSType(m.System >> 4)
    45  }
    46  
    47  // IsElevated will return true if the elevated flag is set to true on this
    48  // device's 'Elevated' flags.
    49  func (m Machine) IsElevated() bool {
    50  	return m.Elevated&1 != 0
    51  }
    52  
    53  // IsDomainJoined will return true if the domain joined flag is set to true
    54  // on this device's 'Elevated' flags.
    55  func (m Machine) IsDomainJoined() bool {
    56  	return m.OS() == Windows && m.Elevated&0x80 != 0
    57  }
    58  
    59  // Arch returns the Machine's Architecture value.
    60  // This value is gained by masking the OS bits of the 'System' value and returning
    61  // the lower 4 bits.
    62  func (m Machine) Arch() arch.Architecture {
    63  	return arch.Architecture(m.System & 0xF)
    64  }
    65  
    66  // MarshalStream transforms this struct into a binary format and writes to the
    67  // supplied data.Writer.
    68  func (m Machine) MarshalStream(w data.Writer) error {
    69  	if err := m.ID.MarshalStream(w); err != nil {
    70  		return err
    71  	}
    72  	if err := w.WriteUint8(m.System); err != nil {
    73  		return err
    74  	}
    75  	if err := w.WriteUint32(m.PID); err != nil {
    76  		return err
    77  	}
    78  	if err := w.WriteUint32(m.PPID); err != nil {
    79  		return err
    80  	}
    81  	if err := w.WriteString(m.User); err != nil {
    82  		return err
    83  	}
    84  	if err := w.WriteString(m.Version); err != nil {
    85  		return err
    86  	}
    87  	if err := w.WriteString(m.Hostname); err != nil {
    88  		return err
    89  	}
    90  	if err := w.WriteUint8(m.Elevated); err != nil {
    91  		return err
    92  	}
    93  	if err := w.WriteUint32(m.Capabilities); err != nil {
    94  		return err
    95  	}
    96  	return m.Network.MarshalStream(w)
    97  }
    98  
    99  // UnmarshalStream transforms this struct from a binary format that is read from
   100  // the supplied data.Reader.
   101  func (m *Machine) UnmarshalStream(r data.Reader) error {
   102  	if err := m.ID.UnmarshalStream(r); err != nil {
   103  		return err
   104  	}
   105  	if err := r.ReadUint8(&m.System); err != nil {
   106  		return err
   107  	}
   108  	if err := r.ReadUint32(&m.PID); err != nil {
   109  		return err
   110  	}
   111  	if err := r.ReadUint32(&m.PPID); err != nil {
   112  		return err
   113  	}
   114  	if err := r.ReadString(&m.User); err != nil {
   115  		return err
   116  	}
   117  	if err := r.ReadString(&m.Version); err != nil {
   118  		return err
   119  	}
   120  	if err := r.ReadString(&m.Hostname); err != nil {
   121  		return err
   122  	}
   123  	if err := r.ReadUint8(&m.Elevated); err != nil {
   124  		return err
   125  	}
   126  	if err := r.ReadUint32(&m.Capabilities); err != nil {
   127  		return err
   128  	}
   129  	return m.Network.UnmarshalStream(r)
   130  }