@@ -93,9 +93,9 @@ export class Network extends EventEmitter {
93
93
94
94
shutdown ( ) {
95
95
this . isClosing = true ;
96
- this . agent && this . agent . destroy ( ) ;
97
- this . samForward && this . samForward . close ( ) ;
98
- this . samUDP && this . samUDP . close ( ) ;
96
+ typeof this . agent . destroy === 'function' && this . agent . destroy ( ) ;
97
+ typeof this . samForward . close === 'function' && this . samForward . close ( ) ;
98
+ typeof this . samUDP . close === 'function' && this . samUDP . close ( ) ;
99
99
clearTimeout ( this . timeoutP2P ) ;
100
100
clearTimeout ( this . timeoutClean ) ;
101
101
}
@@ -105,16 +105,16 @@ export class Network extends EventEmitter {
105
105
106
106
let retry = 0 ;
107
107
let started = false ;
108
- const i = setInterval ( ( ) => {
108
+ const i = setInterval ( async ( ) => {
109
109
retry ++ ;
110
- if ( retry > 60 ) {
110
+ if ( retry > 10 ) {
111
111
throw new Error ( `P2P failed on ${ toB32 ( this . server . config . udp ) } .b32.i2p` ) ;
112
112
}
113
113
114
114
if ( started ) {
115
115
if ( this . hasP2PNetwork ( ) ) {
116
- this . emit ( 'ready' ) ;
117
116
clearInterval ( i ) ;
117
+ this . emit ( 'ready' ) ;
118
118
Logger . info ( `P2P ready on ${ toB32 ( this . server . config . udp ) } .b32.i2p` ) ;
119
119
}
120
120
return ;
@@ -123,57 +123,50 @@ export class Network extends EventEmitter {
123
123
started = true ;
124
124
this . p2pNetwork ( ) ;
125
125
126
- ( async ( ) => {
127
- const _c = this . server . config ;
128
- this . samForward = (
129
- await createForward ( {
130
- sam : {
131
- host : _c . i2p_sam_http_host ,
132
- portTCP : _c . i2p_sam_http_port_tcp ,
133
- publicKey : _c . i2p_public_key_http ,
134
- privateKey : _c . i2p_private_key_http ,
135
- } ,
136
- forward : {
137
- host : _c . i2p_sam_forward_http_host ,
138
- port : _c . i2p_sam_forward_http_port ,
139
- silent : true ,
140
- } ,
141
- } )
142
- ) . on ( 'error' , ( error : any ) => {
143
- Logger . warn ( 'SAM HTTP ' + error . toString ( ) ) ;
126
+ const _c = this . server . config ;
127
+ this . samForward = (
128
+ await createForward ( {
129
+ sam : {
130
+ host : _c . i2p_sam_http_host ,
131
+ portTCP : _c . i2p_sam_http_port_tcp ,
132
+ publicKey : _c . i2p_public_key_http ,
133
+ privateKey : _c . i2p_private_key_http ,
134
+ } ,
135
+ forward : {
136
+ host : _c . i2p_sam_forward_http_host ,
137
+ port : _c . i2p_sam_forward_http_port ,
138
+ silent : true ,
139
+ } ,
140
+ } )
141
+ ) . on ( 'error' , ( error : any ) => {
142
+ Logger . warn ( 'SAM HTTP ' + error . toString ( ) ) ;
143
+ } ) ;
144
+ Logger . info ( `HTTP ${ toB32 ( _c . http ) } .b32.i2p to ${ _c . i2p_sam_forward_http_host } :${ _c . i2p_sam_forward_http_port } ` ) ;
145
+
146
+ this . samUDP = (
147
+ await createDatagram ( {
148
+ sam : {
149
+ host : _c . i2p_sam_udp_host ,
150
+ portTCP : _c . i2p_sam_udp_port_tcp ,
151
+ publicKey : _c . i2p_public_key_udp ,
152
+ privateKey : _c . i2p_private_key_udp ,
153
+ } ,
154
+ listen : {
155
+ address : _c . i2p_sam_listen_udp_host ,
156
+ port : _c . i2p_sam_listen_udp_port ,
157
+ hostForward : _c . i2p_sam_forward_udp_host ,
158
+ portForward : _c . i2p_sam_forward_udp_port ,
159
+ } ,
160
+ } )
161
+ )
162
+ . on ( 'data' , ( data : Buffer , from : string ) => {
163
+ this . incomingData ( data , from ) ;
164
+ } )
165
+ . on ( 'error' , ( error : any ) => {
166
+ Logger . warn ( 'SAM UDP ' + error . toString ( ) ) ;
144
167
} ) ;
145
- Logger . info (
146
- `HTTP ${ toB32 ( _c . http ) } .b32.i2p to ${ _c . i2p_sam_forward_http_host } :${ _c . i2p_sam_forward_http_port } `
147
- ) ;
148
- } ) ( ) ;
149
-
150
- ( async ( ) => {
151
- const _c = this . server . config ;
152
- this . samUDP = (
153
- await createDatagram ( {
154
- sam : {
155
- host : _c . i2p_sam_udp_host ,
156
- portTCP : _c . i2p_sam_udp_port_tcp ,
157
- publicKey : _c . i2p_public_key_udp ,
158
- privateKey : _c . i2p_private_key_udp ,
159
- } ,
160
- listen : {
161
- address : _c . i2p_sam_listen_udp_host ,
162
- port : _c . i2p_sam_listen_udp_port ,
163
- hostForward : _c . i2p_sam_forward_udp_host ,
164
- portForward : _c . i2p_sam_forward_udp_port ,
165
- } ,
166
- } )
167
- )
168
- . on ( 'data' , ( data : Buffer , from : string ) => {
169
- this . incomingData ( data , from ) ;
170
- } )
171
- . on ( 'error' , ( error : any ) => {
172
- Logger . warn ( 'SAM UDP ' + error . toString ( ) ) ;
173
- } ) ;
174
- Logger . info ( `UDP ${ toB32 ( _c . udp ) } .b32.i2p to ${ _c . i2p_sam_forward_udp_host } :${ _c . i2p_sam_forward_udp_port } ` ) ;
175
- } ) ( ) ;
176
- } , 1000 ) ;
168
+ Logger . info ( `UDP ${ toB32 ( _c . udp ) } .b32.i2p to ${ _c . i2p_sam_forward_udp_host } :${ _c . i2p_sam_forward_udp_port } ` ) ;
169
+ } , 10000 ) ;
177
170
}
178
171
179
172
private hasP2PNetwork ( ) : Boolean {
@@ -286,16 +279,16 @@ export class Network extends EventEmitter {
286
279
287
280
const aNetwork = Util . shuffleArray ( this . arrayNetwork . filter ( ( v ) => v . http !== this . server . config . http ) ) ;
288
281
let urlApi = '' ;
289
- do {
290
- urlApi = `http://${ toB32 ( aNetwork . pop ( ) . http ) } .b32.i2p/${ endpoint } ` ;
282
+ let n = aNetwork . pop ( ) ;
283
+ while ( n ) {
284
+ urlApi = `http://${ toB32 ( n . http ) } .b32.i2p/${ endpoint } ` ;
291
285
try {
292
- const json = await this . fetch ( urlApi , timeout ) ;
293
- return JSON . parse ( json ) ;
286
+ return JSON . parse ( await this . fetch ( urlApi , timeout ) ) ;
294
287
} catch ( error : any ) {
295
288
Logger . warn ( `Network.fetchFromApi() ${ urlApi } - ${ error . toString ( ) } ` ) ;
296
289
}
297
- } while ( aNetwork . length > 0 ) ;
298
-
290
+ n = aNetwork . pop ( ) ;
291
+ }
299
292
throw new Error ( 'fetchFromApi failed' ) ;
300
293
}
301
294
0 commit comments