Skip to content

Commit bbbca7c

Browse files
authored
Bandwidth limiter
1 parent 42605cf commit bbbca7c

File tree

9 files changed

+1285
-108
lines changed

9 files changed

+1285
-108
lines changed

README.md

Lines changed: 43 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Daze is a software that helps you pass through firewalls, in other words, a proxy. It uses a simple yet efficient protocol, ensuring that you never get detected or blocked.
44

5-
# Getting Started
5+
## Introduction
66

77
Daze is designed as a single-file application. First, compile or [download](https://github.com/mohanson/daze/releases) daze:
88

@@ -16,7 +16,7 @@ $ cmd/develop.sh
1616
$ cmd/develop.ps1
1717
```
1818

19-
The build results will be saved in the bin directory. You can keep this directory, and all other files are not required.
19+
The build results will be saved in the `bin` directory. You can keep this directory, and all other files are not required.
2020

2121
Daze is dead simple to use:
2222

@@ -29,84 +29,35 @@ $ daze server -l 0.0.0.0:1081 -k $PASSWORD
2929
# Use the following command to link your server(replace $SERVER with your server IP):
3030
$ daze client -s $SERVER:1081 -k $PASSWORD
3131
# Now, you are free to visit the Internet
32-
$ curl -x socks5://127.0.0.1:1080 google.com
33-
```
34-
35-
Daze is still under development. You should make sure that the server and client have the same version number (check with the `daze ver` command) or commit hash.
36-
37-
# Using Daze for Different Platforms
38-
39-
Daze is implemented in pure Go language, so it can run on almost any operating system. The following are some of the browsers/operating systems commonly used by me:
40-
41-
## Android
42-
43-
0. Cross-compile daze for Android: `GOOS=android GOARCH=arm64 go build -o daze github.com/mohanson/daze/cmd/daze`
44-
0. Push the compiled file to the phone. You can use [adb](https://developer.android.com/studio/command-line/adb) or create an HTTP server and download daze with `wget` in [termux](https://play.google.com/store/apps/details?id=com.termux&hl=en).
45-
0. Run `daze client -l 127.0.0.1:1080 ...` in the termux.
46-
0. Set the proxy for phone: WLAN -> Settings -> Proxy -> Fill in `127.0.0.1:1080`
47-
48-
## Chrome
49-
50-
Chrome does not support setting proxies, so a third-party plugin must be used. [Proxy SwitchyOmega](https://chrome.google.com/webstore/detail/proxy-switchyomega/padekgcemlokbadohgkifijomclgjgif?hl=en) works very well.
51-
52-
## Firefox
53-
54-
Firefox can configure a proxy in `Connection Settings` -> `Manual proxy configuration` -> `SOCKSv5 Host=127.0.0.1` and `Port=1080`. If you see an option `Use remote DNS` on the page, check it.
55-
56-
# Network Model And Concepts
57-
58-
Daze's network model consists of 7 components:
59-
60-
```text
61-
+-------------+ +-------------+ +----------+ +-------------+ +-----------+
62-
| Destination | <----> | Daze server | <----> | Firewall | <----> | Daze client | <----> | User |
63-
+-------------+ +------+------+ +----------+ +-------------+ +-----------+
64-
| | |
65-
+------------- Middle Protocol ------------+-- Client Protocol --+
66-
```
67-
68-
- Destination: The destination is an internet service provider, for example, google.com.
69-
- Daze Server: A daze server is an instance that runs using the command `daze server`.
70-
- Firewall: A firewall is a network security system that monitors and controls incoming and outgoing network traffic based on pre-determined security rules.
71-
- Daze Client: A daze client is an instance that runs using the command `daze client`.
72-
- User: A user is a browser or any other application attempting to access the destination.
73-
- Middle Protocol: The middle protocol is the communication protocol between the daze server and daze client. Data is encrypted and obfuscated to bypass firewalls.
74-
- Client Protocol: The client protocol is the communication protocol between the daze client and the user.
75-
76-
# Protocols
77-
78-
## Client Protocols
79-
80-
The daze client implements 6 different proxy protocols in one port. These protocols are http proxy, https tunnel, socks4, socks4a, socks5 and socks5h.
81-
82-
```sh
83-
# Protocol http proxy
8432
$ curl -x http://127.0.0.1:1080 http://google.com
85-
# Protocol https tunnel
8633
$ curl -x http://127.0.0.1:1080 https://google.com
87-
# Protocol socks4
8834
$ curl -x socks4://127.0.0.1:1080 https://google.com
89-
# Protocol socks4a
9035
$ curl -x socks4a://127.0.0.1:1080 https://google.com
91-
# Protocol socks5
9236
$ curl -x socks5://127.0.0.1:1080 https://google.com
93-
# Protocol socks5h
9437
$ curl -x socks5h://127.0.0.1:1080 https://google.com
9538
```
9639

97-
Why can one port support so many protocols? Because it's magic!
40+
Daze is still under development. You should make sure that the server and client have the same version number (check with the `daze ver` command) or commit hash.
41+
42+
## Deployment
43+
44+
Daze is implemented in pure Go language, so it can run on almost any operating system. The following are some of the browsers/operating systems commonly used by me:
9845

99-
## Middle Protocols
46+
0. Android. Cross-compile daze for android: `GOOS=android GOARCH=arm64 go build -o daze github.com/mohanson/daze/cmd/daze`. Push the compiled file to the phone, You can use [adb](https://developer.android.com/studio/command-line/adb) or create an http server and download daze with `wget` in [termux](https://play.google.com/store/apps/details?id=com.termux&hl=en). Run `daze client -l 127.0.0.1:1080 ...` in the termux. Set the proxy for phone: WLAN -> Settings -> Proxy -> Fill in `127.0.0.1:1080`
47+
0. Chrome. Chrome does not support setting proxies, so a third-party plugin must be used. [Proxy SwitchyOmega](https://chrome.google.com/webstore/detail/proxy-switchyomega/padekgcemlokbadohgkifijomclgjgif?hl=en) works very well.
48+
0. Firefox can configure a proxy in `Connection Settings` -> `Manual proxy configuration` -> `SOCKSv5 Host=127.0.0.1` and `Port=1080`. If you see an option `Use remote DNS` on the page, check it.
10049

101-
Daze currently has 4 middle protocols.
50+
## Configuration: Protocols
10251

103-
### Ashe
52+
Daze currently has 4 protocols.
53+
54+
**Ashe**
10455

10556
The default protocol used by daze is called ashe. Ashe is a TCP-based cryptographic proxy protocol designed to bypass firewalls while providing a good user experience.
10657

107-
Please note that **it is the user's responsibility to ensure that the date and time on both the server and client are consistent**. The ashe protocol allows for a deviation of up to two minutes.
58+
Please note that it is the user's responsibility to ensure that the date and time on both the server and client are consistent. The ashe protocol allows for a deviation of up to two minutes.
10859

109-
### Baboon
60+
**Baboon**
11061

11162
Protocol baboon is a variant of the ashe protocol that operates over HTTP. In this protocol, the daze server masquerades as an HTTP service and requires the user to provide the correct password in order to gain access to the proxy service. If the password is not provided, the daze server will behave as a normal HTTP service. To use the baboon protocol, you must specify the protocol name and a fake site:
11263

@@ -115,7 +66,7 @@ $ daze server ... -p baboon -e https://github.com
11566
$ daze client ... -p baboon
11667
```
11768

118-
### Czar
69+
**Czar**
11970

12071
Protocol czar is an implementation of the ashe protocol based on TCP multiplexing. Multiplexing involves reusing a single TCP connection for multiple ashe protocols, which saves time on the TCP three-way handshake. However, this may result in a slight decrease in data transfer rate (approximately 0.19%). In most cases, using Protocol czar provides a better user experience compared to using the ashe protocol directly.
12172

@@ -124,7 +75,7 @@ $ daze server ... -p czar
12475
$ daze client ... -p czar
12576
```
12677

127-
### Dahlia
78+
**Dahlia**
12879

12980
Dahlia is a protocol used for encrypted port forwarding. Unlike many common port forwarding tools, it requires both a server and a client to be configured. Communication between the server and client is encrypted in order to bypass detection by firewalls.
13081

@@ -136,42 +87,57 @@ $ daze client -l :20002 -s 127.0.0.1:20001 -p dahlia
13687

13788
Reminder again: Dahlia is not a proxy protocol but a port forwarding protocol.
13889

139-
# Proxy Control
90+
## Configuration: Proxy Control
14091

14192
Proxy control is a rule that determines whether network requests (TCP and UDP) go directly to the destination or are forwarded to the daze server. Use the `-f` option in the daze client to adjust the proxy configuration.
14293

14394
- Use local network for all requests.
14495
- Use remote server for all requests.
145-
- Use both local and remote server (default).
96+
- Use both local and remote server (default). In this case, the following two configuration files are enabled:
14697

147-
## File rule.ls
98+
**rule.ls**
14899

149-
Daze uses a "rule.ls" file to customize your own rules(optional). "rule.ls" has the highest priority in routers so you should carefully maintain it. The "rule.ls" is located on the "rule.ls" by default, or you can use `daze client -r path/to/rule.ls` to apply it.
100+
Daze uses a "rule.ls" file to customize your own rules. File "rule.ls" has the highest priority in routers so you should carefully maintain it. The "rule.ls" is located on the "rule.ls" by default, or you can use `daze client -r path/to/rule.ls` to apply it. Its syntax is very simple:
150101

151102
```text
152103
L a.com
153104
R b.com
154105
B c.com
155106
```
156107

157-
- L(ocale) means using local network
158-
- R(emote) means using proxy
159-
- B(anned) means to block it, often used to block ads
108+
- L(ocale) means using local network.
109+
- R(emote) means using proxy.
110+
- B(anned) means to block it, often used to block ads.
160111

161112
Glob is supported, such as `R *.google.com`.
162113

163-
## File rule.cidr
114+
**rule.cidr**
164115

165116
Daze also uses a CIDR(Classless Inter-Domain Routing) file to route addresses. The CIDR file is located at "rule.cidr", and has a lower priority than "rule.ls".
166117

167118
By default, daze has configured rule.cidr for China's mainland. You can update it manually via `daze gen cn`, this will pull the latest data from [http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest](http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest).
168119

169-
# DNS resolver
120+
## Configuration: DNS
170121

171122
The DNS server and DNS protocol used by daze can be specified through command line parameters.
172123

173124
- `DNS: daze ... -dns 1.1.1.1:53`
174125
- `DoT: daze ... -dns 1.1.1.1:853`
175126
- `DoH: daze ... -dns https://1.1.1.1/dns-query`
176127

177-
This [article](https://www.cloudflare.com/learning/dns/dns-over-tls/) briefly describes the difference between them. I know many people don't like to read articles, so I just suggest that add `-dns 1.1.1.1:853` in daze.
128+
This [article](https://www.cloudflare.com/learning/dns/dns-over-tls/) briefly describes the difference between them.
129+
130+
## Configuration: Bandwidth Limiter
131+
132+
You can limit the maximum bandwidth used by daze. Generally speaking, for daze server, it is recommended to set the bandwidth to a value slightly smaller than the physical bandwidth.
133+
134+
```sh
135+
# For daze server, set -b 320k if your physical bandwidth is 3M, where 320 = 3 * 1024 / 8 - 64.
136+
$ daze server ... -b 320k
137+
# For daze client, in most cases no configuration is necessary.
138+
$ daze client ...
139+
```
140+
141+
## License
142+
143+
MIT.

cmd/daze/main.go

Lines changed: 50 additions & 6 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"
@@ -14,6 +15,7 @@ import (
1415
"github.com/mohanson/daze"
1516
"github.com/mohanson/daze/lib/doa"
1617
"github.com/mohanson/daze/lib/gracefulexit"
18+
"github.com/mohanson/daze/lib/rate"
1719
"github.com/mohanson/daze/protocol/ashe"
1820
"github.com/mohanson/daze/protocol/baboon"
1921
"github.com/mohanson/daze/protocol/czar"
@@ -65,10 +67,11 @@ func main() {
6567
switch subCommand {
6668
case "server":
6769
var (
70+
flCipher = flag.String("k", "daze", "password, should be same with the one specified by client")
6871
flDnserv = flag.String("dns", "", "specifies the DNS, DoT or DoH server")
6972
flExtend = flag.String("e", "", "extend data for different protocols")
7073
flGpprof = flag.String("g", "", "specify an address to enable net/http/pprof")
71-
flCipher = flag.String("k", "daze", "password, should be same with the one specified by client")
74+
flLimits = flag.String("b", "", "set the maximum bandwidth in bytes per second, for example, 128k or 1.5m")
7275
flListen = flag.String("l", "0.0.0.0:1081", "listen address")
7376
flProtoc = flag.String("p", "ashe", "protocol {ashe, baboon, czar, dahlia}")
7477
)
@@ -89,21 +92,41 @@ func main() {
8992
switch *flProtoc {
9093
case "ashe":
9194
server := ashe.NewServer(*flListen, *flCipher)
95+
if *flLimits != "" {
96+
n := daze.SizeParser(*flLimits)
97+
doa.Doa(n <= math.MaxInt)
98+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
99+
}
92100
defer server.Close()
93101
doa.Nil(server.Run())
94102
case "baboon":
95103
server := baboon.NewServer(*flListen, *flCipher)
96104
if *flExtend != "" {
97105
server.Masker = *flExtend
98106
}
107+
if *flLimits != "" {
108+
n := daze.SizeParser(*flLimits)
109+
doa.Doa(n <= math.MaxInt)
110+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
111+
}
99112
defer server.Close()
100113
doa.Nil(server.Run())
101114
case "czar":
102115
server := czar.NewServer(*flListen, *flCipher)
116+
if *flLimits != "" {
117+
n := daze.SizeParser(*flLimits)
118+
doa.Doa(n <= math.MaxInt)
119+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
120+
}
103121
defer server.Close()
104122
doa.Nil(server.Run())
105123
case "dahlia":
106124
server := dahlia.NewServer(*flListen, *flExtend, *flCipher)
125+
if *flLimits != "" {
126+
n := daze.SizeParser(*flLimits)
127+
doa.Doa(n <= math.MaxInt)
128+
server.Limits = rate.NewLimiter(rate.Limit(n), int(n))
129+
}
107130
defer server.Close()
108131
doa.Nil(server.Run())
109132
}
@@ -117,11 +140,12 @@ func main() {
117140
log.Println("main: exit")
118141
case "client":
119142
var (
120-
flCIDRls = flag.String("c", filepath.Join(resExec, Conf.PathCIDR), "cidr path")
143+
flCidrls = flag.String("c", filepath.Join(resExec, Conf.PathCIDR), "cidr path")
144+
flCipher = flag.String("k", "daze", "password, should be same with the one specified by server")
121145
flDnserv = flag.String("dns", "", "specifies the DNS, DoT or DoH server")
122146
flFilter = flag.String("f", "rule", "filter {rule, remote, locale}")
123147
flGpprof = flag.String("g", "", "specify an address to enable net/http/pprof")
124-
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")
125149
flListen = flag.String("l", "127.0.0.1:1080", "listen address")
126150
flProtoc = flag.String("p", "ashe", "protocol {ashe, baboon, czar, dahlia}")
127151
flRulels = flag.String("r", filepath.Join(resExec, Conf.PathRule), "rule path")
@@ -148,17 +172,27 @@ func main() {
148172
locale := daze.NewLocale(*flListen, daze.NewAimbot(client, &daze.AimbotOption{
149173
Type: *flFilter,
150174
Rule: *flRulels,
151-
Cidr: *flCIDRls,
175+
Cidr: *flCidrls,
152176
}))
177+
if *flLimits != "" {
178+
n := daze.SizeParser(*flLimits)
179+
doa.Doa(n <= math.MaxInt)
180+
locale.Limits = rate.NewLimiter(rate.Limit(n), int(n))
181+
}
153182
defer locale.Close()
154183
doa.Nil(locale.Run())
155184
case "baboon":
156185
client := baboon.NewClient(*flServer, *flCipher)
157186
locale := daze.NewLocale(*flListen, daze.NewAimbot(client, &daze.AimbotOption{
158187
Type: *flFilter,
159188
Rule: *flRulels,
160-
Cidr: *flCIDRls,
189+
Cidr: *flCidrls,
161190
}))
191+
if *flLimits != "" {
192+
n := daze.SizeParser(*flLimits)
193+
doa.Doa(n <= math.MaxInt)
194+
locale.Limits = rate.NewLimiter(rate.Limit(n), int(n))
195+
}
162196
defer locale.Close()
163197
doa.Nil(locale.Run())
164198
case "czar":
@@ -167,12 +201,22 @@ func main() {
167201
locale := daze.NewLocale(*flListen, daze.NewAimbot(client, &daze.AimbotOption{
168202
Type: *flFilter,
169203
Rule: *flRulels,
170-
Cidr: *flCIDRls,
204+
Cidr: *flCidrls,
171205
}))
206+
if *flLimits != "" {
207+
n := daze.SizeParser(*flLimits)
208+
doa.Doa(n <= math.MaxInt)
209+
locale.Limits = rate.NewLimiter(rate.Limit(n), int(n))
210+
}
172211
defer locale.Close()
173212
doa.Nil(locale.Run())
174213
case "dahlia":
175214
client := dahlia.NewClient(*flListen, *flServer, *flCipher)
215+
if *flLimits != "" {
216+
n := daze.SizeParser(*flLimits)
217+
doa.Doa(n <= math.MaxInt)
218+
client.Limits = rate.NewLimiter(rate.Limit(n), int(n))
219+
}
176220
defer client.Close()
177221
doa.Nil(client.Run())
178222
}

0 commit comments

Comments
 (0)