Beispiel #1
0
func OpenVPNFactory(name string) (o OpenVPN, err error) {
	o = *new(OpenVPN)

	openvpn, err := sys.BinaryPrefix("openvpn")
	if err != nil {
		return
	}

	cfg_file, err := sys.ConfigPrefix("openvpn/" + name + ".conf")
	if err != nil {
		return
	}

	o.Name = name
	o.InterfaceName = ""
	o.Interface = network.Interface{}
	o.ConfigFile = cfg_file
	o.PidFile = "/var/run/openvpn-" + name + ".pid"
	o.StatusFile = "/var/run/openvpn-" + name + ".status"
	o.Remote = net.IP{}
	o.Port = 1900
	o.CmdOpenvpn = openvpn

	o.ReadConfig()

	return
}
Beispiel #2
0
func RAFactory(intf string) (r RA, err error) {
	ifconfig, err := sys.BinaryPrefix("ifconfig")
	if err != nil {
		return
	}

	r = RA{intf, ifconfig}
	return
}
Beispiel #3
0
func DhcpcdFactory(intf string) (d Dhcpcd, err error) {
	dhcpcd, err := sys.BinaryPrefix("dhcpcd")
	if err != nil {
		return
	}

	d = Dhcpcd{intf, false, false, dhcpcd}

	return
}
Beispiel #4
0
func ResolvConfFactory(intf string) (r ResolvConf, err error) {
	resolvconf, err := sys.BinaryPrefix("resolvconf")
	if err != nil {
		return
	}

	r = ResolvConf{intf, "", *new([]net.IP), resolvconf}

	return
}
Beispiel #5
0
func ServiceSetup() (err error) {
	service, err := sys.BinaryPrefix("service")
	if err != nil {
		err = errors.New("Failed to initialize Service: " + err.Error())
	}

	CmdService = service

	return
}
Beispiel #6
0
func SystemdSetup() (err error) {
	systemctl, err := sys.BinaryPrefix("systemctl")
	if err != nil {
		return
	}

	CmdSystemd = systemctl

	return
}
Beispiel #7
0
func LinkFactory(intf net.Interface) (l Link, err error) {
	l = *new(Link)

	ifconfig, err := sys.BinaryPrefix("ifconfig")
	if err != nil {
		return
	}

	l.Interface = intf
	l.CmdIfconfig = ifconfig

	return
}
Beispiel #8
0
func AutoSSHFactory(id int, name, runas string) (a AutoSSH, err error) {
	a = *new(AutoSSH)

	autossh, err := sys.BinaryPrefix("autossh")
	if err != nil {
		return
	}

	ssh, err := sys.BinaryPrefix("ssh")
	if err != nil {
		return
	}

	a.Id = id
	a.Name = name
	a.RunAs = runas
	a.CmdAutossh = autossh
	a.CmdSsh = ssh

	err = a.ReadConfig()

	return
}
Beispiel #9
0
/*
 * Send count ARP Request packet(s) to ipaddr using arping. Return true if
 * the return code of arping is zero, false otherwise.
 */
func Arping(ipaddr net.IP, intf net.Interface, count int) (up bool, latency float64, err error) {
	if ipaddr == nil {
		return
	}

	arping, err := sys.BinaryPrefix("arping")
	if err != nil {
		return
	}

	stdout, _, err := sys.Run(arping, "-I", intf.Name, "-c", strconv.Itoa(count), ipaddr.String())

	var tot_latency float64 = 0

	if err == nil {
		up = true

		for _, line := range stdout {
			if !strings.Contains(line, "bytes from") {
				continue
			}

			raw_latency := strings.Fields(line)[6]
			raw_latency = strings.Split(raw_latency, "=")[1]
			l, err := strconv.ParseFloat(raw_latency, 64)
			if err != nil {
				continue
			}

			tot_latency += (l / 1000)
		}
	}

	latency = (tot_latency / float64(count))

	return
}
Beispiel #10
0
/*
 * Send count icmp/ipv6-icmp packet(s) to ipaddr using fping. Return true if
 * the return code of fping is zero, false otherwise.
 */
func Fping(ipaddr net.IP, count int) (up bool, latency float64, err error) {
	var cmd string

	if ipaddr == nil {
		return
	}

	ip_len := len(ipaddr)
	if ip_len == net.IPv4len {
		cmd = "fping"
	} else if ip_len == net.IPv6len {
		cmd = "fping6"
	} else {
		err = errors.New("Unknown address length: " + strconv.Itoa(ip_len))
		return
	}

	fping, err := sys.BinaryPrefix(cmd)
	if err != nil {
		return
	}

	_, stderr, err := sys.Run(fping, "-q", "-c", strconv.Itoa(int(count)), ipaddr.String())
	if err == nil {
		up = true

		latency, err = strconv.ParseFloat(strings.Split(stderr[0], "/")[7], 64)
		if err != nil {
			err = errors.New("Error parsing float: " + strings.Split(stderr[0], "/")[7] + ": " + err.Error())
			up = false
			return
		}
	}

	return
}
Beispiel #11
0
/*
 * Send count ARP Request packet(s) to ipaddr using arping. Return true if
 * the return code of arping is zero, false otherwise.
 */
func Arping(ipaddr net.IP, intf_name string, count int) (up bool, latency float64, err error) {
	if ipaddr == nil {
		return
	}

	arping, err := sys.BinaryPrefix("arping")
	if err != nil {
		return
	}

	stdout, _, err := sys.Run(arping, "-I", intf_name, "-c", strconv.Itoa(count), "-w", "3", ipaddr.String())

	var tot_latency float64 = 0

	if err == nil {
		up = true

		for _, line := range stdout {
			if !strings.HasPrefix(line, "Unicast reply from") {
				continue
			}

			raw_latency := strings.Replace(strings.Split(line, " ")[6], "ms", "", -1)
			l, err := strconv.ParseFloat(raw_latency, 64)
			if err != nil {
				continue
			}

			tot_latency += l
		}
	}

	latency = (tot_latency / float64(count)) / 1000

	return
}