3 changed files with 179 additions and 11 deletions
@ -0,0 +1,164 @@ |
|||||
|
package core |
||||
|
|
||||
|
import ( |
||||
|
"crypto/sha256" |
||||
|
"encoding/base64" |
||||
|
"encoding/json" |
||||
|
"fmt" |
||||
|
"io" |
||||
|
"net/http" |
||||
|
"os" |
||||
|
"path/filepath" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
// json 结构体
|
||||
|
type Response struct { |
||||
|
Status string `json:"status"` //状态
|
||||
|
Data FilesListJson `json:"data"` //目录下的文件
|
||||
|
Curdir string `json:"curdir"` // 扫描的目录
|
||||
|
WorksDir string `json:"workdir"` //监听目录
|
||||
|
} |
||||
|
|
||||
|
// 文件输出的结构
|
||||
|
type FileJson struct { |
||||
|
Fname string `json:"fname"` |
||||
|
Dirflag bool `json:"dirflag"` |
||||
|
Isbackup int `json:"isbackup"` |
||||
|
Fhash string `json:"hash"` // hash
|
||||
|
Fsize string `json:"size"` //文件大小, 输出带单位的大小
|
||||
|
} |
||||
|
|
||||
|
type FilesListJson struct { |
||||
|
Flist []FileJson `json:"list"` |
||||
|
} |
||||
|
|
||||
|
// 获取运行的路径
|
||||
|
var Gpath string |
||||
|
|
||||
|
// 遍历监视目录,发送到json中
|
||||
|
func SerInfo(w http.ResponseWriter, r *http.Request) { |
||||
|
// 监听的目录通过?p=的方式传入
|
||||
|
urlpath := r.URL.Query().Get("p") |
||||
|
// 防止逃逸,造成漏洞
|
||||
|
if strings.Contains(urlpath, "../") { |
||||
|
urlpath = "." |
||||
|
} |
||||
|
|
||||
|
// urlpath 进行base64 解码
|
||||
|
dsrpath := base64dec(urlpath) |
||||
|
// 监听的根目录
|
||||
|
realFilePath := filepath.Join(Gpath, dsrpath) |
||||
|
// 时间目录的情况
|
||||
|
fileInfo, err := os.Stat(realFilePath) |
||||
|
if err != nil { |
||||
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
||||
|
return |
||||
|
} |
||||
|
|
||||
|
// list json
|
||||
|
var flist FilesListJson |
||||
|
//针对目录的情况才输出
|
||||
|
// todo 如果是文件的话 暂时不处理
|
||||
|
if fileInfo.IsDir() { |
||||
|
// 遍历目录
|
||||
|
files, err := os.ReadDir(realFilePath) |
||||
|
if err != nil { |
||||
|
http.Error(w, err.Error(), http.StatusInternalServerError) |
||||
|
return |
||||
|
} |
||||
|
// 遍历
|
||||
|
for _, v := range files { |
||||
|
// fmt.Printf("rang v:%v\n", v)
|
||||
|
isbak := 0 |
||||
|
// 如果有 backup
|
||||
|
if strings.Contains(v.Name(), "backup") { |
||||
|
isbak = 1 |
||||
|
} |
||||
|
// 如果是文件的话,就计算hash和大小
|
||||
|
if v.IsDir() { |
||||
|
//While entry is A Directory
|
||||
|
flist.Flist = append(flist.Flist, FileJson{Fname: v.Name(), Dirflag: v.IsDir(), Isbackup: isbak, Fhash: "", Fsize: ""}) |
||||
|
} else { |
||||
|
// 计算文件的hash
|
||||
|
funame := filepath.Join(realFilePath, v.Name()) |
||||
|
fhash := CalacHash(funame) |
||||
|
// 文件大小
|
||||
|
// 获取文件大小(以字节为单位)
|
||||
|
sizeInBytes := fileInfo.Size() |
||||
|
sizeStr := fmt.Sprintf("%.2f KB", float64(sizeInBytes)/1024) |
||||
|
// output
|
||||
|
flist.Flist = append(flist.Flist, FileJson{Fname: v.Name(), Dirflag: v.IsDir(), Isbackup: isbak, Fhash: fhash, Fsize: sizeStr}) |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
// respone file list
|
||||
|
response := Response{ |
||||
|
Status: "success", |
||||
|
Curdir: urlpath, |
||||
|
WorksDir: Gpath, |
||||
|
Data: flist, |
||||
|
} |
||||
|
|
||||
|
// 开启跨域
|
||||
|
uCorsHadler(w, r) |
||||
|
json.NewEncoder(w).Encode(response) |
||||
|
} |
||||
|
|
||||
|
// 跨域函数
|
||||
|
func uCorsHadler(w http.ResponseWriter, r *http.Request) { |
||||
|
// 设置跨域响应头
|
||||
|
w.Header().Set("Access-Control-Allow-Origin", "*") |
||||
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS,PUT,DELETET") |
||||
|
// w.Header().Set("Access-Control-Allow-Headers", "Content-Type,Accept,Accept-Length,Accept-Encoding,X-XSRF-TOKEN,X-XSRF-TOKEN")
|
||||
|
w.Header().Set("Access-Control-Allow-Headers", "*") |
||||
|
//
|
||||
|
w.Header().Set("Content-Type", "application/json") |
||||
|
|
||||
|
// 如果是OPTIONS请求,返回200 OK
|
||||
|
if r.Method == "OPTIONS" { |
||||
|
// fmt.Printf("options is now \n")
|
||||
|
// w.WriteHeader(http.StatusOK)
|
||||
|
return |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// base64 encode
|
||||
|
// url safe
|
||||
|
func bas64end(str string) string { |
||||
|
// bdata:=
|
||||
|
return base64.URLEncoding.EncodeToString([]byte(str)) |
||||
|
} |
||||
|
|
||||
|
// base64 url safe uneconde
|
||||
|
func base64dec(bsstr string) string { |
||||
|
dedc, _ := base64.URLEncoding.DecodeString(bsstr) |
||||
|
return string(dedc) |
||||
|
} |
||||
|
|
||||
|
// 计算文件的hash
|
||||
|
func CalacHash(rfile string) string { |
||||
|
// 获取到真实地址
|
||||
|
//
|
||||
|
file, err := os.Open(rfile) |
||||
|
if err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
defer file.Close() |
||||
|
|
||||
|
// initlize hash object
|
||||
|
hash := sha256.New() |
||||
|
|
||||
|
// write file into hash object
|
||||
|
if _, err := io.Copy(hash, file); err != nil { |
||||
|
panic(err) |
||||
|
} |
||||
|
// get hash value
|
||||
|
hashBytes := hash.Sum(nil) |
||||
|
//converto to hash string
|
||||
|
hastString := fmt.Sprintf("%x", hashBytes) |
||||
|
return hastString |
||||
|
} |
||||
Loading…
Reference in new issue