jeudi 20 octobre 2016

Go Language: How can I load usernames from file?

I have this code:

package main

import "flag"
import "net"
import "fmt"
import "bufio"
import "bytes"
import "encoding/hex"
import "crypto/rand"
import "time"
import "os"
import "io"
import "sync"

func main() {
    fmt.Println("              ######  ##       ######    #######   ######")
    fmt.Println("                ##    ##       ##  ###   ##   ##     ##")
    fmt.Println("                ##    ##       ######    ##   ##     ##")
    fmt.Println("                ##    ##       ######    ##   ##     ##")
    fmt.Println("                ##    ##       ##  ###   ##   ##     ##")
    fmt.Println("                ##    #######  ######    #######     ##")
    fmt.Println("               *** TLBot by TakinTu (Minecraft 1.7.x) ***")
    fmt.Println("-")
    fmt.Println("                * Polacz sie z serwerem  0.0.0.0:25588 *")
    proxy := flag.String("proxy", "127.0.0.1:25588", "proxy address (client is connecting to it)")
    target := flag.String("target", "127.0.0.1:25565", "target server address")
    count := flag.Int("count", 10, "amount of bots to be connected")
    joindelay := flag.Int("joind", 0, "timeout between bot joins in milliseconds")
    errdelay := flag.Int("errd", 4100, "timeout in milliseconds if client was kicked while connecting")
    eeldelay := flag.Int("eeld", 100, "timeout between bots' actions (snake effect)")
    flag.Parse()

    listener, err := net.Listen("tcp", *proxy)
    if err != nil {
        fmt.Println("## Error starting proxy:", err.Error())
        os.Exit(1)
    }

    var conn net.Conn
    var reader *bufio.Reader
    var writer *bufio.Writer
    var protocolVersion int32
    packetbuf := new(bytes.Buffer)
    fmt.Println("-")
    fmt.Println("             ** ## Czekam na polaczenie sie z serwerem ## **")
    for {
        conn, err = listener.Accept()
        if err != nil {
            fmt.Println("# Error accepting client:", err.Error())
            conn.Close()
            continue
        }
        reader = bufio.NewReader(conn)
        writer = bufio.NewWriter(conn)

        // C->S 0x00 Handshake
        id, err := readHeader(reader)
        if err != nil {
            fmt.Println("# Error receiving from client:", err.Error())
            conn.Close()
            continue
        }
        if id != 0x00 {
            fmt.Println("# Error receiving from client: unexpected packet")
            conn.Close()
            continue
        }
        protocolVersion, _ = readVarInt(reader)
        readVarString(reader, 64)
        readShort(reader)
        if next, err := readVarInt(reader); next != 2 {
            if err != nil {
                fmt.Println("# Error receiving from client:", err.Error())
                conn.Close()
                continue
            }
            fmt.Println("-")
            fmt.Println("                   ** --==Gotowy Do Polaczenia==-- **")
            go handleStatus(conn, reader, writer, protocolVersion, *count)
            continue
        }

        // C->S Login start
        id, err = readHeader(reader)
        if err != nil {
            fmt.Println("# Error receiving from client:", err.Error())
            conn.Close()
            continue
        }
        if id != 0x00 {
            fmt.Println("# Error receiving from client: unexpected packet")
            conn.Close()
            continue
        }
        readVarString(reader, 64)
        break
    }

    // S->C Login success
    writeVarInt(packetbuf, 0x02)
    writeVarString(packetbuf, "069a79f4-44e9-4726-a5be-fca90e38aaf5")
    writeVarString(packetbuf, "eel")
    if err = writePacketBuf(writer, packetbuf); err != nil {
        fmt.Println("## Error sending to client: " + err.Error())
        os.Exit(1)
    }

    keepAliveStop := make(chan int)
    keepAliveStopped := make(chan int)
    go keepAlive(writer, keepAliveStop, keepAliveStopped, true)

    var firstReader *bufio.Reader // one of bots is client (main bot)
    otherWriters := make([]*bufio.Writer, *count)

    fmt.Println("-")
    fmt.Println("                ** --==TTLBot Laczy sie z serwerem==-- **")
    fmt.Println("-")
    for i := 0; i < *count; i++ {
        nick := randomNick()
        fmt.Printf("##Laczenie[%d] Nick: %s\n", i+1, nick)
        other, err := net.Dial("tcp", *target)
        if err != nil {
            fmt.Printf("# Error connecting %d: %s\n", i+1, err.Error())
            time.Sleep(time.Duration(*errdelay) * time.Millisecond)
            i--
            continue
        }
        otherWriter := bufio.NewWriter(other)

        // C->S Handshake
        writeVarInt(packetbuf, 0x00)
        writeVarInt(packetbuf, protocolVersion)
        writeVarString(packetbuf, "minecraft.net")
        writeShort(packetbuf, 25565)
        writeVarInt(packetbuf, 2)
        if err = writePacketBuf(otherWriter, packetbuf); err != nil {
            fmt.Printf("# Error connecting %d: %s\n", i+1, err.Error())
            time.Sleep(time.Duration(*errdelay) * time.Millisecond)
            other.Close()
            i--
            continue
        }

        // C->S Login start
        writeVarInt(packetbuf, 0x00)
        writeVarString(packetbuf, nick)
        if err = writePacketBuf(otherWriter, packetbuf); err != nil {
            fmt.Printf("# Error connecting %d: %s\n", i+1, err.Error())
            time.Sleep(time.Duration(*errdelay) * time.Millisecond)
            writeVarString(packetbuf, nick)
            other.Close()
            i--
            continue
        }

        otherReader := bufio.NewReader(other)

        // S->C Login success
        id, err := readHeader(otherReader)
        if err != nil {
            fmt.Printf("# Error connecting %d: %s\n", i+1, err.Error())
            time.Sleep(time.Duration(*errdelay) * time.Millisecond)
            other.Close()
            i--
            continue
        }
        if id != 0x02 {
            fmt.Printf("# Error connecting %d: unexpected packet\n", i+1)
            time.Sleep(time.Duration(*errdelay) * time.Millisecond)
            other.Close()
            i--
            continue
        }
        readVarString(otherReader, 64)
        _, err = readVarString(otherReader, 64)
        if err != nil {
            fmt.Printf("# Error connecting %d: %s\n", i+1, err.Error())
            time.Sleep(time.Duration(*errdelay) * time.Millisecond)
            other.Close()
            i--
            continue
        }

        if firstReader == nil {
            firstReader = otherReader
        }
        otherWriters[i] = otherWriter
        go keepAlive(otherWriter, keepAliveStop, keepAliveStopped, false)
        time.Sleep(time.Duration(*joindelay) * time.Millisecond)
    }

    firstWriter := otherWriters[0] // main bot
    otherWriters = otherWriters[1:]

    mutexes := make([]*sync.Mutex, len(otherWriters))
    for i := 0; i < len(mutexes); i++ {
        mutexes[i] = new(sync.Mutex)
    }

    // stopping async keep alive
    for i := 0; i < *count+1; i++ { // count of bots + client connected to proxy
        keepAliveStop <- 1
    }
    for i := 0; i < *count+1; i++ {
        <-keepAliveStopped
    }

    go func() {
        var buffer [2048]byte
        for {
            // read from main bot stream and write to client
            count, err := firstReader.Read(buffer[:])
            if err != nil {
                fmt.Println("# Error reading from main connection:", err.Error())
                os.Exit(1)
            }
            _, err = writer.Write(buffer[:count])
            if err != nil {
                fmt.Println("# Error writing to main connection:", err.Error())
                os.Exit(1)
            }
            err = writer.Flush()
            if err != nil {
                fmt.Println("# Error writing to main connection:", err.Error())
                os.Exit(1)
            }
        }
    }()

    eelDuration := time.Duration(*eeldelay) * time.Millisecond
    fmt.Println("## Redirecting!")
    for {
        // reading packet from client
        length, err := readVarInt(reader)
        if err != nil {
            fmt.Println("# Error reading from main connection:", err.Error())
            os.Exit(1)
        }
        buffer := make([]byte, length)
        _, err = io.ReadFull(reader, buffer)
        if err != nil {
            fmt.Println("# Error reading from main connection:", err.Error())
            os.Exit(1)
        }

        // writing packet to main bot
        writeVarInt(firstWriter, length)
        firstWriter.Write(buffer)
        firstWriter.Flush()

        // writing packet to other bots with timeout
        if *eeldelay > 0 {
            go func() {
                for i, otherWriter := range otherWriters {
                    time.Sleep(eelDuration)
                    mutexes[i].Lock()
                    writeVarInt(otherWriter, length)
                    otherWriter.Write(buffer)
                    otherWriter.Flush()
                    mutexes[i].Unlock()
                }
            }()
        } else {
            for _, otherWriter := range otherWriters {
                writeVarInt(otherWriter, length)
                otherWriter.Write(buffer)
                otherWriter.Flush()
            }
        }
    }
}

