-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tun support
auto-redirect
, route-address-set
and `route-exc…
…lude-address-set`
- Loading branch information
Showing
28 changed files
with
746 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package utils | ||
|
||
import ( | ||
"io" | ||
"sync" | ||
|
||
list "github.com/bahlo/generic-list-go" | ||
) | ||
|
||
type Callback[T any] struct { | ||
list list.List[func(T)] | ||
mutex sync.RWMutex | ||
} | ||
|
||
func NewCallback[T any]() *Callback[T] { | ||
return &Callback[T]{} | ||
} | ||
|
||
func (c *Callback[T]) Register(item func(T)) io.Closer { | ||
c.mutex.RLock() | ||
defer c.mutex.RUnlock() | ||
element := c.list.PushBack(item) | ||
return &callbackCloser[T]{ | ||
element: element, | ||
callback: c, | ||
} | ||
} | ||
|
||
func (c *Callback[T]) Emit(item T) { | ||
c.mutex.RLock() | ||
defer c.mutex.RUnlock() | ||
for element := c.list.Front(); element != nil; element = element.Next() { | ||
go element.Value(item) | ||
} | ||
} | ||
|
||
type callbackCloser[T any] struct { | ||
element *list.Element[func(T)] | ||
callback *Callback[T] | ||
once sync.Once | ||
} | ||
|
||
func (c *callbackCloser[T]) Close() error { | ||
c.once.Do(func() { | ||
c.callback.mutex.Lock() | ||
defer c.callback.mutex.Unlock() | ||
c.callback.list.Remove(c.element) | ||
}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
09be5cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wwqgtxx 大佬辛苦了!请问这个auto-redirect不写进配置的话,是默认开启的还是默认关闭的呢?
09be5cb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
肯定是关的呀