/*
		GNU GENERAL PUBLIC LICENSE
		Version 3, 29 June 2007


kyoketsu, a Client-To-Client Network Enumeration System
Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

*/

package main

import (
	"embed"
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"strconv"
	"strings"
	"sync"
	"time"

	kyoketsu "git.aetherial.dev/aeth/kyoketsu/pkg"
	"github.com/fatih/color"
	"github.com/manifoldco/promptui"
)

var licenseMsg = "\n	kyoketsu  Copyright (C) 2024  Russell Hrubesky, ChiralWorks Software LLC\n	This program comes with ABSOLUTELY NO WARRANTY; for details type `kyoketsu --license`\n	This is free software, and you are welcome to redistribute it\n	under certain conditions; type `kyoketsu --redist` for details.\n\n"

var redistMsg = "\n	This program is free software: you can redistribute it and/or modify\n	it under the terms of the GNU General Public License as published by\n	the Free Software Foundation, either version 3 of the License, or\n	(at your option) any later version.\n\n"

var licenseMsgLong = "\n		GNU GENERAL PUBLIC LICENSE\n		Version 3, 29 June 2007\n	Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>\n	Everyone is permitted to copy and distribute verbatim copies\n	of this license document, but changing it is not allowed.\n\n	kyoketsu, An HTTP Proxying framework for bypassing DNS Security\n	Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC\n\n	This program is free software: you can redistribute it and/or modify\n	it under the terms of the GNU General Public License as published by\n	the Free Software Foundation, either version 3 of the License, or\n	(at your option) any later version.\n\n	This program is distributed in the hope that it will be useful,\n	but WITHOUT ANY WARRANTY; without even the implied warranty of\n	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n	GNU General Public License for more details.\n\n	You should have received a copy of the GNU General Public License\n	along with this program.  If not, see <https://www.gnu.org/licenses/>.\n\n"

//go:embed banner.txt
var banner embed.FS

func main() {

	licenseInfo := flag.Bool("license", false, "Pass this flag to display license and warantee information.")
	redistInfo := flag.Bool("redist", false, "Pass this flag to display redistribution information.")
	interactive := flag.Bool("i", false, "Pass this flag to run in interactive mode, which will let you define network parameters")
	output := flag.String("output", "", "Pass path and filename to output the data too after the program exits.")
	flag.Parse()

	if *licenseInfo {
		fmt.Println(licenseMsgLong)
		os.Exit(0)
	}

	if *redistInfo {
		fmt.Println(redistMsg)
		os.Exit(0)
	}

	fmt.Println(licenseMsg)

	banner, err := banner.ReadFile("banner.txt")
	if err != nil {
		log.Fatal(err, "couldnt read from the embedded banner file!\n")
	}

	fmt.Printf("%+v\n", string(banner))
	scanned := make(chan kyoketsu.Host)
	outform := []kyoketsu.Host{}
	mu := &sync.Mutex{}
	go func() {
		green := color.New(color.FgHiGreen).Add(color.Bold)

		for x := range scanned {
			if len(x.ListeningPorts) > 0 {
				green.Printf("Host found. IPv4 .... %s\n", x.IpAddress)
				green.Printf("Domain name .... %s\n", x.Fqdn)
				green.Printf("Listening Ports .... %v\n\n", x.ListeningPorts)
				if *output != "" {
					if mu.TryLock() {
						outform = append(outform, x)
						mu.Unlock()
					}
				}
			}
		}
	}()

	if *interactive {
		localAddr, err := kyoketsu.RetrieveLocalAddresses()
		if err != nil {
			log.Fatal(err)
		}

		searcher := func(input string, index int) bool {
			address := localAddr.Choice[index]
			return strings.Contains(address.NetworkAddress, input)
		}

		prompt := promptui.Select{
			Label:     "Visible networks on current host:",
			Items:     localAddr.Choice,
			Templates: kyoketsu.TuiTemplate(),
			Size:      len(localAddr.Choice),
			Searcher:  searcher,
		}
		choice, _, err := prompt.Run()
		if err != nil {
			log.Fatal(err)
		}

		targetNet := fmt.Sprintf("%s/%s", localAddr.Choice[choice].HostAddress, localAddr.Choice[choice].Cidr)
		subnet, err := kyoketsu.GetNetworkAddresses(targetNet)
		if err != nil {
			log.Fatal(err)
		}
		kyoketsu.NetSweep(subnet.Ipv4s, subnet.Mask, kyoketsu.RetrieveScanDirectives(), scanned)
		os.Exit(0)
	} else {
		red := color.New(color.FgRed)
		boldRed := red.Add(color.Bold)
		ipValidate := func(input string) error {
			if net.ParseIP(input).To4() == nil {
				return errors.New(boldRed.Sprint("Please pass a valid IPv4 Address"))
			}
			return nil
		}

		prompt := promptui.Prompt{
			Label:    "Enter IP address",
			Validate: ipValidate,
		}
		ip, err := prompt.Run()
		if err != nil {
			log.Fatal(err)
		}
		cidrValidate := func(input string) error {
			cidrInt, err := strconv.Atoi(strings.TrimPrefix(input, "/"))
			if err != nil {
				return err
			}
			if cidrInt < 12 {
				return errors.New(boldRed.Sprint("Woah there partner, that's a pretty zesty IP range. Maybe try a smaller block?"))
			}
			if cidrInt > 32 {
				return errors.New(boldRed.Sprint("That was a bad CIDR prefix. Careful not to skill issue yourself."))
			}
			return nil
		}
		prompt = promptui.Prompt{
			Label:    "Enter corresponding CIDR notation",
			Validate: cidrValidate,
		}
		mask, err := prompt.Run()
		if err != nil {
			log.Fatal(err)
		}
		targetNet := fmt.Sprintf("%s/%s", ip, strings.TrimPrefix(mask, "/"))
		subnet, err := kyoketsu.GetNetworkAddresses(targetNet)
		if err != nil {
			log.Fatal(err)
		}
		start := time.Now()
		kyoketsu.NetSweep(subnet.Ipv4s, subnet.Mask, kyoketsu.RetrieveScanDirectives(), scanned)

		fmt.Printf("Time elapsed: %s", time.Since(start))
		if *output != "" {
			mu.Lock()
			b, err := json.Marshal(&outform)
			if err != nil {
				log.Fatal(err)
			}
			os.WriteFile(*output, b, os.ModePerm)
		}
		os.Exit(0)

	}

}
