p2p2 0.19.0 → 0.24.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: b139ce8cd373285af412d083c4dcbe9a227d882d19d8cb5fdd1389849645c69c
4
- data.tar.gz: 451f47afc5b16d62b7d1c316b00eecc49f947c333abd69e1235539b43a61c1bd
3
+ metadata.gz: 4c19c4e83055ee003d93923f5bc75aa21c2cfb8b678dd09f35405f4ccd7e93cb
4
+ data.tar.gz: b827a910f9dcbcf48712eb308da98d3231bf8ebe3dc66bf2a07138ef318bbea8
5
5
  SHA512:
6
- metadata.gz: 3f504248e0c27936e246de5b90705a48d84682a858778bd7ab085f1c846375b0a249a11860a6cc0edc45f8ad7cb66cb66990d8fe3c3d91ea0a397bd40cfc67c6
7
- data.tar.gz: da4849fa322f4e98aa9e44a3f185a0d694e5bbefc11009aa303b94d61ecb113b89b7fba2eb4f5014264b4674236ce863c7f83fac61f64ee8e34e78b4ad572816
6
+ metadata.gz: e1b716246402ff1f8cd6771b0607b78c3d54e754a7ea0189fb9168ad71294569943b7d641c2d3d77046d440cbd50bb22eb4f9cac092741ce6a6555ad3bcb255a
7
+ data.tar.gz: 730efff6c7c46ddb64d5f87196be76a47fe4aaf74a6a57bee493c76b83f9accc1c0501c4b15aa88a5bc71bdbf6f4e6441e44174fea25cb29605c474bb5641798
@@ -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
-
137
- need_trigger = true
138
- end
139
117
 
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
@@ -531,6 +519,7 @@ module P2p2
531
519
  # puts "debug2 write dst #{ written }"
532
520
  data = data[ written..-1 ]
533
521
  dst_info[ from ] = data
522
+ dst_info[ :last_continue_at ] = Time.new
534
523
  end
535
524
 
536
525
  ##
@@ -551,6 +540,12 @@ module P2p2
551
540
  tund.sendmsg( data, 0, to_addr )
552
541
  rescue IO::WaitWritable, Errno::EINTR
553
542
  return
543
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
544
+ puts "#{ Time.new } #{ e.class }, close tund"
545
+ close_tund( tund )
546
+ sleep HEARTBEAT_INTERVAL
547
+ new_a_tund
548
+ return
554
549
  end
555
550
 
556
551
  @tund_info[ :ctlmsgs ].shift
@@ -569,6 +564,12 @@ module P2p2
569
564
  tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
570
565
  rescue IO::WaitWritable, Errno::EINTR
571
566
  return
567
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
568
+ puts "#{ Time.new } #{ e.class }, close tund"
569
+ close_tund( tund )
570
+ sleep HEARTBEAT_INTERVAL
571
+ new_a_tund
572
+ return
572
573
  end
573
574
  end
574
575
  end
@@ -637,6 +638,12 @@ module P2p2
637
638
  tund.sendmsg( data, 0, @tund_info[ :tun_addr ] )
638
639
  rescue IO::WaitWritable, Errno::EINTR
639
640
  return
641
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
642
+ puts "#{ Time.new } #{ e.class }, close tund"
643
+ close_tund( tund )
644
+ sleep HEARTBEAT_INTERVAL
645
+ new_a_tund
646
+ return
640
647
  end
641
648
 
642
649
  # puts "debug2 written pack #{ pack_id }"
@@ -673,7 +680,7 @@ module P2p2
673
680
 
674
681
  # puts "debug2 read dst #{ data.inspect }"
675
682
  dst_info = @dst_infos[ dst ]
676
- dst_info[ :last_recv_at ] = Time.new
683
+ dst_info[ :last_continue_at ] = Time.new
677
684
 
678
685
  if @tund.closed?
679
686
  puts "#{ Time.new } tund closed, close dst"
@@ -751,8 +758,7 @@ module P2p2
751
758
  cache: '', # 块读出缓存
752
759
  chunks: [], # 块队列,写前达到块大小时结一个块 filename
753
760
  spring: 0, # 块后缀,结块时,如果块队列不为空,则自增,为空,则置为0
754
- created_at: Time.new, # 创建时间
755
- last_recv_at: nil, # 上一次收到流量的时间,过期关闭
761
+ last_continue_at: Time.new, # 上一次发生流量的时间
756
762
  is_closing: false # 是否准备关闭
