Fix dynamic port allocation

pull/109/head
v2ray 2016-03-06 16:36:03 +01:00
parent 441a198b86
commit 83e0482d2b
1 changed files with 11 additions and 10 deletions

View File

@ -51,7 +51,6 @@ func (this *InboundDetourHandlerDynamic) pickUnusedPort() v2net.Port {
port := this.config.PortRange.From + v2net.Port(r) port := this.config.PortRange.From + v2net.Port(r)
_, used := this.portsInUse[port] _, used := this.portsInUse[port]
if !used { if !used {
this.portsInUse[port] = true
return port return port
} }
} }
@ -84,31 +83,33 @@ func (this *InboundDetourHandlerDynamic) Close() {
} }
func (this *InboundDetourHandlerDynamic) refresh() error { func (this *InboundDetourHandlerDynamic) refresh() error {
this.Lock()
defer this.Unlock()
this.lastRefresh = time.Now() this.lastRefresh = time.Now()
this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle for _, ich := range this.ich2Recycle {
for _, ich := range this.ichInUse { port2Delete := ich.Port()
port := this.pickUnusedPort()
delete(this.portsInUse, ich.Port())
ich.Close() ich.Close()
err := retry.Timed(100 /* times */, 1000 /* ms */).On(func() error { err := retry.Timed(100 /* times */, 1000 /* ms */).On(func() error {
port := this.pickUnusedPort()
err := ich.Listen(port) err := ich.Listen(port)
if err != nil { if err != nil {
log.Error("Point: Failed to start inbound detour on port ", port, ": ", err) log.Error("Point: Failed to start inbound detour on port ", port, ": ", err)
return err return err
} }
this.portsInUse[port] = true
return nil return nil
}) })
if err != nil { if err != nil {
return err continue
} }
delete(this.portsInUse, port2Delete)
} }
this.Lock()
this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
this.Unlock()
return nil return nil
} }