p2p2 0.18.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69c08f082bef92a9287f03eb20927a2dbb3913f99c3b7bc76ea9db0a088417d8
4
- data.tar.gz: c9b9f43b4007240b38521e6f2450220b487309b2bace4456ca1ea281acc93193
3
+ metadata.gz: 93622e13a1c4a94debd120c4295bed86f0d451f83f05ecfc4bb2b29f63320a83
4
+ data.tar.gz: edcf0aefb969653a5b73201722ac2ab61a24488f5124a0cba3d78f2c0fc17073
5
5
  SHA512:
6
- metadata.gz: 482067dd01dc0996137473e968396d21420f15bcccc2ede0fbf38e45cf55117925d355d5aebe19b8c88a7cd70dc2f08aa43009b48030a981e256ce8e846f10cb
7
- data.tar.gz: 57a677e430ba6fd813207e10a5bfa769ec922f8f9759562f40468f4eac08f85bea0635672eda75d2e6578937aa96c0fd3ffb43eb61768cf4a3837518c0117f57
6
+ metadata.gz: 558629edb45e53c9a9d0f1ad964e91b3b89c313181e8bbce2477ad0fbdad7aae43db37f59235340e4004dd6390b555817c513b88417c9e95fa9fbcdb2083e772
7
+ data.tar.gz: 387354b5932fb4a7994a1970e649521d357db94e7eb6521fa33991ccd25a4436c2064e43508e51adeb5827871f309cb41dbe0caf2c22fa18a375d269e8606232
@@ -6,12 +6,13 @@ 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
- UPDATE_ROOM_INTERVAL = 5 # 刷新房间间隔
14
+ HEARTBEAT_INTERVAL = 5 # 心跳间隔
15
+ UPDATE_ROOM_INTERVAL = 60 # 刷新房间间隔
15
16
  PEER_ADDR = 1
16
17
  HEARTBEAT = 2
17
18
  A_NEW_SOURCE = 3
@@ -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,76 +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( update_at = Time.new )
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
- now = Time.new
93
-
94
- if now - update_at >= 60
95
- data = @room
96
- update_at = now
97
- else
98
- data = [ rand( 128 ) ].pack( 'C' )
99
- end
100
-
101
- add_tund_ctlmsg( data, @p2pd_addr )
102
- next_tick
103
- end
104
- end
105
- end
106
- end
107
- end
108
-
109
- ##
110
- # loop check expire
111
- #
112
- def loop_check_expire
113
- Thread.new do
114
- loop do
115
- sleep CHECK_EXPIRE_INTERVAL
116
-
117
- @mutex.synchronize do
118
- need_trigger = false
119
90
  now = Time.new
120
91
 
121
- if !@tund.closed? && @tund_info[ :tun_addr ]
122
- if now - @tund_info[ :last_recv_at ] > EXPIRE_AFTER
123
- puts "#{ Time.new } expire tund"
124
- set_is_closing( @tund )
125
- else
126
- # puts "debug1 #{ Time.new } heartbeat"
127
- add_tund_ctlmsg( pack_a_heartbeat )
128
-
129
- @tund_info[ :dst_exts ].each do | dst_local_port, dst_ext |
130
- if dst_ext[ :dst ].closed? && ( now - dst_ext[ :last_continue_at ] > EXPIRE_AFTER )
131
- puts "#{ Time.new } expire dst ext #{ dst_local_port }"
132
- 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"
103
+ del_dst_ext( dst_local_port )
104
+ end
105
+ end
106
+ end
107
+
108
+ @dst_infos.each do | dst, dst_info |
109
+ if now - dst_info[ :last_continue_at ] > EXPIRE_AFTER
110
+ puts "#{ Time.new } expire dst"
111
+ set_is_closing( dst )
112
+ end
113
+ end
114
+
115
+ check_at = now
133
116
  end
134
- end
135
- end
136
117
 
137
- need_trigger = true
138
- end
139
-
140
- @dst_infos.each do | dst, dst_info |
141
- 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
142
135
 
143
- if is_expired
144
- puts "#{ Time.new } expire dst"
145
- set_is_closing( dst )
146
- need_trigger = true
136
+ # puts "debug2 update room"
137
+ add_tund_ctlmsg( data, @p2pd_addr )
138
+ next_tick
147
139
  end
148
140
  end
149
-
150
- if need_trigger
151
- next_tick
152
- end
153
141
  end
154
142
  end
155
143
  end
@@ -261,6 +249,8 @@ module P2p2
261
249
  # is match tun addr
262
250
  #
263
251
  def is_match_tun_addr( addrinfo )
252
+ return false unless @tund_info[ :tun_addr ]
253
+
264
254
  from_addr = addrinfo.to_sockaddr
265
255
 
266
256
  if from_addr != @tund_info[ :tun_addr ]
@@ -269,6 +259,7 @@ module P2p2
269
259
  end
270
260
 
271
261
  @tund_info[ :last_recv_at ] = Time.new
262
+
272
263
  true
273
264
  end
274
265
 
