-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (94 loc) · 2.4 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/go-redis/redis"
"github.com/pelletier/go-toml/v2"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"golang.design/x/clipboard"
"io/ioutil"
"os/exec"
"runtime"
"time"
)
type config struct {
RedisAddr string
RedisPassword string
TopicKey string
DeviceToken string
}
var flag = false
var RedisAddr = ""
var RedisPassword = ""
var DeviceToken = ""
var TopicKey = ""
func PublishRedis(client *redis.Client) {
ch := clipboard.Watch(context.TODO(), clipboard.FmtText)
for data := range ch {
if flag == false {
clipboardContent := string(data)
encodeString := base64.StdEncoding.EncodeToString([]byte(clipboardContent))
jsondata := make(map[string]string)
jsondata["type"] = "text"
jsondata["msg"] = encodeString
jsondata["uuid"] = DeviceToken
dataType, _ := json.Marshal(jsondata)
dataString := string(dataType)
client.Publish(TopicKey, dataString)
}
}
}
func SubscribeRedis(client *redis.Client) {
pubsub := client.Subscribe(TopicKey)
defer pubsub.Close()
for msg := range pubsub.Channel() {
message := gjson.Get(msg.Payload, "msg").String()
uuid := gjson.Get(msg.Payload, "uuid").String()
if uuid != DeviceToken {
decodeString, _ := base64.StdEncoding.DecodeString(message)
log.Infof("向剪切板写入信息: %s\n", decodeString)
clipboard.Write(clipboard.FmtText, decodeString)
sysType := runtime.GOOS
if sysType == "darwin" {
notifyCommand := fmt.Sprintf("display notification \"%s\" with title \"SyncClipBoard同步消息\"", decodeString)
command := exec.Command("osascript", "-e", notifyCommand)
err := command.Run()
if err != nil {
fmt.Println(err.Error())
}
}
flag = true
time.Sleep(time.Second * 1)
flag = false
}
}
}
func main() {
log.SetFormatter(&log.TextFormatter{
ForceQuote: true,
TimestampFormat: "2006-01-02 15:04:05",
FullTimestamp: true,
})
var Config config
bytes, err := ioutil.ReadFile("config.toml")
if err != nil {
panic(err)
}
err = toml.Unmarshal(bytes, &Config)
if err != nil {
panic(err)
}
RedisAddr = Config.RedisAddr
RedisPassword = Config.RedisPassword
DeviceToken = Config.DeviceToken
TopicKey = Config.TopicKey
client := redis.NewClient(&redis.Options{
Addr: RedisAddr,
Password: RedisPassword,
})
go SubscribeRedis(client)
PublishRedis(client)
}