Hunter0x7c7
2022-08-11 b8230139fb40edea387617b6accd8371e37eda58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package vmess
 
import (
    "crypto/hmac"
    "crypto/sha256"
    "hash/crc64"
    "strings"
    "sync"
    "sync/atomic"
    "time"
 
    "github.com/v2fly/v2ray-core/v5/common"
    "github.com/v2fly/v2ray-core/v5/common/dice"
    "github.com/v2fly/v2ray-core/v5/common/protocol"
    "github.com/v2fly/v2ray-core/v5/common/serial"
    "github.com/v2fly/v2ray-core/v5/common/task"
    "github.com/v2fly/v2ray-core/v5/proxy/vmess/aead"
)
 
const (
    updateInterval   = 10 * time.Second
    cacheDurationSec = 120
)
 
type user struct {
    user    protocol.MemoryUser
    lastSec protocol.Timestamp
}
 
// TimedUserValidator is a user Validator based on time.
type TimedUserValidator struct {
    sync.RWMutex
    users    []*user
    userHash map[[16]byte]indexTimePair
    hasher   protocol.IDHash
    baseTime protocol.Timestamp
    task     *task.Periodic
 
    behaviorSeed  uint64
    behaviorFused bool
 
    aeadDecoderHolder *aead.AuthIDDecoderHolder
 
    legacyWarnShown bool
}
 
type indexTimePair struct {
    user    *user
    timeInc uint32
 
    taintedFuse *uint32
}
 
// NewTimedUserValidator creates a new TimedUserValidator.
func NewTimedUserValidator(hasher protocol.IDHash) *TimedUserValidator {
    tuv := &TimedUserValidator{
        users:             make([]*user, 0, 16),
        userHash:          make(map[[16]byte]indexTimePair, 1024),
        hasher:            hasher,
        baseTime:          protocol.Timestamp(time.Now().Unix() - cacheDurationSec*2),
        aeadDecoderHolder: aead.NewAuthIDDecoderHolder(),
    }
    tuv.task = &task.Periodic{
        Interval: updateInterval,
        Execute: func() error {
            tuv.updateUserHash()
            return nil
        },
    }
    common.Must(tuv.task.Start())
    return tuv
}
 
// visible for testing
func (v *TimedUserValidator) GetBaseTime() protocol.Timestamp {
    return v.baseTime
}
 
func (v *TimedUserValidator) generateNewHashes(nowSec protocol.Timestamp, user *user) {
    var hashValue [16]byte
    genEndSec := nowSec + cacheDurationSec
    genHashForID := func(id *protocol.ID) {
        idHash := v.hasher(id.Bytes())
        genBeginSec := user.lastSec
        if genBeginSec < nowSec-cacheDurationSec {
            genBeginSec = nowSec - cacheDurationSec
        }
        for ts := genBeginSec; ts <= genEndSec; ts++ {
            common.Must2(serial.WriteUint64(idHash, uint64(ts)))
            idHash.Sum(hashValue[:0])
            idHash.Reset()
 
            v.userHash[hashValue] = indexTimePair{
                user:        user,
                timeInc:     uint32(ts - v.baseTime),
                taintedFuse: new(uint32),
            }
        }
    }
 
    account := user.user.Account.(*MemoryAccount)
 
    genHashForID(account.ID)
    for _, id := range account.AlterIDs {
        genHashForID(id)
    }
    user.lastSec = genEndSec
}
 
func (v *TimedUserValidator) removeExpiredHashes(expire uint32) {
    for key, pair := range v.userHash {
        if pair.timeInc < expire {
            delete(v.userHash, key)
        }
    }
}
 
func (v *TimedUserValidator) updateUserHash() {
    now := time.Now()
    nowSec := protocol.Timestamp(now.Unix())
 
    v.Lock()
    defer v.Unlock()
 
    for _, user := range v.users {
        v.generateNewHashes(nowSec, user)
    }
 
    expire := protocol.Timestamp(now.Unix() - cacheDurationSec)
    if expire > v.baseTime {
        v.removeExpiredHashes(uint32(expire - v.baseTime))
    }
}
 
