girl 0.95.0 → 1.0.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.
Potentially problematic release.
This version of girl might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/girl.gemspec +0 -2
- data/lib/girl/proxy_worker.rb +312 -313
- data/lib/girl/proxyd_worker.rb +227 -178
- data/lib/girl/version.rb +1 -1
- metadata +2 -4
- data/lib/girl/udp.rb +0 -326
- data/lib/girl/udpd.rb +0 -316
    
        data/lib/girl/proxyd_worker.rb
    CHANGED
    
    | @@ -9,6 +9,13 @@ module Girl | |
| 9 9 | 
             
                  @mutex = Mutex.new
         | 
| 10 10 | 
             
                  @reads = []
         | 
| 11 11 | 
             
                  @writes = []
         | 
| 12 | 
            +
                  @closing_tunds = []
         | 
| 13 | 
            +
                  @closing_dsts = []
         | 
| 14 | 
            +
                  @closing_streamds = []
         | 
| 15 | 
            +
                  @paused_dsts = []
         | 
| 16 | 
            +
                  @paused_streamds = []
         | 
| 17 | 
            +
                  @resume_dsts = []
         | 
| 18 | 
            +
                  @resume_streamds = []
         | 
| 12 19 | 
             
                  @roles = {}           # sock => :dotr / :proxyd / :infod / :dst / :tund / :tcpd / :streamd
         | 
| 13 20 | 
             
                  @tund_infos = {}      # tund => {}
         | 
| 14 21 | 
             
                  @tcpd_infos = {}      # tcpd => {}
         | 
| @@ -39,7 +46,6 @@ module Girl | |
| 39 46 | 
             
                    rs, ws = IO.select( @reads, @writes )
         | 
| 40 47 |  | 
| 41 48 | 
             
                    @mutex.synchronize do
         | 
| 42 | 
            -
                      # 先读,再写,避免打上关闭标记后读到
         | 
| 43 49 | 
             
                      rs.each do | sock |
         | 
| 44 50 | 
             
                        case @roles[ sock ]
         | 
| 45 51 | 
             
                        when :dotr then
         | 
| @@ -98,12 +104,30 @@ module Girl | |
| 98 104 | 
             
                private
         | 
| 99 105 |  | 
| 100 106 | 
             
                ##
         | 
| 101 | 
            -
                # add  | 
| 107 | 
            +
                # add closing dst
         | 
| 102 108 | 
             
                #
         | 
| 103 | 
            -
                def  | 
| 104 | 
            -
                   | 
| 105 | 
            -
                  @ | 
| 106 | 
            -
                   | 
| 109 | 
            +
                def add_closing_dst( dst )
         | 
| 110 | 
            +
                  return if dst.closed? || @closing_dsts.include?( dst )
         | 
| 111 | 
            +
                  @closing_dsts << dst
         | 
| 112 | 
            +
                  next_tick
         | 
| 113 | 
            +
                end
         | 
| 114 | 
            +
             | 
| 115 | 
            +
                ##
         | 
| 116 | 
            +
                # add closing streamd
         | 
| 117 | 
            +
                #
         | 
| 118 | 
            +
                def add_closing_streamd( streamd )
         | 
| 119 | 
            +
                  return if streamd.closed? || @closing_streamds.include?( streamd )
         | 
| 120 | 
            +
                  @closing_streamds << streamd
         | 
| 121 | 
            +
                  next_tick
         | 
| 122 | 
            +
                end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
                ##
         | 
| 125 | 
            +
                # add closing tund
         | 
| 126 | 
            +
                #
         | 
| 127 | 
            +
                def add_closing_tund( tund )
         | 
| 128 | 
            +
                  return if tund.closed? || @closing_tunds.include?( tund )
         | 
| 129 | 
            +
                  @closing_tunds << tund
         | 
| 130 | 
            +
                  next_tick
         | 
| 107 131 | 
             
                end
         | 
| 108 132 |  | 
| 109 133 | 
             
                ##
         | 
| @@ -115,11 +139,68 @@ module Girl | |
| 115 139 | 
             
                  add_write( tund )
         | 
| 116 140 | 
             
                end
         | 
| 117 141 |  | 
| 142 | 
            +
                ##
         | 
| 143 | 
            +
                # add dst rbuff
         | 
| 144 | 
            +
                #
         | 
| 145 | 
            +
                def add_dst_rbuff( dst, data )
         | 
| 146 | 
            +
                  dst_info = @dst_infos[ dst ]
         | 
| 147 | 
            +
                  dst_info[ :rbuff ] << data
         | 
| 148 | 
            +
             | 
| 149 | 
            +
                  if dst_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
         | 
| 150 | 
            +
                    # puts "debug1 dst.rbuff full"
         | 
| 151 | 
            +
                    add_closing_dst( dst )
         | 
| 152 | 
            +
                  end
         | 
| 153 | 
            +
                end
         | 
| 154 | 
            +
             | 
| 155 | 
            +
                ##
         | 
| 156 | 
            +
                # add dst wbuff
         | 
| 157 | 
            +
                #
         | 
| 158 | 
            +
                def add_dst_wbuff( dst, data )
         | 
| 159 | 
            +
                  return if dst.closed? || @closing_dsts.include?( dst )
         | 
| 160 | 
            +
             | 
| 161 | 
            +
                  dst_info = @dst_infos[ dst ]
         | 
| 162 | 
            +
                  dst_info[ :wbuff ] << data
         | 
| 163 | 
            +
                  dst_info[ :last_recv_at ] = Time.new
         | 
