-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b336151
Showing
7 changed files
with
1,733 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# robin-go | ||
|
||
一个用go写的非常简单的文件检查脚本。 | ||
|
||
*注意: windows系统需把 `lib/func.go` 所注释的部分代码去掉才能够正常编译,Linux 开箱即用就行了* | ||
|
||
## 功能 | ||
|
||
- #### 自动恢复检查目录 | ||
|
||
当遇到以下情况时,会进行文件替换(复原): | ||
|
||
- 文件/目录权限发生变化 | ||
- 文件/目录最后修改时间发生变化 | ||
- 文件/目录所有者发生变化(**仅限Linux**) | ||
- 文件内容发生变化 | ||
- 文件/目录被删除 | ||
|
||
当遇到以下情况时,会进行文件删除(需要时会恢复): | ||
|
||
- 文件/目录被更名 | ||
|
||
- 未被记录的文件/目录被添加 | ||
|
||
- #### 实时生成监控日志 | ||
|
||
会根据交互、变化以及处理情况生成实时日志记录 | ||
|
||
- #### 可存档 | ||
|
||
当执行检查后会生成指定的hash文件,该文件记录主要的配置参数以及需检查目录中各个目录/文件的数据,当二次或往后再次执行监控检查时可通过加载该hash文件继续进行上一次的监控检查。 | ||
|
||
## 使用说明 | ||
|
||
- #### _config.json | ||
|
||
即默认配置文件,需放在和该文件同一目录下(或直接手动输入参数也可),其配置格式如下 | ||
|
||
```json | ||
{ | ||
"check_dir":"[需检查目录]", | ||
"log_dir":"[用作存储日志的目录]", | ||
"back_dir":"[用作备份的目录]", | ||
"log_file_name":"[日志文件名称]", | ||
"hash_file":"[用作记录各个文件hash值文件(可作为存档)的路径]", | ||
"coroutines":"[主动检查|被动检查|替换协程数]", | ||
"time_sec":"[检查间隔(秒)]" | ||
} | ||
``` | ||
|
||
- #### 参数解析 | ||
|
||
- **需检查目录** > 需要实时监控、恢复的目录 | ||
- **日志存储目录** > 用来存储日志文件的目录 | ||
- **日志文件名称** > 日志文件的名称,可以按照自己的想法自定义格式 | ||
- **hash文件** -> 存储需检查目录中各个目录、文件数据的存档文件 | ||
- **备份目录** > 用来存放需检查目录的备份的目录,需要依靠其进行目录或文件的恢复 | ||
- **[主动检查|被动检查|替换协程]** > | ||
- **主动检查** > 遍历存储的数据进行检查 | ||
- **被动检查** > 遍历需检查目录的各个文件进行检查 | ||
- **替换** > 当检查到有差异时从备份目录选取正确的目录或文件进行恢复 | ||
- **检查时间间隔** > 主动检查以及被动检查的频率(秒) | ||
|
||
## 免责声明 | ||
|
||
本项目仅是练手玩意,以便~~我自己~~学习与交流. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"check_dir":"..\/..\/test", | ||
"log_dir":"..\/..\/log", | ||
"back_dir":"..\/..\/back", | ||
"log_file_name":"123.txt", | ||
"hash_file":".\/hash.txt", | ||
"coroutines":"1|1|1", | ||
"time_sec":"2" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module robin | ||
|
||
go 1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
package lib | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"math" | ||
"math/rand" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// 横幅 | ||
|
||
var Banner = ` | ||
=================================== | ||
robin v1.00a | ||
=================================== | ||
` | ||
|
||
// 随机种子 | ||
|
||
var Seed = rand.New(rand.NewSource(int64(float64(time.Now().Unix())*math.Pi))) | ||
|
||
// 同步 | ||
|
||
var WG =sync.WaitGroup{} | ||
|
||
// 默认值 | ||
|
||
var defaultConfig = map[string]string{} | ||
|
||
// 选项 | ||
|
||
var options = []string{ | ||
|
||
"[*] 请输入需检查的目录(留空则使用默认值) -> ", | ||
"[*] 请输入日志存储的目录(留空则使用默认值) -> ", | ||
"[*] 请输入日志文件名称(留空则使用默认值) -> ", | ||
"[*] 请输入hash文件存储位置(留空则使用默认值) -> ", | ||
"[*] 请输入备份目录(留空则使用默认值) -> ", | ||
"[*] 请输入被动检查协程|主动检查协程|替换协程数(留空则使用默认值) -> ", | ||
"[*] 请输入检查时间间隔(留空则使用默认值) -> ", | ||
|
||
} | ||
|
||
var history = "[*] 是否加载以前的hash文件(y/N) -> " | ||
|
||
var display = "[*] 请输入是否回显(留空则使用默认值|y/N) -> " | ||
|
||
// hash文件format | ||
|
||
type hashFormat struct { | ||
|
||
BasicConfig map[string]string `json:"basic_config"` | ||
DirsConfig map[string]map[string]string `json:"dirs_config"` | ||
FilesConfig map[string]map[string]string `json:"files_config"` | ||
|
||
} | ||
|
||
// config文件format | ||
|
||
type configFormat struct { | ||
|
||
CheckDir string `json:"check_dir"` | ||
LogDir string `json:"log_dir"` | ||
BackDir string `json:"back_dir"` | ||
LogFileName string `json:"log_file_name"` | ||
HashFile string `json:"hash_file"` | ||
Coroutines string `json:"coroutines"` | ||
TimeSec string `json:"time_sec"` | ||
|
||
} | ||
|
||
// 日志文件配置 | ||
|
||
var logFileConfig = map[string]string{} | ||
|
||
// 准备参数 | ||
|
||
func prepare() *Control{ | ||
|
||
var optionData = make([]string,len(options)) | ||
var optionDict map[string]string | ||
|
||
// 控制结构 | ||
var control *Control | ||
|
||
// 显示横幅 | ||
fmt.Print(Banner) | ||
|
||
// 读取配置文件 | ||
src,err := os.OpenFile("_config.json",os.O_RDONLY,0) | ||
defer func() { | ||
err = src.Close() | ||
if err != nil{ | ||
fmt.Println("[*] 配置文件无法正常关闭 -> _config.json") | ||
} | ||
}() | ||
if err != nil{ | ||
fmt.Println("[*] 无法加载配置文件 -> _config.json") | ||
} | ||
configData := &configFormat{} | ||
contents,err := io.ReadAll(src) | ||
if err != nil{ | ||
fmt.Println("[*] 无法读取配置文件内容 -> _config.json") | ||
} | ||
err = json.Unmarshal(contents,configData) | ||
// 映射至字典 | ||
if err != nil{ | ||
fmt.Println("[*] 无法解析配置文件内容 -> _config.json") | ||
|
||
defaultConfig = map[string]string{ | ||
"enable":"false", | ||
} | ||
|
||
}else{ | ||
|
||
defaultConfig = map[string]string{ | ||
|
||
"enable":"true", | ||
"checkDir":configData.CheckDir, | ||
"logDir":configData.LogDir, | ||
"backDir":configData.BackDir, | ||
"logFileName":configData.LogFileName, | ||
"hashFile":configData.HashFile, | ||
"coroutines":configData.Coroutines, | ||
"timeSec":configData.TimeSec, | ||
|
||
} | ||
|
||
fmt.Println("[+] 配置文件加载成功!") | ||
|
||
} | ||
|
||
// 判断是否回显 | ||
if isDisplay(){ | ||
logFileConfig["display"] = "true" | ||
} | ||
|
||
// 判断是否使用历史hash文件 | ||
if !isUseHistory() { | ||
|
||
// 加入用户输入数据 | ||
for key,each := range options{ | ||
optionData[key] = input(each, '\n') | ||
} | ||
optionDict = loadConfig(optionData) | ||
control = &Control{Config: optionDict,DirSet: make(map[string]*Dirs)} | ||
}else{ | ||
|
||
// 载入hash文件 | ||
control = loadHistory() | ||
// 若失败则返回错误 | ||
if control == nil { | ||
log("warning","加载hash文件失败````",nil) | ||
return prepare() | ||
} | ||
|
||
|
||
} | ||
|
||
return control | ||
|
||
} | ||
|
||
// 生成日志 | ||
|
||
func record(control *Control){ | ||
|
||
log("run","开始生成日志````",nil) | ||
if logConfig(control){ | ||
log("ok","完成生成日志````",nil) | ||
}else{ | ||
log("warning","未完成生成文件````",nil) | ||
} | ||
|
||
} | ||
|
||
// 遍历文件 | ||
|
||
func load(control *Control){ | ||
log("run","开始遍历文件````",nil) | ||
if loadFiles(control) { | ||
log("ok","完成遍历文件````",nil) | ||
}else{ | ||
log("warning","未完成遍历文件````",nil) | ||
} | ||
} | ||
|
||
// 备份文件 | ||
|
||
func backup(control *Control){ | ||
log("run","开始备份文件````",nil) | ||
if backupFiles(control){ | ||
log("ok","完成备份文件````",nil) | ||
}else{ | ||
log("warning","未完成备份文件````",nil) | ||
} | ||
} | ||
|
||
// 计算hash | ||
|
||
func hash(control *Control){ | ||
log("run","开始计算文件hash````",nil) | ||
if hashFiles(control) { | ||
log("ok","完成文件hash计算````",nil) | ||
}else{ | ||
log("warning","未完成文件hash计算````",nil) | ||
} | ||
} | ||
|
||
// 输出hash文件 | ||
|
||
func out(control *Control){ | ||
log("run","开始输出hash文件````",nil) | ||
if outputHashFile(control){ | ||
log("ok","完成输出hash文件````",nil) | ||
}else{ | ||
log("warning","未完成输出hash文件````",nil) | ||
} | ||
} | ||
|
||
// 运行 | ||
|
||
func Run(){ | ||
|
||
var control = prepare() | ||
// 生成日志 | ||
record(control) | ||
// 遍历文件 | ||
load(control) | ||
// 备份文件 | ||
backup(control) | ||
// 计算文件hash | ||
hash(control) | ||
// 输出hash文件 | ||
out(control) | ||
// 开始 | ||
log("run","监控开始````",nil) | ||
checkCore(control) | ||
// 结束 | ||
log("end","监控结束````",nil) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package lib | ||
|
||
var E = `控制集 > 目录集 > 文件集 | ||
控制集:主目录,其他参数 | ||
主目录: | ||
- 目录最后修改时间 | ||
- 备份目录路径 | ||
- 文件集 | ||
文件集: | ||
- 文件原始hash | ||
- 文件最后修改时间 | ||
- 备份文件路径` | ||
|
||
type Control struct { | ||
DirSet map[string]*Dirs | ||
Config map[string]string | ||
} | ||
|
||
type Dirs struct { | ||
Data map[string]string | ||
FileSet map[string]*Files | ||
} | ||
|
||
type Files struct { | ||
Data map[string]string | ||
} | ||
|
||
func (c *Control) AddDirs(dirName string,dir *Dirs) { | ||
c.DirSet[dirName] = dir | ||
} | ||
func (c *Control) SetConfig(key string, value string) { | ||
c.Config[key] = value | ||
} | ||
|
||
func (d *Dirs) addFiles(fileName string,file *Files){ | ||
d.FileSet[fileName] = file | ||
} | ||
func (d *Dirs) SetData(key, value string) { | ||
d.Data[key] = value | ||
} | ||
|
||
func (f *Files) SetData(key, value string) { | ||
f.Data[key] = value | ||
} | ||
|
Oops, something went wrong.