package main import ( "archive/tar" "bufio" "bytes" "compress/gzip" "flag" "fmt" "io" "net/http" "os" "os/exec" "regexp" "sort" "text/template" "time" ) type ID_info map[string]string var Config *ID_info var re *regexp.Regexp func 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 []string for _, 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 []string for _, 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 []string for k, _ := range *self { list = append(list, k) } sort.Strings(list) return list } func parse(r *http.Request) []string { var list []string for 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>`
代码写的很低效,时间特别短,为了实现功能而实现,有时间再优化代码吧
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-29 16:12:58