| 164 | 
            +
                  add_write( dst )
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                  if dst_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
         | 
| 167 | 
            +
                    puts "p#{ Process.pid } #{ Time.new } pause streamd #{ dst_info[ :domain_port ] }"
         | 
| 168 | 
            +
                    add_paused_streamd( dst_info[ :streamd ] )
         | 
| 169 | 
            +
                  end
         | 
| 170 | 
            +
                end
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                ##
         | 
| 173 | 
            +
                # add paused dst
         | 
| 174 | 
            +
                #
         | 
| 175 | 
            +
                def add_paused_dst( dst )
         | 
| 176 | 
            +
                  return if dst.closed? || @paused_dsts.include?( dst )
         | 
| 177 | 
            +
                  @reads.delete( dst )
         | 
| 178 | 
            +
                  @paused_dsts << dst
         | 
| 179 | 
            +
                end
         | 
| 180 | 
            +
             | 
| 181 | 
            +
                ##
         | 
| 182 | 
            +
                # add paused streamd
         | 
| 183 | 
            +
                #
         | 
| 184 | 
            +
                def add_paused_streamd( streamd )
         | 
| 185 | 
            +
                  return if streamd.closed? || @paused_streamds.include?( streamd )
         | 
| 186 | 
            +
                  @reads.delete( streamd )
         | 
| 187 | 
            +
                  @paused_streamds << streamd
         | 
| 188 | 
            +
                end
         | 
| 189 | 
            +
             | 
| 190 | 
            +
                ##
         | 
| 191 | 
            +
                # add proxyd ctlmsg
         | 
| 192 | 
            +
                #
         | 
| 193 | 
            +
                def add_proxyd_ctlmsg_tund_port( tund_info )
         | 
| 194 | 
            +
                  data = [ 0, TUND_PORT, tund_info[ :port ], tund_info[ :tcpd_port ] ].pack( 'Q>Cnn' )
         | 
| 195 | 
            +
                  @proxyd_info[ :ctlmsgs ] << [ data, tund_info[ :tun_addr ] ]
         | 
| 196 | 
            +
                  add_write( @proxyd )
         | 
| 197 | 
            +
                end
         | 
| 198 | 
            +
             | 
| 118 199 | 
             
                ##
         | 
| 119 200 | 
             
                # add read
         | 
| 120 201 | 
             
                #
         | 
| 121 202 | 
             
                def add_read( sock, role = nil )
         | 
| 122 | 
            -
                   | 
| 203 | 
            +
                  if !sock.closed? && !@reads.include?( sock ) then
         | 
| 123 204 | 
             
                    @reads << sock
         | 
| 124 205 |  | 
| 125 206 | 
             
                    if role then
         | 
| @@ -128,11 +209,44 @@ module Girl | |
| 128 209 | 
             
                  end
         | 
| 129 210 | 
             
                end
         | 
| 130 211 |  | 
| 212 | 
            +
                ##
         | 
| 213 | 
            +
                # add resume dst
         | 
| 214 | 
            +
                #
         | 
| 215 | 
            +
                def add_resume_dst( dst )
         | 
| 216 | 
            +
                  return if @resume_dsts.include?( dst )
         | 
| 217 | 
            +
                  @resume_dsts << dst
         | 
| 218 | 
            +
                  next_tick
         | 
| 219 | 
            +
                end
         | 
| 220 | 
            +
             | 
| 221 | 
            +
                ##
         | 
| 222 | 
            +
                # add resume streamd
         | 
| 223 | 
            +
                #
         | 
| 224 | 
            +
                def add_resume_streamd( streamd )
         | 
| 225 | 
            +
                  return if @resume_streamds.include?( streamd )
         | 
| 226 | 
            +
                  @resume_streamds << streamd
         | 
| 227 | 
            +
                  next_tick
         | 
| 228 | 
            +
                end
         | 
| 229 | 
            +
             | 
| 230 | 
            +
                ##
         | 
| 231 | 
            +
                # add streamd wbuff
         | 
| 232 | 
            +
                #
         | 
| 233 | 
            +
                def add_streamd_wbuff( streamd, data )
         | 
| 234 | 
            +
                  return if streamd.closed? || @closing_streamds.include?( streamd )
         | 
| 235 | 
            +
                  streamd_info = @streamd_infos[ streamd ]
         | 
| 236 | 
            +
                  streamd_info[ :wbuff ] << data
         | 
| 237 | 
            +
                  add_write( streamd )
         | 
| 238 | 
            +
             | 
| 239 | 
            +
                  if streamd_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
         | 
| 240 | 
            +
                    puts "p#{ Process.pid } #{ Time.new } pause dst #{ streamd_info[ :domain_port ] }"
         | 
| 241 | 
            +
                    add_paused_dst( streamd_info[ :dst ] )
         | 
| 242 | 
            +
                  end
         | 
| 243 | 
            +
                end
         | 
| 244 | 
            +
             | 
| 131 245 | 
             
                ##
         | 
| 132 246 | 
             
                # add write
         | 
| 133 247 | 
             
                #
         | 
| 134 248 | 
             
                def add_write( sock )
         | 
| 135 | 
            -
                   | 
| 249 | 
            +
                  if !sock.closed? && !@writes.include?( sock ) then
         | 
| 136 250 | 
             
                    @writes << sock
         | 
| 137 251 | 
             
                  end
         | 
| 138 252 | 
             
                end
         | 
| @@ -141,14 +255,15 @@ module Girl | |
| 141 255 | 
             
                # close dst
         | 
| 142 256 | 
             
                #
         | 
