Skip to content

Commit 580d751

Browse files
committed
2025-06-23 08:47:21
1 parent 9a1a816 commit 580d751

File tree

5 files changed

+59
-9
lines changed

5 files changed

+59
-9
lines changed

cmd/daze/main.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"flag"
55
"fmt"
66
"log"
7+
"math"
78
"net"
89
"net/http"
910
"net/http/pprof"
@@ -94,7 +95,8 @@ func main() {
9495
defer server.Close()
9596
if *flLimits != "" {
9697
n := daze.SizeParser(*flLimits)
97-
server.Limits = rate.NewLimiter(rate.Limit(n), 1024*1024)
98+
doa.Doa(n <= math.MaxInt)
99+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
98100
}
99101
doa.Nil(server.Run())
100102
case "baboon":
@@ -105,23 +107,26 @@ func main() {
105107
}
106108
if *flLimits != "" {
107109
n := daze.SizeParser(*flLimits)
108-
server.Limits = rate.NewLimiter(rate.Limit(n), 1024*1024)
110+
doa.Doa(n <= math.MaxInt)
111+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
109112
}
110113
doa.Nil(server.Run())
111114
case "czar":
112115
server := czar.NewServer(*flListen, *flCipher)
113116
defer server.Close()
114117
if *flLimits != "" {
115118
n := daze.SizeParser(*flLimits)
116-
server.Limits = rate.NewLimiter(rate.Limit(n), 1024*1024)
119+
doa.Doa(n <= math.MaxInt)
120+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
117121
}
118122
doa.Nil(server.Run())
119123
case "dahlia":
120124
server := dahlia.NewServer(*flListen, *flExtend, *flCipher)
121125
defer server.Close()
122126
if *flLimits != "" {
123127
n := daze.SizeParser(*flLimits)
124-
server.Limits = rate.NewLimiter(rate.Limit(n), 1024*1024)
128+
doa.Doa(n <= math.MaxInt)
129+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
125130
}
126131
doa.Nil(server.Run())
127132
}
@@ -140,6 +145,7 @@ func main() {
140145
flFilter = flag.String("f", "rule", "filter {rule, remote, locale}")
141146
flGpprof = flag.String("g", "", "specify an address to enable net/http/pprof")
142147
flCipher = flag.String("k", "daze", "password, should be same with the one specified by server")
148+
flLimits = flag.String("b", "", "set the maximum bandwidth in bytes per second, for example, 128k or 1.5m")
143149
flListen = flag.String("l", "127.0.0.1:1080", "listen address")
144150
flProtoc = flag.String("p", "ashe", "protocol {ashe, baboon, czar, dahlia}")
145151
flRulels = flag.String("r", filepath.Join(resExec, Conf.PathRule), "rule path")
@@ -163,6 +169,11 @@ func main() {
163169
switch *flProtoc {
164170
case "ashe":
165171
client := ashe.NewClient(*flServer, *flCipher)
172+
if *flLimits != "" {
173+
n := daze.SizeParser(*flLimits)
174+
doa.Doa(n <= math.MaxInt)
175+
client.Limits = rate.NewLimiter(rate.Limit(n), int(n))
176+
}
166177
locale := daze.NewLocale(*flListen, daze.NewAimbot(client, &daze.AimbotOption{
167178
Type: *flFilter,
168179
Rule: *flRulels,
@@ -172,6 +183,11 @@ func main() {
172183
doa.Nil(locale.Run())
173184
case "baboon":
174185
client := baboon.NewClient(*flServer, *flCipher)
186+
if *flLimits != "" {
187+
n := daze.SizeParser(*flLimits)
188+
doa.Doa(n <= math.MaxInt)
189+
client.Limits = rate.NewLimiter(rate.Limit(n), int(n))
190+
}
175191
locale := daze.NewLocale(*flListen, daze.NewAimbot(client, &daze.AimbotOption{
176192
Type: *flFilter,
177193
Rule: *flRulels,
@@ -182,6 +198,11 @@ func main() {
182198
case "czar":
183199
client := czar.NewClient(*flServer, *flCipher)
184200
defer client.Close()
201+
if *flLimits != "" {
202+
n := daze.SizeParser(*flLimits)
203+
doa.Doa(n <= math.MaxInt)
204+
client.Limits = rate.NewLimiter(rate.Limit(n), int(n))
205+
}
185206
locale := daze.NewLocale(*flListen, daze.NewAimbot(client, &daze.AimbotOption{
186207
Type: *flFilter,
187208
Rule: *flRulels,
@@ -192,6 +213,11 @@ func main() {
192213
case "dahlia":
193214
client := dahlia.NewClient(*flListen, *flServer, *flCipher)
194215
defer client.Close()
216+
if *flLimits != "" {
217+
n := daze.SizeParser(*flLimits)
218+
doa.Doa(n <= math.MaxInt)
219+
client.Limits = rate.NewLimiter(rate.Limit(n), int(n))
220+
}
195221
doa.Nil(client.Run())
196222
}
197223
if *flGpprof != "" {

protocol/ashe/engine.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ type Server struct {
107107
// Cipher is a pre-shared key.
108108
Cipher []byte
109109
Closer io.Closer
110-
Listen string
111110
Limits *rate.Limiter
111+
Listen string
112112
}
113113

114114
// Hello creates an encrypted channel.
@@ -256,6 +256,7 @@ func NewServer(listen string, cipher string) *Server {
256256
type Client struct {
257257
// Cipher is a pre-shared key.
258258
Cipher []byte
259+
Limits *rate.Limiter
259260
Server string
260261
}
261262

@@ -344,7 +345,11 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
344345
if err != nil {
345346
return nil, err
346347
}
347-
con, err := c.Estab(ctx, srv, network, address)
348+
rwc := &daze.RateConn{
349+
Conn: srv,
350+
Rate: c.Limits,
351+
}
352+
con, err := c.Estab(ctx, rwc, network, address)
348353
if err != nil {
349354
srv.Close()
350355
}
@@ -355,6 +360,7 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
355360
func NewClient(server, cipher string) *Client {
356361
return &Client{
357362
Cipher: daze.Salt(cipher),
363+
Limits: rate.NewLimiter(rate.Inf, 0),
358364
Server: server,
359365
}
360366
}

protocol/baboon/engine.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func NewServer(listen string, cipher string) *Server {
159159
// Client implemented the baboon protocol.
160160
type Client struct {
161161
Cipher []byte
162+
Limits *rate.Limiter
162163
Server string
163164
}
164165

@@ -174,6 +175,10 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
174175
if err != nil {
175176
return nil, err
176177
}
178+
srv = &daze.RateConn{
179+
Conn: srv,
180+
Rate: c.Limits,
181+
}
177182
buf = make([]byte, 32)
178183
io.ReadFull(&daze.RandomReader{}, buf[:16])
179184
copy(buf[16:], c.Cipher[:16])
@@ -197,6 +202,7 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
197202
func NewClient(server string, cipher string) *Client {
198203
return &Client{
199204
Cipher: daze.Salt(cipher),
205+
Limits: rate.NewLimiter(rate.Inf, 0),
200206
Server: server,
201207
}
202208
}

protocol/czar/engine.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func NewServer(listen string, cipher string) *Server {
135135
type Client struct {
136136
Cancel chan struct{}
137137
Cipher []byte
138+
Limits *rate.Limiter
138139
Mux chan *Mux
139140
Server string
140141
}
@@ -155,9 +156,13 @@ func (c *Client) Dial(ctx *daze.Context, network string, address string) (io.Rea
155156
}
156157
log.Printf("czar: mux slot stream id=0x%02x", srv.idx)
157158
spy := &ashe.Client{Cipher: c.Cipher}
158-
con, err := spy.Estab(ctx, srv, network, address)
159+
rwc := &daze.RateConn{
160+
Conn: srv,
161+
Rate: c.Limits,
162+
}
163+
con, err := spy.Estab(ctx, rwc, network, address)
159164
if err != nil {
160-
srv.Close()
165+
rwc.Close()
161166
}
162167
return con, err
163168
case <-time.After(daze.Conf.DialerTimeout):
@@ -228,6 +233,7 @@ func NewClient(server, cipher string) *Client {
228233
client := &Client{
229234
Cancel: make(chan struct{}),
230235
Cipher: daze.Salt(cipher),
236+
Limits: rate.NewLimiter(rate.Inf, 0),
231237
Mux: make(chan *Mux),
232238
Server: server,
233239
}

protocol/dahlia/engine.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func NewServer(listen string, server string, cipher string) *Server {
9999
type Client struct {
100100
Cipher []byte
101101
Closer io.Closer
102+
Limits *rate.Limiter
102103
Listen string
103104
Server string
104105
}
@@ -146,12 +147,16 @@ func (c *Client) Run() error {
146147
}
147148
break
148149
}
150+
rwc := &daze.RateConn{
151+
Conn: cli,
152+
Rate: c.Limits,
153+
}
149154
idx++
150155
ctx := &daze.Context{Cid: idx}
151156
log.Printf("conn: %08x accept remote=%s", ctx.Cid, cli.RemoteAddr())
152157
go func() {
153158
defer cli.Close()
154-
if err := c.Serve(ctx, cli); err != nil {
159+
if err := c.Serve(ctx, rwc); err != nil {
155160
log.Printf("conn: %08x error %s", ctx.Cid, err)
156161
}
157162
log.Printf("conn: %08x closed", ctx.Cid)
@@ -165,6 +170,7 @@ func (c *Client) Run() error {
165170
func NewClient(listen string, server string, cipher string) *Client {
166171
return &Client{
167172
Cipher: daze.Salt(cipher),
173+
Limits: rate.NewLimiter(rate.Inf, 0),
168174
Listen: listen,
169175
Server: server,
170176
}

0 commit comments

Comments
 (0)