Beispiel #1
0
func ParseConf(f string) Bird {
	b, err := ioutil.ReadFile(f)
	chkErr(err)
	strs := strings.Split(string(b), "\n")
	strs = strs[:len(strs)-1]
	bird := make(Bird)
	var providerKey, edgeKey string
	var endEdge bool
	var rs []Route
	for _, v := range strs {
		if reProtocol.MatchString(v) {
			providerKey = reProtocol.FindStringSubmatch(v)[1]
			if _, ok := bird[providerKey]; !ok {
				edge := make(Edge)
				bird[providerKey] = edge
			}
			endEdge = false
		}
		if reEdge.MatchString(strings.ToLower(v)) {
			edgeKey = "edge" + reEdge.FindStringSubmatch(strings.ToLower(v))[1]
			rs = []Route{}
			bird[providerKey][edgeKey] = rs
		}
		if reRoute.MatchString(v) && !endEdge {
			block := reRoute.FindStringSubmatch(v)[1]
			ips := subnettool.GetAllIP(block)
			for _, ip := range ips {
				var r Route
				r.Ip = ip
				r.active = true
				if reRouteComment.MatchString(v) {
					r.active = false
				}
				token := subnettool.ParseIPInt(r.Ip)
				if token[3] != 255 && token[3] != 0 && token[3] != 1 && token[3] != 2 { // if ip is boardcast or swith
					rs = append(rs, r)
				}
			}
			rs = RemoveDup(rs)
			bird[providerKey][edgeKey] = rs
		}
		if reEndEdge.MatchString(strings.ToLower(v)) {
			endEdge = true
		}
	}
	// 	fmt.Println(bird)
	for _, v := range bird { // delete empty key map
		delete(v, "")
	}
	// 	j, _ := json.MarshalIndent(bird, "", "    ")
	return bird
}
Beispiel #2
0
func (this *IpInfo) getIP() {
	ips, err := net.InterfaceAddrs()
	if err != nil {
		panic(err)
	}
	this.Ip = Ip{}
	for _, v := range ips {
		ipstr := strings.Split(v.String(), "/")[0]
		ip := net.ParseIP(ipstr)
		if ip.To4() != nil {
			token := subnettool.ParseIPInt(ip.To4())
			if token[0] == 127 {
				continue
			} else if token[0] == 10 || token[0] == 192 || token[0] == 176 {
				this.Ip.PrivIps = append(this.Ip.PrivIps, ip)
			} else {
				this.Ip.PubIps = append(this.Ip.PubIps, ip)
			}
		}
	}
}
Beispiel #3
0
func getIP() []parsebird.Route {
	var routes []parsebird.Route
	ips, err := net.InterfaceAddrs()
	if err != nil {
		fmt.Println(err.Error())
	}
	for _, v := range ips {
		ipstr := strings.Split(v.String(), "/")[0]
		ip := net.ParseIP(ipstr)
		if ip.To4() != nil {
			token := subnettool.ParseIPInt(ip.To4())
			if token[0] == 127 || token[0] == 10 {
				continue
			}
			var r parsebird.Route
			r.Ip = ip
			routes = append(routes, r)
		}
	}
	return routes
}