| 143 257 | 
             
                def close_dst( dst )
         | 
| 258 | 
            +
                  return if dst.closed?
         | 
| 144 259 | 
             
                  # puts "debug1 close dst"
         | 
| 145 260 | 
             
                  close_sock( dst )
         | 
| 146 261 | 
             
                  dst_info = del_dst_info( dst )
         | 
| 147 262 | 
             
                  streamd = dst_info[ :streamd ]
         | 
| 148 263 |  | 
| 149 264 | 
             
                  if streamd then
         | 
| 150 | 
            -
                     | 
| 151 | 
            -
                     | 
| 265 | 
            +
                    close_sock( streamd )
         | 
| 266 | 
            +
                    @streamd_infos.delete( streamd )
         | 
| 152 267 | 
             
                  end
         | 
| 153 268 | 
             
                end
         | 
| 154 269 |  | 
| @@ -169,7 +284,6 @@ module Girl | |
| 169 284 | 
             
                    dst_info = @dst_infos[ dst ]
         | 
| 170 285 | 
             
                  end
         | 
| 171 286 |  | 
| 172 | 
            -
                  dst_info[ :paused ] = false
         | 
| 173 287 | 
             
                  dst_info
         | 
| 174 288 | 
             
                end
         | 
| 175 289 |  | 
| @@ -207,14 +321,15 @@ module Girl | |
| 207 321 | 
             
                # close streamd
         | 
| 208 322 | 
             
                #
         | 
| 209 323 | 
             
                def close_streamd( streamd )
         | 
| 324 | 
            +
                  return if streamd.closed?
         | 
| 210 325 | 
             
                  # puts "debug1 close streamd"
         | 
| 211 326 | 
             
                  close_sock( streamd )
         | 
| 212 327 | 
             
                  streamd_info = @streamd_infos.delete( streamd )
         | 
| 213 328 | 
             
                  dst = streamd_info[ :dst ]
         | 
| 214 329 |  | 
| 215 330 | 
             
                  if dst then
         | 
| 216 | 
            -
                     | 
| 217 | 
            -
                     | 
| 331 | 
            +
                    close_sock( dst )
         | 
| 332 | 
            +
                    del_dst_info( dst )
         | 
| 218 333 | 
             
                  end
         | 
| 219 334 | 
             
                end
         | 
| 220 335 |  | 
| @@ -228,8 +343,8 @@ module Girl | |
| 228 343 | 
             
                  tcpd = tund_info[ :tcpd ]
         | 
| 229 344 | 
             
                  close_sock( tcpd )
         | 
| 230 345 | 
             
                  @tcpd_infos.delete( tcpd )
         | 
| 231 | 
            -
                  tund_info[ :dsts ].each{ | _, dst | set_dst_closing( dst ) }
         | 
| 232 346 | 
             
                  @tunneling_tunds.delete( tund_info[ :tun_addr ] )
         | 
| 347 | 
            +
                  tund_info[ :dsts ].each{ | _, dst | close_dst( dst ) }
         | 
| 233 348 | 
             
                end
         | 
| 234 349 |  | 
| 235 350 | 
             
                ##
         | 
| @@ -249,7 +364,6 @@ module Girl | |
| 249 364 | 
             
                    dst_info = @dst_infos[ dst ]
         | 
| 250 365 | 
             
                  end
         | 
| 251 366 |  | 
| 252 | 
            -
                  dst_info[ :closed_write ] = true
         | 
| 253 367 | 
             
                  dst_info
         | 
| 254 368 | 
             
                end
         | 
| 255 369 |  | 
| @@ -270,7 +384,6 @@ module Girl | |
| 270 384 | 
             
                    streamd_info = @streamd_infos[ streamd ]
         | 
| 271 385 | 
             
                  end
         | 
| 272 386 |  | 
| 273 | 
            -
                  streamd_info[ :closed_write ] = true
         | 
| 274 387 | 
             
                  streamd_info
         | 
| 275 388 | 
             
                end
         | 
| 276 389 |  | 
| @@ -286,6 +399,7 @@ module Girl | |
| 286 399 | 
             
                  rescue IO::WaitWritable
         | 
| 287 400 | 
             
                  rescue Exception => e
         | 
| 288 401 | 
             
                    puts "p#{ Process.pid } #{ Time.new } connect destination #{ domain_port } #{ e.class }"
         | 
| 402 | 
            +
                    dst.close
         | 
| 289 403 | 
             
                    return false
         | 
| 290 404 | 
             
                  end
         | 
| 291 405 |  | 
| @@ -304,10 +418,7 @@ module Girl | |
| 304 418 | 
             
                    created_at: Time.new,     # 创建时间
         | 
| 305 419 | 
             
                    last_recv_at: nil,        # 上一次收到新流量(由streamd收到)的时间
         | 
| 306 420 | 
             
                    last_sent_at: nil,        # 上一次发出流量(由streamd发出)的时间
         | 
| 307 | 
            -
                     | 
| 308 | 
            -
                    closing: false,           # 准备关闭
         | 
| 309 | 
            -
                    closing_write: false,     # 准备关闭写
         | 
| 310 | 
            -
                    closed_write: false       # 已关闭写
         | 
| 421 | 
            +
                    closing_write: false      # 准备关闭写
         | 
| 311 422 | 
             
                  }
         | 
| 312 423 |  | 
| 313 424 | 
             
                  add_read( dst, :dst )
         | 
| @@ -327,9 +438,9 @@ module Girl | |
| 327 438 | 
             
                def del_dst_info( dst )
         | 
