<commit_before>package main

import (
	"crypto/tls"
	"encoding/json"
	"github.com/streadway/amqp"
	"koding/fujin/fastproxy"
	"koding/fujin/proxyconfig"
	"koding/tools/config"
	"log"
	"net"
	"net/url"
	"os"
	// "sort"
	// "strconv"
	"strings"
	"time"
)

func init() {
	log.SetPrefix("fujin ")
}

type IncomingMessage struct {
	ProxyResponse *proxyconfig.ProxyResponse
}

var proxy proxyconfig.Proxy // this will be only updated whenever we receive a msg from kontrold
var proxyDB *proxyconfig.ProxyConfiguration
var amqpStream *AmqpStream
var start chan bool
var first bool = true

func main() {
	log.Printf("fujin proxy started ")
	start = make(chan bool)

	// open kontrol-daemon database connection
	var err error
	proxyDB, err = proxyconfig.Connect()
	if err != nil {
		log.Fatalf("proxyconfig mongodb connect: %s", err)
	}

	// register fujin instance to kontrol-daemon
	amqpStream = setupAmqp()
	log.Printf("register fujin to kontrold with uuid '%s'", amqpStream.uuid)
	amqpStream.Publish(buildProxyCmd("addProxy", amqpStream.uuid))
	log.Println("register command is send. waiting for response from kontrold...")
	go handleInput(amqpStream.input, amqpStream.uuid)

	select {
	case <-time.After(time.Second * 15):
		log.Fatalf("ERROR: no repsonse received from kontrold, aborting process.")
		os.Exit(1)
	case <-start: // wait until we got message from kontrold or exit via above chan
	}

	addHTTP, err := net.ResolveTCPAddr("tcp", ":"+config.HttpPort)
	if err != nil {
		log.Println(err)
		return
	}

	addHTTPS, err := net.ResolveTCPAddr("tcp", ":"+config.HttpsPort)
	if err != nil {
		log.Println(err)
		return
	}

	cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
	if err != nil {
		log.Println("https mode is disabled. please add cert.pem and key.pem files.")
	} else {
		log.Printf("https mode is enabled. serving at :%s ...", config.HttpsPort)
		go listenProxy(addHTTPS, &cert, amqpStream.uuid)
	}

	log.Printf("normal mode is enabled. serving at :%s ...", config.HttpPort)
	listenProxy(addHTTP, nil, amqpStream.uuid)
}

func listenProxy(localAddr *net.TCPAddr, cert *tls.Certificate, uuid string) {
	err := fastproxy.Listen(localAddr, cert, func(req fastproxy.Request) {
		var deaths int
		name, key := parseKey(req.Host)

		target := targetUrl(deaths, name, key)
		remoteAddr, err := net.ResolveTCPAddr("tcp", target.Host)
		if err != nil {
			log.Println(err)
			return
		}

		if err := req.Relay(remoteAddr); err != nil {
			log.Println(err)
			// req.Redirect("http://www.koding.com/notactive.html")
		}
	})

	if err != nil {
		log.Fatalf("FATAL ERROR: %s", err)
	}
}

func parseKey(host string) (string, string) {
	log.Println("HOST string", host)

	partsFirst := strings.Split(host, ".")
	firstSub := partsFirst[0]

	partsSecond := strings.Split(firstSub, "-")
	name := partsSecond[0]
	key := partsSecond[1]

	return name, key
}

