Implement a small program that reads /proc/net/tcp every 10 seconds and outputs any new connections.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.2 KiB

package main
import (
//"./timer"
"time"
"fmt"
"strings"
"regexp"
"io/ioutil"
"encoding/hex"
)
var tcp_data string = read_tcp_file("/proc/net/tcp")
func main() {
// How to split on space-delimited fields, then colon-delimited fields?
// How to turn this byte into a string?
// /proc/net/tcp hex addresses need to be reversed two-character-wise
// and then converted byte by byte (2 char at a time)
//fmt.Println(convert_address(reverse_string(reverse_bytes("0100007F"))))
// tensec := timer.New(10, "Second")
// onemin := timer.New(1, "Minute")
// tensec.Reset(
tensec := time.NewTimer(10 * time.Second)
onemin := time.NewTimer(1 * time.Minute)
// if !tensec.Stop() {
// fmt.Println("Done waiting 10 seconds!")
// }
fmt.Println("Waiting 10 seconds...")
<-tensec.C
tcp_rows(tcp_data)
fmt.Println("Waiting 1 minute...")
<-onemin.C
tcp_rows(tcp_data)
}
func check(e error) {
if e != nil {
panic(e)
}
}
func tcp_rows(connections_file string) {
regexpression := regexp.MustCompile(`[\r\n\t\v]+`)
tcp_lines := strings.Split(connections_file, "\n")
for _, line := range tcp_lines {
trim_line := strings.TrimSpace(line)
values := regexpression.Split(trim_line, -1)
// Debug
// fmt.Println("New Line ==========")
fmt.Println(values)
}
}
// Take a string, create a rune slice, and use a loop to reverse it character-wise.
func reverse_bytes(s string) string {
runes := []rune(s)
for i, j := len(runes)-2, len(runes)-1; j > 0; i, j = i-2, j-2 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func reverse_string(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
// Store data function
// Loads all fields and subfields into storage (memory? where?)
// State check function
// hash each line, use to determine new lines, convert to readable, and print
func convert_address(address string) []byte {
output, err := hex.DecodeString(address)
check(err)
return output
}
func read_tcp_file(connections_file_path string) string {
data, err := ioutil.ReadFile(connections_file_path)
check(err)
fmt.Println("Data file read successfully.\n")
return string(data)
}