-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_peer_udp.go
112 lines (94 loc) · 2.39 KB
/
remote_peer_udp.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
package xchg
import (
"crypto/rsa"
"errors"
"net"
"strconv"
"sync"
"time"
)
type RemotePeerUdp struct {
mtx sync.Mutex
nonces *Nonces
privateKey *rsa.PrivateKey
remoteAddress string
allowLocalConnection bool
lanConnectionPoint *net.UDPAddr
}
func NewRemotePeerUdp(nonces *Nonces, remoteAddress string, privateKey *rsa.PrivateKey) *RemotePeerUdp {
var c RemotePeerUdp
c.nonces = nonces
c.remoteAddress = remoteAddress
c.privateKey = privateKey
c.allowLocalConnection = true
return &c
}
func (c *RemotePeerUdp) Send(conn net.PacketConn, frame []byte) error {
c.mtx.Lock()
lanConnectionPoint := c.lanConnectionPoint
c.mtx.Unlock()
if c.lanConnectionPoint == nil {
return errors.New("no LAN connection point")
}
_, err := conn.WriteTo(frame, lanConnectionPoint)
return err
}
func (c *RemotePeerUdp) IsValid() (result bool) {
c.mtx.Lock()
result = c.lanConnectionPoint != nil
c.mtx.Unlock()
return
}
func (c *RemotePeerUdp) Check(conn net.PacketConn) {
go c.checkLANConnectionPoint(conn)
}
func (c *RemotePeerUdp) SetConnectionPoint(udpAddress *net.UDPAddr) {
c.mtx.Lock()
c.lanConnectionPoint = udpAddress
c.mtx.Unlock()
}
func (c *RemotePeerUdp) ResetConnectionPoint() {
c.mtx.Lock()
c.lanConnectionPoint = nil
c.mtx.Unlock()
}
func (c *RemotePeerUdp) checkLANConnectionPoint(conn net.PacketConn) (err error) {
if conn == nil {
return
}
if !c.allowLocalConnection {
return
}
c.mtx.Lock()
lanConnectionPoint := c.lanConnectionPoint
c.mtx.Unlock()
if lanConnectionPoint != nil {
return
}
nonce := c.nonces.Next()
addressBS := []byte(c.remoteAddress)
transaction := NewTransaction(0x20, AddressForPublicKey(&c.privateKey.PublicKey), c.remoteAddress, 0, 0, 0, 0, nil)
transaction.Data = make([]byte, 16+len(addressBS))
copy(transaction.Data[0:], nonce[:])
copy(transaction.Data[16:], addressBS)
for i := PEER_UDP_START_PORT; i < PEER_UDP_END_PORT; i++ {
c.mtx.Lock()
lanConnectionPoint = c.lanConnectionPoint
c.mtx.Unlock()
if lanConnectionPoint != nil {
break
}
var broadcastAddress *net.UDPAddr
//fmt.Println("send to " + fmt.Sprint(i))
broadcastAddress, err = net.ResolveUDPAddr("udp4", "127.0.0.1:"+strconv.FormatInt(int64(i), 10))
if err != nil {
break
}
_, err = conn.WriteTo(transaction.Marshal(), broadcastAddress)
if err != nil {
break
}
time.Sleep(10 * time.Millisecond)
}
return
}