Port scanner sharing implemented in GO language

  • 2020-05-10 18:20:15
  • OfStack


//GO language   Implement port scanning
    // defects
    //port  Can't be set to a global variable. Don't know how to set it
    //var l = list.New()   This is an array operation, not a message queue     Similar to the message queue function     // Realize the function
    // Implemented to generate IP Period of
    // Implement port scanning
    // Implement parameter pass in
    // Write the file locally
    //main.go 58.215.20.30 58.215.201.30 80
    // The file name start IP The end of the IP Scanning ports
    //QQ29295842   Hope to know more friends technical exchange
    //QQ Group of 367196336   go   golang WEB Security development
    package main     import (
        "container/list"
        "fmt"
        "net"
        "os"
        "strconv"
        "strings"
        "time"
    )     func ip2num(ip string) int {
        canSplit := func(c rune) bool { return c == '.' }
        lisit := strings.FieldsFunc(ip, canSplit) //[58 215 20 30]
        //fmt.Println(lisit)
        ip1_str_int, _ := strconv.Atoi(lisit[0])
        ip2_str_int, _ := strconv.Atoi(lisit[1])
        ip3_str_int, _ := strconv.Atoi(lisit[2])
        ip4_str_int, _ := strconv.Atoi(lisit[3])
        return ip1_str_int<<24 | ip2_str_int<<16 | ip3_str_int<<8 | ip4_str_int
    }     func num2ip(num int) string {
        ip1_int := (num & 0xff000000) >> 24
        ip2_int := (num & 0x00ff0000) >> 16
        ip3_int := (num & 0x0000ff00) >> 8
        ip4_int := num & 0x000000ff
        //fmt.Println(ip1_int)
        data := fmt.Sprintf("%d.%d.%d.%d", ip1_int, ip2_int, ip3_int, ip4_int)
        return data
    }     func gen_ip(Aip1 int, Aip2 int) {
        index := Aip1
        for index < Aip2 {
            //fmt.Println(num2ip(index))
            // The team , The pressure of stack
            ip_data := num2ip(index)
            //fmt.Println(ip_data)
            l.PushBack(ip_data)
            index++
        }
    }     func text_add(name string, data string) { // Writes data to a file    text_add("file2.txt", "qqqqqqqqqqqqqqqqqqqqqqq")
        f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0x644)
        if err != nil {
            panic(err)
        }
        defer f.Close()         _, err = f.WriteString(data)
        _, err = f.WriteString("\r\n")
        if err != nil {
            panic(err)
        }
    }     //text_add("file2.txt", "qqqqqqqqqqqqqqqqqqqqqqq")
    var l = list.New()     func socket_ip(host string, port string) bool {
        var (
            remote = host + ":" + port
        )         tcpAddr, _ := net.ResolveTCPAddr("tcp4", remote) // conversion IP format
        //fmt.Printf("%s", tcpAddr)
        conn, err := net.DialTCP("tcp", nil, tcpAddr) // Check to see if the connection was successful
        if err != nil {
            fmt.Printf("no==%s:%s\r\n", host, port)
            return false         }
        defer conn.Close()
        fmt.Printf("ok==%s:%s\r\n", host, port)
        return true
    }     func for_ip(port string) {
        now := time.Now()
        year, mon, day := now.UTC().Date()
        file_name := fmt.Sprintf("%d-%d-%d_%s", year, mon, day, port)
        for { // Infinite loop
            if l.Len() <= 0 {
                fmt.Println(" Jump out of the loop ")
                break //# Jump out of the
            }
            // Out of the team   Once upon a time to read
            i1 := l.Front()
            l.Remove(i1)
            IP, _ := i1.Value.(string)
            if socket_ip(IP, port) {
                //OK
                // Gets the current   Date as file name   In the IP Write in
                text_add(file_name+"_ok.txt", IP)
            } //else {
            //  text_add(file_name+"_no.txt", IP)
            // }             time.Sleep(time.Millisecond * 500) // nanoseconds
        }
    }     func main() {
        argsLen := len(os.Args)
        //fmt.Println(argsLen)
        if argsLen != 4 {
            fmt.Println("main.go 58.215.20.30 58.215.201.30 80")
        } else {
            gen_ip(ip2num(os.Args[1]), ip2num(os.Args[2]))
            for index := 0; index < 200; index++ {
                go for_ip(os.Args[3])
            }
            for {
                time.Sleep(1 * time.Second) // nanoseconds
            }         }
    }


Related articles: