Example #1
0
//目前仅支持linux
func (a DeviceAddr) IpAddrDel() (err error) {
	if !kmgPlatform.IsLinux() {
		panic("[DeviceAddr.IpAddrDel] only support linux now")
	}
	one, _ := a.IPNet.Mask.Size()
	return kmgCmd.CmdString(fmt.Sprintf("ip addr del %s/%d dev %s", a.IP.String(), one, a.DevString)).Run()
}
Example #2
0
// 仅支持linux ipv4
func MustGetRouteTable() []*RouteRule {
	if !kmgPlatform.IsLinux() {
		panic("[MustGetRouteTable] only support linux now")
	}
	outputS := kmgCmd.MustCombinedOutput("netstat -nr")
	lineList := strings.Split(string(outputS), "\n")
	if len(lineList) < 2 {
		panic("[getRouteTable] len(lineList)<2 " + string(outputS))
	}
	output := []*RouteRule{}
	for i, line := range lineList[2:] {
		line = strings.TrimSpace(line)
		if line == "" {
			continue
		}
		part := strings.Fields(line)
		if len(part) < 4 {
			panic(fmt.Errorf("[getRouteTable] len(part)<4 lineNum:%d content:%s", i, string(outputS)))
		}
		thisRule := &RouteRule{
			Destination: part[0],
			Gateway:     part[1],
			Genmask:     part[2],
			Iface:       part[len(part)-1],
		}
		output = append(output, thisRule)
	}
	return output
}
Example #3
0
//目前仅支持linux
func GetCurrentDeviceAddr() (ipnets []DeviceAddr, err error) {
	if !kmgPlatform.IsLinux() {
		panic("[GetCurrentDeviceAddr] only support linux now")
	}
	out, err := kmgCmd.CmdString("ip addr").RunAndReturnOutput()
	if err != nil {
		return
	}
	return getCurrentDeviceAddrFromIPAddr(out)
}
Example #4
0
// 仅支持linux ipv4
// 返回nil表示没找到.
func MustGetDefaultRoute() *RouteRule {
	if !kmgPlatform.IsLinux() {
		panic("[MustGetDefaultRoute] only support linux now")
	}
	for _, route := range MustGetRouteTable() {
		if route.Genmask == "0.0.0.0" {
			return route
		}
	}
	return nil
}
Example #5
0
// 证实可用
func SetIpForwardOn() {
	if !kmgPlatform.IsLinux() {
		panic("[SetIpForwardOn] only support linux now")
	}
	kmgFile.MustWriteFile("/proc/sys/net/ipv4/ip_forward", []byte("1"))
	// 已经证实,多次写入不会出现任何问题.
	// TODO 正确解析/etc/sysctl.conf 如果后面又加一条 = 0 估计就挂了.
	if !bytes.Contains(kmgFile.MustReadFile("/etc/sysctl.conf"), []byte("\nnet.ipv4.ip_forward = 1")) {
		kmgFile.MustAppendFile("/etc/sysctl.conf", []byte("\nnet.ipv4.ip_forward = 1"))
	}
}
Example #6
0
func IsIpForwardOn() bool {
	if !kmgPlatform.IsLinux() {
		panic("[IsIpForwardOn] only support linux now")
	}
	b := kmgFile.MustReadFile("/proc/sys/net/ipv4/ip_forward")
	if bytes.Contains(b, []byte{'0'}) {
		return false
	}
	if bytes.Contains(b, []byte{'1'}) {
		return true
	}
	panic(fmt.Errorf("[IsIpForwardOn] unable to understand info in /proc/sys/net/ipv4/ip_forward %#v", b))
}
Example #7
0
// 返回nil表示没找到这个设备,或者这个设备上面没有ip
func MustGetFirstIPByNetDeviceName(devname string) net.IP {
	if !kmgPlatform.IsLinux() {
		panic("[GetFirstIPByNetDeviceName] only support linux now")
	}
	deviceAddrList, err := GetCurrentDeviceAddr()
	if err != nil {
		panic(err)
	}
	for _, deviceAddr := range deviceAddrList {
		if deviceAddr.DevString == devname {
			return deviceAddr.IP
		}
	}
	return nil
}
Example #8
0
//返回当前机器上面的所有ip列表.没有ip会报错
func MustGetCurrentIpList() (ipList []net.IP) {
	if !kmgPlatform.IsLinux() {
		panic("[MustGetCurrentIpList] only support linux now")
	}
	deviceAddrList, err := GetCurrentDeviceAddr()
	if err != nil {
		panic(err)
	}
	if len(deviceAddrList) == 0 {
		panic(errors.New("[MustGetCurrentIpList] do not find any ip address."))
	}
	ipList = make([]net.IP, len(deviceAddrList))
	for i, addr := range deviceAddrList {
		ipList[i] = addr.IP
	}
	return ipList
}
Example #9
0
func MustGetCurrentIpWithPortList(port uint16) (sList []string) {
	if !kmgPlatform.IsLinux() {
		panic("[MustGetCurrentIpWithPortList] only support linux now")
	}
	deviceAddrList, err := GetCurrentDeviceAddr()
	if err != nil {
		panic(err)
	}
	if len(deviceAddrList) == 0 {
		panic(errors.New("[MustGetCurrentIpList] do not find any ip address."))
	}
	sList = make([]string, 0, len(deviceAddrList))
	sPort := strconv.Itoa(int(port))
	for _, addr := range deviceAddrList {
		ones, size := addr.IPNet.Mask.Size()
		if ones == size { // 实践表明 不能监听这种子网只有一个ip的地址.
			continue
		}
		sList = append(sList, net.JoinHostPort(addr.IP.String(), sPort))
	}
	return sList
}