-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
10 changed files
with
222 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,89 @@ | ||
package limiter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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" | ||
) | ||
|
||
const ( | ||
limiterDefault = "default" | ||
limiterUser = "user" | ||
limiterInbound = "inbound" | ||
) | ||
|
||
var m sync.Map | ||
|
||
func New(ctx context.Context, logger log.ContextLogger, options []option.Limiter) (err error) { | ||
for _, option := range options { | ||
err = new(ctx, logger, option) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
return | ||
} | ||
|
||
func new(ctx context.Context, logger log.ContextLogger, 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("limiter bandwith must be set") | ||
} | ||
l := newLimiter(download, upload) | ||
valid := false | ||
if len(option.Tag) > 0 { | ||
valid = true | ||
m.Store(buildKey(limiterDefault, option.Tag), newLimiter(download, upload)) | ||
} | ||
if len(option.AuthUser) > 0 { | ||
valid = true | ||
for _, user := range option.AuthUser { | ||
m.Store(buildKey(limiterUser, user), l) | ||
} | ||
} | ||
if len(option.Inbound) > 0 { | ||
valid = true | ||
for _, inbound := range option.Inbound { | ||
m.Store(buildKey(limiterInbound, inbound), l) | ||
} | ||
} | ||
if !valid { | ||
return E.New("limiter tag or constraint must be set") | ||
} | ||
logger.InfoContext(ctx, fmt.Sprintf("limiter created, download:%s, upload:%s, tag:%s, users:%v, inbounds:%v", | ||
option.Download, option.Upload, option.Tag, option.AuthUser, option.Inbound)) | ||
return | ||
} | ||
|
||
func buildKey(prefix string, tag string) string { | ||
return fmt.Sprintf("%s-%s", prefix, tag) | ||
} | ||
|
||
func LoadLimiters(tags []string, user, inbound string) (limiters []*limiter) { | ||
for _, t := range tags { | ||
if v, ok := m.Load(buildKey(limiterDefault, t)); ok { | ||
limiters = append(limiters, v.(*limiter)) | ||
} | ||
} | ||
if v, ok := m.Load(buildKey(limiterUser, user)); ok { | ||
limiters = append(limiters, v.(*limiter)) | ||
} | ||
if v, ok := m.Load(buildKey(limiterInbound, inbound)); ok { | ||
limiters = append(limiters, v.(*limiter)) | ||
} | ||
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,84 @@ | ||
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 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 | ||
} | ||
|
||
func (conn *connWithLimiter) Read(p []byte) (n int, err error) { | ||
if 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.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
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