|
|
|
@ -1,15 +1,11 @@ |
|
|
|
package core |
|
|
|
|
|
|
|
import ( |
|
|
|
"aufs/config" |
|
|
|
"aufs/util" |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"net" |
|
|
|
"net/http" |
|
|
|
"net/rpc/jsonrpc" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"os" |
|
|
|
"strings" |
|
|
|
"time" |
|
|
|
) |
|
|
|
@ -70,6 +66,8 @@ func SendZip(w http.ResponseWriter, r *http.Request) { |
|
|
|
RemoteAddr: remoteaddr[0], |
|
|
|
RemotePath: remotepath[0], |
|
|
|
} |
|
|
|
// 记录日志
|
|
|
|
afsLog(zipblock) |
|
|
|
|
|
|
|
// 从serip 切割出ip 和端口
|
|
|
|
splitip := strings.Split(serip[0], ":") |
|
|
|
@ -111,93 +109,31 @@ func SendZip(w http.ResponseWriter, r *http.Request) { |
|
|
|
json.NewEncoder(w).Encode(resp) |
|
|
|
} |
|
|
|
|
|
|
|
// 将文件打包成压缩包
|
|
|
|
func SendZip00(w http.ResponseWriter, r *http.Request) { |
|
|
|
r.ParseForm() |
|
|
|
// 选择文件,并生成zip包
|
|
|
|
// 文件
|
|
|
|
zipfarr := r.Form["sfiles"] |
|
|
|
|
|
|
|
// 服务器ip地址
|
|
|
|
serip := r.Form["serverip"] |
|
|
|
if serip[0] == "" { |
|
|
|
http.Error(w, "remote server ip is blank!", http.StatusInternalServerError) |
|
|
|
return |
|
|
|
// 自动升级的日志
|
|
|
|
func afsLog(zipblock ZipBlock) { |
|
|
|
// 年月日格式文件夹
|
|
|
|
year, month, day := time.Now().Date() |
|
|
|
logdir := fmt.Sprintf("%v/%v/%v", year, month, day) |
|
|
|
// 日志文件
|
|
|
|
logfile := fmt.Sprintf("%v/aufs.log", logdir) |
|
|
|
// 检查目录是否存在
|
|
|
|
if _, err := os.Stat(logdir); os.IsNotExist(err) { |
|
|
|
// 不存在就创建
|
|
|
|
os.MkdirAll(logdir, 0755) |
|
|
|
} |
|
|
|
|
|
|
|
tpath := "" |
|
|
|
// 选中的路径,可以为空
|
|
|
|
wtculpath := r.Form["curpath"] |
|
|
|
if wtculpath != nil { |
|
|
|
tpath = util.Base64dec(wtculpath[0]) |
|
|
|
// 检查日志文件是否存在
|
|
|
|
if _, err := os.Stat(logfile); os.IsNotExist(err) { |
|
|
|
// 不存在就创建
|
|
|
|
os.Create(logfile) |
|
|
|
} |
|
|
|
// 实际路径
|
|
|
|
realFilePath := filepath.Join(config.G.FilePath, tpath) |
|
|
|
|
|
|
|
// zip 文件名
|
|
|
|
zpFileName := "BIU_" + time.Now().Format("20060102_150405") + ".zip" |
|
|
|
|
|
|
|
// 创建zip 异步?
|
|
|
|
taskId := make(chan string) |
|
|
|
go func() { |
|
|
|
util.CompressToZip(zpFileName, realFilePath, zipfarr) |
|
|
|
taskId <- "arcok" |
|
|
|
// fmt.Fprintln(w, "create archive:", err)
|
|
|
|
}() |
|
|
|
// go util.CompressToZip(zpFileName, realFilePath, zipfarr)
|
|
|
|
fmt.Println("archive is createding...") |
|
|
|
|
|
|
|
// 当前运行的目录
|
|
|
|
|
|
|
|
// ZIP 文件的实际路径
|
|
|
|
ziprl := path.Join("./sync_zips/", zpFileName) |
|
|
|
|
|
|
|
// zip 创建成功后
|
|
|
|
rest := <-taskId |
|
|
|
// 有压缩包 才可以操作
|
|
|
|
if strings.EqualFold(strings.ToLower(rest), "arcok") { |
|
|
|
fmt.Println("archive is sending...") |
|
|
|
// 创建udp 渠道发送数据
|
|
|
|
message := fmt.Sprintf("%s%s%s", config.G.DeviceName, "|", "sender") |
|
|
|
UdpSendFile(serip[0], ziprl, zpFileName, message, w) |
|
|
|
} else { |
|
|
|
fmt.Println("archive is not exist!!!") |
|
|
|
} |
|
|
|
// 执行完 跳转到 首页
|
|
|
|
// http.Redirect(w, r, "/", http.StatusFound)
|
|
|
|
} |
|
|
|
|
|
|
|
// udp 模式发送文件
|
|
|
|
/* |
|
|
|
* serip 服务器ip, |
|
|
|
* absfilepath 发送文件的实际路径, |
|
|
|
* fname 文件名 |
|
|
|
* message 携带部分信息的消息 |
|
|
|
* http.ResponseWriter |
|
|
|
*/ |
|
|
|
func UdpSendFile(serip string, absfilepath string, fname string, message string, w http.ResponseWriter) { |
|
|
|
|
|
|
|
// 1、获取udp addr
|
|
|
|
remoteAddr, err := net.ResolveUDPAddr("udp", serip+":9099") |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
// 2、 监听端口
|
|
|
|
conn, err := net.DialUDP("udp", nil, remoteAddr) |
|
|
|
// 打开日志文件
|
|
|
|
f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
fmt.Printf("open log file faild %v", err) |
|
|
|
} |
|
|
|
defer conn.Close() |
|
|
|
defer f.Close() |
|
|
|
// 写入日志
|
|
|
|
logfmt := fmt.Sprintf("%v %v %v %v %v\n", time.Now().Format("2006-01-02 15:04:05"), zipblock.Basepath, zipblock.Curpath, zipblock.SelFile, zipblock.RemoteAddr, zipblock.RemotePath) |
|
|
|
f.WriteString(logfmt) |
|
|
|
|
|
|
|
// 3、在端口发送数据
|
|
|
|
// message := fmt.Sprintf("%s%s%s", config.G.DeviceName, "|", "sender")
|
|
|
|
// 向链接通道发送数据 数据包头
|
|
|
|
conn.Write([]byte(message)) |
|
|
|
// 发送文件
|
|
|
|
go func() { |
|
|
|
err := util.SendFiles(absfilepath, fmt.Sprintf("http://%s/rc", remoteAddr)) |
|
|
|
if err != nil { |
|
|
|
fmt.Printf("Send file to %s error: %s\n", remoteAddr, err) |
|
|
|
} |
|
|
|
}() |
|
|
|
} |
|
|
|
|