| 328 439 | 
             
                  dst_info = @dst_infos.delete( dst )
         | 
| 329 440 | 
             
                  tund = dst_info[ :tund ]
         | 
| 441 | 
            +
                  tund_info = @tund_infos[ tund ]
         | 
| 330 442 |  | 
| 331 | 
            -
                   | 
| 332 | 
            -
                    tund_info = @tund_infos[ tund ]
         | 
| 443 | 
            +
                  if tund_info then
         | 
| 333 444 | 
             
                    tund_info[ :dsts ].delete( dst_info[ :id ] )
         | 
| 334 445 | 
             
                    tund_info[ :dst_ids ].delete( dst_info[ :src_id ] )
         | 
| 335 446 | 
             
                  end
         | 
| @@ -346,7 +457,6 @@ module Girl | |
| 346 457 | 
             
                      sleep CHECK_EXPIRE_INTERVAL
         | 
| 347 458 |  | 
| 348 459 | 
             
                      @mutex.synchronize do
         | 
| 349 | 
            -
                        trigger = false
         | 
| 350 460 | 
             
                        now = Time.new
         | 
| 351 461 |  | 
| 352 462 | 
             
                        @tund_infos.each do | tund, tund_info |
         | 
| @@ -354,8 +464,7 @@ module Girl | |
| 354 464 |  | 
| 355 465 | 
             
                          if tund_info[ :dsts ].empty? && ( now - last_recv_at >= EXPIRE_AFTER ) then
         | 
| 356 466 | 
             
                            puts "p#{ Process.pid } #{ Time.new } expire tund #{ tund_info[ :port ] }"
         | 
| 357 | 
            -
                             | 
| 358 | 
            -
                            trigger = true
         | 
| 467 | 
            +
                            add_closing_tund( tund )
         | 
| 359 468 | 
             
                          end
         | 
| 360 469 | 
             
                        end
         | 
| 361 470 |  | 
| @@ -365,12 +474,9 @@ module Girl | |
| 365 474 |  | 
| 366 475 | 
             
                          if ( now - last_recv_at >= EXPIRE_AFTER ) && ( now - last_sent_at >= EXPIRE_AFTER ) then
         | 
| 367 476 | 
             
                            puts "p#{ Process.pid } #{ Time.new } expire dst #{ dst_info[ :domain_port ] }"
         | 
| 368 | 
            -
                             | 
| 369 | 
            -
                            trigger = true
         | 
| 477 | 
            +
                            add_closing_dst( dst )
         | 
| 370 478 | 
             
                          end
         | 
| 371 479 | 
             
                        end
         | 
| 372 | 
            -
             | 
| 373 | 
            -
                        next_tick if trigger
         | 
| 374 480 | 
             
                      end
         | 
| 375 481 | 
             
                    end
         | 
| 376 482 | 
             
                  end
         | 
| @@ -385,33 +491,35 @@ module Girl | |
| 385 491 | 
             
                      sleep CHECK_RESUME_INTERVAL
         | 
| 386 492 |  | 
| 387 493 | 
             
                      @mutex.synchronize do
         | 
| 388 | 
            -
                         | 
| 389 | 
            -
             | 
| 390 | 
            -
             | 
| 391 | 
            -
                           | 
| 392 | 
            -
             | 
| 393 | 
            -
             | 
| 394 | 
            -
             | 
| 395 | 
            -
             | 
| 396 | 
            -
                             | 
| 397 | 
            -
             | 
| 398 | 
            -
             | 
| 494 | 
            +
                        @paused_dsts.each do | dst |
         | 
| 495 | 
            +
                          if dst.closed? then
         | 
| 496 | 
            +
                            add_resume_dst( dst )
         | 
| 497 | 
            +
                          else
         | 
| 498 | 
            +
                            dst_info = @dst_infos[ dst ]
         | 
| 499 | 
            +
                            streamd = dst_info[ :streamd ]
         | 
| 500 | 
            +
                            streamd_info = @streamd_infos[ streamd ]
         | 
| 501 | 
            +
             | 
| 502 | 
            +
                            if streamd_info[ :wbuff ].size < RESUME_BELOW then
         | 
| 503 | 
            +
                              puts "p#{ Process.pid } #{ Time.new } resume dst #{ dst_info[ :domain_port ] }"
         | 
| 504 | 
            +
                              add_resume_dst( dst )
         | 
| 505 | 
            +
                            end
         | 
| 399 506 | 
             
                          end
         | 
| 400 507 | 
             
                        end
         | 
| 401 508 |  | 
| 402 | 
            -
                        @ | 
| 403 | 
            -
                           | 
| 404 | 
            -
             | 
| 405 | 
            -
             | 
| 406 | 
            -
             | 
| 407 | 
            -
                             | 
| 408 | 
            -
                             | 
| 409 | 
            -
             | 
| 410 | 
            -
                             | 
| 509 | 
            +
                        @paused_streamds.each do | streamd |
         | 
| 510 | 
            +
                          if streamd.closed? then
         | 
| 511 | 
            +
                            add_resume_streamd( streamd )
         | 
| 512 | 
            +
                          else
         | 
| 513 | 
            +
                            streamd_info = @streamd_infos[ streamd ]
         | 
| 514 | 
            +
                            dst = streamd_info[ :dst ]
         | 
| 515 | 
            +
                            dst_info = @dst_infos[ dst ]
         | 
| 516 | 
            +
             | 
| 517 | 
            +
                            if dst_info[ :wbuff ].size < RESUME_BELOW then
         | 
