-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
246 additions
and
4 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package limiter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"sync" | ||
|
||
"github.com/dustin/go-humanize" | ||
"github.com/sagernet/sing-box/log" | ||
"github.com/sagernet/sing-box/option" | ||
E "github.com/sagernet/sing/common/exceptions" | ||
"github.com/sagernet/sing/service" | ||
) | ||
|
||
const ( | ||
limiterDefault = "default" | ||
limiterUser = "user" | ||
limiterInbound = "inbound" | ||
) | ||
|
||
var _ Manager = (*defaultManager)(nil) | ||
|
||
type defaultManager struct { | ||
mp *sync.Map | ||
} | ||
|
||
func WithDefault(ctx context.Context, logger log.ContextLogger, options []option.Limiter) context.Context { | ||
m := &defaultManager{mp: &sync.Map{}} | ||
for i, option := range options { | ||
if err := m.createLimiter(ctx, option); err != nil { | ||
logger.ErrorContext(ctx, fmt.Sprintf("id=%d, %s", i, err)) | ||
} else { | ||
logger.InfoContext(ctx, fmt.Sprintf("id=%d, tag=%s, users=%v, inbounds=%v, download=%s, upload=%s", | ||
i, option.Tag, option.AuthUser, option.Inbound, option.Download, option.Upload)) | ||
} | ||
} | ||
return service.ContextWith[Manager](ctx, m) | ||
} | ||
|
||
func buildKey(prefix string, tag string) string { | ||
return fmt.Sprintf("%s-%s", prefix, tag) | ||
} | ||
|
||
func (m *defaultManager) createLimiter(ctx context.Context, option option.Limiter) (err error) { | ||
var download, upload uint64 | ||
if len(option.Download) > 0 { | ||
download, err = humanize.ParseBytes(option.Download) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if len(option.Upload) > 0 { | ||
upload, err = humanize.ParseBytes(option.Upload) | ||
} | ||
if download == 0 && upload == 0 { | ||
return E.New("bandwith must be set") | ||
} | ||
l := newLimiter(download, upload) | ||
valid := false | ||
if len(option.Tag) > 0 { | ||
valid = true | ||
m.mp.Store(buildKey(limiterDefault, option.Tag), newLimiter(download, upload)) | ||
} | ||
if len(option.AuthUser) > 0 { | ||
valid = true | ||
for _, user := range option.AuthUser { | ||
m.mp.Store(buildKey(limiterUser, user), l) | ||
} | ||
} | ||
if len(option.Inbound) > 0 { | ||
valid = true | ||
for _, inbound := range option.Inbound { | ||
m.mp.Store(buildKey(limiterInbound, inbound), l) | ||
} | ||
} | ||
if !valid { | ||
return E.New("tag or constraint must be set") | ||
} | ||
return | ||
} | ||
|
||
func (m *defaultManager) LoadLimiters(tags []string, user, inbound string) (limiters []*limiter) { | ||
for _, t := range tags { | ||
if v, ok := m.mp.Load(buildKey(limiterDefault, t)); ok { | ||
limiters = append(limiters, v.(*limiter)) | ||
} | ||
} | ||
if v, ok := m.mp.Load(buildKey(limiterUser, user)); ok { | ||
limiters = append(limiters, v.(*limiter)) | ||
} | ||
if v, ok := m.mp.Load(buildKey(limiterInbound, inbound)); ok { | ||
limiters = append(limiters, v.(*limiter)) | ||
} | ||
return | ||
} | ||
|
||
func (m *defaultManager) NewConnWithLimiters(ctx context.Context, conn net.Conn, limiters []*limiter) net.Conn { | ||
for _, limiter := range limiters { | ||
conn = &connWithLimiter{Conn: conn, limiter: limiter, ctx: ctx} | ||
} | ||
return conn | ||
} |
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,77 @@ | ||
package limiter | ||
|
||
import ( | ||
"context" | ||
"net" | ||
|
||
"golang.org/x/time/rate" | ||
) | ||
|
||
type limiter struct { | ||
downloadLimiter *rate.Limiter | ||
uploadLimiter *rate.Limiter | ||
} | ||
|
||
func newLimiter(download, upload uint64) *limiter { | ||
var downloadLimiter, uploadLimiter *rate.Limiter | ||
if download > 0 { | ||
downloadLimiter = rate.NewLimiter(rate.Limit(float64(download)), int(download)) | ||
} | ||
if upload > 0 { | ||
uploadLimiter = rate.NewLimiter(rate.Limit(float64(upload)), int(upload)) | ||
} | ||
return &limiter{downloadLimiter: downloadLimiter, uploadLimiter: uploadLimiter} | ||
} | ||
|
||
type connWithLimiter struct { | ||
net.Conn | ||
limiter *limiter | ||
ctx context.Context | ||
} | ||
|
||
func (conn *connWithLimiter) Read(p []byte) (n int, err error) { | ||
if conn.limiter == nil || conn.limiter.downloadLimiter == nil { | ||
return conn.Conn.Read(p) | ||
} | ||
b := conn.limiter.downloadLimiter.Burst() | ||
if b < len(p) { | ||
p = p[:b] | ||
} | ||
n, err = conn.Conn.Read(p) | ||
if err != nil { | ||
return | ||
} | ||
err = conn.limiter.downloadLimiter.WaitN(conn.ctx, n) | ||
if err != nil { | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (conn *connWithLimiter) Write(p []byte) (n int, err error) { | ||
if conn.limiter == nil || conn.limiter.uploadLimiter == nil { | ||
return conn.Conn.Write(p) | ||
} | ||
var nn int | ||
b := conn.limiter.uploadLimiter.Burst() | ||
for { | ||
end := len(p) | ||
if end == 0 { | ||
break | ||
} | ||
if b < len(p) { | ||
end = b | ||
} | ||
err = conn.limiter.uploadLimiter.WaitN(conn.ctx, end) | ||
if err != nil { | ||
return | ||
} | ||
nn, err = conn.Conn.Write(p[:end]) | ||
n += nn | ||
if err != nil { | ||
return | ||
} | ||
p = p[end:] | ||
} | ||
return | ||
} |
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,11 @@ | ||
package limiter | ||
|
||
import ( | ||
"context" | ||
"net" | ||
) | ||
|
||
type Manager interface { | ||
LoadLimiters(tags []string, user, inbound string) []*limiter | ||
NewConnWithLimiters(ctx context.Context, conn net.Conn, limiters []*limiter) net.Conn | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package option | ||
|
||
type Limiter struct { | ||
Tag string `json:"tag"` | ||
Download string `json:"download,omitempty"` | ||
Upload string `json:"upload,omitempty"` | ||
AuthUser Listable[string] `json:"auth_user,omitempty"` | ||
Inbound Listable[string] `json:"inbound,omitempty"` | ||
} |
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