p2p2 0.17.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca6aa5bbfa64978db926f846e6dce0c55124dc82b1b4930b9eb30959c5023cc7
4
- data.tar.gz: a246104c3a8a738fd0056aea6698220b3b4ffe0f0b343114123cdba2eba07752
3
+ metadata.gz: ed5928f54a6129f64cc843748d5f337d9f5fdccfed90239b9a594bf6ecd577bc
4
+ data.tar.gz: 1ec20b3e82bf69b5e371ceb89dcc666b97f0a2407072647271e481c7a3166e4e
5
5
  SHA512:
6
- metadata.gz: da481b7d08c79145f2fa340d753be73e3a67f3867aab76719e0823c7d13ed42cb4bb097a3056419b9fc2bc2ddd951a9cc3cfd9930eaca6e3c4571a1c29cd5288
7
- data.tar.gz: 4ababd5dc972ba2012e20fc653eef308f35111b48e0e3b57e3dd82641ef58b1585469b693f03c9faff0c435fed525d3a826ad0c8c8054333c1dde321bdfeb230
6
+ metadata.gz: 2b35b3bc6a54ceb236aee2f9258580c381397619dcdfbe4dfd3ea61c8c6deeda1e6e188a64b6921794d801fd2838f5db3c7e00462be3cebfe5158e589356a8ff
7
+ data.tar.gz: 9bd26990e4e3aff5285a10b011c322251ba3c64f5379e02172d70f16d9624355c8a9ec3f194b108c3681b966b38749d4bded464a9c9588bf3aca4e7645026e3f
@@ -6,11 +6,12 @@ module P2p2
6
6
  RESUME_BELOW = 50_000 # 降到多少以下恢复写
7
7
  EXPIRE_NEW = 10 # 创建之后多久没有流量进来,过期
8
8
  EXPIRE_AFTER = 300 # 多久没有新流量,过期
9
- CHECK_EXPIRE_INTERVAL = 30 # 检查过期间隔
9
+ CHECK_EXPIRE_INTERVAL = 60 # 检查过期间隔
10
10
  STATUS_INTERVAL = 0.5 # 发送状态间隔
11
11
  SEND_STATUS_UNTIL = 10 # 持续的告之对面状态,直到没有流量往来,持续多少秒
12
12
  BREAK_SEND_MISS = 10_000 # miss包个数上限,达到上限忽略要后面的段,可控碎片缓存
13
13
  CONFUSE_UNTIL = 5 # 混淆前几个包
14
+ HEARTBEAT_INTERVAL = 5 # 心跳间隔
14
15
  UPDATE_ROOM_INTERVAL = 60 # 刷新房间间隔
15
16
  PEER_ADDR = 1
16
17
  HEARTBEAT = 2
@@ -28,8 +28,7 @@ module P2p2
28
28
  #
29
29
  def looping
30
30
  puts "#{ Time.new } looping"
31
- loop_update_room
32
- loop_check_expire
31
+ loop_heartbeat
33
32
  loop_check_status
34
33
 
35
34
  loop do
@@ -80,67 +79,65 @@ module P2p2
80
79
  private
81
80
 
82
81
  ##
83
- # loop update room
82
+ # loop heartbeat
84
83
  #
85
- def loop_update_room
84
+ def loop_heartbeat( check_at = Time.new )
86
85
  Thread.new do
87
86
  loop do
88
- sleep UPDATE_ROOM_INTERVAL
87
+ sleep HEARTBEAT_INTERVAL
89
88
 
90
89
  @mutex.synchronize do
91
- if !@tund.closed? && @tund_info[ :peer_addr ].nil?
92
- add_tund_ctlmsg( @room, @p2pd_addr )
93
- next_tick
94
- end
95
- end
96
- end
97
- end
98
- end
99
-
100
- ##
101
- # loop check expire
102
- #
103
- def loop_check_expire
104
- Thread.new do
105
- loop do
106
- sleep CHECK_EXPIRE_INTERVAL
107
-
108
- @mutex.synchronize do
109
- need_trigger = false
110
90
  now = Time.new
111
91
 
