@@ -66,22 +66,7 @@ func nodeID(s string) string {
66
66
return fmt .Sprintf ("%s-%s" , hostname , s )
67
67
}
68
68
69
- func allocateLocalService (ctx context.Context , node * node.Node , listenAddr , service string ) error {
70
-
71
- zlog .Info ().Msgf ("Allocating service '%s' on: %s" , service , listenAddr )
72
- // Open local port for listening
73
- l , err := net .Listen ("tcp" , listenAddr )
74
- if err != nil {
75
- zlog .Error ().Err (err ).Msg ("Error listening" )
76
- return err
77
- }
78
- go func () {
79
- <- ctx .Done ()
80
- l .Close ()
81
- }()
82
-
83
- // ll.Info("Binding local port on", srcaddr)
84
-
69
+ func nodeAnnounce (ctx context.Context , node * node.Node ) {
85
70
ledger , _ := node .Ledger ()
86
71
87
72
// Announce ourselves so nodes accepts our connection
@@ -97,6 +82,66 @@ func allocateLocalService(ctx context.Context, node *node.Node, listenAddr, serv
97
82
ledger .Add (protocol .UsersLedgerKey , updatedMap )
98
83
},
99
84
)
85
+ }
86
+
87
+ func proxyP2PConnection (ctx context.Context , node * node.Node , serviceID string , conn net.Conn ) {
88
+ ledger , _ := node .Ledger ()
89
+ // Retrieve current ID for ip in the blockchain
90
+ existingValue , found := ledger .GetKey (protocol .ServicesLedgerKey , serviceID )
91
+ service := & types.Service {}
92
+ existingValue .Unmarshal (service )
93
+ // If mismatch, update the blockchain
94
+ if ! found {
95
+ zlog .Error ().Msg ("Service not found on blockchain" )
96
+ conn .Close ()
97
+ // ll.Debugf("service '%s' not found on blockchain", serviceID)
98
+ return
99
+ }
100
+
101
+ // Decode the Peer
102
+ d , err := peer .Decode (service .PeerID )
103
+ if err != nil {
104
+ zlog .Error ().Msg ("cannot decode peer" )
105
+
106
+ conn .Close ()
107
+ // ll.Debugf("could not decode peer '%s'", service.PeerID)
108
+ return
109
+ }
110
+
111
+ // Open a stream
112
+ stream , err := node .Host ().NewStream (ctx , d , protocol .ServiceProtocol .ID ())
113
+ if err != nil {
114
+ zlog .Error ().Err (err ).Msg ("cannot open stream peer" )
115
+
116
+ conn .Close ()
117
+ // ll.Debugf("could not open stream '%s'", err.Error())
118
+ return
119
+ }
120
+ // ll.Debugf("(service %s) Redirecting", serviceID, l.Addr().String())
121
+ zlog .Info ().Msgf ("Redirecting %s to %s" , conn .LocalAddr ().String (), stream .Conn ().RemoteMultiaddr ().String ())
122
+ closer := make (chan struct {}, 2 )
123
+ go copyStream (closer , stream , conn )
124
+ go copyStream (closer , conn , stream )
125
+ <- closer
126
+
127
+ stream .Close ()
128
+ conn .Close ()
129
+ }
130
+
131
+ func allocateLocalService (ctx context.Context , node * node.Node , listenAddr , service string ) error {
132
+ zlog .Info ().Msgf ("Allocating service '%s' on: %s" , service , listenAddr )
133
+ // Open local port for listening
134
+ l , err := net .Listen ("tcp" , listenAddr )
135
+ if err != nil {
136
+ zlog .Error ().Err (err ).Msg ("Error listening" )
137
+ return err
138
+ }
139
+ go func () {
140
+ <- ctx .Done ()
141
+ l .Close ()
142
+ }()
143
+
144
+ nodeAnnounce (ctx , node )
100
145
101
146
defer l .Close ()
102
147
for {
@@ -114,47 +159,7 @@ func allocateLocalService(ctx context.Context, node *node.Node, listenAddr, serv
114
159
115
160
// Handle connections in a new goroutine, forwarding to the p2p service
116
161
go func () {
117
- // Retrieve current ID for ip in the blockchain
118
- existingValue , found := ledger .GetKey (protocol .ServicesLedgerKey , service )
119
- service := & types.Service {}
120
- existingValue .Unmarshal (service )
121
- // If mismatch, update the blockchain
122
- if ! found {
123
- zlog .Error ().Msg ("Service not found on blockchain" )
124
- conn .Close ()
125
- // ll.Debugf("service '%s' not found on blockchain", serviceID)
126
- return
127
- }
128
-
129
- // Decode the Peer
130
- d , err := peer .Decode (service .PeerID )
131
- if err != nil {
132
- zlog .Error ().Msg ("cannot decode peer" )
133
-
134
- conn .Close ()
135
- // ll.Debugf("could not decode peer '%s'", service.PeerID)
136
- return
137
- }
138
-
139
- // Open a stream
140
- stream , err := node .Host ().NewStream (ctx , d , protocol .ServiceProtocol .ID ())
141
- if err != nil {
142
- zlog .Error ().Msg ("cannot open stream peer" )
143
-
144
- conn .Close ()
145
- // ll.Debugf("could not open stream '%s'", err.Error())
146
- return
147
- }
148
- // ll.Debugf("(service %s) Redirecting", serviceID, l.Addr().String())
149
- zlog .Info ().Msgf ("Redirecting %s to %s" , conn .LocalAddr ().String (), stream .Conn ().RemoteMultiaddr ().String ())
150
- closer := make (chan struct {}, 2 )
151
- go copyStream (closer , stream , conn )
152
- go copyStream (closer , conn , stream )
153
- <- closer
154
-
155
- stream .Close ()
156
- conn .Close ()
157
- // ll.Infof("(service %s) Done handling %s", serviceID, l.Addr().String())
162
+ proxyP2PConnection (ctx , node , service , conn )
158
163
}()
159
164
}
160
165
}
@@ -258,6 +263,7 @@ var muservice sync.Mutex
258
263
func ensureService (ctx context.Context , n * node.Node , nd * NodeData , sserv string , allocate bool ) {
259
264
muservice .Lock ()
260
265
defer muservice .Unlock ()
266
+ nd .ServiceID = sserv
261
267
if ndService , found := service [nd .Name ]; ! found {
262
268
if ! nd .IsOnline () {
263
269
// if node is offline and not present, do nothing
0 commit comments