@@ -528,6 +519,7 @@ module P2p2
528
519
  # puts "debug2 write dst #{ written }"
529
520
  data = data[ written..-1 ]
530
521
  dst_info[ from ] = data
522
+ dst_info[ :last_continue_at ] = Time.new
531
523
  end
532
524
 
533
525
  ##
@@ -548,6 +540,10 @@ module P2p2
548
540
  tund.sendmsg( data, 0, to_addr )
549
541
  rescue IO::WaitWritable, Errno::EINTR
550
542
  return
543
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
544
+ puts "#{ Time.new } #{ e.class }, close tund"
545
+ close_tund( tund )
546
+ return
551
547
  end
552
548
 
553
549
  @tund_info[ :ctlmsgs ].shift
@@ -566,6 +562,10 @@ module P2p2
566
562
  tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
567
563
  rescue IO::WaitWritable, Errno::EINTR
568
564
  return
565
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
566
+ puts "#{ Time.new } #{ e.class }, close tund"
567
+ close_tund( tund )
568
+ return
569
569
  end
570
570
  end
571
571
  end
@@ -634,6 +634,10 @@ module P2p2
634
634
  tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
635
635
  rescue IO::WaitWritable, Errno::EINTR
636
636
  return
637
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
638
+ puts "#{ Time.new } #{ e.class }, close tund"
639
+ close_tund( tund )
640
+ return
637
641
  end
638
642
 
639
643
  # puts "debug2 written pack #{ pack_id }"
@@ -670,7 +674,7 @@ module P2p2
670
674
 
671
675
  # puts "debug2 read dst #{ data.inspect }"
672
676
  dst_info = @dst_infos[ dst ]
673
- dst_info[ :last_recv_at ] = Time.new
677
+ dst_info[ :last_continue_at ] = Time.new
674
678
 
675
679
  if @tund.closed?
676
680
  puts "#{ Time.new } tund closed, close dst"
@@ -748,8 +752,7 @@ module P2p2
748
752
  cache: '', # 块读出缓存
749
753
  chunks: [], # 块队列,写前达到块大小时结一个块 filename
750
754
  spring: 0, # 块后缀,结块时,如果块队列不为空,则自增,为空,则置为0
751
- created_at: Time.new, # 创建时间
752
- last_recv_at: nil, # 上一次收到流量的时间,过期关闭
755
+ last_continue_at: Time.new, # 上一次发生流量的时间
753
756
  is_closing: false # 是否准备关闭
754
757
  }
755
758
  add_read( dst, :dst )
@@ -766,7 +769,7 @@ module P2p2
766
769
  is_src_closed: false, # src是否已关闭
767
770
  biggest_src_pack_id: 0, # src最大包号码
768
771
  completed_pack_id: 0, # 完成到几(对面收到几)
769
- last_continue_at: Time.new # 创建,或者上一次收到连续流量,或者发出新包的时间
772
+ last_continue_at: Time.new # 上一次发生流量的时间
770
773
  }
771
774
  end
772
775
 
@@ -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"
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 now - src_info[ :last_continue_at ] > EXPIRE_AFTER
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
@@ -261,7 +263,7 @@ module P2p2
261
263
  @tun = tun
262
264
  @tun_info = tun_info
263
265
  add_read( tun, :tun )
264
- add_tun_ctlmsg( @room, @p2pd_addr )
266
+ add_tun_ctlmsg( [ '2', @room ].join, @p2pd_addr )
265
267
  end
266
268
 
267
269
  ##
@@ -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
 
@@ -541,6 +546,7 @@ module P2p2
541
546
  # puts "debug2 write src #{ written }"
542
547
  data = data[ written..-1 ]
543
548
  src_info[ from ] = data
549
+ src_info[ :last_continue_at ] = Time.new
544
550
  end
545
551
 
546
552
  ##
@@ -560,6 +566,10 @@ module P2p2
560
566
  tun.sendmsg( data, 0, to_addr )
561
567
  rescue IO::WaitWritable, Errno::EINTR
562
568
  return
569
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
570
+ puts "#{ Time.new } #{ e.class }, close tun"
571
+ close_tun( tun )
572
+ return
563
573
  end
564
574
 
565
575
  @tun_info[ :ctlmsgs ].shift
@@ -578,6 +588,10 @@ module P2p2
578
588
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
579
589
  rescue IO::WaitWritable, Errno::EINTR
580
590
  return
591
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
592
+ puts "#{ Time.new } #{ e.class }, close tun"
593
+ close_tun( tun )
594
+ return
581
595
  end
582
596
  end
583
597
  end
@@ -646,6 +660,10 @@ module P2p2
646
660
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
647
661
  rescue IO::WaitWritable, Errno::EINTR
648
662
  return
663
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
664
+ puts "#{ Time.new } #{ e.class }, close tun"
665
+ close_tun( tun )
666
+ return
649
667
  end
650
668
 
651
669
  # puts "debug2 written pack #{ pack_id }"
@@ -680,16 +698,15 @@ module P2p2
680
698
  # puts "debug1 accept a src #{ addrinfo.inspect } #{ id }"
681
699
 
