<commit_before>// server.go
package main

import (
	"fmt"
	"net"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"sync"
	"time"
)

func runServer(s *[]byte) {
	fmt.Println("fluffy-tribble")

	historyLength := 3
	ports := newPortList(s, knockSequenceLength, historyLength, portRangeHigh, portRangeLow)

	wg := &sync.WaitGroup{}
	events := make(chan net.Conn)
	var listeners [][]net.Listener = make([][]net.Listener, historyLength)

	updateListenState := func(intervalNum int64) error {
		ports.update(intervalNum)
		newListeners, err := openPorts(ports.current, events, wg)
		if err != nil {
			return err
		}
		closePorts(listeners[0])
		listeners = append(listeners[1:], newListeners)
		return nil
	}

	last := interval(time.Now())
	updateListenState(last + 1)

	ticktock := time.Tick(refreshInterval / 2)

	for true {
		select {
		case now := <-ticktock:
			if present := interval(now); present != last {
				err := updateListenState(present + 1)
				if err != nil {
					fmt.Println("Open ports failed.")
					fmt.Println(err)
				}
				last = present
			}

		case conn := <-events:
			laddr := conn.LocalAddr().String()
			portString := laddr[strings.LastIndex(laddr, ":")+1:]
			port, _ := strconv.Atoi(portString)
			if ports.checkFull(port) {
				//				fmt.Println("KNOCK SUCCEEDED")
				cmd := exec.Command(ftServerFile)
				cmd.Stderr = os.Stderr
				cmd.Stdout = os.Stdout
				err := cmd.Start()
				time.Sleep(100 * time.Millisecond)
				conn.Close()
				if err != nil {
					fmt.Fprint(os.Stderr, err)
				} else {
					err = cmd.Wait()
					if err != nil {
						fmt.Fprint(os.Stderr, err)
					}
				}
			} else {
				conn.Close()
			}
		}
	}
}<commit_msg>Give stdin to server script, set its cwd, and fork it, instead of running it in-line.<commit_after>// server.go
package main

import (
	"fmt"
	"net"
	"os"
	"os/exec"
	"os/user"
	"strconv"
	"strings"
	"sync"
	"time"
)

func runServer(s *[]byte) {
	fmt.Println("fluffy-tribble")

	historyLength := 3
	ports := newPortList(s, knockSequenceLength, historyLength, portRangeHigh, portRangeLow)

	wg := &sync.WaitGroup{}
	events := make(chan net.Conn)
	var listeners [][]net.Listener = make([][]net.Listener, historyLength)

	updateListenState := func(intervalNum int64) error {
		ports.update(intervalNum)
		newListeners, err := openPorts(ports.current, events, wg)
		if err != nil {
			return err
		}
		closePorts(listeners[0])
		listeners = append(listeners[1:], newListeners)
		return nil
	}

	last := interval(time.Now())
	updateListenState(last + 1)

	ticktock := time.Tick(refreshInterval / 2)

	for true {
		select {
		case now := <-ticktock:
			if present := interval(now); present != last {
				err := updateListenState(present + 1)
				if err != nil {
					fmt.Println("Open ports failed.")
					fmt.Println(err)
				}
				last = present
			}

		case conn := <-events:
			laddr := conn.LocalAddr().String()
			portString := laddr[strings.LastIndex(laddr, ":")+1:]
			port, _ := strconv.Atoi(portString)
			if ports.checkFull(port) {
				//				fmt.Println("KNOCK SUCCEEDED")
				cmd := exec.Command(ftServerFile)
				cmd.Stdin = os.Stdin
				cmd.Stderr = os.Stderr
				cmd.Stdout = os.Stdout
				me, _ := user.Current()
				cmd.Dir = me.HomeDir
				err := cmd.Start()
				time.Sleep(100 * time.Millisecond)
				if err != nil {
					fmt.Fprint(os.Stderr, err)
				} else {
					go cmd.Wait()
				}
			}
			conn.Close()
		}
	}
}
{'fraction_non_alphanumeric': 0.15187260395163668, 'fraction_numerical': 0.00707755824240637, 'mean_word_length': 3.1365853658536587, 'pattern_counts': {'":': 2, '<': 7, '<?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': '2577354', 'n_tokens_mistral': 1468, 'n_tokens_neox': 1229, 'n_words': 267}