首页 > 其他分享 >golang接收文件脚本

golang接收文件脚本

时间:2022-11-20 21:11:38浏览次数:36  
标签:脚本 http fmt args golang part Println path 接收

golang接收文件脚本

package main

import (
    "io"
    "os"
    "fmt"
    "io/ioutil"
    "net/http"
)
// https://www.jianshu.com/p/b49cc19d26f0     参考资料


func uploadHandler(w http.ResponseWriter, r *http.Request) {
    reader, err := r.MultipartReader()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    for {
        part, err := reader.NextPart()
        if err == io.EOF {
            break
        }

        fmt.Printf("FileName=[%s], FormName=[%s]\n", part.FileName(), part.FormName())
        if part.FileName() == "" {  // this is FormData
            data, _ := ioutil.ReadAll(part)
            fmt.Printf("FormData=[%s]\n", string(data))
        } else {    // This is FileData
            // dst, _ := os.Create("./" + part.FileName() + ".upload")
            dst, _ := os.Create("./" + part.FileName())
            defer dst.Close()
            io.Copy(dst, part)
        }
    }
}



func main() {
    var port string = "10442"
    var path_url string = "/uploads"

    args := os.Args
    // fmt.Println(len(args), args)
    if len(args) == 2 || args == nil {
        fmt.Println("======================Usage========================")
        fmt.Println("[*]  ./xx  port  url_path")
        fmt.Println("[*]  ./xx  8089  /admins")
        return
    }
    if len(args) == 3 {
        port = args[1]
        path_url = args[2]
    }
    // if args == nil {
    //     fmt.Println(args[2], path_url)
    //     port := "8888"
    //     path_url := "/uploads"
    // }



    fmt.Println("[*] Start receiving files ", port, path_url)
    var tmp_str string = port + path_url
    fmt.Println("[*] curl -F \"boundary=@C:\\Users\\test\\Desktop\\1.jpg\" http://127.0.0.1:" + tmp_str + " -v ")
    fmt.Println("[*] powershell \"$floc='C:\\Users\\test\\Desktop\\1.jpg';$wc=New-Object System.Net.Webclient;$res=$wc.UploadFile('http://127.0.0.1:" + tmp_str + "', $floc);\"")

    http.HandleFunc(path_url, uploadHandler)
    http.ListenAndServe(":"+port, nil)
}

package main

import (
    "io"
    "os"
    "fmt"
    "net"
    "io/ioutil"
    "net/http"
    "container/list"
)
// https://www.jianshu.com/p/b49cc19d26f0     参考资料


func uploadHandler(w http.ResponseWriter, r *http.Request) {
    reader, err := r.MultipartReader()
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    for {
        part, err := reader.NextPart()
        if err == io.EOF {
            break
        }

        fmt.Printf("FileName=[%s], FormName=[%s]\n", part.FileName(), part.FormName())
        if part.FileName() == "" {  // this is FormData
            data, _ := ioutil.ReadAll(part)
            fmt.Printf("FormData=[%s]\n", string(data))
        } else {    // This is FileData
            // dst, _ := os.Create("./" + part.FileName() + ".upload")
            dst, _ := os.Create("./" + part.FileName())
            defer dst.Close()
            io.Copy(dst, part)
        }
    }
}


func main() {
    var port string = "10442"
    var path_url string = "/uploads"

    args := os.Args
    // fmt.Println(len(args), args)
    if len(args) == 2 || args == nil {
        fmt.Println("======================Usage========================")
        fmt.Println("[*]  ./xx  port  url_path")
        fmt.Println("[*]  ./xx  8089  /admins")
        return
    }
    if len(args) == 3 {
        port = args[1]
        path_url = args[2]
    }
    // if args == nil {
    //     fmt.Println(args[2], path_url)
    //     port := "8888"
    //     path_url := "/uploads"
    // }
    addrs, err := net.InterfaceAddrs()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    listip := list.New()

    for _, address := range addrs {
        if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
            if ipnet.IP.To4() != nil {
                // fmt.Println(ipnet.IP.String())
                listip.PushBack(ipnet.IP.String())
            }
        }
    }



    fmt.Println("[*] Start receiving files ", port, path_url)
    var tmp_str string = port + path_url

    for i := listip.Front(); i != nil; i = i.Next() {
        //fmt.Println(i)
        //fmt.Println(reflect.TypeOf(i.Value))
        var ipaddress string = i.Value.(string)
        //fmt.Println(ipaddress)
        fmt.Println("[*] curl -F \"boundary=@C:\\Users\\test\\Desktop\\1.jpg\" http://"+ ipaddress +":" + tmp_str + " -v ")
        fmt.Println("[*] $floc='D:\\mm\\aaa.txt';$wc=New-Object System.Net.Webclient;$res=$wc.UploadFile('http://"+ ipaddress +":"+ tmp_str +"', $floc);")
    }

    http.HandleFunc(path_url, uploadHandler)
    http.ListenAndServe(":"+port, nil)
}

标签:脚本,http,fmt,args,golang,part,Println,path,接收
From: https://www.cnblogs.com/startstart/p/16909544.html

相关文章