<commit_before>package commands

import (
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strings"

	"github.com/phase2/rig/cli/util"
	"github.com/urfave/cli"
)

type Dns struct {
	BaseCommand
}

func (cmd *Dns) Commands() cli.Command {
	return cli.Command{
		Name:  "dns",
		Usage: "Start DNS services on the docker-machine",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:   "nameservers",
				Value:  "8.8.8.8:53",
				Usage:  "Comma separated list of fallback names servers.",
				EnvVar: "RIG_NAMESERVERS",
			},
		},
		Before: cmd.Before,
		Action: cmd.Run,
	}
}

func (cmd *Dns) Run(c *cli.Context) error {
	if cmd.machine.IsRunning() {
		cmd.out.Info.Println("Configuring DNS")
		cmd.ConfigureDns(cmd.machine, c.String("nameservers"))
		cmd.ConfigureRoutes(cmd.machine)
	} else {
		cmd.out.Error.Fatalf("Machine '%s' is not running.", cmd.machine.Name)
	}

	return nil
}

// Remove the host filter from the xhyve bridge interface
func (cmd *Dns) RemoveHostFilter(ipAddr string) {
	// #1: route -n get <machineIP> to find the interface name
	routeData, err := exec.Command("route", "-n", "get", ipAddr).CombinedOutput()
	if err != nil {
		cmd.out.Warning.Println("Unable to determine bridge interface to remove hostfilter")
		return
	}
	ifaceRegexp := regexp.MustCompile(`interface:\s(\w+)`)
	iface := ifaceRegexp.FindStringSubmatch(string(routeData))[1]

	// #2: ifconfig <interface name> to get the details
	ifaceData, err := exec.Command("ifconfig", iface).CombinedOutput()
	if err != nil {
		cmd.out.Warning.Println("Unable to determine member to remove hostfilter")
		return
	}
	memberRegexp := regexp.MustCompile(`member:\s(\w+)\s`)
	member := memberRegexp.FindStringSubmatch(string(ifaceData))[1]

	// #4: ifconfig <bridge> -hostfilter <member>
	util.StreamCommand(exec.Command("sudo", "ifconfig", iface, "-hostfilter", member))
}

func (cmd *Dns) ConfigureRoutes(machine Machine) {
	cmd.out.Info.Println("Setting up local networking (may require your admin password)")

	machineIp := machine.GetIP()
	bridgeIp := machine.GetBridgeIP()
	if runtime.GOOS == "windows" {
		exec.Command("runas", "/noprofile", "/user:Administrator", "route", "DELETE", "172.17.0.0").Run()
		util.StreamCommand(exec.Command("runas", "/noprofile", "/user:Administrator", "route", "-p", "ADD", "172.17.0.0/16", machineIp))

		// Delete this in version > 0.4.x
		exec.Command("runas", "/noprofile", "/user:Administrator", "route", "DELETE", "172.17.42.1").Run()
	} else {
		if machine.IsXhyve() {
			cmd.RemoveHostFilter(machine.GetIP())
		}
		exec.Command("sudo", "mkdir", "-p", "/etc/resolver").Run()
		exec.Command("bash", "-c", "echo \"nameserver "+bridgeIp+"\" | sudo tee /etc/resolver/vm").Run()
		exec.Command("sudo", "route", "-n", "delete", "-net", "172.17.0.0").Run()
		util.StreamCommand(exec.Command("sudo", "route", "-n", "add", "172.17.0.0/16", machineIp))

		// Delete this in version > 0.4.x
		exec.Command("sudo", "route", "-n", "delete", "-net", "172.17.42.1").Run()

		if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
			// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
			cmd.out.Verbose.Println("Restarting discoveryutil to flush DNS caches")
			util.StreamCommand(exec.Command("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
			util.StreamCommand(exec.Command("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
		} else {<commit_msg>removed deprecated route removal<commit_after>package commands

import (
	"os"
	"os/exec"
	"regexp"
	"runtime"
	"strings"

	"github.com/phase2/rig/cli/util"
	"github.com/urfave/cli"
)

type Dns struct {
	BaseCommand
}

func (cmd *Dns) Commands() cli.Command {
	return cli.Command{
		Name:  "dns",
		Usage: "Start DNS services on the docker-machine",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:   "nameservers",
				Value:  "8.8.8.8:53",
				Usage:  "Comma separated list of fallback names servers.",
				EnvVar: "RIG_NAMESERVERS",
			},
		},
		Before: cmd.Before,
		Action: cmd.Run,
	}
}

func (cmd *Dns) Run(c *cli.Context) error {
	if cmd.machine.IsRunning() {
		cmd.out.Info.Println("Configuring DNS")
		cmd.ConfigureDns(cmd.machine, c.String("nameservers"))
		cmd.ConfigureRoutes(cmd.machine)
	} else {
		cmd.out.Error.Fatalf("Machine '%s' is not running.", cmd.machine.Name)
	}

	return nil
}

// Remove the host filter from the xhyve bridge interface
func (cmd *Dns) RemoveHostFilter(ipAddr string) {
	// #1: route -n get <machineIP> to find the interface name
	routeData, err := exec.Command("route", "-n", "get", ipAddr).CombinedOutput()
	if err != nil {
		cmd.out.Warning.Println("Unable to determine bridge interface to remove hostfilter")
		return
	}
	ifaceRegexp := regexp.MustCompile(`interface:\s(\w+)`)
	iface := ifaceRegexp.FindStringSubmatch(string(routeData))[1]

	// #2: ifconfig <interface name> to get the details
	ifaceData, err := exec.Command("ifconfig", iface).CombinedOutput()
	if err != nil {
		cmd.out.Warning.Println("Unable to determine member to remove hostfilter")
		return
	}
	memberRegexp := regexp.MustCompile(`member:\s(\w+)\s`)
	member := memberRegexp.FindStringSubmatch(string(ifaceData))[1]

	// #4: ifconfig <bridge> -hostfilter <member>
	util.StreamCommand(exec.Command("sudo", "ifconfig", iface, "-hostfilter", member))
}

func (cmd *Dns) ConfigureRoutes(machine Machine) {
	cmd.out.Info.Println("Setting up local networking (may require your admin password)")

	machineIp := machine.GetIP()
	bridgeIp := machine.GetBridgeIP()
	if runtime.GOOS == "windows" {
		exec.Command("runas", "/noprofile", "/user:Administrator", "route", "DELETE", "172.17.0.0").Run()
		util.StreamCommand(exec.Command("runas", "/noprofile", "/user:Administrator", "route", "-p", "ADD", "172.17.0.0/16", machineIp))
	} else {
		if machine.IsXhyve() {
			cmd.RemoveHostFilter(machine.GetIP())
		}
		exec.Command("sudo", "mkdir", "-p", "/etc/resolver").Run()
		exec.Command("bash", "-c", "echo \"nameserver "+bridgeIp+"\" | sudo tee /etc/resolver/vm").Run()
		exec.Command("sudo", "route", "-n", "delete", "-net", "172.17.0.0").Run()
		util.StreamCommand(exec.Command("sudo", "route", "-n", "add", "172.17.0.0/16", machineIp))

		if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
			// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
			cmd.out.Verbose.Println("Restarting discoveryutil to flush DNS caches")
			util.StreamCommand(exec.Command("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
			util.StreamCommand(exec.Command("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist"))
		} else {
{'fraction_non_alphanumeric': 0.18430085061931056, 'fraction_numerical': 0.019101626622892106, 'mean_word_length': 4.3616, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, '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': '5730437', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2378, 'n_words': 533}