Files
RedisClipSync/README.md

199 lines
6.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Redis Clipboard Sync
基于 Redis 的双向剪切板日志同步:主机将本地剪切板广播到从机并记录日志,从机将本地剪切板上传到主机并记录日志。
## 版本对应
- `v1.1.0`(当前):双向传输 AES-256-GCM 加密;从机 `master_to_slave.log` 按条目 TTL 清理(默认 30 秒)。
- `v1.0.0`:双向日志同步(无传输加密、无按条目 TTL 清理)。
## 目录结构
```
RedisClipSync/
code/ # 代码与构建
pom.xml
src/main/java/
target/ # 打包输出 redis-clip-sync.jar
windows/ # Windows 脚本
build-and-run.bat # 一键打包并运行
run.bat # 仅运行(需已打包)
linux/ # Linux 脚本
build-and-run.sh # 一键打包并运行
run.sh # 仅运行(需已打包)
build-slave-release.sh # 生成从机发布包 tar.gz
slave-package/ # 从机安装脚本与 systemd 模板
config.master.properties.example
config.slave.properties.example
config.properties # 运行前从 example 复制并修改
README.md
剪切板同步.md
部署方案.md
```
## 构建与运行
### 一键脚本(推荐)
脚本会从各自目录自动切换到项目根目录再执行,配置与 `clipboard_logs/` 均在根目录。
**Windows**(在项目根目录或 `windows/` 下执行均可)
- **windows/build-and-run.bat**:先打包再启动。
双击运行,或:`windows\build-and-run.bat [配置文件路径]`(默认 `config.properties`)。
- **windows/run.bat**:不重新打包,直接运行。
用法:`windows\run.bat [配置文件路径]`
**Linux**(需先赋予执行权限:`chmod +x linux/*.sh`
- **linux/build-and-run.sh**:先打包再启动。
`./linux/build-and-run.sh [配置文件路径]`(默认 `config.properties`)。
- **linux/run.sh**:不重新打包,直接运行。
用法:`./linux/run.sh [配置文件路径]`
运行前请将 `config.master.properties.example``config.slave.properties.example` 复制为 `config.properties` 并修改。
### 手动构建与运行
在项目根目录执行:
```bash
mvn -f code/pom.xml clean package
```
`code/target/` 下生成 `redis-clip-sync.jar`(已包含 Jedis 依赖)。
确保工作目录为项目根目录并包含 `config.properties`,或通过参数指定配置路径:
```bash
java -jar code/target/redis-clip-sync.jar
java -jar code/target/redis-clip-sync.jar /path/to/config.properties
```
## 内网快速部署(主机打包,从机解压即装)
### 1) 在主机生成从机发布包
```bash
chmod +x linux/build-slave-release.sh
./linux/build-slave-release.sh
```
生成物:`dist/redis-clip-sync-slave-<version>.tar.gz`
### 2) 传输到从机并解压
```bash
tar -xzf redis-clip-sync-slave-<version>.tar.gz
cd redis-clip-sync-slave-<version>
```
### 3) 从机只改配置并一键安装开机自启
编辑 `conf/config.properties`(至少修改 `redis.host``slave.listen.channel``slave.id``crypto.key`),然后执行:
```bash
sudo ./bin/install-slave.sh
```
默认安装目录:`/opt/redis-clip-sync`
### 4) 常用运维命令
```bash
systemctl status redis-clip-sync
systemctl restart redis-clip-sync
journalctl -u redis-clip-sync -f
```
### 5) 一键验收命令(从机)
```bash
bash -c 'set -e; systemctl is-enabled redis-clip-sync >/dev/null; systemctl is-active redis-clip-sync >/dev/null; test -f /opt/redis-clip-sync/lib/redis-clip-sync.jar; test -f /opt/redis-clip-sync/conf/config.properties; echo "[PASS] service enabled + active, files ready"'
```
如需完整部署步骤与回滚方案,见 `部署方案.md`
## 配置
- **config.properties**:主配置,字段与 `剪切板同步.md` 一致。
- **config.master.properties.example**:主机示例,复制为 `config.properties` 后修改。
- **config.slave.properties.example**:从机示例,复制为 `config.properties` 后修改。
### 主机 (Master)
```properties
role=MASTER
redis.host=127.0.0.1
redis.port=6379
redis.password=
master.target.channels=machine_a,machine_b
master.id=master
log.save.dir=./clipboard_logs/
crypto.enabled=true
crypto.key=REPLACE_WITH_BASE64_32_BYTE_KEY
```
### 从机 (Slave)
```properties
role=SLAVE
redis.host=192.168.1.100
redis.port=6379
redis.password=
slave.listen.channel=machine_a
slave.id=192.168.1.50
log.save.dir=./clipboard_logs/
crypto.enabled=true
crypto.key=REPLACE_WITH_BASE64_32_BYTE_KEY
slave.log.ttl.seconds=30
slave.log.prune.interval.seconds=5
```
`master.id``slave.id` 会写入日志的 `source/target` 字段,便于审计。
`crypto.enabled=true` 时,主从消息会使用 AES-256-GCM 加密传输(`crypto.key` 需主从一致)。
从机只对 `master_to_slave.log` 启用按条目保留:每条默认保留 30 秒,超时自动从日志内清除。
## 1主2从配置示例
- 主机:`master.target.channels=machine_a,machine_b`
- 从机1`slave.listen.channel=machine_a`
- 从机2`slave.listen.channel=machine_b`
- 三端 `crypto.enabled=true``crypto.key` 必须一致
## 运行目录说明
程序启动后会在配置的 `log.save.dir`(默认 `./clipboard_logs/`)下自动创建保存目录。建议在项目根目录运行脚本,这样 `config.properties``clipboard_logs/` 均在根目录下:
```
RedisClipSync/
- config.properties
- clipboard_logs/ # 自动创建Master/Slave
- master_to_slave.log # 主机发往从机的日志
- slave_to_master.log # 从机发往主机的日志
```
## 功能验证
1. **主机 → 从机**:主机复制一段文字(如 "Hello Slave"),主机会广播到所有目标频道;各从机收到后仅写 `master_to_slave.log`,不修改本机剪切板。
2. **从机 → 主机**:从机复制一段文字(如 "Password123"),主机会在 `./clipboard_logs/slave_to_master.log` 追加日志记录。
记录格式示例:
```
[2024-05-20 10:00:01] direction=slave_to_master source=192.168.1.50 target=master
Password123
------------------
```
## 依赖
- Java 8+
- Redis用于 Pub/Sub 与连接)
- Maven 3.x仅构建时