-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
205 lines (187 loc) · 3.8 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"bytes"
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"time"
"github.com/denisbrodbeck/machineid"
log "github.com/schollz/logger"
)
func main() {
log.SetLevel("info")
connector := ""
if len(os.Args) > 1 {
connector = os.Args[1]
}
err := run(connector)
if err != nil {
log.Error(err)
}
}
type Message struct {
ID string
Addresses []string
}
func run(connector string) (err error) {
id, iderr := machineid.ID()
token := make([]byte, 16)
rand.Read(token)
if iderr != nil {
id = fmt.Sprintf("%x", token)
}
if connector == "" {
token = make([]byte, 16)
rand.Read(token)
connector = fmt.Sprintf("%x", token)
}
// fmt.Printf("your id: %s\n", id)
fmt.Printf("add another computer to your swarm by running:\n\nipfs-connect %s\n\n", connector)
go listenForAddresses(id, connector)
go func() {
time.Sleep(1 * time.Second)
sendAddresses(id, connector)
}()
for {
var resp *http.Response
resp, err = http.Get("https://duct.schollz.com/" + id)
if err != nil {
return
}
var b []byte
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
resp.Body.Close()
var msg Message
err = json.Unmarshal(b, &msg)
if err != nil {
log.Error(err)
continue
}
go connectToAddresses(msg.Addresses)
}
return
}
func listenForAddresses(id, connector string) (err error) {
for {
var resp *http.Response
resp, err = http.Get("https://duct.schollz.com/" + connector)
if err != nil {
return
}
var b []byte
b, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
resp.Body.Close()
var msg Message
err = json.Unmarshal(b, &msg)
if err != nil {
log.Error(err)
continue
}
if msg.ID != "" && msg.ID != id {
log.Debugf("got addresses: %+v", msg.Addresses)
go connectToAddresses(msg.Addresses)
err = sendAddresses(msg.ID, msg.ID)
if err != nil {
log.Error(err)
return
}
}
}
}
func sendAddresses(id, sendto string) (err error) {
addresses, err := getAddresses()
if err != nil {
return
}
b, err := json.Marshal(Message{
ID: id,
Addresses: addresses,
})
if err != nil {
return
}
err = postData(sendto, b)
return
}
func postData(sendto string, data []byte) (err error) {
body := bytes.NewReader(data)
req, err := http.NewRequest("POST", "https://duct.schollz.com/"+sendto+"?pubsub=true", body)
if err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
return
}
func getAddresses() (addresses []string, err error) {
cmd := exec.Command("ipfs", "id", "--encoding", "json")
out, err := cmd.CombinedOutput()
if err != nil {
return
}
type IDData struct {
ID string
Addresses []string
}
var iddata IDData
err = json.Unmarshal(out, &iddata)
if err != nil {
return
}
addresses = iddata.Addresses
return
}
func connectToAddresses(addresses []string) (err error) {
if len(addresses) == 0 {
err = fmt.Errorf("no addresses")
return
}
foo := strings.Split(addresses[0], "/")
fmt.Printf("establishing connection to %s...", foo[len(foo)-1])
var wg sync.WaitGroup
wg.Add(len(addresses))
didConnect := false
for _, addr := range addresses {
go func(addr2 string) {
connected := connectToAddress(addr2)
if connected {
didConnect = true
}
wg.Done()
}(addr)
}
wg.Wait()
if didConnect {
fmt.Printf("success!\n")
} else {
fmt.Printf("failed.\n")
}
return
}
func connectToAddress(addr string) (connected bool) {
log.Debugf("connecting to %s", addr)
cmd := exec.Command("ipfs", "swarm", "connect", addr, "--encoding", "json")
out, err := cmd.CombinedOutput()
if err != nil {
connected = false
} else {
connected = bytes.Contains(out, []byte("success"))
}
return
}