package core import ( "aufs/util" "encoding/json" "fmt" "net/http" "net/rpc/jsonrpc" "path/filepath" "strings" ) type AuResp struct { Status int `json:"status"` Message string `json:"message"` Data map[string]interface{} `json:"data"` } // 文件操作 func Flist(w http.ResponseWriter, r *http.Request) { // 监视运行目录 wdir := r.URL.Query().Get("path") // 如果请求的path中有 ../ 需要删除 if strings.Contains(wdir, "../") { // 删除多余的../ wdir = strings.ReplaceAll(wdir, "../", "") // 正则替换多余的../ // wdir = regexp.MustCompile(`/{2,}`).ReplaceAllString(wdir, "/") } // 主机 srcip := r.URL.Query().Get("srcip") srcip = util.Base64dec(srcip) // 端口 sport := r.URL.Query().Get("sport") // sport = util.Base64dec(sport) // fmt.Printf("scip:%v", srcip) // fmt.Printf("wdir is %s\n", wdir) // 获取需要监听的服务器 // sc := r.Form.Get("srcip") // 测试JSONRPC service := fmt.Sprintf("%v:%v", srcip, sport) // fmt.Printf("service:%v", service) // service := "192.168.66.92:9098" client, err := jsonrpc.Dial("tcp", service) if err != nil { fmt.Fprintf(w, "jsonrpc dial faild %v", err) return } // 基础目录名称 basepath := "/www/wwwroot" // 拼装路径,并转位unix路径 newpath := filepath.ToSlash(filepath.Join(basepath, wdir)) // basepath = fmt.Sprintf("%v%v", basepath, wdir) // 调用远程方法 var reply string var args = Args{FilePath: newpath, Scope: "dir"} // var args = Args{"/www/wwwroot/fsc.com", "dir"} err = client.Call("FileRpcService.GetFilePath", args, &reply) // 执行完成后退出 defer client.Close() if err != nil { fmt.Fprintf(w, "jsonrpc call faild %v", err) return } // 输出内容 w.Header().Set("Content-Type", "application/json") // json.NewEncoder(w).Encode(dtm) // w.Write([]byte(reply)) var m map[string]interface{} err = json.Unmarshal([]byte(reply), &m) if err != nil { fmt.Println("json.Unmarshal err:", err) return } // 封装json resp := AuResp{ Status: 200, Message: "success", Data: m, } json.NewEncoder(w).Encode(resp) }