| 518 | 
            +
                              puts "p#{ Process.pid } #{ Time.new } resume streamd #{ streamd_info[ :domain_port ] }"
         | 
| 519 | 
            +
                              add_resume_streamd( streamd )
         | 
| 520 | 
            +
                            end
         | 
| 411 521 | 
             
                          end
         | 
| 412 522 | 
             
                        end
         | 
| 413 | 
            -
             | 
| 414 | 
            -
                        next_tick if trigger
         | 
| 415 523 | 
             
                      end
         | 
| 416 524 | 
             
                    end
         | 
| 417 525 | 
             
                  end
         | 
| @@ -538,80 +646,83 @@ module Girl | |
| 538 646 | 
             
                  written
         | 
| 539 647 | 
             
                end
         | 
| 540 648 |  | 
| 541 | 
            -
                ##
         | 
| 542 | 
            -
                # set dst closing
         | 
| 543 | 
            -
                #
         | 
| 544 | 
            -
                def set_dst_closing( dst )
         | 
| 545 | 
            -
                  return if dst.closed?
         | 
| 546 | 
            -
                  dst_info = @dst_infos[ dst ]
         | 
| 547 | 
            -
                  dst_info[ :closing ] = true
         | 
| 548 | 
            -
             | 
| 549 | 
            -
                  if dst_info[ :closed_write ] then
         | 
| 550 | 
            -
                    add_read( dst )
         | 
| 551 | 
            -
                  else
         | 
| 552 | 
            -
                    @reads.delete( dst )
         | 
| 553 | 
            -
                    add_write( dst )
         | 
| 554 | 
            -
                  end
         | 
| 555 | 
            -
                end
         | 
| 556 | 
            -
             | 
| 557 649 | 
             
                ##
         | 
| 558 650 | 
             
                # set dst closing write
         | 
| 559 651 | 
             
                #
         | 
| 560 652 | 
             
                def set_dst_closing_write( dst )
         | 
| 561 | 
            -
                  return if dst.closed?
         | 
| 653 | 
            +
                  return if dst.closed? || @closing_dsts.include?( dst )
         | 
| 562 654 |  | 
| 563 655 | 
             
                  dst_info = @dst_infos[ dst ]
         | 
