2015-12-04 19:16:21 -05:00
|
|
|
package outbound
|
|
|
|
|
|
|
|
import (
|
2016-01-20 11:31:43 -05:00
|
|
|
"sync"
|
|
|
|
"time"
|
2015-12-04 19:16:21 -05:00
|
|
|
|
2016-01-21 16:45:44 -05:00
|
|
|
"github.com/v2ray/v2ray-core/common/dice"
|
2015-12-04 19:16:21 -05:00
|
|
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
2016-02-03 05:58:42 -05:00
|
|
|
proto "github.com/v2ray/v2ray-core/common/protocol"
|
2015-12-04 19:16:21 -05:00
|
|
|
)
|
|
|
|
|
2015-12-07 14:32:38 -05:00
|
|
|
type Receiver struct {
|
2016-01-20 11:31:43 -05:00
|
|
|
sync.RWMutex
|
2015-12-16 17:53:38 -05:00
|
|
|
Destination v2net.Destination
|
2016-02-03 05:58:42 -05:00
|
|
|
Accounts []*proto.User
|
2015-12-07 14:32:38 -05:00
|
|
|
}
|
|
|
|
|
2016-02-03 05:58:42 -05:00
|
|
|
func NewReceiver(dest v2net.Destination, users ...*proto.User) *Receiver {
|
2016-01-20 11:31:43 -05:00
|
|
|
return &Receiver{
|
|
|
|
Destination: dest,
|
|
|
|
Accounts: users,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 05:58:42 -05:00
|
|
|
func (this *Receiver) HasUser(user *proto.User) bool {
|
2016-01-20 11:31:43 -05:00
|
|
|
this.RLock()
|
|
|
|
defer this.RUnlock()
|
|
|
|
for _, u := range this.Accounts {
|
|
|
|
// TODO: handle AlterIds difference.
|
|
|
|
if u.ID.Equals(user.ID) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-02-03 05:58:42 -05:00
|
|
|
func (this *Receiver) AddUser(user *proto.User) {
|
2016-01-20 11:31:43 -05:00
|
|
|
if this.HasUser(user) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.Lock()
|
|
|
|
this.Accounts = append(this.Accounts, user)
|
|
|
|
this.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-02-03 05:58:42 -05:00
|
|
|
func (this *Receiver) PickUser() *proto.User {
|
2016-01-21 16:45:44 -05:00
|
|
|
return this.Accounts[dice.Roll(len(this.Accounts))]
|
2016-01-20 11:31:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type ExpiringReceiver struct {
|
|
|
|
*Receiver
|
|
|
|
until time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ExpiringReceiver) Expired() bool {
|
2016-01-21 11:22:56 -05:00
|
|
|
return this.until.Before(time.Now())
|
2016-01-20 11:31:43 -05:00
|
|
|
}
|
|
|
|
|
2015-12-04 19:16:21 -05:00
|
|
|
type ReceiverManager struct {
|
2016-01-20 11:31:43 -05:00
|
|
|
receivers []*Receiver
|
|
|
|
detours []*ExpiringReceiver
|
|
|
|
detourAccess sync.RWMutex
|
2015-12-04 19:16:21 -05:00
|
|
|
}
|
|
|
|
|
2015-12-07 14:32:38 -05:00
|
|
|
func NewReceiverManager(receivers []*Receiver) *ReceiverManager {
|
2015-12-04 19:16:21 -05:00
|
|
|
return &ReceiverManager{
|
|
|
|
receivers: receivers,
|
2016-01-20 11:31:43 -05:00
|
|
|
detours: make([]*ExpiringReceiver, 0, 16),
|
2015-12-04 19:16:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 11:31:43 -05:00
|
|
|
func (this *ReceiverManager) AddDetour(rec *Receiver, availableMin byte) {
|
|
|
|
if availableMin < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.detourAccess.RLock()
|
|
|
|
destExists := false
|
|
|
|
for _, r := range this.detours {
|
|
|
|
if r.Destination == rec.Destination {
|
|
|
|
destExists = true
|
|
|
|
// Destination exists, add new user if necessary
|
|
|
|
for _, u := range rec.Accounts {
|
|
|
|
r.AddUser(u)
|
|
|
|
}
|
2016-01-21 11:22:56 -05:00
|
|
|
break
|
2016-01-20 11:31:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.detourAccess.RUnlock()
|
|
|
|
if !destExists {
|
2016-01-21 11:22:56 -05:00
|
|
|
expRec := &ExpiringReceiver{
|
|
|
|
Receiver: rec,
|
|
|
|
until: time.Now().Add(time.Duration(availableMin-1) * time.Minute),
|
|
|
|
}
|
2016-01-20 11:31:43 -05:00
|
|
|
this.detourAccess.Lock()
|
|
|
|
this.detours = append(this.detours, expRec)
|
|
|
|
this.detourAccess.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ReceiverManager) pickDetour() *Receiver {
|
|
|
|
if len(this.detours) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
this.detourAccess.RLock()
|
2016-01-21 16:45:44 -05:00
|
|
|
idx := dice.Roll(len(this.detours))
|
2016-01-20 11:31:43 -05:00
|
|
|
rec := this.detours[idx]
|
|
|
|
this.detourAccess.RUnlock()
|
|
|
|
|
|
|
|
if rec.Expired() {
|
|
|
|
this.detourAccess.Lock()
|
|
|
|
detourLen := len(this.detours)
|
2016-01-21 19:10:25 -05:00
|
|
|
if detourLen > idx && this.detours[idx].Expired() {
|
|
|
|
this.detours[idx] = this.detours[detourLen-1]
|
|
|
|
this.detours = this.detours[:detourLen-1]
|
|
|
|
}
|
2016-01-20 11:31:43 -05:00
|
|
|
this.detourAccess.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec.Receiver
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *ReceiverManager) pickStdReceiver() *Receiver {
|
2016-01-21 16:45:44 -05:00
|
|
|
return this.receivers[dice.Roll(len(this.receivers))]
|
2016-01-20 11:31:43 -05:00
|
|
|
}
|
2015-12-04 19:16:21 -05:00
|
|
|
|
2016-02-03 05:58:42 -05:00
|
|
|
func (this *ReceiverManager) PickReceiver() (v2net.Destination, *proto.User) {
|
2016-01-20 11:31:43 -05:00
|
|
|
rec := this.pickDetour()
|
|
|
|
if rec == nil {
|
|
|
|
rec = this.pickStdReceiver()
|
2015-12-04 19:16:21 -05:00
|
|
|
}
|
2016-01-20 11:31:43 -05:00
|
|
|
user := rec.PickUser()
|
|
|
|
|
|
|
|
return rec.Destination, user
|
2015-12-04 19:16:21 -05:00
|
|
|
}
|