func handleInput(input <-chan amqp.Delivery, uuid string) {
	for {
		select {
		case d := <-input:
			// log.Printf("got %dB message data: [%v] %s",
			// 	len(d.Body),
			// 	d.DeliveryTag,
			// 	d.Body)<commit_msg>We should improve parseKey<commit_after>package main

import (
	"crypto/tls"
	"encoding/json"
	"github.com/streadway/amqp"
	"koding/fujin/fastproxy"
	"koding/fujin/proxyconfig"
	"koding/tools/config"
	"log"
	"net"
	"net/url"
	"os"
	// "sort"
	// "strconv"
	"strings"
	"time"
)

func init() {
	log.SetPrefix("fujin ")
}

type IncomingMessage struct {
	ProxyResponse *proxyconfig.ProxyResponse
}

var proxy proxyconfig.Proxy // this will be only updated whenever we receive a msg from kontrold
var proxyDB *proxyconfig.ProxyConfiguration
var amqpStream *AmqpStream
var start chan bool
var first bool = true

func main() {
	log.Printf("fujin proxy started ")
	start = make(chan bool)

	// open kontrol-daemon database connection
	var err error
	proxyDB, err = proxyconfig.Connect()
	if err != nil {
		log.Fatalf("proxyconfig mongodb connect: %s", err)
	}

	// register fujin instance to kontrol-daemon
	amqpStream = setupAmqp()
	log.Printf("register fujin to kontrold with uuid '%s'", amqpStream.uuid)
	amqpStream.Publish(buildProxyCmd("addProxy", amqpStream.uuid))
	log.Println("register command is send. waiting for response from kontrold...")
	go handleInput(amqpStream.input, amqpStream.uuid)

	select {
	case <-time.After(time.Second * 15):
		log.Fatalf("ERROR: no repsonse received from kontrold, aborting process.")
		os.Exit(1)
	case <-start: // wait until we got message from kontrold or exit via above chan
	}

	addHTTP, err := net.ResolveTCPAddr("tcp", ":"+config.HttpPort)
	if err != nil {
		log.Println(err)
		return
	}

	addHTTPS, err := net.ResolveTCPAddr("tcp", ":"+config.HttpsPort)
	if err != nil {
		log.Println(err)
		return
	}

	cert, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
	if err != nil {
		log.Println("https mode is disabled. please add cert.pem and key.pem files.")
	} else {
		log.Printf("https mode is enabled. serving at :%s ...", config.HttpsPort)
		go listenProxy(addHTTPS, &cert, amqpStream.uuid)
	}

	log.Printf("normal mode is enabled. serving at :%s ...", config.HttpPort)
	listenProxy(addHTTP, nil, amqpStream.uuid)
}

func listenProxy(localAddr *net.TCPAddr, cert *tls.Certificate, uuid string) {
	err := fastproxy.Listen(localAddr, cert, func(req fastproxy.Request) {
		var deaths int
		name, key := parseKey(req.Host)
		if name == "homepage" {
			req.Redirect("http://example.com")
			return
		}

		target := targetUrl(deaths, name, key)
		remoteAddr, err := net.ResolveTCPAddr("tcp", target.Host)
		if err != nil {
			log.Println(err)
			return
		}

		if err := req.Relay(remoteAddr); err != nil {
			log.Println(err)
			req.Redirect("http://example.com")
		}
	})

	if err != nil {
		log.Fatalf("FATAL ERROR: %s", err)
	}
}

func parseKey(host string) (string, string) {
	log.Println("HOST string", host)
	counts := strings.Count(host, "-")
	log.Println("count string", counts)
	if counts == 0 {
		return "homepage", ""
	}

	partsFirst := strings.Split(host, ".")
	firstSub := partsFirst[0]

	partsSecond := strings.Split(firstSub, "-")
	name := partsSecond[0]
	key := partsSecond[1]

	return name, key
}

func handleInput(input <-chan amqp.Delivery, uuid string) {
	for {
		select {
		case d := <-input:
			// log.Printf("got %dB message data: [%v] %s",
			// 	len(d.Body),
			// 	d.DeliveryTag,
			// 	d.Body)
{'fraction_non_alphanumeric': 0.14691943127962084, 'fraction_numerical': 0.0030015797788309636, 'mean_word_length': 3.896365042536736, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '3074460', 'n_tokens_mistral': 2405, 'n_tokens_neox': 2297, 'n_words': 579}