ossupport

package
v0.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: AGPL-3.0 Imports: 19 Imported by: 0

README

OS Support

主要是不同的操作系统级的支持接口分类。将特殊操作系统的实现放在这里。现阶段暂时只有Windows和Linux(ubuntu系)。

Documentation

Overview

Copyright (C) 2024 wwhai

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

View Source
const (
	// Rulex 工作目录
	MainWorkDir = "/usr/local"
	// RULEX 的配置数据库
	RunDbPath = "/usr/local/rulex.db"
	// 固件保存路径
	FirmwarePath = "/usr/local/upload/Firmware/Firmware.zip"
	// 升级日志
	UpgradeLogPath = "/usr/local/local-upgrade-log.txt"
	// 运行时日志
	RunningLogPath = "/usr/local/rulexlog.txt"
	// 数据恢复日志
	RecoverLogPath = "/usr/local/local-recover-log.txt"
	// 备份锁
	BackupLockPath = "/var/run/rulex-upgrade.lock"
	// 升级锁
	UpgradeLockPath = BackupLockPath
	// 备份数据库
	RecoveryDbPath = "/usr/local/upload/Backup/recovery.db"
)

* * Linux系统下的一些和应用交互的系统级路径 *

Variables

This section is empty.

Functions

func BindMacAndIP

func BindMacAndIP()
    IP和MAC地址绑定
	host hostname {
	    hardware ethernet 00:11:22:33:44:55;
	    fixed-address 192.168.1.200;
		client-hostname "wwhai"
	}

func ConfigDefaultIpTable

func ConfigDefaultIpTable(Iface string) error

func ConfigDefaultIscServeDhcp

func ConfigDefaultIscServeDhcp(IpRoute IscServerDHCPConfig) error

func ConfigDefaultIscServer

func ConfigDefaultIscServer(Iface string) error

* # vim /etc/dhcp/dhcpd.conf

subnet 192.168.64.0 netmask 255.255.255.0 {
   range 192.168.64.100 192.168.64.200;      # 开放的地址池
   option routers 192.168.64.100;            # 网关地址
   option broadcast-address 192.168.64.255;  # 广播地址
   default-lease-time 600;                   # 默认租期,单位:秒
   max-lease-time 7200;                      # 最大租期
}

* 这个初始化特殊在咬对两个软件的配置进行刷新,一个是 dnsmasq, 一个是isc-dhcp-server

func DisableWifi

func DisableWifi() error

* * 关闭WIFI开关 *

func EnableWifi

func EnableWifi() error

* * 打开WIFI开关 *

func FileExists added in v0.6.5

func FileExists(filename string) bool

* * 恢复上传的DB 1 停止RULEX 2 删除老DB 3 复制新DB到路径 3 删除PID,停止守护进程 4 重启(脚本会新建PID) - path: /usr/local/rulex, args: recover=true *

func GetBSPNetIfaces

func GetBSPNetIfaces() ([]string, error)

* * 获取板子支持的网络接口 *

func GetEarliestProcessPID added in v0.6.5

func GetEarliestProcessPID(processName string) (int, error)

* * 取最老的那个进程Id *

func GetPortsListUnix added in v0.6.5

func GetPortsListUnix() ([]string, error)

func GetProcessPID added in v0.6.5

func GetProcessPID(processName string) (int, error)

* * pgrep rulex -> 38506\n *

func GetProcessStatus

func GetProcessStatus(pid int) (string, error)

func GetSystemTime

func GetSystemTime() (string, error)

* * 获取当前系统时间 *

func GetUptime

func GetUptime() (string, error)

func GetVolume

func GetVolume() (string, error)

*

  • 获取音量百分比 20% amixer get Master | grep 'Front Left:' | awk -F '[][]' '{print $2}'

*

func GetWlanList

func GetWlanList() ([]string, error)

-------------------------------------------------------------------------------------- 注意: 这些设置主要是针对RhinoH3 Ubuntu16.04 的,有可能在不同的发行版有不同的指令,不一定通用 !!!! Warning: MUST RUN WITH SUDO or ROOT USER !!!! --------------------------------------------------------------------------------------

* * 专门针对H3的一些系统指令封装 *

func IsIPRangeValid

func IsIPRangeValid(startIPStr, endIPStr string) bool

func IsIPv4InDHCPRange

func IsIPv4InDHCPRange(ip, start, end string) bool

IsIPv4InDHCPRange 检查给定的 IPv4 地址是否在 DHCP 地址池范围内

func KillProcess added in v0.6.5

func KillProcess(processID int) error

* * kill -9 *

func MoveFile

func MoveFile(sourcePath, destPath string) error

* * 移动文件 *

func NewSysProcAttr

func NewSysProcAttr() *syscall.SysProcAttr

func ReInitForwardRule

func ReInitForwardRule(ifaceFrom, ifaceTo string) error

* * 重构ip table, 目前默认以Eth1 <--> 4G *

func ReadIfaceMacAddr added in v0.6.5

func ReadIfaceMacAddr(ifaceName string) (string, error)

* * 获取网卡的MAC地址 *

func Reboot

func Reboot() error

* * 直接重启Linux *

func ReloadDNS16

func ReloadDNS16() error

* * Ubuntu16.04 刷新DNS, *

func ReloadDNS18xx

func ReloadDNS18xx() error

* * Ubuntu18+ 刷新DNS, *

func RestartRulex added in v0.6.5

func RestartRulex() error

* * 重启, 依赖于守护进程脚本, 因此这个不是通用的 *

func RestoreIpTablesConfig

func RestoreIpTablesConfig() error

* * 恢复IP Table *

func SaveIpTablesConfig

func SaveIpTablesConfig() error

* * 保存IP Table *

func ScanWIFIWithNmcli

func ScanWIFIWithNmcli() ([]string, error)

* * WIFI 控制 nmcli device wifi rescan nmcli device wifi list

获取WIFI网卡: iw dev | awk '$1=="Interface"{print $2}' 扫描WIFI列表: iwlist wlx0cc6551c5026 scan | grep ESSID | awk -F: '{print $2}' | sed 's/"//g' *

func SendKillSignal

func SendKillSignal(pid int) error

* * 以后可能用于非 systemctl *

func SetSystemTime

func SetSystemTime(newTime string) error

* * 设置时间,格式为 "YYYY-MM-DD HH:MM:SS" *

func SetTimeZone

func SetTimeZone(timezone string) error

SetTimeZone 设置系统时区 timezone := "Asia/Shanghai"

func SetVolume

func SetVolume(v int) (string, error)

* amixer 设置音量, 输入参数是个数值, 每次增加或者减少1% * amixer set 'Line Out' 1 | grep 'Front Left:' | awk -F '[][]' '{print $2}' *

func StartRecoverProcess

func StartRecoverProcess()

* * 数据备份 *

func StartSoftRoute

func StartSoftRoute() error

* * 开启软路由 *

func StartUpgradeProcess

func StartUpgradeProcess()

* * 启用升级进程 *

func StopRulex

func StopRulex() error

* * Stop RULEX *

func StopSoftRoute

func StopSoftRoute() error

* * 关闭软路由 service dnsmasq stop service isc-dhcp-server stop *

func UnzipFirmware

func UnzipFirmware(zipFile, destDir string) error

* * 解压安装包 *

func UpdateTimeByNtp

func UpdateTimeByNtp() error

func WifiAlreadyConfig added in v0.6.6

func WifiAlreadyConfig(wifiSSIDName string) bool

*

  • 初始化 // 删除之前的连接 // if exists ${name} -> nmcli connection delete ${name} // 重新连接 // sudo nmcli dev wifi connect "ssid" password "password"

Types

type DeviceStatus

type DeviceStatus struct {
	DEVICE     string `json:"device"`
	TYPE       string `json:"type"`
	STATE      string `json:"state"`
	CONNECTION string `json:"connection"`
}

* rer@revb-h3:~$ nmcli device status

DEVICE           TYPE      STATE         CONNECTION
usb0             ethernet  connected     Wired connection 1
wlx0cc6551c5026  wifi      connected     AABBCC
eth1             ethernet  connected     eth1
eth0             ethernet  disconnected  --
lo               loopback  unmanaged     --

*

func GetCurrentNetConnection

func GetCurrentNetConnection() ([]DeviceStatus, error)

type DhcpLease

type DhcpLease struct {
	MacAddress string `json:"mac_address"`
	IpAddress  string `json:"ip_address"`
	Hostname   string `json:"hostname"`
}

func GetDhcpList

func GetDhcpList() ([]DhcpLease, error)

* rhino@RH-PI1:~$ cat /var/lib/dhcp/dhcpd.leases # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-4.3.5

authoring-byte-order entry is generated, DO NOT DELETE

    authoring-byte-order little-endian;
	lease 192.168.64.101 {
	  hardware ethernet a8:a1:59:2e:a2:d9;
	}
	lease 192.168.64.102 {
	  hardware ethernet a8:a1:59:2e:a2:d9;
	}

*

func (DhcpLease) JsonString

func (v DhcpLease) JsonString() string

type EtcNetworkConfig

type EtcNetworkConfig struct {
	Interface   string   `json:"interface"`
	Address     string   `json:"address"`
	Netmask     string   `json:"netmask"`
	Gateway     string   `json:"gateway"`
	DNS         []string `json:"dns"`
	DHCPEnabled bool     `json:"dhcp_enabled"`
}

func (*EtcNetworkConfig) GenEtcConfig

func (iface *EtcNetworkConfig) GenEtcConfig() string

* * 将结构体写入配置文件 sudo systemctl restart networking sudo ossupport networking restart *

func (*EtcNetworkConfig) JsonString

func (nc *EtcNetworkConfig) JsonString() string

type EthInterface

type EthInterface struct {
	Eth0 HwPort `yaml:"eth0" json:"eth0"`
	Eth1 HwPort `yaml:"eth1" json:"eth1"`
}

type HwPort

type HwPort struct {
	Dhcp4       *bool    `yaml:"dhcp4" json:"dhcp4"`
	Addresses   []string `yaml:"addresses" json:"addresses"`
	Gateway4    string   `yaml:"gateway4" json:"gateway4"`
	Nameservers []string `yaml:"nameservers" json:"nameservers"`
}

* Ubuntu 18 以后的版本才支持 /etc/netplan/01-netcfg.yaml network:

version: 2
renderer: networkd
ethernets:
  enp0s9:
    dhcp4: no
    addresses:
      - 192.168.121.221/24
    gateway4: 192.168.121.1
    nameservers:
        addresses: [8.8.8.8, 1.1.1.1]

*

读取Ip状态(静态/动态) yaml

type IscServerDHCPConfig

type IscServerDHCPConfig struct {
	Iface       string // 用来做子网的那个网卡的网卡名
	Ip          string // 用来做子网的那个网卡的IP地址
	Gateway     string // 用来做子网的那个网卡的网关
	Network     string // 用来做子网的那个网卡的网段
	Netmask     string // 用来做子网的那个网卡子网掩码
	IpPoolBegin string // DHCP IP地址池起始
	IpPoolEnd   string // DHCP IP地址池结束
	//------------------------------------
	// IP 路由方向, 默认 ETH1 透传到 4G
	//------------------------------------
	IfaceFrom string // 流量入口
	IfaceTo   string // 流量出口
}

*

subnet 192.168.64.0 netmask 255.255.255.0 {
   range 192.168.64.100 192.168.64.200;          # 开放的地址池
   option domain-name-servers 192.168.64.100;    # DNS域名服务器,如果没有就注释掉
   #option domain-name "internal.example.org";   # 域名
   option routers 192.168.64.100;                # 网关地址
   option broadcast-address 192.168.64.255;      # 广播地址
   default-lease-time 600;                       # 默认租期,单位:秒
   max-lease-time 7200;                          # 最大租期
}

*isc-dhcp-server 会加载 /etc/dhcp/dhcpd.conf 配置文件 这里需要注意一下,此处配置要和网卡的配置一致

type NetInterfaceInfo added in v0.6.6

type NetInterfaceInfo struct {
	Name string `json:"name"`
	Mac  string `json:"mac"`
	Addr string `json:"addr"`
}

func GetAvailableInterfaces added in v0.6.6

func GetAvailableInterfaces() ([]NetInterfaceInfo, error)

* * 获取网卡 *

type NetplanConfig

type NetplanConfig struct {
	Network Network `yaml:"network" json:"network"`
}

func (*NetplanConfig) ApplyEthConfig

func (nc *NetplanConfig) ApplyEthConfig() error

* * 将配置写入文件并且重启网卡 *

func (*NetplanConfig) FromYaml

func (nc *NetplanConfig) FromYaml(jsons string) error

func (*NetplanConfig) YAMLString

func (nc *NetplanConfig) YAMLString() string

type Network

type Network struct {
	Version   int          `yaml:"version" json:"version"`
	Renderer  string       `yaml:"renderer" json:"renderer"`
	Ethernets EthInterface `yaml:"ethernets" json:"ethernets"`
}

type TimeZoneInfo

type TimeZoneInfo struct {
	CurrentTimezone string `json:"currentTimezone"`
	NTPSynchronized string `json:"NTPSynchronized"`
}

* * 时区 *

func GetTimeZone

func GetTimeZone() (TimeZoneInfo, error)

type UnixCameraDevice added in v0.6.7

type UnixCameraDevice struct {
	Node     string `json:"node"`
	DeviceID string `json:"deviceId"`
	Name     string `json:"name"`
}

* * Unix摄像头 *

func GetUnixVideos added in v0.6.7

func GetUnixVideos() ([]UnixCameraDevice, error)

* * Unix 获取视频设备 *

type WLANInterface

type WLANInterface struct {
	Interface string `yaml:"-" json:"interface"`
	SSID      string `yaml:"-" json:"ssid"`
	Password  string `yaml:"-" json:"password"`
	Security  string `yaml:"-" json:"security"`
}

type WindowsCameraDevice added in v0.6.7

type WindowsCameraDevice struct {
	Node     string `json:"node"`
	DeviceID string `json:"deviceId"`
	Name     string `json:"name"`
}

* * Windows 获取视频设备 *

func GetWindowsVideos added in v0.6.7

func GetWindowsVideos() ([]WindowsCameraDevice, error)

* PS C:\Users> WMIC Path Win32_PnPEntity WHERE "Caption LIKE '%CAMERA%'" GET DeviceID,Name /FORMAT:CSV ------ Node,DeviceID,Name DESKTOP-EMD3M3C,USB\VID_1908&amp;PID_2311&amp;MI_00\6&amp;1666943&amp;0&amp;0000,USB2.0 PC CAMERA *

type WlanConfig

type WlanConfig struct {
	Wlan0 WLANInterface `yaml:"-" json:"wlan0"`
}

func (*WlanConfig) ApplyWlan0Config

func (nc *WlanConfig) ApplyWlan0Config() error

* * 将配置写入文件并且重启网卡 *

func (*WlanConfig) YAMLString

func (nc *WlanConfig) YAMLString() string

* * 专门配置WIFI *

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL