<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
)

// NOTEs from messing with iptables proxying:
// For external:
// iptables -A PREROUTING -t nat -p tcp -m tcp --dport 5555 -j REDIRECT --to-ports 49278
// For internal:
// iptables -A OUTPUT -t nat -p tcp -m tcp --dport 5555 -j REDIRECT --to-ports 49278
// To delete a rule, use -D rather than -A.

type Action bool

const (
	INSERT Action = true
	DELETE        = false
)

func CheckIPTables() error {
	return exec.Command("iptables", "-L").Run()
}

// Invoke one iptables command.
// Expects "iptables" in the path to be runnable with reasonable permissions.
func iptables(action Action, chain string, source, target int, ipAddress string) *exec.Cmd {
	var cmd *exec.Cmd

	switch action {
	case INSERT:
		cmd = exec.Command(
			"iptables", "--insert", chain, "1",
			"--table", "nat",
			"--protocol", "tcp",
			// Prevent redirection of packets already going to the container
			"--match", "tcp", "!", "--destination", ipAddress,
			// Prevent redirection of ports on remote servers
			// (i.e, don't make google:80 hit our container)
			"--match", "addrtype", "--dst-type", "LOCAL",
			"--dport", fmt.Sprint(source),
			"--jump", "REDIRECT",
			"--to-ports", fmt.Sprint(target))
	case DELETE:
		cmd = exec.Command(
			"iptables", "--delete", chain,
			"--table", "nat",
			"--protocol", "tcp",
			"--match", "tcp", "!", "--destination", ipAddress,
			"--match", "addrtype", "--dst-type", "LOCAL",
			"--dport", fmt.Sprint(source),
			"--jump", "REDIRECT",
			"--to-ports", fmt.Sprint(target))
	}<commit_msg>Pickup iptables if in $PWD<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"os/exec"
	"path/filepath"
)

var IPTablesPath = "iptables"

func init() {

	err := CheckIPTables()
	if err != nil {
		wd, err := os.Getwd()
		if err != nil {
			return
		}
		IPTablesPath = filepath.Join(wd, IPTablesPath)
	}

}

// NOTEs from messing with iptables proxying:
// For external:
// iptables -A PREROUTING -t nat -p tcp -m tcp --dport 5555 -j REDIRECT --to-ports 49278
// For internal:
// iptables -A OUTPUT -t nat -p tcp -m tcp --dport 5555 -j REDIRECT --to-ports 49278
// To delete a rule, use -D rather than -A.

type Action bool

const (
	INSERT Action = true
	DELETE        = false
)

func CheckIPTables() error {
	return exec.Command(IPTablesPath, "-L").Run()
}

// Invoke one iptables command.
// Expects "iptables" in the path to be runnable with reasonable permissions.
func iptables(action Action, chain string, source, target int, ipAddress string) *exec.Cmd {
	var cmd *exec.Cmd

	switch action {
	case INSERT:
		cmd = exec.Command(
			IPTablesPath, "--insert", chain, "1",
			"--table", "nat",
			"--protocol", "tcp",
			// Prevent redirection of packets already going to the container
			"--match", "tcp", "!", "--destination", ipAddress,
			// Prevent redirection of ports on remote servers
			// (i.e, don't make google:80 hit our container)
			"--match", "addrtype", "--dst-type", "LOCAL",
			"--dport", fmt.Sprint(source),
			"--jump", "REDIRECT",
			"--to-ports", fmt.Sprint(target))
	case DELETE:
		cmd = exec.Command(
			IPTablesPath, "--delete", chain,
			"--table", "nat",
			"--protocol", "tcp",
			"--match", "tcp", "!", "--destination", ipAddress,
			"--match", "addrtype", "--dst-type", "LOCAL",
			"--dport", fmt.Sprint(source),
			"--jump", "REDIRECT",
			"--to-ports", fmt.Sprint(target))
	}
{'fraction_non_alphanumeric': 0.17946454839658724, 'fraction_numerical': 0.01235657546337158, 'mean_word_length': 3.028436018957346, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6904205', 'n_tokens_mistral': 1340, 'n_tokens_neox': 1174, 'n_words': 377}