This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpeer.go
186 lines (167 loc) · 4.42 KB
/
peer.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
package dctk
import (
"fmt"
"github.com/aler9/go-dc/adc"
atypes "github.com/aler9/go-dc/adc/types"
"github.com/aler9/go-dc/nmdc"
"github.com/aler9/dctk/pkg/log"
"github.com/aler9/dctk/pkg/protoadc"
)
// Peer represents a remote client connected to a Hub.
type Peer struct {
// peer nickname
Nick string
// peer description (if provided)
Description string
// peer email (if provided)
Email string
// whether peer is a bot
IsBot bool
// whether peer is a operator
IsOperator bool
// client used by peer (in NMDC this could be hidden)
Client string
// version of client (in NMDC this could be hidden)
Version string
// overall size of files shared by peer
ShareSize uint64
// whether peer is in passive mode (in NMDC this could be hidden)
IsPassive bool
// peer ip (if provided by both peer and hub)
IP string
adcSessionID atypes.SID
adcClientID atypes.CID
adcFingerprint string
adcFeatures adc.ExtFeatures
adcUDPPort uint
nmdcConnection string
nmdcFlag nmdc.UserFlag
}
// Peers returns a map containing all the peers connected to current hub.
func (c *Client) Peers() map[string]*Peer {
return c.peers
}
func (c *Client) peerByNick(nick string) *Peer {
if p, ok := c.peers[nick]; ok {
return p
}
return nil
}
func (c *Client) peerBySessionID(sessionID adc.SID) *Peer {
for _, p := range c.peers {
if p.adcSessionID == sessionID {
return p
}
}
return nil
}
func (c *Client) peerByClientID(clientID adc.CID) *Peer {
for _, p := range c.peers {
if p.adcClientID == clientID {
return p
}
}
return nil
}
func (c *Client) peerSupportsAdc(p *Peer, f adc.Feature) bool {
return p.adcFeatures.Has(f)
}
func (c *Client) peerSupportsEncryption(p *Peer) bool {
if c.protoIsAdc() {
if p.adcFingerprint != "" {
return true
}
if c.peerSupportsAdc(p, adc.FeaADCS) {
return true
}
return false
}
// we check only for bit 4
return (p.nmdcFlag & nmdc.FlagTLSDownload) != 0
}
func (c *Client) peerRequestConnection(peer *Peer, adcToken string) {
if !c.conf.IsPassive {
c.peerConnectToMe(peer, adcToken)
} else {
c.peerRevConnectToMe(peer, adcToken)
}
}
func (c *Client) peerConnectToMe(peer *Peer, adcToken string) {
if c.protoIsAdc() {
c.hubConn.conn.Write(&protoadc.AdcDConnectToMe{ //nolint:govet
&adc.DirectPacket{ID: c.adcSessionID, To: peer.adcSessionID},
&adc.ConnectRequest{ //nolint:govet
func() string {
if c.conf.PeerEncryptionMode != DisableEncryption && c.peerSupportsEncryption(peer) {
return adc.ProtoADCS
}
return adc.ProtoADC
}(),
func() int {
if c.conf.PeerEncryptionMode != DisableEncryption && c.peerSupportsEncryption(peer) {
return int(c.conf.TLSPort)
}
return int(c.conf.TCPPort)
}(),
adcToken,
},
})
} else {
c.hubConn.conn.Write(&nmdc.ConnectToMe{
Targ: peer.Nick,
Address: fmt.Sprintf("%s:%d", c.ip, func() uint {
if c.conf.PeerEncryptionMode != DisableEncryption && c.peerSupportsEncryption(peer) {
return c.conf.TLSPort
}
return c.conf.TCPPort
}()),
Secure: (c.conf.PeerEncryptionMode != DisableEncryption && c.peerSupportsEncryption(peer)),
})
}
}
func (c *Client) peerRevConnectToMe(peer *Peer, adcToken string) {
if c.protoIsAdc() {
c.hubConn.conn.Write(&protoadc.AdcDRevConnectToMe{ //nolint:govet
&adc.DirectPacket{ID: c.adcSessionID, To: peer.adcSessionID},
&adc.RevConnectRequest{ //nolint:govet
func() string {
if c.conf.PeerEncryptionMode != DisableEncryption && c.peerSupportsEncryption(peer) {
return adc.ProtoADCS
}
return adc.ProtoADC
}(),
adcToken,
},
})
} else {
c.hubConn.conn.Write(&nmdc.RevConnectToMe{
From: c.conf.Nick,
To: peer.Nick,
})
}
}
func (c *Client) handlePeerConnected(peer *Peer) {
c.peers[peer.Nick] = peer
log.Log(c.conf.LogLevel, log.LevelInfo, "[hub] [peer on] %s (%v)", peer.Nick, peer.ShareSize)
if c.OnPeerConnected != nil {
c.OnPeerConnected(peer)
}
}
func (c *Client) handlePeerUpdated(peer *Peer) {
if c.OnPeerUpdated != nil {
c.OnPeerUpdated(peer)
}
}
func (c *Client) handlePeerDisconnected(peer *Peer) {
delete(c.peers, peer.Nick)
log.Log(c.conf.LogLevel, log.LevelInfo, "[hub] [peer off] %s", peer.Nick)
if c.OnPeerDisconnected != nil {
c.OnPeerDisconnected(peer)
}
}
func (c *Client) handlePeerRevConnectToMe(peer *Peer, adcToken string) {
// we can process RevConnectToMe only in active mode
if !c.conf.IsPassive {
c.peerConnectToMe(peer, adcToken)
}
}