func (v *TimedUserValidator) Add(u *protocol.MemoryUser) error {
    v.Lock()
    defer v.Unlock()
 
    nowSec := time.Now().Unix()
 
    uu := &user{
        user:    *u,
        lastSec: protocol.Timestamp(nowSec - cacheDurationSec),
    }
    v.users = append(v.users, uu)
    v.generateNewHashes(protocol.Timestamp(nowSec), uu)
 
    account := uu.user.Account.(*MemoryAccount)
    if !v.behaviorFused {
        hashkdf := hmac.New(sha256.New, []byte("VMESSBSKDF"))
        hashkdf.Write(account.ID.Bytes())
        v.behaviorSeed = crc64.Update(v.behaviorSeed, crc64.MakeTable(crc64.ECMA), hashkdf.Sum(nil))
    }
 
    var cmdkeyfl [16]byte
    copy(cmdkeyfl[:], account.ID.CmdKey())
    v.aeadDecoderHolder.AddUser(cmdkeyfl, u)
 
    return nil
}
 
func (v *TimedUserValidator) Get(userHash []byte) (*protocol.MemoryUser, protocol.Timestamp, bool, error) {
    v.RLock()
    defer v.RUnlock()
 
    v.behaviorFused = true
 
    var fixedSizeHash [16]byte
    copy(fixedSizeHash[:], userHash)
    pair, found := v.userHash[fixedSizeHash]
    if found {
        user := pair.user.user
        if atomic.LoadUint32(pair.taintedFuse) == 0 {
            return &user, protocol.Timestamp(pair.timeInc) + v.baseTime, true, nil
        }
        return nil, 0, false, ErrTainted
    }
    return nil, 0, false, ErrNotFound
}
 
func (v *TimedUserValidator) GetAEAD(userHash []byte) (*protocol.MemoryUser, bool, error) {
    v.RLock()
    defer v.RUnlock()
 
    var userHashFL [16]byte
    copy(userHashFL[:], userHash)
 
    userd, err := v.aeadDecoderHolder.Match(userHashFL)
    if err != nil {
        return nil, false, err
    }
    return userd.(*protocol.MemoryUser), true, err
}
 
func (v *TimedUserValidator) Remove(email string) bool {
    v.Lock()
    defer v.Unlock()
 
    email = strings.ToLower(email)
    idx := -1
    for i, u := range v.users {
        if strings.EqualFold(u.user.Email, email) {
            idx = i
            var cmdkeyfl [16]byte
            copy(cmdkeyfl[:], u.user.Account.(*MemoryAccount).ID.CmdKey())
            v.aeadDecoderHolder.RemoveUser(cmdkeyfl)
            break
        }
    }
    if idx == -1 {
        return false
    }
    ulen := len(v.users)
 
    v.users[idx] = v.users[ulen-1]
    v.users[ulen-1] = nil
    v.users = v.users[:ulen-1]
 
    return true
}
 
// Close implements common.Closable.
func (v *TimedUserValidator) Close() error {
    return v.task.Close()
}
 
func (v *TimedUserValidator) GetBehaviorSeed() uint64 {
    v.Lock()
    defer v.Unlock()
 
    v.behaviorFused = true
    if v.behaviorSeed == 0 {
        v.behaviorSeed = dice.RollUint64()
    }
    return v.behaviorSeed
}
 
func (v *TimedUserValidator) BurnTaintFuse(userHash []byte) error {
    v.RLock()
    defer v.RUnlock()
 
    var userHashFL [16]byte
    copy(userHashFL[:], userHash)
 
    pair, found := v.userHash[userHashFL]
    if found {
        if atomic.CompareAndSwapUint32(pair.taintedFuse, 0, 1) {
            return nil
        }
        return ErrTainted
    }
    return ErrNotFound
}
 
/*
    ShouldShowLegacyWarn will return whether a Legacy Warning should be shown
 
Not guaranteed to only return true once for every inbound, but it is okay.
*/
func (v *TimedUserValidator) ShouldShowLegacyWarn() bool {
    if v.legacyWarnShown {
        return false
    }
    v.legacyWarnShown = true
    return true
}
 
var ErrNotFound = newError("Not Found")
 
var ErrTainted = newError("ErrTainted")