682
700
  @src_infos[ src ] = {
683
- id: id, # id
684
- biggest_pack_id: 0, # 最大包号码
685
- rbuffs: [], # p1端dst未准备好,暂存流量 [ pack_id, data ]
686
- wbuff: '', # 写前
687
- cache: '', # 块读出缓存
688
- chunks: [], # 块队列,写前达到块大小时结一个块 filename
689
- spring: 0, # 块后缀,结块时,如果块队列不为空,则自增,为空,则置为0
690
- created_at: Time.new, # 创建时间
691
- last_recv_at: nil, # 上一次收到流量的时间,过期关闭
692
- is_closing: false # 是否准备关闭
701
+ id: id, # id
702
+ biggest_pack_id: 0, # 最大包号码
703
+ rbuffs: [], # p1端dst未准备好,暂存流量 [ pack_id, data ]
704
+ wbuff: '', # 写前
705
+ cache: '', # 块读出缓存
706
+ chunks: [], # 块队列,写前达到块大小时结一个块 filename
707
+ spring: 0, # 块后缀,结块时,如果块队列不为空,则自增,为空,则置为0
708
+ last_continue_at: Time.new, # 上一次发生流量的时间
709
+ is_closing: false # 是否准备关闭
693
710
  }
694
711
 
695
712
  add_read( src, :src )
@@ -709,7 +726,7 @@ module P2p2
709
726
  is_dst_closed: false, # dst是否已关闭
710
727
  biggest_dst_pack_id: 0, # dst最大包号码
711
728
  completed_pack_id: 0, # 完成到几(对面收到几)
712
- last_continue_at: Time.new # 创建,或者上一次收到连续流量,或者发出新包的时间
729
+ last_continue_at: Time.new # 上一次发生流量的时间
713
730
  }
714
731
 
715
732
  @tun_info[ :src_exts ][ id ] = src_ext
@@ -733,7 +750,7 @@ module P2p2
733
750
 
734
751
  # puts "debug2 read src #{ data.inspect }"
735
752
  src_info = @src_infos[ src ]
736
- src_info[ :last_recv_at ] = Time.new
753
+ src_info[ :last_continue_at ] = Time.new
737
754
  src_id = src_info[ :id ]
738
755
  src_ext = @tun_info[ :src_exts ][ src_id ]
739
756
 
@@ -32,17 +32,21 @@ module P2p2
32
32
  data, addrinfo, rflags, *controls = p2pd.recvmsg
33
33
  return if ( data.bytesize == 1 ) || ( data.bytesize > 255 ) || ( data =~ /\/|\.|\ / )
34
34
 
35
+ if data[0] == '2'
36
+ is_p2 = true
37
+ data = data[ 1..-1 ]
38
+ else
39
+ is_p2 = false
40
+ end
41
+
35
42
  from_addr = addrinfo.to_sockaddr
36
43
  room_path = File.join( @p2pd_tmp_dir, data.gsub( "\u0000" , '' ) )
37
44
 
38
- unless File.exist?( room_path )
39
- puts "#{ Time.new } create #{ room_path } #{ addrinfo.inspect }"
40
- write_room( room_path, from_addr )
41
- return
42
- end
43
-
44
- if Time.new - File.mtime( room_path ) > EXPIRE_AFTER
45
- puts "#{ Time.new } overwrite #{ room_path } #{ addrinfo.inspect }"
45
+ if is_p2
46
+ unless File.exist?( room_path )
47
+ return
48
+ end
49
+ else
46
50
  write_room( room_path, from_addr )
47
51
  return
48
52
  end
@@ -50,13 +54,9 @@ module P2p2
50
54
  op_addr = IO.binread( room_path )
51
55
  op_addrinfo = Addrinfo.new( op_addr )
52
56
 
53
- if ( addrinfo.ip_address == op_addrinfo.ip_address ) || ( addrinfo.ip_port == op_addrinfo.ip_port )
54
- write_room( room_path, from_addr )
55
- else
56
- puts "#{ Time.new } paired #{ addrinfo.inspect } #{ op_addrinfo.inspect }"
57
- send_pack( [ [ 0, PEER_ADDR ].pack( 'Q>C' ), op_addr ].join, from_addr )
58
- send_pack( [ [ 0, PEER_ADDR ].pack( 'Q>C' ), from_addr ].join, op_addr )
59
- end
57
+ puts "#{ Time.new } paired #{ addrinfo.inspect } #{ op_addrinfo.inspect }"
58
+ send_pack( [ [ 0, PEER_ADDR ].pack( 'Q>C' ), op_addr ].join, from_addr )
59
+ send_pack( [ [ 0, PEER_ADDR ].pack( 'Q>C' ), from_addr ].join, op_addr )
60
60
  end
61
61
 
62
62
  def write_room( room_path, data )
@@ -1,3 +1,3 @@
1
1
  module P2p2
2
- VERSION = '0.18.0'.freeze
2
+ VERSION = '0.23.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.18.0
4
+ version: 0.23.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-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: 内网里的任意应用,访问另一个内网里的应用服务端。
14
14
  email: