mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
Add Fake DNS support (#309)
Co-authored-by: Xiaokang Wang <xiaokangwang@outlook.com> Co-authored-by: loyalsoldier <10487845+Loyalsoldier@users.noreply.github.com> Co-authored-by: kslr <kslrwang@gmail.com>
This commit is contained in:
parent
db32ce6fd9
commit
f50eff5ebb
43 changed files with 1351 additions and 301 deletions
85
common/cache/lru.go
vendored
Normal file
85
common/cache/lru.go
vendored
Normal file
|
@ -0,0 +1,85 @@
|
|||
package cache
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
// Lru simple, fast lru cache implementation
|
||||
type Lru interface {
|
||||
Get(key interface{}) (value interface{}, ok bool)
|
||||
GetKeyFromValue(value interface{}) (key interface{}, ok bool)
|
||||
PeekKeyFromValue(value interface{}) (key interface{}, ok bool) // Peek means check but NOT bring to top
|
||||
Put(key, value interface{})
|
||||
}
|
||||
|
||||
type lru struct {
|
||||
capacity int
|
||||
doubleLinkedlist *list.List
|
||||
keyToElement *sync.Map
|
||||
valueToElement *sync.Map
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
type lruElement struct {
|
||||
key interface{}
|
||||
value interface{}
|
||||
}
|
||||
|
||||
// NewLru init a lru cache
|
||||
func NewLru(cap int) Lru {
|
||||
return lru{
|
||||
capacity: cap,
|
||||
doubleLinkedlist: list.New(),
|
||||
keyToElement: new(sync.Map),
|
||||
valueToElement: new(sync.Map),
|
||||
mu: new(sync.Mutex),
|
||||
}
|
||||
}
|
||||
|
||||
func (l lru) Get(key interface{}) (value interface{}, ok bool) {
|
||||
if v, ok := l.keyToElement.Load(key); ok {
|
||||
element := v.(*list.Element)
|
||||
l.doubleLinkedlist.MoveBefore(element, l.doubleLinkedlist.Front())
|
||||
return element.Value.(lruElement).value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (l lru) GetKeyFromValue(value interface{}) (key interface{}, ok bool) {
|
||||
if k, ok := l.valueToElement.Load(value); ok {
|
||||
element := k.(*list.Element)
|
||||
l.doubleLinkedlist.MoveBefore(element, l.doubleLinkedlist.Front())
|
||||
return element.Value.(lruElement).key, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (l lru) PeekKeyFromValue(value interface{}) (key interface{}, ok bool) {
|
||||
if k, ok := l.valueToElement.Load(value); ok {
|
||||
element := k.(*list.Element)
|
||||
return element.Value.(lruElement).key, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (l lru) Put(key, value interface{}) {
|
||||
e := lruElement{key, value}
|
||||
if v, ok := l.keyToElement.Load(key); ok {
|
||||
element := v.(*list.Element)
|
||||
element.Value = e
|
||||
l.doubleLinkedlist.MoveBefore(element, l.doubleLinkedlist.Front())
|
||||
} else {
|
||||
l.mu.Lock()
|
||||
element := l.doubleLinkedlist.PushFront(e)
|
||||
l.keyToElement.Store(key, element)
|
||||
l.valueToElement.Store(value, element)
|
||||
if l.doubleLinkedlist.Len() > l.capacity {
|
||||
toBeRemove := l.doubleLinkedlist.Back()
|
||||
l.doubleLinkedlist.Remove(toBeRemove)
|
||||
l.keyToElement.Delete(toBeRemove.Value.(lruElement).key)
|
||||
l.valueToElement.Delete(toBeRemove.Value.(lruElement).value)
|
||||
}
|
||||
l.mu.Unlock()
|
||||
}
|
||||
}
|
86
common/cache/lru_test.go
vendored
Normal file
86
common/cache/lru_test.go
vendored
Normal file
|
@ -0,0 +1,86 @@
|
|||
package cache_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "github.com/xtls/xray-core/common/cache"
|
||||
)
|
||||
|
||||
func TestLruReplaceValue(t *testing.T) {
|
||||
lru := NewLru(2)
|
||||
lru.Put(2, 6)
|
||||
lru.Put(1, 5)
|
||||
lru.Put(1, 2)
|
||||
v, _ := lru.Get(1)
|
||||
if v != 2 {
|
||||
t.Error("should get 2", v)
|
||||
}
|
||||
v, _ = lru.Get(2)
|
||||
if v != 6 {
|
||||
t.Error("should get 6", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLruRemoveOld(t *testing.T) {
|
||||
lru := NewLru(2)
|
||||
v, ok := lru.Get(2)
|
||||
if ok {
|
||||
t.Error("should get nil", v)
|
||||
}
|
||||
lru.Put(1, 1)
|
||||
lru.Put(2, 2)
|
||||
v, _ = lru.Get(1)
|
||||
if v != 1 {
|
||||
t.Error("should get 1", v)
|
||||
}
|
||||
lru.Put(3, 3)
|
||||
v, ok = lru.Get(2)
|
||||
if ok {
|
||||
t.Error("should get nil", v)
|
||||
}
|
||||
lru.Put(4, 4)
|
||||
v, ok = lru.Get(1)
|
||||
if ok {
|
||||
t.Error("should get nil", v)
|
||||
}
|
||||
v, _ = lru.Get(3)
|
||||
if v != 3 {
|
||||
t.Error("should get 3", v)
|
||||
}
|
||||
v, _ = lru.Get(4)
|
||||
if v != 4 {
|
||||
t.Error("should get 4", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetKeyFromValue(t *testing.T) {
|
||||
lru := NewLru(2)
|
||||
lru.Put(3, 3)
|
||||
lru.Put(2, 2)
|
||||
lru.GetKeyFromValue(3)
|
||||
lru.Put(1, 1)
|
||||
v, ok := lru.GetKeyFromValue(2)
|
||||
if ok {
|
||||
t.Error("should get nil", v)
|
||||
}
|
||||
v, _ = lru.GetKeyFromValue(3)
|
||||
if v != 3 {
|
||||
t.Error("should get 3", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPeekKeyFromValue(t *testing.T) {
|
||||
lru := NewLru(2)
|
||||
lru.Put(3, 3)
|
||||
lru.Put(2, 2)
|
||||
lru.PeekKeyFromValue(3)
|
||||
lru.Put(1, 1)
|
||||
v, ok := lru.PeekKeyFromValue(3)
|
||||
if ok {
|
||||
t.Error("should get nil", v)
|
||||
}
|
||||
v, _ = lru.PeekKeyFromValue(2)
|
||||
if v != 2 {
|
||||
t.Error("should get 2", v)
|
||||
}
|
||||
}
|
|
@ -63,6 +63,7 @@ type SniffingRequest struct {
|
|||
ExcludeForDomain []string
|
||||
OverrideDestinationForProtocol []string
|
||||
Enabled bool
|
||||
MetadataOnly bool
|
||||
}
|
||||
|
||||
// Content is the metadata of the connection content.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue