func (ovpn *OpenVPN) IsConnected() (result bool, err error) { o := *ovpn if !sys.FileExists(o.StatusFile) { err = errors.New("Status file " + o.StatusFile + " does not exist") return } content, err := ioutil.ReadFile(o.StatusFile) if err != nil { err = errors.New("Failed to read status file " + o.StatusFile + ": " + err.Error()) return } var t_lastupdate time.Time for _, line := range strings.Split(string(content), "\n") { t := strings.Split(line, ",") if t[0] == OVPN_STATUS_UPDATE { t_lastupdate, err = time.Parse("Mon Jan 2 15:04:05 2006", t[1]) if err != nil { err = errors.New("Failed to parse date: " + err.Error()) return } break } } result = time.Now().Sub(t_lastupdate) < (61 * time.Second) return }
func (ovpn *OpenVPN) Stop() (err error) { o := *ovpn if !sys.FileExists(o.PidFile) { return } pid, err := o.GetPid() if err != nil { return } proc, err := os.FindProcess(pid) if err != nil { err = errors.New("Failed to find running process for pid " + strconv.Itoa(pid) + ": " + err.Error()) return } proc.Kill() os.Remove(o.PidFile) os.Remove(o.StatusFile) o.Interface = network.Interface{} *ovpn = o return }
func (assh *AutoSSH) SetAgentVars() (err error) { a := *assh if !sys.FileExists(a.AgentFile) { err = errors.New(a.AgentFile + " does not exist") return } content, err := ioutil.ReadFile(a.AgentFile) if err != nil { err = errors.New("Failed to read agent vars file: " + err.Error()) return } for _, line := range strings.Split(string(content), "\n") { if strings.HasPrefix(line, "export ") { line = strings.Replace(line, "export ", "", -1) } t := strings.Split(line, "=") if len(t) != 2 { continue } k := t[0] v := strings.Replace(t[1], "\"", "", -1) os.Setenv(k, v) } return }
func (d Dhcpcd) RemoveLeaseFile() (err error) { lease_file := "/var/lib/dhcpcd/dhcpcd-" + d.Interface + ".lease" if sys.FileExists(lease_file) { os.Remove(lease_file) } return }
func (ovpn *OpenVPN) ReadConfig() (err error) { o := *ovpn if !sys.FileExists(o.ConfigFile) { err = errors.New("Cannot find" + o.ConfigFile) return } content, err := ioutil.ReadFile(o.ConfigFile) if err != nil { err = errors.New("Cannot read " + o.ConfigFile + ": " + err.Error()) return } for _, line := range strings.Split(string(content), "\n") { t := strings.Split(line, " ") switch t[0] { case OVPN_CFG_REMOTE: { o.Remote, _, err = net.ParseCIDR(t[1] + "/32") if err != nil { err = errors.New("Failed to parse remote: " + err.Error()) return } } case OVPN_CFG_PORT: { o.Port, err = strconv.Atoi(t[1]) if err != nil { err = errors.New("Failed to parse port: " + err.Error()) return } } case OVPN_CFG_DEVICE: { o.InterfaceName = t[1] } } } *ovpn = o return }
func (assh *AutoSSH) ReadConfig() (err error) { a := *assh pwd, err := sys.GetPasswdInfo(a.RunAs) if err != nil { return } a.ConfigFile = pwd.Homedir + "/.ssh/config" a.AgentFile = pwd.Homedir + "/.ssh/ssh-agent.vars" a.EchoPort = 50100 + (a.Id * 2) if !sys.FileExists(a.ConfigFile) { err = errors.New(a.ConfigFile + " does not exists") return } *assh = a return }
func (ovpn *OpenVPN) GetPid() (pid int, err error) { o := *ovpn if !sys.FileExists(o.PidFile) { return } content, err := ioutil.ReadFile(o.PidFile) if err != nil { err = errors.New("Cannot read pidfile " + o.PidFile + ": " + err.Error()) return } pid, err = strconv.Atoi(strings.Split(string(content), "\n")[0]) if err != nil { err = errors.New("Failed to convert pid to int: " + string(content) + ": " + err.Error()) pid = 0 return } return }
/* * Look in /sys/class/net/<interface>/type to see if this interface is * a loopback interface. Return if it is. Afterwards, look in * /sys/class/net/<interface>/device/class and check the pci class of the * device. If * this is "20000", it's an ethernet nic, if it's "28000", it's * a wireless nic. All other pci classes get flagged unknown. */ func (link *Link) GetType() (intf_type byte, err error) { var sys_file string l := *link if !l.HasLink() { if err = l.SetLinkStatus(LINK_UP); err != nil { err = errors.New("SetLinkStatus failed: " + err.Error()) return } } sys_file = "/sys/class/net/" + l.Interface.Name + "/type" content, err := ioutil.ReadFile(sys_file) if err != nil { return } value := string(content[0:3]) if value == LINK_LOOPBACK { intf_type = INTF_TYPE_LOOPBACK return } sys_file = "/sys/class/net/" + l.Interface.Name + "/tun_flags" if sys.FileExists(sys_file) { content, err = ioutil.ReadFile(sys_file) if err != nil { return } value = string(content[2:6]) if value == LINK_TAP { intf_type = INTF_TYPE_TAP return } } sys_file = "/sys/class/net/" + l.Interface.Name + "/device/class" if sys.FileExists(sys_file) { if content, err = ioutil.ReadFile(sys_file); err != nil { return } value = string(content[0:8]) if value == LINK_WIRELESS { intf_type = INTF_TYPE_WIRELESS return } else if value == LINK_ETHERNET { intf_type = INTF_TYPE_ETHERNET return } } sys_file = "/sys/class/net/" + l.Interface.Name + "/bonding/mode" if sys.FileExists(sys_file) { if content, err = ioutil.ReadFile(sys_file); err != nil { return } value = string(content[0:13]) if value == LINK_BONDING { intf_type = INTF_TYPE_BONDING return } } err = errors.New("Unknown interface type") return }