112
- if !@tund.closed? && @tund_info[ :tun_addr ]
113
- if now - @tund_info[ :last_recv_at ] > EXPIRE_AFTER
114
- puts "#{ Time.new } expire tund"
115
- set_is_closing( @tund )
116
- else
117
- # puts "debug1 #{ Time.new } heartbeat"
118
- add_tund_ctlmsg( pack_a_heartbeat )
119
-
120
- @tund_info[ :dst_exts ].each do | dst_local_port, dst_ext |
121
- if dst_ext[ :dst ].closed? && ( now - dst_ext[ :last_continue_at ] > EXPIRE_AFTER )
122
- puts "#{ Time.new } expire dst ext #{ dst_local_port }"
123
- del_dst_ext( dst_local_port )
92
+ unless @tund.closed?
93
+ if @tund_info[ :peer_addr ]
94
+ if @tund_info[ :tun_addr ]
95
+ if now - check_at >= CHECK_EXPIRE_INTERVAL
96
+ if now - @tund_info[ :last_recv_at ] > EXPIRE_AFTER
97
+ puts "#{ Time.new } expire tund"
98
+ set_is_closing( @tund )
99
+ else
100
+ @tund_info[ :dst_exts ].each do | dst_local_port, dst_ext |
101
+ if dst_ext[ :dst ].closed? && ( now - dst_ext[ :last_continue_at ] > EXPIRE_AFTER )
102
+ puts "#{ Time.new } expire dst ext #{ dst_local_port }"
103
+ del_dst_ext( dst_local_port )
104
+ end
105
+ end
106
+ end
107
+
108
+ @dst_infos.each do | dst, dst_info |
109
+ if dst_info[ :last_recv_at ].nil? && ( now - dst_info[ :created_at ] > EXPIRE_NEW )
110
+ puts "#{ Time.new } expire dst"
111
+ set_is_closing( dst )
112
+ end
113
+ end
114
+
115
+ check_at = now
124
116
  end
125
- end
126
- end
127
117
 
128
- need_trigger = true
129
- end
130
-
131
- @dst_infos.each do | dst, dst_info |
132
- is_expired = dst_info[ :last_recv_at ].nil? && ( now - dst_info[ :created_at ] > EXPIRE_NEW )
118
+ # puts "debug2 heartbeat"
119
+ add_tund_ctlmsg( pack_a_heartbeat )
120
+ next_tick
121
+ elsif now - @tund_info[ :created_at ] > EXPIRE_NEW
122
+ # no tun addr
123
+ puts "#{ Time.new } expire new tund"
124
+ set_is_closing( @tund )
125
+ next_tick
126
+ end
127
+ else
128
+ # no peer addr
129
+ if now - check_at >= UPDATE_ROOM_INTERVAL
130
+ data = @room
131
+ check_at = now
132
+ else
133
+ data = [ rand( 128 ) ].pack( 'C' )
134
+ end
133
135
 
134
- if is_expired
135
- puts "#{ Time.new } expire dst"
136
- set_is_closing( dst )
137
- need_trigger = true
136
+ # puts "debug2 update room"
137
+ add_tund_ctlmsg( data, @p2pd_addr )
138
+ next_tick
138
139
  end
139
140
  end
140
-
141
- if need_trigger
142
- next_tick
143
- end
144
141
  end
145
142
  end
146
143
  end
@@ -252,6 +249,8 @@ module P2p2
252
249
  # is match tun addr
253
250
  #
254
251
  def is_match_tun_addr( addrinfo )
252
+ return false unless @tund_info[ :tun_addr ]
253
+
255
254
  from_addr = addrinfo.to_sockaddr
256
255
 
257
256
  if from_addr != @tund_info[ :tun_addr ]
@@ -260,6 +259,7 @@ module P2p2
260
259
  end
261
260
 
262
261
  @tund_info[ :last_recv_at ] = Time.new
262
+
263
263
  true
264
264
  end
265
265
 
@@ -539,6 +539,10 @@ module P2p2
539
539
  tund.sendmsg( data, 0, to_addr )
540
540
  rescue IO::WaitWritable, Errno::EINTR
541
541
  return
542
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
543
+ puts "#{ Time.new } #{ e.class }, close tund"
544
+ close_tund( tund )
545
+ return
542
546
  end
543
547
 
544
548
  @tund_info[ :ctlmsgs ].shift
@@ -557,6 +561,10 @@ module P2p2
557
561
  tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
558
562
  rescue IO::WaitWritable, Errno::EINTR
559
563
  return
564
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
565
+ puts "#{ Time.new } #{ e.class }, close tund"
566
+ close_tund( tund )
567
+ return
560
568
  end
561
569
  end
562
570
  end
@@ -625,6 +633,10 @@ module P2p2
625
633
  tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
626
634
  rescue IO::WaitWritable, Errno::EINTR
627
635
  return
636
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
637
+ puts "#{ Time.new } #{ e.class }, close tund"
638
+ close_tund( tund )
639
+ return
628
640
  end
629
641
 
630
642
  # puts "debug2 written pack #{ pack_id }"
@@ -28,7 +28,7 @@ module P2p2
28
28
  #
29
29
  def looping
30
30
  puts "#{ Time.new } looping"
31
- loop_check_expire
31
+ loop_heartbeat
32
32
  loop_check_status
33
33
 
34
34
  loop do
@@ -81,49 +81,51 @@ module P2p2
81
81
  private
82
82
 
83
83
  ##
84
- # loop check expire
84
+ # loop heartbeat
85
85
  #
86
- def loop_check_expire
86
+ def loop_heartbeat( check_at = Time.new )
87
87
  Thread.new do
88
88
  loop do
89
- sleep CHECK_EXPIRE_INTERVAL
89
+ sleep HEARTBEAT_INTERVAL
90
90
 
91
91
  @mutex.synchronize do