func randomNick() string {
    buf := make([]byte, 4)
    _, err := rand.Read(buf)
    if err != nil {
        panic(err.Error())
    }
    return hex.EncodeToString(buf)
}

func keepAlive(writer *bufio.Writer, stop, stopped chan int, quitOnErr bool) { // used to keep alive while connecting other bots
    ticker := time.NewTicker(time.Second)
    defer func() {
        ticker.Stop()
        stopped <- 1
    }()
    for i := int32(1); ; i++ {
        select {
        case <-ticker.C:
        case <-stop:
            return
        }
        // C->S 0x00 Keep alive
        writeByte(writer, 5) // packet length
        writeByte(writer, 0) // packet id
        writeInt(writer, i)  // keep alive id
        if err := writer.Flush(); quitOnErr && err != nil {
            fmt.Println("## Pakiet wysylany przez ciebie jest zbyt duzy")
            fmt.Println("   ## Zresetuj Klient MC lub zmien versje ##")
            os.Exit(1)
        }
    }
}

And I want to load the usernames from the file with this funcion:

func randomNick() string {
    buf := make([]byte, 4)
    _, err := rand.Read(buf)
    if err != nil {
        panic(err.Error())
    }
    return hex.EncodeToString(buf)
}

This function creating random usernames but as I say I want to load the usernames from the file. Then use it here:

for i := 0; i < *count; i++ {
    nick := randomNick()
    fmt.Printf("##Laczenie[%d] Nick: %s\n", i+1, nick)
    other, err := net.Dial("tcp", *target)
    if err != nil {
        fmt.Printf("# Error connecting %d: %s\n", i+1, err.Error())
        time.Sleep(time.Duration(*errdelay) * time.Millisecond)
        i--
        continue
    }

I mean this line:

nick := randomNick()

If you have more question please ask me here I really need to create this.

Thanks!




Aucun commentaire:

Enregistrer un commentaire