<commit_before>package main

import (
	"crypto/sha256"
	"encoding/hex"
	"flag"
	"fmt"
	"io"
	"log"
	"log/syslog"
	"math/rand"
	"net/http"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"syscall"
	"time"

	"code.google.com/p/gcfg"
	"github.com/kballard/go-shellquote"
	"github.com/xorpaul/gencerts"
)

var start time.Time
var buildtime string
var mainCfgSection = make(map[string]string)
var commandsCfgSection = make(map[string]string)
var allowedHosts []string
var allowedPushHosts []string
var uploadDir string
var requestCounter int
var forbiddenRequestCounter int
var failedRequestCounter int
var nasty_metachars string = "|`&><'\"\\[]{};\\n"

// ConfigSettings contains the key value pairs from the config file
type ConfigSettings struct {
	main     map[string]string
	commands map[string]string
}

// CheckResult represent the result of an check script
type CheckResult struct {
	text       string
	returncode int
}

func httpHandler(w http.ResponseWriter, r *http.Request) {
	ip := strings.Split(r.RemoteAddr, ":")[0]
	method := r.Method
	rid := randSeq()
	Debugf(rid + "Incoming " + method + " request from IP: " + ip)

	allowed := false
	switch method {
	case "GET":
		for _, allowedHost := range allowedHosts {
			if ip == allowedHost {
				allowed = true
			}
		}
		if !allowed {
			forbiddenRequestCounter += 1
			log.Print(rid + "Incoming IP " + ip + " not in allowed_hosts config setting!")
			CheckResult{"Your IP " + ip + " is not allowed to query anything from me!", 3}.Exit(w)
			return
		}

		Debugf(rid + "Request path: " + r.URL.Path)

		reqParts := strings.Split(r.URL.Path[1:], "/")
		Debugf(rid + "Request path parts are: " + strings.Join(reqParts, " "))
		command := reqParts[0]
		cmdArguments := reqParts[1:len(reqParts)]
		Debugf(rid + "Found command arguments: " + strings.Join(cmdArguments, " "))
		if strings.ContainsAny(strings.Join(cmdArguments, ""), nasty_metachars) {
			forbiddenRequestCounter += 1
			log.Print(rid + "Command arguments are not allowed to contain any of: " + nasty_metachars)
			CheckResult{"Command arguments are not allowed to contain any of: " + nasty_metachars, 3}.Exit(w)
			return
		}

		if r.URL.Path == "/" {
			requestCounter += 1
			perfData := "|gorpe_uptime=" + strconv.FormatFloat(time.Since(start).Seconds(), 'f', 1, 64) + "s"
			perfData += " requests=" + strconv.Itoa(requestCounter)
			perfData += " forbiddenrequests=" + strconv.Itoa(forbiddenRequestCounter)
			perfData += " failedrequests=" + strconv.Itoa(failedRequestCounter)
			CheckResult{"GORPE version 1.0 Build time: " + buildtime + perfData, 0}.Exit(w)
			return
		}

		if _, ok := commandsCfgSection[command]; ok {
			argCount := strings.Count(commandsCfgSection[command], "$ARG$")
			Debugf(rid + "Found " + strconv.Itoa(len(cmdArguments)) + " command arguments in this command")
			if argCount > len(cmdArguments) {
				failedRequestCounter += 1
				log.Print(rid + "Too few command arguments! Expected " + strconv.Itoa(argCount) + " and found " + strconv.Itoa(len(cmdArguments)))
				CheckResult{"UNKNOWN: Too few command arguments! Expected " + strconv.Itoa(argCount) + " and found " + strconv.Itoa(len(cmdArguments)), 3}.Exit(w)
			} else {
				cmdString := commandsCfgSection[command]
				Debugf(rid + "Got command from config: " + cmdString)
				for _, arg := range cmdArguments {
					if arg != "" {
						cmdString = strings.Replace(cmdString, "$ARG$", arg, 1)
						Debugf(rid + "Replacing $ARG$ with " + arg + " resulting in " + cmdString)
					}<commit_msg>fix nasty meta characters detection in command arguments, add failedRequestCounter perfdata and general code clean up<commit_after>package main

import (
	"crypto/sha256"
	"encoding/hex"
	"flag"
	"fmt"
	"io"
	"log"
	"log/syslog"
	"math/rand"
	"net/http"
	"os"
	"os/exec"
	"strconv"
	"strings"
	"syscall"
	"time"

	"code.google.com/p/gcfg"
	"github.com/kballard/go-shellquote"
	"github.com/xorpaul/gencerts"
)

var start time.Time
var buildtime string
var mainCfgSection = make(map[string]string)
var commandsCfgSection = make(map[string]string)
var allowedHosts []string
var allowedPushHosts []string
var uploadDir string
var requestCounter int
var forbiddenRequestCounter int
var failedRequestCounter int
var nasty_metachars string = "|`&><'\"\\[]{};\\n"

// ConfigSettings contains the key value pairs from the config file
type ConfigSettings struct {
	main     map[string]string
	commands map[string]string
}

// CheckResult represent the result of an check script
type CheckResult struct {
	text       string
	returncode int
}

func httpHandler(w http.ResponseWriter, r *http.Request) {
	ip := strings.Split(r.RemoteAddr, ":")[0]
	method := r.Method
	rid := randSeq()
	Debugf(rid + "Incoming " + method + " request from IP: " + ip)

	allowed := false
	switch method {
	case "GET":
		for _, allowedHost := range allowedHosts {
			if ip == allowedHost {
				allowed = true
			}
		}
		if !allowed {
			forbiddenRequestCounter += 1
			log.Print(rid + "Incoming IP " + ip + " not in allowed_hosts config setting!")
			CheckResult{"Your IP " + ip + " is not allowed to query anything from me!", 3}.Exit(w)
			return
		}

		Debugf(rid + "Request path: " + r.URL.Path)

		reqParts := strings.Split(r.URL.Path[1:], "/")
		Debugf(rid + "Request path parts are: " + strings.Join(reqParts, " "))
		command := reqParts[0]
		cmdArguments := reqParts[1:len(reqParts)]
		Debugf(rid + "Found command arguments: " + strings.Join(cmdArguments, " "))
		if strings.ContainsAny(strings.Join(cmdArguments, ""), nasty_metachars) {
			forbiddenRequestCounter += 1
			log.Print(rid + "Command arguments are not allowed to contain any of: " + nasty_metachars)
			CheckResult{"Found nasty meta character in command arguments!", 3}.Exit(w)
			return
		}

		if r.URL.Path == "/" {
			requestCounter += 1
			perfData := "|gorpe_uptime=" + strconv.FormatFloat(time.Since(start).Seconds(), 'f', 1, 64) + "s"
			perfData += " requests=" + strconv.Itoa(requestCounter)
			perfData += " forbiddenrequests=" + strconv.Itoa(forbiddenRequestCounter)
			perfData += " failedrequests=" + strconv.Itoa(failedRequestCounter)
			CheckResult{"GORPE version 1.0 Build time: " + buildtime + perfData, 0}.Exit(w)
			return
		}

		if _, ok := commandsCfgSection[command]; ok {
			argCount := strings.Count(commandsCfgSection[command], "$ARG$")
			Debugf(rid + "Found " + strconv.Itoa(len(cmdArguments)) + " command arguments in this command")
			if argCount > len(cmdArguments) {
				failedRequestCounter += 1
				log.Print(rid + "Too few command arguments! Expected " + strconv.Itoa(argCount) + " and found " + strconv.Itoa(len(cmdArguments)))
				CheckResult{"UNKNOWN: Too few command arguments! Expected " + strconv.Itoa(argCount) + " and found " + strconv.Itoa(len(cmdArguments)), 3}.Exit(w)
			} else {
				cmdString := commandsCfgSection[command]
				Debugf(rid + "Got command from config: " + cmdString)
				for _, arg := range cmdArguments {
					if arg != "" {
						cmdString = strings.Replace(cmdString, "$ARG$", arg, 1)
						Debugf(rid + "Replacing $ARG$ with " + arg + " resulting in " + cmdString)
					}
{'fraction_non_alphanumeric': 0.13612565445026178, 'fraction_numerical': 0.00594311589075987, 'mean_word_length': 4.095890410958904, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 7, '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': '1934863', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2323, 'n_words': 600}