標籤:
package mainimport ("archive/tar""bufio""bytes""compress/gzip""flag""fmt""io""net/http""os""os/exec""regexp""sort""text/template""time")type ID_info map[string]stringvar Config *ID_infovar re *regexp.Regexpfunc main() {listen := flag.String("l", ":1789", "-l= 127.0.0.1:1789")config := flag.String("c", "cfg", "-c=cfg")flag.Parse()Config = parseconfig(*config)re, _ = regexp.Compile("[0-9]{7}$")http.Handle("/download/", http.FileServer(http.Dir("./")))http.HandleFunc("/", router)http.ListenAndServe(*listen, nil)}func router(w http.ResponseWriter, r *http.Request) {if r.URL.Path == "/" {M := template.New("")M.Parse(Index)M.Execute(w, Config.Sort())return}if s := re.FindString(r.URL.Path); len(s) != 0 {ip, x := Config.Map()[s]if x {if _, e := os.Stat(s); e == nil {if list := parse(r); len(list) > 0 {var tar_list []stringfor _, v := range list {desc := fmt.Sprintf("download/%s/%s_%s.log", s, time.Now().Format("20060102"), v)tar_list = append(tar_list, desc)Excute(ip, v, desc)}zip(s, tar_list)w.Write([]byte(fmt.Sprintf(":http://%s/download/%s/%s.tar.gz", r.Host, s, time.Now().Format("20060102"))))return}w.Write([]byte(package_page))} else {if list := parse(r); len(list) > 0 {os.MkdirAll(fmt.Sprintf("download/%s", s), 0644)var tar_list []stringfor _, v := range list {desc := fmt.Sprintf("download/%s/%s_%s.log", s, time.Now().Format("20060102"), v)tar_list = append(tar_list, desc)Excute(ip, v, desc)}zip(s, tar_list)w.Write([]byte(fmt.Sprintf(":http://%s/download/%s/%s.tar.gz", r.Host, s, time.Now().Format("20060102"))))return}w.Write([]byte(package_page))}return}w.Write([]byte(fmt.Sprintf("%s 不存在", s)))return}}func Excute(ip, processname, name string) {fmt.Printf("ton %s:/data/gamehome/server/%s/nohub.out %s\n", ip, processname, name)cmd := exec.Command("ton", fmt.Sprintf("%s:/data/gamehome/server/%s/nohub.out", ip, processname), name)cmd.Run()}func parseconfig(path string) *ID_info {File, err := os.Open(path)defer File.Close()if err != nil {panic(err)}result := make(ID_info)B := bufio.NewReader(File)for {line, _, e := B.ReadLine()if e != nil {if e == io.EOF {break}panic(e)}list := bytes.Split(line, []byte{58})result[string(list[0])] = string(list[1])}return &result}func (self *ID_info) Map() ID_info {return *self}func (self *ID_info) Sort() []string {var list []stringfor k, _ := range *self {list = append(list, k)}sort.Strings(list)return list}func parse(r *http.Request) []string {var list []stringfor k, _ := range r.URL.Query() {list = append(list, k)}return list}func zip(id string, list []string) {File, _ := os.OpenFile(fmt.Sprintf("download/%s/%s.tar.gz", id, time.Now().Format("20060102")), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)defer File.Close()Gzip := gzip.NewWriter(File)defer Gzip.Close()Tar := tar.NewWriter(Gzip)defer Tar.Close()for _, v := range list {T, err := os.Open(v)if err != nil {fmt.Printf("壓縮檔:%s\n", v)fmt.Println(err)continue}defer T.Close()head := new(tar.Header)Fileinfo, _ := T.Stat()head.Name = Fileinfo.Name()head.Size = Fileinfo.Size()head.Mode = int64(Fileinfo.Mode())head.ModTime = Fileinfo.ModTime()Tar.WriteHeader(head)io.Copy(Tar, T)}return}const Index = `<html> <title>檔案清單</title> <body> {{range .}}<a href="/{{.}}">{{.}}</a><br>{{end}}</body> </html>`const package_page = `<html><meta charset="utf-8"><body><form action="" method="get"> 選取需要拖取的日誌:<br /><br /><label><input name="1" type="checkbox" value="true" />Game </label> <label><input name="2" type="checkbox" value="true" />Gate </label> <label><input name="3" type="checkbox" value="true" />Mail </label> <label><input name="4" type="checkbox" value="true" />Fight</label> <label><input name="5" type="checkbox" value="true" />Fight2</label> <label><input name="5" type="checkbox" value="true" />Fight3</label> <br /><br /><input type="submit" value="打包下載"><br />打包需要後台執行,這個過程是阻塞的,時間可能會比較久</form> </body></html>`
代碼寫的很低效,時間特別短,為了實現功能而實現,有時間再最佳化代碼吧
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
用GO按老大要求寫了一個小工具,用他指令碼下載記錄檔,然後讓開發自己去下載