当前位置: 代码迷 >> Web前端 >> 应用Go语言写个简单的小Web工具:SWF文件去除Security限制工具
  详细解决方案

应用Go语言写个简单的小Web工具:SWF文件去除Security限制工具

热度:129   发布时间:2012-08-30 09:55:54.0
使用Go语言写个简单的小Web工具:SWF文件去除Security限制工具
使用Go编写的一个简单的WebApp?,将提交上来的SWF中插入一个Security.allowDomain("*")代码。 --- 需要事先安装好swftools,并可通过命令行调用swfdump和swfcombine

项目地址:http://code.google.com/p/swfconserver/


package main
import (
    "fmt"
    "net/http"
    "io/ioutil"
    "path"
    "path/filepath"
    "os"
    "os/exec"
    "strings"
    "net/url"
    "html/template"
    "strconv"
)

var __DIR__,_=filepath.Abs(".")
var _,_=fmt.Println("Current Dir:"+__DIR__)
var SecuritySWF=path.Join(__DIR__,"Security.swf")
var indexHtml=path.Join(__DIR__,"index.html")
var indexHtmlContent,_=ioutil.ReadFile(indexHtml)
var postHtml=path.Join(__DIR__,"post.html")
var postHtmlTpl=template.Must(template.ParseFiles(postHtml))

type SwfInfo struct {
    Filename string
    Width string
    Height string
    Rate string
    Frames string
    Duration int
    TempFilename string
}

func main() {
    var port="8998"
    fmt.Printf("Usage:%s [port]\n",os.Args[0])
    if len(os.Args)>1 {
        port=os.Args[1]
    }
    http.HandleFunc("/",indexView)
    http.HandleFunc("/submit-swf/",submitView)
    http.HandleFunc("/download-swf/",downloadView)
    fmt.Printf("Serve on port %s.\n",port)

    err:=http.ListenAndServe(":"+port,nil)
    if err!=nil {
        fmt.Println("ListenAndServe Error:",err);
    }
}

func submitView(resp http.ResponseWriter,req *http.Request) {
    resp.Header()["Content-Type"]=[]string{"text/html; charset=UTF-8"}

    file,fileHeader,err:=req.FormFile("swf")
    if err!=nil  {
        fmt.Fprintf(resp,`<script>alert('请上传文件!\n%v');history.back();</script>`,err)
        return
    }
    ct,ctok:=fileHeader.Header["Content-Type"]
    if !ctok || ct[0]!="application/x-shockwave-flash"  {
        fmt.Fprint(resp,`<script>alert('请上传有效的SWF文件!\n');history.back();</script>`)
        return
    }
    tmpdir:=os.TempDir()
    tmpfile,err:=ioutil.TempFile(tmpdir,"uploaded-swf-")
    if err!=nil {
        fmt.Fprint(resp,`创建Temp文件出错!<br />%v`,err)
        return
    }
    filename:=fileHeader.Filename
    tmpfilePath:=tmpfile.Name()
    newFilePath:=tmpfilePath+"-generated.swf"
    swfdata,_:=ioutil.ReadAll(file)
    tmpfile.Write(swfdata)

    width,height,rate,frames:=getSwfInfo(tmpfilePath)
    cmd:=exec.Command("swfcombine","-a",SecuritySWF,tmpfilePath,"-X",width,
                        "-Y",height,"-o",newFilePath)
    cmd.Run()

    tf:=path.Base(newFilePath)
    iRate,_:=strconv.ParseFloat(rate,32)
    iFrames,_:=strconv.ParseFloat(frames,32)
    duration:=int(float32(iFrames/iRate)*1000)

    swfinfo:=&SwfInfo{
        Width:width,
        Height:height,
        Filename:filename,
        Rate:rate,
        Frames:frames,
        TempFilename:tf,
        Duration:duration,
    }

    postHtmlTpl.Execute(resp,swfinfo)


}

func downloadView(resp http.ResponseWriter,req *http.Request) {
    tmpfilename:=path.Base(req.FormValue("tf"))
    filename:=req.FormValue("filename")
    tmpdir:=os.TempDir()
    filePath:=path.Join(tmpdir,tmpfilename)
    result,err:=ioutil.ReadFile(filePath)
    if err!=nil {
        resp.Header()["Content-Type"]=[]string{"text/html; charset=UTF-8"}
        fmt.Fprintf(resp,`
            <script>
                alert("您要下载的SWF文件找不到,请重新提交SWF文件。\n%v");
                window.close();
                history.back();
            </script>
        `,err)
        return
    }
    //os.Remove(filePath) //不删除了就
    resp.Header()["Content-Type"]=[]string{"application/x-shockwave-flash"}
    resp.Header()["Content-Disposition"]=[]string{"attachment; filename=Converted-"+url.QueryEscape(filename)}
    resp.Write(result)
}

func getSwfInfo(swf string) (width,height,rate,frames string) {
    cmd:=exec.Command("swfdump",
                        "--width",
                        "--height",
                        "--rate",
                        "--frames",
                        swf)
    outputb,_:=cmd.Output()
    output:=strings.TrimSpace(string(outputb))
    outa:=strings.Split(output," ")
    if len(outa)==8 {
        return outa[1],outa[3],outa[5],outa[7]
    }
    return "0","0","0","0"
}

func indexView(resp http.ResponseWriter,req *http.Request) {
    resp.Header()["Content-Type"]=[]string{"text/html; charset=UTF-8"}
    resp.Write(indexHtmlContent)
}






http://www.jamcode.org/
  相关解决方案