757
763
  }
758
764
  add_read( dst, :dst )
@@ -769,7 +775,7 @@ module P2p2
769
775
  is_src_closed: false, # src是否已关闭
770
776
  biggest_src_pack_id: 0, # src最大包号码
771
777
  completed_pack_id: 0, # 完成到几(对面收到几)
772
- last_continue_at: Time.new # 创建,或者上一次收到连续流量,或者发出新包的时间
778
+ last_continue_at: Time.new # 上一次发生流量的时间
773
779
  }
774
780
  end
775
781
 
@@ -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
  ##
@@ -544,6 +546,7 @@ module P2p2
544
546
  # puts "debug2 write src #{ written }"
545
547
  data = data[ written..-1 ]
546
548
  src_info[ from ] = data
549
+ src_info[ :last_continue_at ] = Time.new
547
550
  end
548
551
 
549
552
  ##
@@ -563,6 +566,10 @@ module P2p2
563
566
  tun.sendmsg( data, 0, to_addr )
564
567
  rescue IO::WaitWritable, Errno::EINTR
565
568
  return
569
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
570
+ puts "#{ Time.new } #{ e.class }, close tun"
571
+ close_tun( tun )
572
+ return
566
573
  end
567
574
 
568
575
  @tun_info[ :ctlmsgs ].shift
@@ -581,6 +588,10 @@ module P2p2
581
588
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
582
589
  rescue IO::WaitWritable, Errno::EINTR
583
590
  return
591
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
592
+ puts "#{ Time.new } #{ e.class }, close tun"
593
+ close_tun( tun )
594
+ return
584
595
  end
585
596
  end
586
597
  end
@@ -649,6 +660,10 @@ module P2p2
649
660
  tun.sendmsg( data, 0, @tun_info[ :tund_addr ] )
650
661
  rescue IO::WaitWritable, Errno::EINTR
651
662
  return
663
+ rescue Errno::EHOSTUNREACH, Errno::ENETUNREACH => e
664
+ puts "#{ Time.new } #{ e.class }, close tun"
665
+ close_tun( tun )
666
+ return
652
667
  end
653
668
 
654
669
  # puts "debug2 written pack #{ pack_id }"
@@ -683,16 +698,15 @@ module P2p2
683
698
  # puts "debug1 accept a src #{ addrinfo.inspect } #{ id }"
684
699
 
685
700
  @src_infos[ src ] = {
686
- id: id, # id
687
- biggest_pack_id: 0, # 最大包号码
688
- rbuffs: [], # p1端dst未准备好,暂存流量 [ pack_id, data ]
689
- wbuff: '', # 写前
690
- cache: '', # 块读出缓存
691
- chunks: [], # 块队列,写前达到块大小时结一个块 filename
692
- spring: 0, # 块后缀,结块时,如果块队列不为空,则自增,为空,则置为0
693
- created_at: Time.new, # 创建时间
694
- last_recv_at: nil, # 上一次收到流量的时间,过期关闭
695
- 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 # 是否准备关闭
696
710
  }
697
711
 
698
712
  add_read( src, :src )
@@ -712,7 +726,7 @@ module P2p2
712
726
  is_dst_closed: false, # dst是否已关闭
713
727
  biggest_dst_pack_id: 0, # dst最大包号码
714
728
  completed_pack_id: 0, # 完成到几(对面收到几)
715
- last_continue_at: Time.new # 创建,或者上一次收到连续流量,或者发出新包的时间
729
+ last_continue_at: Time.new # 上一次发生流量的时间
716
730
  }
717
731
 
718
732
  @tun_info[ :src_exts ][ id ] = src_ext
@@ -736,7 +750,7 @@ module P2p2
736
750
 
737
751
  # puts "debug2 read src #{ data.inspect }"
738
752
  src_info = @src_infos[ src ]
739
- src_info[ :last_recv_at ] = Time.new
753
+ src_info[ :last_continue_at ] = Time.new
740
754
  src_id = src_info[ :id ]
741
755
  src_ext = @tun_info[ :src_exts ][ src_id ]
742
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.19.0'.freeze
2
+ VERSION = '0.24.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.19.0
4
+ version: 0.24.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:
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  version: '0'
51
51
  requirements: []
52
52
  rubyforge_project:
53
- rubygems_version: 2.7.6.2
53
+ rubygems_version: 2.7.7
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: p2p