<commit_before>package judge

import (
	"encoding/json"
	"fmt"
	"log"
	"net"
	"net/http"
	"strings"
	"github.com/alekc/proxy"
)

var hostnameMarkers = []string{"cache",
	"squid",
	"proxy"}

var proxyHeaderMarkers = []string{"Client-Ip",
	"HTTP_CLIENT_IP",
	"FORWARDED",
	"FORWARDED-FOR",
	"FORWARDED-FOR-IP",
	"X-FORWARDED",
	"X-FORWARDED-FOR",
	"PROXY_CONNECTION",
	"Via",
	"X-Proxy-Id",
	"X-Bluecoat-Via",
}

func (self *Judge) Start() {
	http.HandleFunc("/", self.analyzeRequest)
	err := http.ListenAndServe(fmt.Sprintf(self.ListenAddress), nil) // set listen port
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

func (self *Judge) analyzeRequest(w http.ResponseWriter, req *http.Request) {
	//Debug Block
	self.debugLog(formatRequest(req))

	//set up markers
	showsRealIp := false
	showsProxyUsage := false

	result := proxy.NewJudgeTestResult()

	//if cloudflare is supported set the country
	if self.CloudFlareSupport {
		result.Country = req.Header.Get("Cf-Ipcountry")
	}

	//getRealIpFromPost
	result.RealIp = self.getRealIpFromPost(req)
	result.RemoteIp = self.getRemoteIp(req)

	//check hostnames for markers
	if msg := self.CheckReverse(result.RemoteIp.String()); len(msg) > 0 {
		showsProxyUsage = true
		result.AppendMessages(msg)
	}

	//normalize xforwardedFor removing cloudflare and trusted gateways
	self.normalizeXForwardedFor(req)

	//search our ip in all headers
	if msg := self.checkIpInHeaders(req, result.RealIp); len(msg) > 0 {
		showsRealIp = true
		result.AppendMessages(msg)
	}

	//check headers
	if msg := self.hasProxyHeaderMarkings(req); len(msg) > 0 {
		showsProxyUsage = true
		result.AppendMessages(msg)
	}

	//final judgement
	if showsRealIp {
		if showsProxyUsage {
			result.Type = 0
		} else {
			result.Type = 1
		}
	} else {
		if showsProxyUsage {
			result.Type = 2
		} else {
			result.Type = 3
		}
	}

	//todo: write json response to output<commit_msg>fixed wrong response in case we have not transmitted our real ip in post.<commit_after>package judge

import (
	"encoding/json"
	"fmt"
	"log"
	"net"
	"net/http"
	"strings"
	"github.com/alekc/proxy"
)

var hostnameMarkers = []string{"cache",
	"squid",
	"proxy"}

var proxyHeaderMarkers = []string{"Client-Ip",
	"HTTP_CLIENT_IP",
	"FORWARDED",
	"FORWARDED-FOR",
	"FORWARDED-FOR-IP",
	"X-FORWARDED",
	"X-FORWARDED-FOR",
	"PROXY_CONNECTION",
	"Via",
	"X-Proxy-Id",
	"X-Bluecoat-Via",
}

func (self *Judge) Start() {
	http.HandleFunc("/", self.analyzeRequest)
	err := http.ListenAndServe(fmt.Sprintf(self.ListenAddress), nil) // set listen port
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}

func (self *Judge) analyzeRequest(w http.ResponseWriter, req *http.Request) {
	//Debug Block
	self.debugLog(formatRequest(req))

	//set up markers
	showsRealIp := false
	showsProxyUsage := false

	result := proxy.NewJudgeTestResult()

	//if cloudflare is supported set the country
	if self.CloudFlareSupport {
		result.Country = req.Header.Get("Cf-Ipcountry")
	}

	//getRealIpFromPost
	result.RealIp = self.getRealIpFromPost(req)
	result.RemoteIp = self.getRemoteIp(req)

	//check hostnames for markers
	if msg := self.CheckReverse(result.RemoteIp.String()); len(msg) > 0 {
		showsProxyUsage = true
		result.AppendMessages(msg)
	}

	//normalize xforwardedFor removing cloudflare and trusted gateways
	self.normalizeXForwardedFor(req)

	//search our ip in all headers
	if result.RealIp != "" {
		if msg := self.checkIpInHeaders(req, result.RealIp); len(msg) > 0 {
			showsRealIp = true
			result.AppendMessages(msg)
		}
	}

	//check headers
	if msg := self.hasProxyHeaderMarkings(req); len(msg) > 0 {
		showsProxyUsage = true
		result.AppendMessages(msg)
	}

	//final judgement
	if showsRealIp {
		if showsProxyUsage {
			result.Type = 0
		} else {
			result.Type = 1
		}
	} else {
		if showsProxyUsage {
			result.Type = 2
		} else {
			result.Type = 3
		}
	}

	//todo: write json response to output
{'fraction_non_alphanumeric': 0.1377720870678617, 'fraction_numerical': 0.003585147247119078, 'mean_word_length': 3.9568527918781724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3653025', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1430, 'n_words': 314}