92
- need_trigger = false
93
92
  now = Time.new
94
93
 
95
- if @tun && !@tun.closed? && @tun_info[ :tund_addr ]
96
- if now - @tun_info[ :last_recv_at ] > EXPIRE_AFTER
97
- puts "#{ Time.new } expire tun"
98
- set_is_closing( @tun )
99
- else
100
- # puts "debug1 #{ Time.new } heartbeat"
101
- add_tun_ctlmsg( pack_a_heartbeat )
102
-
103
- @tun_info[ :src_exts ].each do | src_id, src_ext |
104
- if src_ext[ :src ].closed? && ( now - src_ext[ :last_continue_at ] > EXPIRE_AFTER )
105
- puts "#{ Time.new } expire src ext #{ src_id }"
106
- del_src_ext( src_id )
94
+ if @tun && !@tun.closed? && @tun_info[ :peer_addr ]
95
+ if @tun_info[ :tund_addr ]
96
+ if now - check_at >= CHECK_EXPIRE_INTERVAL
97
+ if now - @tun_info[ :last_recv_at ] > EXPIRE_AFTER
98
+ puts "#{ Time.new } expire tun"
99
+ set_is_closing( @tun )
100
+ else
101
+ @tun_info[ :src_exts ].each do | src_id, src_ext |
102
+ if src_ext[ :src ].closed? && ( now - src_ext[ :last_continue_at ] > EXPIRE_AFTER )
103
+ puts "#{ Time.new } expire src ext #{ src_id }"
104
+ del_src_ext( src_id )
105
+ end
106
+ end
107
107
  end
108
- end
109
- end
110
108
 
111
- need_trigger = true
112
- end
109
+ @src_infos.each do | src, src_info |
110
+ if src_info[ :last_recv_at ].nil? && ( now - src_info[ :created_at ] > EXPIRE_NEW )
111
+ puts "#{ Time.new } expire src"
112
+ set_is_closing( src )
113
+ end
114
+ end
113
115
 
114
- @src_infos.each do | src, src_info |
115
- is_expired = src_info[ :last_recv_at ].nil? && ( now - src_info[ :created_at ] > EXPIRE_NEW )
116
+ check_at = now
117
+ end
116
118
 
117
- if is_expired
118
- puts "#{ Time.new } expire src"
119
- set_is_closing( src )
120
- need_trigger = true
119
+ # puts "debug2 heartbeat"
120
+ add_tun_ctlmsg( pack_a_heartbeat )
121
+ next_tick
122
+ elsif now - @tun_info[ :created_at ] > EXPIRE_NEW
123
+ # no tund addr
124
+ puts "#{ Time.new } expire new tun"
125
+ set_is_closing( @tun )
126
+ next_tick
121
127
  end
122
128
  end
123
-
124
- if need_trigger
125
- next_tick
126
- end
127
129
  end
128
130
  end
129
131
  end
@@ -275,6 +277,8 @@ module P2p2
275
277
  # is match tund addr
276
278
  #
277
279
  def is_match_tund_addr( addrinfo )
280
+ return false unless @tun_info[ :tund_addr ]
281
+
278
282
  from_addr = addrinfo.to_sockaddr
279
283
 
280
284
  if from_addr != @tun_info[ :tund_addr ]
@@ -283,6 +287,7 @@ module P2p2
283
287
  end
284
288
 
285
289
  @tun_info[ :last_recv_at ] = Time.new
290
+
286
291
  true
287
292
  end
288
293
 
@@ -560,6 +565,10 @@ module P2p2
560
565
  tun.sendmsg( data, 0, to_addr )
561
566
  rescue IO::WaitWritable, Errno::EINTR
562
567
  return
568
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
569
+ puts "#{ Time.new } #{ e.class }, close tun"
570
+ close_tun( tun )
571
+ return
563
572
  end
564
573
 
565
574
  @tun_info[ :ctlmsgs ].shift
@@ -578,6 +587,10 @@ module P2p2
578
587
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
579
588
  rescue IO::WaitWritable, Errno::EINTR
580
589
  return
590
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
591
+ puts "#{ Time.new } #{ e.class }, close tun"
592
+ close_tun( tun )
593
+ return
581
594
  end
582
595
  end
583
596
  end
@@ -646,6 +659,10 @@ module P2p2
646
659
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
647
660
  rescue IO::WaitWritable, Errno::EINTR
648
661
  return
662
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
663
+ puts "#{ Time.new } #{ e.class }, close tun"
664
+ close_tun( tun )
665
+ return
649
666
  end
650
667
 
651
668
  # puts "debug2 written pack #{ pack_id }"
@@ -1,3 +1,3 @@
1
1
  module P2p2
2
- VERSION = '0.17.0'.freeze
2
+ VERSION = '0.22.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: p2p2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.0
4
+ version: 0.22.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - takafan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-03 00:00:00.000000000 Z
11
+ date: 2020-07-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 内网里的任意应用,访问另一个内网里的应用服务端。
14
14
  email: