-
Notifications
You must be signed in to change notification settings - Fork 22
/
main.go
121 lines (105 loc) · 2.25 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
package main
import (
"flag"
"fmt"
"net"
"github.com/indexsupply/x/enr"
"github.com/indexsupply/x/rlpx"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
)
func check(err error) {
if err != nil {
panic(err)
}
}
type errRW struct {
c net.Conn
b [1 << 14]byte //16 MiB devp2p max. TODO(ryan): use dynamic buffer
err error
}
func (rw *errRW) Read(f func([]byte) error) {
if rw.err != nil {
return
}
var n int
n, rw.err = rw.c.Read(rw.b[:])
if rw.err != nil {
return
}
rw.err = f(rw.b[:n])
}
func (rw *errRW) Write(f func() ([]byte, error)) {
if rw.err != nil {
return
}
var b []byte
b, rw.err = f()
if rw.err != nil {
return
}
_, rw.err = rw.c.Write(b)
}
func serve(c net.Conn, node *enr.Record) {
defer c.Close()
hs := rlpx.Recipient(node.PrivateKey)
rw := errRW{c: c}
rw.Read(hs.HandleAuth)
rw.Write(hs.Ack)
rs, err := rlpx.Session(node, hs)
check(err)
rw.Read(rs.HandleMessage)
rw.Write(rs.Hello)
if rw.err != nil {
fmt.Printf("serve-error: %s\n", rw.err)
}
}
func main() {
var remoteURL string
flag.StringVar(&remoteURL, "remote", "", "enode://XXX@host:port")
flag.Parse()
self := new(enr.Record)
self.PrivateKey, _ = secp256k1.GeneratePrivateKey()
self.PublicKey = self.PrivateKey.PubKey()
self.Ip = net.ParseIP("127.0.0.1")
self.TcpPort = 30303
self.UdpPort = 30303
remote := new(enr.Record)
remote.PrivateKey, _ = secp256k1.GeneratePrivateKey()
remote.PublicKey = remote.PrivateKey.PubKey()
remote.Ip = net.ParseIP("127.0.0.1")
remote.TcpPort = 30304
remote.UdpPort = 30304
if remoteURL != "" {
var err error
remote, err = enr.ParseV4(remoteURL)
check(err)
} else {
listening := make(chan struct{}, 1)
go func() {
ln, err := net.Listen("tcp", remote.TCPAddr().String())
check(err)
listening <- struct{}{}
for {
conn, err := ln.Accept()
check(err)
go serve(conn, remote)
}
}()
<-listening
}
conn, err := net.Dial("tcp", remote.TCPAddr().String())
check(err)
hs := rlpx.Initiator(self.PrivateKey, remote.PublicKey)
rw := errRW{c: conn}
rw.Write(hs.Auth)
rw.Read(hs.HandleAck)
check(rw.err)
rs, err := rlpx.Session(self, hs)
check(err)
rs.Verbose = true
rw.Write(rs.Hello)
rw.Read(rs.HandleMessage)
rw.Write(rs.EthStatus)
rw.Read(rs.HandleMessage)
check(rw.err)
}