2018-08-30 12:36:03 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-08-31 02:04:59 +00:00
|
|
|
"encoding/base64"
|
2018-08-30 12:36:03 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"yumc.pw/cloud/clipboard-sync/lib/clipboard"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
t := flag.String("t", "c", "Type c(client) or s(server)")
|
|
|
|
a := flag.String("a", "http://127.0.0.1:8080", "Server Address")
|
|
|
|
b := flag.String("b", ":8080", "Server Listen Port")
|
|
|
|
flag.Parse()
|
|
|
|
if *t == "c" {
|
|
|
|
if clipboard.Unsupported {
|
|
|
|
panic("Unsupported On This Machine")
|
|
|
|
}
|
|
|
|
readClipboard(*a)
|
|
|
|
} else {
|
|
|
|
startServer(*b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2018-08-30 14:04:24 +00:00
|
|
|
textHeader = "Go-Clipboard-Text"
|
|
|
|
timeHeader = "Go-Clipboard-Time"
|
2018-08-30 12:36:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func startServer(b string) {
|
|
|
|
text := ""
|
|
|
|
time := time.Now().Unix()
|
2018-08-30 14:04:24 +00:00
|
|
|
g := gin.New()
|
2018-08-30 12:36:03 +00:00
|
|
|
g.GET("/", func(c *gin.Context) {
|
2018-08-30 14:04:24 +00:00
|
|
|
c.Header(textHeader, text)
|
|
|
|
c.Header(timeHeader, strconv.FormatInt(time, 10))
|
2018-08-30 12:36:03 +00:00
|
|
|
})
|
|
|
|
g.POST("/", func(c *gin.Context) {
|
|
|
|
text = c.Query("text")
|
2018-08-30 14:04:24 +00:00
|
|
|
time, _ = strconv.ParseInt(c.Query("time"), 10, 64)
|
2018-08-30 12:36:03 +00:00
|
|
|
})
|
|
|
|
g.Run(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readClipboard(a string) {
|
|
|
|
old, _ := clipboard.ReadAll()
|
|
|
|
updateTime := time.Now().Unix()
|
|
|
|
client := &http.Client{}
|
|
|
|
address := a + "?text=%s&time=%s"
|
|
|
|
for {
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
text, _ := clipboard.ReadAll()
|
|
|
|
if old != text {
|
|
|
|
old = text
|
|
|
|
updateTime = time.Now().Unix()
|
|
|
|
request, _ := http.NewRequest("POST",
|
2018-08-31 02:04:59 +00:00
|
|
|
fmt.Sprintf(address, encode(text), strconv.FormatInt(updateTime, 10)), nil)
|
2018-08-30 12:36:03 +00:00
|
|
|
client.Do(request)
|
|
|
|
} else {
|
|
|
|
response, _ := http.Get(a)
|
2018-08-30 14:04:24 +00:00
|
|
|
texts, ok := response.Header[textHeader]
|
2018-08-30 12:36:03 +00:00
|
|
|
if ok {
|
2018-08-31 02:04:59 +00:00
|
|
|
sText := decode(texts[0])
|
2018-08-30 14:04:24 +00:00
|
|
|
sTime, _ := strconv.ParseInt(response.Header[timeHeader][0], 10, 64)
|
|
|
|
if sTime > updateTime && sText != old {
|
2018-08-30 12:36:03 +00:00
|
|
|
old = sText
|
2018-08-30 14:16:18 +00:00
|
|
|
updateTime = sTime
|
2018-08-30 12:36:03 +00:00
|
|
|
clipboard.WriteAll(sText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-31 02:04:59 +00:00
|
|
|
|
|
|
|
func encode(text string) string {
|
|
|
|
return base64.URLEncoding.EncodeToString([]byte(text))
|
|
|
|
}
|
|
|
|
|
|
|
|
func decode(text string) string {
|
|
|
|
origin, _ := base64.URLEncoding.DecodeString(text)
|
|
|
|
return string(origin)
|
|
|
|
}
|