| 564 | 
            -
                  return if dst_info[ : | 
| 656 | 
            +
                  return if dst_info[ :closing_write ]
         | 
| 565 657 |  | 
| 566 658 | 
             
                  dst_info[ :closing_write ] = true
         | 
| 567 659 | 
             
                  add_write( dst )
         | 
| 568 660 | 
             
                end
         | 
| 569 661 |  | 
| 570 | 
            -
                ##
         | 
| 571 | 
            -
                # set streamd closing
         | 
| 572 | 
            -
                #
         | 
| 573 | 
            -
                def set_streamd_closing( streamd )
         | 
| 574 | 
            -
                  return if streamd.closed?
         | 
| 575 | 
            -
                  streamd_info = @streamd_infos[ streamd ]
         | 
| 576 | 
            -
                  streamd_info[ :closing ] = true
         | 
| 577 | 
            -
             | 
| 578 | 
            -
                  if streamd_info[ :closed_write ] then
         | 
| 579 | 
            -
                    add_read( streamd )
         | 
| 580 | 
            -
                  else
         | 
| 581 | 
            -
                    @reads.delete( streamd )
         | 
| 582 | 
            -
                    add_write( streamd )
         | 
| 583 | 
            -
                  end
         | 
| 584 | 
            -
                end
         | 
| 585 | 
            -
             | 
| 586 662 | 
             
                ##
         | 
| 587 663 | 
             
                # set streamd closing write
         | 
| 588 664 | 
             
                #
         | 
| 589 665 | 
             
                def set_streamd_closing_write( streamd )
         | 
| 590 | 
            -
                  return if streamd.closed?
         | 
| 666 | 
            +
                  return if streamd.closed? || @closing_streamds.include?( streamd )
         | 
| 591 667 |  | 
| 592 668 | 
             
                  streamd_info = @streamd_infos[ streamd ]
         | 
| 593 | 
            -
                  return if streamd_info[ : | 
| 669 | 
            +
                  return if streamd_info[ :closing_write ]
         | 
| 594 670 |  | 
| 595 671 | 
             
                  streamd_info[ :closing_write ] = true
         | 
| 596 672 | 
             
                  add_write( streamd )
         | 
| 597 673 | 
             
                end
         | 
| 598 674 |  | 
| 599 | 
            -
                ##
         | 
| 600 | 
            -
                # set tund is closing
         | 
| 601 | 
            -
                #
         | 
| 602 | 
            -
                def set_tund_closing( tund )
         | 
| 603 | 
            -
                  return if tund.closed?
         | 
| 604 | 
            -
                  tund_info = @tund_infos[ tund ]
         | 
| 605 | 
            -
                  tund_info[ :closing ] = true
         | 
| 606 | 
            -
                  @reads.delete( tund )
         | 
| 607 | 
            -
                  add_write( tund )
         | 
| 608 | 
            -
                end
         | 
| 609 | 
            -
             | 
| 610 675 | 
             
                ##
         | 
| 611 676 | 
             
                # read dotr
         | 
| 612 677 | 
             
                #
         | 
| 613 678 | 
             
                def read_dotr( dotr )
         | 
| 614 | 
            -
                  dotr. | 
| 679 | 
            +
                  dotr.read_nonblock( READ_SIZE )
         | 
| 680 | 
            +
             | 
| 681 | 
            +
                  # 处理关闭
         | 
| 682 | 
            +
                  if @closing_tunds.any? then
         | 
| 683 | 
            +
                    @closing_tunds.each do | tund |
         | 
| 684 | 
            +
                      unless tund.closed? then
         | 
| 685 | 
            +
                        tund_info = @tund_infos[ tund ]
         | 
| 686 | 
            +
             | 
| 687 | 
            +
                        if tund_info[ :changed_tun_addr ] then
         | 
| 688 | 
            +
                          data = [ 0, IP_CHANGED ].pack( 'Q>C' )
         | 
| 689 | 
            +
                          send_data( tund, data, tund_info[ :changed_tun_addr ] )
         | 
| 690 | 
            +
                        end
         | 
| 691 | 
            +
             | 
| 692 | 
            +
                        close_tund( tund )
         | 
| 693 | 
            +
                      end
         | 
| 694 | 
            +
                    end
         | 
| 695 | 
            +
             | 
| 696 | 
            +
                    @closing_tunds.clear
         | 
| 697 | 
            +
                  end
         | 
| 698 | 
            +
             | 
| 699 | 
            +
                  if @closing_dsts.any? then
         | 
| 700 | 
            +
                    @closing_dsts.each{ | dst | close_dst( dst ) }
         | 
| 701 | 
            +
                    @closing_dsts.clear
         | 
| 702 | 
            +
                  end
         | 
| 703 | 
            +
             | 
| 704 | 
            +
                  if @closing_streamds.any? then
         | 
| 705 | 
            +
                    @closing_streamds.each{ | streamd | close_streamd( streamd ) }
         | 
| 706 | 
            +
                    @closing_streamds.clear
         | 
| 707 | 
            +
                  end
         | 
| 708 | 
            +
             | 
| 709 | 
            +
                  if @resume_dsts.any? then
         | 
| 710 | 
            +
                    @resume_dsts.each do | dst |
         | 
| 711 | 
            +
                      add_read( dst )
         | 
| 712 | 
            +
                      @paused_dsts.delete( dst )
         | 
| 713 | 
            +
                    end
         | 
| 714 | 
            +
             | 
| 715 | 
            +
                    @resume_dsts.clear
         | 
| 716 | 
            +
                  end
         | 
| 717 | 
            +
             | 
| 718 | 
            +
                  if @resume_streamds.any? then
         | 
| 719 | 
            +
                    @resume_streamds.each do | streamd |
         | 
| 720 | 
            +
                      add_read( streamd )
         | 
| 721 | 
            +
                      @paused_streamds.delete( streamd )
         | 
| 722 | 
            +
                    end
         | 
| 723 | 
            +
             | 
| 724 | 
            +
                    @resume_streamds.clear
         | 
| 725 | 
            +
                  end
         | 
| 615 726 | 
             
                end
         | 
| 616 727 |  | 
| 617 728 | 
             
                ##
         | 
| @@ -666,7 +777,6 @@ module Girl | |
| 666 777 | 
             
                    dst_ids: {},          # src_id => dst_id
         | 
| 667 778 | 
             
                    created_at: Time.new, # 创建时间
         | 
| 668 779 | 
             
                    last_recv_at: nil,    # 上一次收到流量的时间
         | 
| 669 | 
            -
                    closing: false,       # 准备关闭
         | 
| 670 780 | 
             
                    changed_tun_addr: nil # 记录到和tun addr不符的来源地址
         | 
| 671 781 | 
             
                  }
         | 
| 672 782 |  | 
| @@ -706,6 +816,8 @@ module Girl | |
| 706 816 | 
             
                # read tund
         | 
| 707 817 | 
             
                #
         | 
| 708 818 | 
             
                def read_tund( tund )
         | 
| 819 | 
            +
                  return if tund.closed?
         | 
| 820 | 
            +
             | 
| 709 821 | 
             
                  begin
         | 
| 710 822 | 
             
                    data, addrinfo, rflags, *controls = tund.recvmsg_nonblock
         | 
| 711 823 | 
             
                  rescue IO::WaitReadable, Errno::EINTR
         | 
| @@ -720,7 +832,7 @@ module Girl | |
| 720 832 | 
             
                    # 通常是光猫刷新ip和端口,但万一不是,为了避免脏数据注入,关闭tund
         | 
| 721 833 | 
             
                    puts "p#{ Process.pid } #{ Time.new } from #{ addrinfo.inspect } not match tun addr #{ Addrinfo.new( tund_info[ :tun_addr ] ).inspect }"
         | 
| 722 834 | 
             
                    tund_info[ :changed_tun_addr ] = from_addr
         | 
| 723 | 
            -
                     | 
| 835 | 
            +
                    add_closing_tund( tund )
         | 
| 724 836 | 
             
                    return
         | 
| 725 837 | 
             
                  end
         | 
| 726 838 |  | 
| @@ -755,7 +867,7 @@ module Girl | |
| 755 867 | 
             
                    resolve_domain( tund, src_id, domain_port )
         | 
| 756 868 | 
             
                  when TUN_FIN then
         | 
| 757 869 | 
             
                    puts "p#{ Process.pid } #{ Time.new } recv tun fin"
         | 
| 758 | 
            -
                     | 
| 870 | 
            +
                    add_closing_tund( tund )
         | 
| 759 871 | 
             
                  end
         | 
| 760 872 | 
             
                end
         | 
| 761 873 |  | 
| @@ -763,6 +875,8 @@ module Girl | |
| 763 875 | 
             
                # read tcpd
         | 
| 764 876 | 
             
                #
         | 
| 765 877 | 
             
                def read_tcpd( tcpd )
         | 
| 878 | 
            +
                  return if tcpd.closed?
         | 
| 879 | 
            +
             | 
| 766 880 | 
             
                  begin
         | 
| 767 881 | 
             
                    streamd, addrinfo = tcpd.accept_nonblock
         | 
| 768 882 | 
             
                  rescue IO::WaitReadable, Errno::EINTR
         | 
| @@ -781,10 +895,7 @@ module Girl | |
| 781 895 | 
             
                    dst: nil,             # 对应dst
         | 
| 782 896 | 
             
                    domain_port: nil,     # dst的目的地和端口
         | 
| 783 897 | 
             
                    wbuff: '',            # 写前,写往近端stream
         | 
| 784 | 
            -
                     | 
| 785 | 
            -
                    closing: false,       # 准备关闭
         | 
| 786 | 
            -
                    closing_write: false, # 准备关闭写
         | 
| 787 | 
            -
                    closed_write: false   # 已关闭写
         | 
| 898 | 
            +
                    closing_write: false  # 准备关闭写
         | 
| 788 899 | 
             
                  }
         | 
| 789 900 |  | 
| 790 901 | 
             
                  add_read( streamd, :streamd )
         | 
| @@ -810,13 +921,6 @@ module Girl | |
| 810 921 | 
             
                  end
         | 
| 811 922 |  | 
| 812 923 | 
             
                  dst_info = @dst_infos[ dst ]
         | 
| 813 | 
            -
             | 
| 814 | 
            -
                  # 处理关闭
         | 
| 815 | 
            -
                  if dst_info[ :closing ] then
         | 
| 816 | 
            -
                    close_dst( dst )
         | 
| 817 | 
            -
                    return
         | 
| 818 | 
            -
                  end
         | 
| 819 | 
            -
             | 
| 820 924 | 
             
                  @traff_ins[ dst_info[ :im ] ] += data.bytesize
         | 
| 821 925 | 
             
                  streamd = dst_info[ :streamd ]
         | 
| 822 926 |  | 
| @@ -825,22 +929,10 @@ module Girl | |
| 825 929 | 
             
                      streamd_info = @streamd_infos[ streamd ]
         | 
| 826 930 | 
             
                      data = @custom.encode( data )
         | 
| 827 931 | 
             
                      # puts "debug2 add streamd.wbuff encoded #{ data.bytesize }"
         | 
| 828 | 
            -
                       | 
| 829 | 
            -
                      add_write( streamd )
         | 
| 830 | 
            -
             | 
| 831 | 
            -
                      if streamd_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
         | 
| 832 | 
            -
                        puts "p#{ Process.pid } #{ Time.new } pause dst #{ dst_info[ :domain_port ] }"
         | 
| 833 | 
            -
                        dst_info[ :paused ] = true
         | 
| 834 | 
            -
                        @reads.delete( dst )
         | 
| 835 | 
            -
                      end
         | 
| 932 | 
            +
                      add_streamd_wbuff( streamd, data )
         | 
| 836 933 | 
             
                    end
         | 
| 837 934 | 
             
                  else
         | 
| 838 | 
            -
                     | 
| 839 | 
            -
             | 
| 840 | 
            -
                    if dst_info[ :rbuff ].bytesize >= WBUFF_LIMIT then
         | 
| 841 | 
            -
                      # puts "debug1 dst.rbuff full"
         | 
| 842 | 
            -
                      set_dst_closing( dst )
         | 
| 843 | 
            -
                    end
         | 
| 935 | 
            +
                    add_dst_rbuff( dst, data )
         | 
| 844 936 | 
             
                  end
         | 
| 845 937 | 
             
                end
         | 
| 846 938 |  | 
| @@ -864,13 +956,6 @@ module Girl | |
| 864 956 | 
             
                  end
         | 
| 865 957 |  | 
| 866 958 | 
             
                  streamd_info = @streamd_infos[ streamd ]
         | 
| 867 | 
            -
             | 
| 868 | 
            -
                  # 处理关闭
         | 
| 869 | 
            -
                  if streamd_info[ :closing ] then
         | 
| 870 | 
            -
                    close_streamd( streamd )
         | 
| 871 | 
            -
                    return
         | 
| 872 | 
            -
                  end
         | 
| 873 | 
            -
             | 
| 874 959 | 
             
                  @traff_ins[ streamd_info[ :im ] ] += data.bytesize
         | 
| 875 960 | 
             
                  dst = streamd_info[ :dst ]
         | 
| 876 961 |  | 
| @@ -879,7 +964,7 @@ module Girl | |
| 879 964 | 
             
                    tund = streamd_info[ :tund ]
         | 
| 880 965 |  | 
| 881 966 | 
             
                    if tund.closed? then
         | 
| 882 | 
            -
                       | 
| 967 | 
            +
                      add_closing_streamd( streamd )
         | 
| 883 968 | 
             
                      return
         | 
| 884 969 | 
             
                    end
         | 
| 885 970 |  | 
| @@ -887,7 +972,7 @@ module Girl | |
| 887 972 | 
             
                    dst = tund_info[ :dsts ][ dst_id ]
         | 
| 888 973 |  | 
| 889 974 | 
             
                    unless dst then
         | 
| 890 | 
            -
                       | 
| 975 | 
            +
                      add_closing_streamd( streamd )
         | 
| 891 976 | 
             
                      return
         | 
| 892 977 | 
             
                    end
         | 
| 893 978 |  | 
| @@ -898,7 +983,8 @@ module Girl | |
| 898 983 |  | 
| 899 984 | 
             
                    unless dst_info[ :rbuff ].empty? then
         | 
| 900 985 | 
             
                      # puts "debug1 encode and move dst.rbuff to streamd.wbuff"
         | 
| 901 | 
            -
                       | 
| 986 | 
            +
                      data2 = @custom.encode( dst_info[ :rbuff ] )
         | 
| 987 | 
            +
                      add_streamd_wbuff( streamd, data2 )
         | 
| 902 988 | 
             
                    end
         | 
| 903 989 |  | 
| 904 990 | 
             
                    dst_info[ :streamd ] = streamd
         | 
| @@ -907,20 +993,9 @@ module Girl | |
| 907 993 | 
             
                    return if data.empty?
         | 
| 908 994 | 
             
                  end
         | 
| 909 995 |  | 
| 910 | 
            -
                   | 
| 911 | 
            -
             | 
| 912 | 
            -
             | 
| 913 | 
            -
                    # puts "debug2 add dst.wbuff decoded #{ data.bytesize }"
         | 
| 914 | 
            -
                    dst_info[ :wbuff ] << data
         | 
| 915 | 
            -
                    dst_info[ :last_recv_at ] = Time.new
         | 
| 916 | 
            -
                    add_write( dst )
         | 
| 917 | 
            -
             | 
| 918 | 
            -
                    if dst_info[ :wbuff ].bytesize >= WBUFF_LIMIT then
         | 
| 919 | 
            -
                      puts "p#{ Process.pid } #{ Time.new } pause streamd #{ streamd_info[ :domain_port ] }"
         | 
| 920 | 
            -
                      streamd_info[ :paused ] = true
         | 
| 921 | 
            -
                      @reads.delete( streamd )
         | 
| 922 | 
            -
                    end
         | 
| 923 | 
            -
                  end
         | 
| 996 | 
            +
                  data = @custom.decode( data )
         | 
| 997 | 
            +
                  # puts "debug2 add dst.wbuff decoded #{ data.bytesize }"
         | 
| 998 | 
            +
                  add_dst_wbuff( dst, data )
         | 
| 924 999 | 
             
                end
         | 
| 925 1000 |  | 
| 926 1001 | 
             
                ##
         | 
| @@ -947,21 +1022,9 @@ module Girl | |
| 947 1022 | 
             
                # write tund
         | 
| 948 1023 | 
             
                #
         | 
| 949 1024 | 
             
                def write_tund( tund )
         | 
| 1025 | 
            +
                  return if tund.closed?
         | 
| 950 1026 | 
             
                  tund_info = @tund_infos[ tund ]
         | 
| 951 1027 |  | 
| 952 | 
            -
                  # 处理关闭
         | 
| 953 | 
            -
                  if tund_info[ :closing ] then
         | 
| 954 | 
            -
                    if tund_info[ :changed_tun_addr ] then
         | 
| 955 | 
            -
                      data = [ 0, IP_CHANGED ].pack( 'Q>C' )
         | 
| 956 | 
            -
                      send_data( tund, data, tund_info[ :changed_tun_addr ] )
         | 
| 957 | 
            -
                    end
         | 
| 958 | 
            -
             | 
| 959 | 
            -
                    close_tund( tund )
         | 
| 960 | 
            -
                    return
         | 
| 961 | 
            -
                  end
         | 
| 962 | 
            -
             | 
| 963 | 
            -
                  now = Time.new
         | 
| 964 | 
            -
             | 
| 965 1028 | 
             
                  # 发ctlmsg
         | 
| 966 1029 | 
             
                  while tund_info[ :ctlmsgs ].any? do
         | 
| 967 1030 | 
             
                    data = tund_info[ :ctlmsgs ].first
         | 
| @@ -988,13 +1051,6 @@ module Girl | |
| 988 1051 | 
             
                  return if dst.closed?
         | 
| 989 1052 | 
             
                  dst_info = @dst_infos[ dst ]
         | 
| 990 1053 | 
             
                  streamd = dst_info[ :streamd ]
         | 
| 991 | 
            -
             | 
| 992 | 
            -
                  # 处理关闭
         | 
| 993 | 
            -
                  if dst_info[ :closing ] then
         | 
| 994 | 
            -
                    close_dst( dst )
         | 
| 995 | 
            -
                    return
         | 
| 996 | 
            -
                  end
         | 
| 997 | 
            -
             | 
| 998 1054 | 
             
                  data = dst_info[ :wbuff ]
         | 
| 999 1055 |  | 
| 1000 1056 | 
             
                  # 写前为空,处理关闭写
         | 
| @@ -1034,13 +1090,6 @@ module Girl | |
| 1034 1090 | 
             
                  return if streamd.closed?
         | 
| 1035 1091 | 
             
                  streamd_info = @streamd_infos[ streamd ]
         | 
| 1036 1092 | 
             
                  dst = streamd_info[ :dst ]
         | 
| 1037 | 
            -
             | 
| 1038 | 
            -
                  # 处理关闭
         | 
| 1039 | 
            -
                  if streamd_info[ :closing ] then
         | 
| 1040 | 
            -
                    close_streamd( streamd )
         | 
| 1041 | 
            -
                    return
         | 
| 1042 | 
            -
                  end
         | 
| 1043 | 
            -
             | 
| 1044 1093 | 
             
                  data = streamd_info[ :wbuff ]
         | 
| 1045 1094 |  | 
| 1046 1095 | 
             
                  # 写前为空,处理关闭写
         |