<commit_before>// +build linux

package route

import (
	"bufio"
	"net/http"
	"os"
	"strings"

	"github.com/docker/docker/pkg/log"
	"github.com/docker/docker/pkg/version"
	"github.com/hacking-thursday/sysd/mods"
)

func init() {
	log.Debugf("Initializing module...")
	mods.Register("GET", "/route", handler)
}

func handler(eng_ifce interface{}, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) (err error) {
	target_path := "/proc/net/route"
	result := []map[string]string{}
	header := []string{}
	var out []byte

	f, err := os.Open(target_path)
	scanner := bufio.NewScanner(f)
	i := 0
	for scanner.Scan() {
		i += 1
		line := scanner.Text()
		fields := strings.Fields(line)

		// 針對第一行處理
		if i == 1 {
			for _, v := range fields {
				header = append(header, v)
			}
			continue
		}

		row := map[string]string{}
		for ii := 0; ii < len(fields); ii++ {
			key := header[ii]
			val := fields[ii]
                        row[key] = val
		}
                result = append( result, row )
	}

	if out, err = mods.Marshal(r, result); err != nil {
		mods.HttpError(w, err)
		return
	}

	if _, err = w.Write(out); err != nil {
		mods.HttpError(w, err)
		return
	}
	return
}<commit_msg>將 IP 的值從 hex 轉換成 ip string.<commit_after>// +build linux

package route

import (
	"bufio"
	"encoding/hex"
	"fmt"
	"net/http"
	"os"
	"strings"

	"github.com/docker/docker/pkg/log"
	"github.com/docker/docker/pkg/version"
	"github.com/hacking-thursday/sysd/mods"
)

func init() {
	log.Debugf("Initializing module...")
	mods.Register("GET", "/route", handler)
}

func hex_to_ip(input string) string {
	a, _ := hex.DecodeString(input)
	return fmt.Sprintf("%v.%v.%v.%v", a[3], a[2], a[1], a[0])
}

func handler(eng_ifce interface{}, version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) (err error) {
	target_path := "/proc/net/route"
	result := []map[string]string{}
	header := []string{}
	var out []byte

	f, err := os.Open(target_path)
	scanner := bufio.NewScanner(f)
	i := 0
	for scanner.Scan() {
		i += 1
		line := scanner.Text()
		fields := strings.Fields(line)

		// 針對第一行處理
		if i == 1 {
			for _, v := range fields {
				header = append(header, v)
			}
			continue
		}

		row := map[string]string{}
		for ii := 0; ii < len(fields); ii++ {
			key := header[ii]
			val := fields[ii]
			if key == "Destination" || key == "Gateway" || key == "Mask" {
				val = hex_to_ip(val)
			}
			row[key] = val
		}
		result = append(result, row)
	}

	if out, err = mods.Marshal(r, result); err != nil {
		mods.HttpError(w, err)
		return
	}

	if _, err = w.Write(out); err != nil {
		mods.HttpError(w, err)
		return
	}
	return
}
{'fraction_non_alphanumeric': 0.177836016473231, 'fraction_numerical': 0.004492699363534257, 'mean_word_length': 3.142635658914729, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3555183', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1047, 'n_words': 240}