Ejemplo n.º 1
0
Archivo: route6.go Proyecto: novag/wok
func CRoute6(channel string, sender string, arg string) {
	status := 0

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "route6 %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "route6 %s\n", arg)

	for {
		res, err := bufio.NewReader(lg).ReadString('\n')
		if err == io.EOF {
			lg.Close()
			break
		}
		if strings.Contains(res, "via") {
			status = 1
			arg = res[:strings.Index(res, " ")]
			break
		}
	}
	if status == 1 {
		bot.Noticef(channel, "route6 %s: Network in table", arg)
	} else {
		bot.Noticef(channel, "route6 %s: Network not in table", arg)
	}
}
Ejemplo n.º 2
0
Archivo: ping.go Proyecto: novag/wok
func CPing(channel string, sender string, arg string) {
	status := 0
	var average float64

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "ping %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "ping %s\n", arg)

	buf := make([]byte, 1024)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])

	if strings.Contains(strbuf, "unreachable") {
		status = 1
	} else if strings.Contains(strbuf, "Time to live exceeded") {
		status = 2
	} else if strings.Contains(strbuf, "statistics") {
		re := regexp.MustCompile(`time=(\d+\.?\d*)`)
		matches := re.FindAllStringSubmatch(strbuf, -1)
		if matches != nil {
			for _, ms := range matches[1:] {
				fms, _ := strconv.ParseFloat(ms[1], 64)
				average += fms
			}
			average = average / float64(len(matches[1:]))
		} else {
			status = 3
		}
	} else {
		status = 4
	}
	if status == 1 {
		bot.Noticef(channel, "ping %s: Unreachable", arg)
	} else if status == 2 {
		bot.Noticef(channel, "ping %s: Time to live exceeded", arg)
	} else if status == 3 {
		bot.Noticef(channel, "ping %s: 100%% packet loss", arg)
	} else if status == 4 {
		bot.Noticef(channel, "ping %s: Unknown error occurred", arg)
	} else {
		bot.Noticef(channel, "ping %s: %.1f ms", arg, average)
	}
}
Ejemplo n.º 3
0
Archivo: trace.go Proyecto: novag/wok
func CTrace(channel string, sender string, arg string) {
	status := 0
	var hops int

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "traceroute %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "traceroute %s\n", arg)

	buf := make([]byte, 1024)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])

	if strings.Contains(strbuf, "unreachable") {
		status = 1
	} else if strings.Contains(strbuf, "Name or service") {
		status = 2
	} else if strings.Contains(strbuf, "traceroute to") {
		if strings.Contains(strbuf, "*") {
			status = 3
		}
		hops = len(strings.Split(strbuf, "\n")) - 2
	} else {
		status = 4
	}
	if status == 1 {
		bot.Noticef(channel, "traceroute %s: Unreachable", arg)
	} else if status == 2 {
		bot.Noticef(channel, "traceroute %s: Name or service not known", arg)
	} else if status == 4 {
		bot.Noticef(channel, "traceroute %s: Unknown error occurred", arg)
	} else {
		if hops == 1 {
			bot.Noticef(channel, "traceroute %s: 1 hop", arg)
		} else if hops == 30 {
			bot.Noticef(channel, "traceroute %s: 30+ hops", arg)
		} else if status == 3 {
			bot.Noticef(channel, "traceroute %s: %d hops (Timeouts occured)", arg, hops)
		} else {
			bot.Noticef(channel, "traceroute %s: %d hops", arg, hops)
		}
	}
}
Ejemplo n.º 4
0
Archivo: route6.go Proyecto: novag/wok
func Route6(channel string, sender string, arg string) {
	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Notice(sender, "Error: Please try again later")
		return
	}
	fmt.Fprintf(lg, "route6 %s\n", arg)

	reader := bufio.NewReader(lg)
	for i := 0; ; i++ {
		line, err := reader.ReadString('\n')
		if err == io.EOF || (strings.Contains(line, "via") && i > 0) {
			lg.Close()
			break
		}

		bot.Notice(sender, line)
	}
}
Ejemplo n.º 5
0
Archivo: trace.go Proyecto: novag/wok
func Trace(channel string, sender string, arg string) {
	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Notice(sender, "Error: Please try again later")
		return
	}
	fmt.Fprintf(lg, "traceroute %s\n", arg)

	reader := bufio.NewReader(lg)
	for {
		line, err := reader.ReadString('\n')
		if err == io.EOF {
			lg.Close()
			break
		}

		bot.Notice(sender, line)
	}
}
Ejemplo n.º 6
0
Archivo: whois.go Proyecto: novag/wok
func Whois(channel string, sender string, arg string) {
	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Notice(sender, "Error: Please try again later")
		return
	}
	fmt.Fprintf(lg, "whois %s\n", arg)

	buf := make([]byte, 3072)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])
	ilb := strings.LastIndex(strbuf, "% Information")
	if ilb == -1 {
		ilb = 0
	}
	for _, line := range strings.Split(strbuf[ilb:], "\n") {
		bot.Notice(sender, line)
		time.Sleep(1 * time.Second)
	}
}
Ejemplo n.º 7
0
Archivo: whois.go Proyecto: novag/wok
func CWhois(channel string, sender string, arg string) {
	status := 0

	lg, err := net.Dial("tcp", bot.GetLGServer())
	if err != nil {
		bot.Noticef(channel, "whois %s: Error", arg)
		return
	}
	fmt.Fprintf(lg, "whois %s\n", arg)

	buf := make([]byte, 3072)
	n, err := io.ReadFull(lg, buf)
	if err == io.EOF {
		lg.Close()
		return
	}
	strbuf := string(buf[:n])

	if strings.Contains(strbuf, "No match found") {
		status = 1
	} else if strings.Contains(strbuf, "dns/") {
		re_admin := regexp.MustCompile(`admin-c:\s+([\w-]+)`)
		re_status := regexp.MustCompile(`status:\s+([\w\s#:']+)`)
		admin_matches := re_admin.FindAllStringSubmatch(strbuf, -1)
		status_matches := re_status.FindAllStringSubmatch(strbuf, -1)
		if admin_matches != nil && status_matches != nil {
			bot.Noticef(channel, "whois %s: Admin-C: %s, Status: %s", arg, admin_matches[len(admin_matches)-1][1], status_matches[len(status_matches)-1][1])
			return
		} else {
			status = 2
		}
	} else if strings.Contains(strbuf, "inetnum/") {
		re_admin := regexp.MustCompile(`admin-c:\s+([\w-]+)`)
		re_status := regexp.MustCompile(`bgp-status:\s+([\w\s#:']+)`)
		admin_matches := re_admin.FindAllStringSubmatch(strbuf, -1)
		status_matches := re_status.FindAllStringSubmatch(strbuf, -1)
		if admin_matches != nil && status_matches != nil {
			bot.Noticef(channel, "whois %s: Admin-C: %s, BGP: %s", arg, admin_matches[len(admin_matches)-1][1], status_matches[len(status_matches)-1][1])
			return
		} else {
			status = 2
		}
	} else if strings.Contains(strbuf, "person/") {
		re_email := regexp.MustCompile(`e-mail:\s+([\w@\.-]+)`)
		re_contact := regexp.MustCompile(`contact:\s+(.+)`)
		email_matches := re_email.FindAllStringSubmatch(strbuf, -1)
		contact_matches := re_contact.FindAllStringSubmatch(strbuf, -1)
		if email_matches != nil && contact_matches == nil {
			bot.Noticef(channel, "whois %s: E-Mail: %s", arg, email_matches[len(email_matches)-1][1])
			return
		} else if contact_matches != nil && email_matches == nil {
			bot.Noticef(channel, "whois %s: Contact: %s", arg, contact_matches[len(contact_matches)-1][1])
			return
		} else if contact_matches != nil && email_matches != nil {
			bot.Noticef(channel, "whois %s: E-Mail: %s, Contact: %s", arg, email_matches[len(email_matches)-1][1], contact_matches[len(contact_matches)-1][1])
			return
		} else {
			status = 2
		}
	} else if strings.Contains(strbuf, "aut-num/") {
		re := regexp.MustCompile(`admin-c:\s+([\w-]+)`)
		matches := re.FindAllStringSubmatch(strbuf, -1)
		if matches != nil {
			bot.Noticef(channel, "whois %s: Admin-C: %s", arg, matches[len(matches)-1][1])
			return
		} else {
			status = 2
		}
	} else {
		status = 2
	}
	if status == 1 {
		bot.Noticef(channel, "whois %s: No match found", arg)
	} else if status == 2 {
		bot.Noticef(channel, "whois %s: No channel preview available, please query me", arg)
	}
}