-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (162 loc) · 4.42 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"crypto/subtle"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/atotto/clipboard"
"github.com/schollz/peerdiscovery"
log "github.com/sirupsen/logrus"
)
const peerDiscoveryPort = "30561"
const serverPort = 30562
const username = "clipu"
var password = ""
var peerLimit = 1
var peers = make([]string, 0)
var mutex = &sync.Mutex{}
var lastReceived = ""
func captureClipboard(clipboardContents chan<- string) {
previousContent := ""
for {
currentContent, err := clipboard.ReadAll()
if err != nil {
log.WithError(err).Fatal("can't read clipboard content")
}
if previousContent != currentContent && lastReceived != currentContent {
log.Infof("got text '%s'", currentContent)
previousContent = currentContent
clipboardContents <- currentContent
}
time.Sleep(1 * time.Second)
}
}
func startDiscovery() {
for {
log.Debug("started peer discovery")
discoveries, _ := peerdiscovery.Discover(peerdiscovery.Settings{Limit: peerLimit, Port: peerDiscoveryPort})
newPeers := make([]string, 0)
for _, d := range discoveries {
if authorized(d.Address) {
newPeers = append(newPeers, d.Address)
log.Debug("discovered '%s'", d.Address)
}
}
mutex.Lock()
peers = newPeers
mutex.Unlock()
log.Debug("finished peer discovery")
time.Sleep(5 * time.Second)
}
}
func basicAuth(handler http.HandlerFunc, username, password, realm string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
user, pass, ok := r.BasicAuth()
if !ok || subtle.ConstantTimeCompare([]byte(user), []byte(username)) != 1 || subtle.ConstantTimeCompare([]byte(pass), []byte(password)) != 1 {
w.Header().Set("WWW-Authenticate", `Basic realm="`+realm+`"`)
w.WriteHeader(401)
w.Write([]byte("Unauthorised.\n"))
return
}
handler(w, r)
}
}
func receive(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
data, _ := ioutil.ReadAll(r.Body)
text := fmt.Sprintf("%s", data)
if text != "" {
log.Infof("received %s", text)
err := clipboard.WriteAll(text)
lastReceived = text
if err != nil {
log.WithError(err).Fatal("can't write into clipboard")
}
}
fmt.Fprintf(w, "received: '%s'\n", text)
}
func ack(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
fmt.Fprintf(w, "ok\n")
}
func authorized(peer string) bool {
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%d/ack", peer, serverPort), nil)
if err != nil {
log.WithError(err).Errorf("could not create request to ask for authorization")
return false
}
req.SetBasicAuth(username, password)
res, err := client.Do(req)
if err != nil {
return false
}
if res.StatusCode != http.StatusOK {
return false
}
log.Infof("peer %s responded positively to authorization inquiry", peer)
return true
}
func startServer() {
http.HandleFunc("/receive", basicAuth(receive, username, password, ""))
http.HandleFunc("/ack", basicAuth(ack, username, password, ""))
http.ListenAndServe(fmt.Sprintf(":%d", serverPort), nil)
}
func sendText(text string, peer string) error {
client := &http.Client{}
req, err := http.NewRequest("POST", fmt.Sprintf("http://%s:%d/receive", peer, serverPort), strings.NewReader(text))
if err != nil {
return err
}
req.SetBasicAuth(username, password)
res, err := client.Do(req)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("peer has returned an invalid code %d", res.StatusCode)
}
return nil
}
func init() {
text, err := clipboard.ReadAll()
if err != nil {
log.WithError(err).Fatal("can't read clipboard content")
}
lastReceived = text
password = os.Getenv("CLIPU_PASSWORD")
if password == "" {
log.Fatal("running on empty password! Set CLIPU_PASSWORD to start.")
}
peerLimitStr, found := os.LookupEnv("CLIPU_PEER_LIMIT")
if found {
peerLimitInt, err := strconv.Atoi(peerLimitStr)
if err != nil {
log.WithError(err).Errorf("Can't parse peer limit from %s", peerLimitStr)
} else {
peerLimit = peerLimitInt
}
}
}
func main() {
clipboardContent := make(chan string)
go captureClipboard(clipboardContent)
go startDiscovery()
go startServer()
for value := range clipboardContent {
for _, peer := range peers {
mutex.Lock()
log.Infof("sending %s to peer %s", value, peer)
err := sendText(value, peer)
if err != nil {
log.WithError(err).Errorf("an error has occurred while sending text %s to %s", value, peer)
}
mutex.Unlock()
}
}
}