Brook

A cross-platform programmable network tool.

Sponsor

❤️ Shiliew - A network app designed for those who value their time

Getting Started

Server

bash <(curl https://bash.ooo/nami.sh)
nami install brook
brook server -l :9999 -p hello

Client

You may want to use brook link to customize some parameters

Client

Brook GUI will pass different global variables to the script at different times, and the script only needs to assign the processing result to the global variable out

CLI

Before discussing the GUI client, let's first talk about the command line client brook. As we know, after you have deployed the server, you can use the command line client brook to create a local socks5 proxy or http proxy on your machine, and then configure it in your system proxy settings or in your browser to use this proxy. However:

  1. Not all apps will use this proxy, whether they use it is up to the app itself.
  2. Generally, all UDP protocols will not go through this proxy, such as http3.

For the specifics of socks5 and http proxy, you can read this article.

GUI

The GUI client does not use socks5 and http proxy mode, so there is no issue with some software not using the system proxy. Instead, it uses a virtual network card to take over the entire system's network, including UDP-based http3. Moreover, Brook allows us to control network requests programmatically, so it is necessary to have basic knowledge of network requests.

Without Brook: Basic Knowledge of Network Requests

Note: When we talk about addresses, we mean addresses that include the port number, such as a domain address: google.com:443, or an IP address: 8.8.8.8:53

  1. When an app requests a domain address, such as google.com:443
  2. It will first perform a DNS resolution, which means that the app will send a network request to the system-configured DNS, such as 8.8.8.8:53, to inquire about the IP of google.com
    1. The system DNS will return the IP of google.com, such as 1.2.3.4, to the app
  3. The app will combine the IP and port into an IP address, such as: 1.2.3.4:443
  4. The app makes a network request to this IP address 1.2.3.4:443
  5. The app receives the response data

In the above process, the app actually makes two network requests: one to the IP address 8.8.8.8:53 and another to the IP address 1.2.3.4:443. In other words, the domain name is essentially an alias for the IP, and must obtain the domain's IP to establish a connection.

With Brook: Fake DNS On

Brook has a Fake DNS feature, which can parse the domain name out of the query requests that an app sends to the system DNS and decide how to respond to the app.

  1. When an app requests a domain name address, such as google.com:443
  2. A DNS resolution will be performed first. That is, the app will send a network request to the system-configured DNS, such as 8.8.8.8:53, to inquire about the IP of google.com
  3. The Brook client detects that an app is sending a network request to 8.8.8.8:53. This will trigger the in_dnsquery variable, carrying information such as domain
    1. The Brook client returns a fake IP to the app, such as 240.0.0.1
  4. The app combines the IP and port into an IP address, such as: 240.0.0.1:443
  5. The app makes a network request to the IP address 240.0.0.1:443
  6. The Brook client detects that an app is sending a network request to 240.0.0.1:443, discovers that this is a fake IP, and will convert the fake IP address back to the domain address google.com:443. This will trigger the in_address variable, carrying information such as domainaddress
    1. The Brook client sends google.com:443 to the Brook Server
    2. The Brook Server first requests its own DNS to resolve the domain name to find out the IP of google.com, such as receiving 1.2.3.4
    3. The Brook Server combines the IP and port into an IP address, such as: 1.2.3.4:443
    4. The Brook Server sends a network request to 1.2.3.4:443 and returns the data to the Brook client
    5. The Brook client then returns the data to the app
  7. The app receives the response data

However, if the following situations occur, the domain name will not/cannot be parsed, meaning that the Brook client will not/cannot know what the domain name is and will treat it as a normal request sent to an IP address:

To avoid the ineffectiveness of Fake DNS, please refer to this article.

With Brook: Fake DNS Off

  1. When an app requests a domain address, such as google.com:443
  2. A DNS resolution will be performed first. That is, the app will send a network request to the system-configured DNS, such as 8.8.8.8:53, to inquire about the IP of google.com
  3. The Brook client detects that an app is sending a network request to 8.8.8.8:53. This will trigger the in_address variable, carrying information such as ipaddress
    1. The Brook client sends 8.8.8.8:53 to the Brook Server
    2. The Brook Server sends a network request to 8.8.8.8:53 and returns the result, such as 1.2.3.4, to the Brook client
    3. The Brook client then returns the result to the app
  4. The app combines the IP and port into an IP address, such as: 1.2.3.4:443
  5. The app makes a network request to the IP address 1.2.3.4:443
  6. The Brook client detects that an app is sending a network request to 1.2.3.4:443. This will trigger the in_address variable, carrying information such as ipaddress
    1. The Brook client sends 1.2.3.4:443 to the Brook Server
    2. The Brook Server sends a network request to 1.2.3.4:443 and returns the data to the Brook client
    3. The Brook client then returns the data to the app
  7. The app receives the response data

With Brook: Fake DNS On, But the App Uses the System's Secure DNS or Its Own Secure DNS

  1. When an app requests a domain name address, such as google.com:443
  2. A DNS resolution will be performed first. That is, the app will send a network request to the secure DNS, such as 8.8.8.8:443, to inquire about the IP of google.com
  3. The Brook client detects that an app is sending a network request to 8.8.8.8:443. This will trigger the in_address variable, carrying information such as ipaddress
    1. The Brook client sends 8.8.8.8:443 to the Brook Server
    2. The Brook Server sends a network request to 8.8.8.8:443, and returns the result, such as 1.2.3.4, to the Brook client
    3. The Brook client then returns the result to the app
  4. The app combines the IP and port into an IP address, such as: 1.2.3.4:443
  5. The app makes a network request to the IP address 1.2.3.4:443
  6. The Brook client detects that an app is sending a network request to 1.2.3.4:443. This will trigger the in_address variable, carrying information such as ipaddress
    1. The Brook client sends 1.2.3.4:443 to the Brook Server
    2. The Brook Server sends a network request to 1.2.3.4:443 and returns the data to the Brook client
    3. The Brook client then returns the data to the app
  7. The app receives the response data

To avoid the ineffectiveness of Fake DNS, please refer to this article.

Handle Variable Trigger

For detailed information on the properties and responses of variables, please refer to the following content.

Variables

variable type condition timing description out type
in_brooklinks map / Before connecting Predefine multiple brook links, and then programmatically specify which one to connect to map
in_dnsquery map FakeDNS: On When a DNS query occurs Script can decide how to handle this request map
in_address map / When connecting to an address script can decide how to connect map
in_httprequest map / When an HTTP(S) request comes in the script can decide how to handle this request map
in_httprequest,in_httpresponse map / when an HTTP(S) response comes in the script can decide how to handle this response map
Key Type Description Example
_ bool meaningless true

out, ignored if not of type map

Key Type Description Example
... ... ... ...
custom name string brook link brook://...
... ... ... ...

in_dnsquery

Key Type Description Example
domain string domain name google.com
type string query type A
appid string App ID or path com.google.Chrome.helper
interface string network interface. Mac only en0

out, if it is error type will be recorded in the log. Ignored if not of type map

Key Type Description Example
block bool Whether Block, default false false
ip string Specify IP directly, only valid when type is A/AAAA 1.2.3.4
system bool Resolve by System DNS, default false false
bypass bool Resolve by Bypass DNS, default false false
brooklinkkey string When need to connect the Server,instead, connect to the Server specified by the key in_brooklinks custom name

in_address

Key Type Description Example
network string Network type, the value tcp/udp tcp
ipaddress string IP type address. There is only of ipaddress and domainaddress. Note that there is no relationship between these two 1.2.3.4:443
domainaddress string Domain type address, because of FakeDNS we can get the domain name address here google.com:443
appid string App ID or path com.google.Chrome.helper
interface string network interface. Mac only en0

out, if it is error type will be recorded in the log. Ignored if not of type map

Key Type Description Example
block bool Whether Block, default false false
ipaddress string IP type address, rewrite destination 1.2.3.4:443
ipaddressfrombypassdns string Use Bypass DNS to obtain A or AAAA IP and rewrite the destination, only valid when domainaddress exists, the value A/AAAA A
bypass bool Bypass, default false. If true and domainaddress, then ipaddress or ipaddressfrombypassdns must be specified false
mitm bool Whether to perform MITM, default false. Only valid when network is tcp. Need to install CA, see below false
mitmprotocol string MITM protocol needs to be specified explicitly, the value is http/https https
mitmcertdomain string The MITM certificate domain name, which is taken from domainaddress by default. If ipaddress and mitm is true and mitmprotocol is https then must be must be specified explicitly example.com
mitmwithbody bool Whether to manipulate the http body, default false. will read the body of the request and response into the memory and interact with the script. iOS 50M total memory limit may kill process false
mitmautohandlecompress bool Whether to automatically decompress the http body when interacting with the script, default false false
mitmclienttimeout int Timeout for MITM talk to server, second, default 0 0
mitmserverreadtimeout int Timeout for MITM read from client, second, default 0 0
mitmserverwritetimeout int Timeout for MITM write to client, second, default 0 0
brooklinkkey string When need to connect the Server,instead, connect to the Server specified by the key in_brooklinks custom name

in_httprequest

Key Type Description Example
URL string URL https://example.com/hello
Method string HTTP method GET
Body bytes HTTP request body /
... string other fields are HTTP headers /

out, must be set to a request or response

in_httpresponse

Key Type Description Example
StatusCode int HTTP status code 200
Body bytes HTTP response body /
... string other fields are HTTP headers /

out, must be set to a response

Modules

In Brook GUI, scripts are abstracted into Modules. There are already some modules, and thre is no magic, it just automatically combine _header.tengo and _footer.tengo, so you only need to write the module itself.

modules = append(modules, {
    // If you want to predefine multiple brook links, and then programmatically specify which one to connect to, then define `brooklinks` key a function
    brooklinks: func(m) {
        // Please refer to the example in `brooklinks.tengo`
    },
    // If you want to intercept and handle a DNS query, then define `dnsquery` key a function, `m` is the `in_dnsquery`
    dnsquery: func(m) {
        // Please refer to the example in `block_aaaa.tengo`
    },
    // If you want to intercept and handle an address, then define `address` key a function, `m` is the `in_address`
    address: func(m) {
        // Please refer to the example in `block_google_secure_dns.tengo`
    },
    // If you want to intercept and handle a http request, then define `httprequest` key a function, `request` is the `in_httprequest`
    httprequest: func(request) {
        // Please refer to the example in `ios_app_downgrade.tengo` or `redirect_google_cn.tengo`
    },
    // If you want to intercept and handle a http response, then define `httpresponse` key a function, `request` is the `in_httprequest`, `response` is the `in_httpresponse`
    httpresponse: func(request, response) {
        // Please refer to the example in `response_sample.tengo`
    }
})

tun2brook

https://github.com/txthinking/tun2brook

If you are using tun2brook, you can manually combine multiple modules into a complete script in the following way. For example:

cat _header.tengo > my.tengo

cat block_google_secure_dns.tengo >> my.tengo
cat block_aaaa.tengo >> my.tengo

cat _footer.tengo >> my.tengo

Syntax

Tengo Language Syntax

Library

Debug

If you are writing complex scripts, the GUI may not be convenient for debugging. It is recommended to use tun2brook on desktop to debug with fmt.println

CA

https://txthinking.github.io/ca/ca.pem

OS How
iOS https://www.youtube.com/watch?v=HSGPC2vpDGk
Android Android has user CA and system CA, must be installed in the system CA after ROOT
macOS nami install mad ca.txthinking, sudo mad install --ca ~/.nami/bin/ca.pem
Windows nami install mad ca.txthinking, Admin: mad install --ca ~/.nami/bin/ca.pem

Some software may not read the system CA,you can use curl --cacert ~/.nami/bin/ca.pem to debug

OpenWrt

Brook OpenWRT: Perfectly supports IPv4/IPv6/TCP/UDP

IPv6

Brook's stance on IPv6 is positive, if your server or local environment doesn't have an IPv6 stack, read this article.

Troubleshooting Steps

  1. After adding your Server to the Brook client
  2. If your Server uses a domain and has not specified an IP address via brook link --address, then Brook client will attempt to resolve the domain's IP using local DNS, preferring AAAA record. For example:
    • domain.com:9999
    • ws://domain.com:9999
    • wss://domain.com:9999
    • quic://domain.com:9999
  3. Connectivity check: Go to the Server details page and click Connectivity Check. If it works sometimes but not others, this indicates instability.
  4. After connected
  5. Brook will change your system DNS to the System DNS configured in Brook (by default Google's DNS). In very rare cases, this change may be ignored on Windows, you can confirm this in the system settings.
  6. Test IPv4 TCP: Use Test IPv4 TCP for testing; this test has hardcoded the IP address, so does not trigger DNS resolution.
  7. Test IPv4 UDP: Use Test IPv4 UDP for testing; this test has hardcoded the IP address, so does not trigger DNS resolution.
  8. Test IPv6 TCP: Use Test IPv6 TCP for testing; this test has hardcoded the IP address, so does not trigger DNS resolution.
  9. Test IPv6 UDP: Use Test IPv6 UDP for testing; this test has hardcoded the IP address, so does not trigger DNS resolution.
  10. Test TCP and UDP: Use the Echo Client for testing. If the echo server entered is a domain address, it will trigger DNS resolution.
  11. Ensure the effectiveness of Fake DNS: Fake DNS is essential to do something with a domain or domain address. Generally, enable the Block Google Secure DNS module is sufficient. For other cases, refer to this article.
  12. If your local or Server does not support IPv6: Refer to this article.
  13. macOS App Mode: Refer to this article.
  14. Windows:
    • The client can pass the tests without any special configuration on a brand-new, genuine Windows 11.
    • Be aware that the Windows system time is often incorrect.
    • Do not have other similar network software installed; they can cause conflicting network settings in the system.
    • Try restarting the computer.
    • Windows Defender may ask for permission to connect to the network or present other issues.
    • System DNS may need to be set to 8.8.8.8 and/or 2001:4860:4860::8888
  15. Android:
    • The client can pass the tests without any special configuration on the official Google ROM.
    • Different ROMs may have made different modifications to the system.
    • Permission for background running might require separate settings.
    • System DNS may need to be set to 8.8.8.8 and/or 2001:4860:4860::8888
  16. Bypass traffic such as China, usually requires the following modules to be activated:
    • Block Google Secure DNS
    • Bypass Geo
    • Bypass Apple: To prevent issues receiving Apple message notifications.
    • Bypass China domain or Bypass China domain A: The former uses Bypass DNS to obtain the IP, then Bypass Geo or other modules decide whether to bypass; the latter bypasses directly after obtaining the IP with Bypass DNS using A records. The latter is needed if your local does not support IPv6.
    • If you are a Shiliew user, some modules are enabled by default, which is usually sufficient.
  17. Search GitHub issues
  18. Read the blog
  19. Read the documentation
  20. Submit new issue
  21. Seek help in the group

CLI Documentation

Each subcommand has a --example parameter that can print the minimal example of usage

NAME

Brook - A cross-platform programmable network tool

SYNOPSIS

Brook

brook --help

Usage:

Brook [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]

GLOBAL OPTIONS

COMMANDS

server

Start a brook server that supports tcp and udp

client

Start a brook client that supports tcp and udp. It can open a socks5 proxy, [src <-> socks5 <-> $ brook client <-> $ brook server <-> dst]

wsserver

Start a brook wsserver that supports tcp and udp. It opens a standard http server and a websocket server

wsclient

Start a brook wsclient that supports tcp and udp. It can open a socks5 proxy, [src <-> socks5 <-> $ brook wsclient <-> $ brook wsserver <-> dst]

wssserver

Start a brook wssserver that supports tcp and udp. It opens a standard https server and a websocket server

wssclient

Start a brook wssclient that supports tcp and udp. It can open a socks5 proxy, [src <-> socks5 <-> $ brook wssclient <-> $ brook wssserver <-> dst]

quicserver

Start a brook quicserver that supports tcp and udp.

quicclient

Start a brook quicclient that supports tcp and udp. It can open a socks5 proxy, [src <-> socks5 <-> $ brook quicclient <-> $ brook quicserver <-> dst]. (The global-dial-parameter is ignored)

relayoverbrook

Relay network traffic over brook, which supports TCP and UDP. Accessing [from address] is equal to accessing [to address], [src <-> from address <-> $ brook server/wsserver/wssserver/quicserver <-> to address]

dnsserveroverbrook

Run a dns server over brook, which supports TCP and UDP, [src <-> $ brook dnserversoverbrook <-> $ brook server/wsserver/wssserver/quicserver <-> dns] or [src <-> $ brook dnsserveroverbrook <-> dnsForBypass]

Generate a brook link

connect

Run a client and connect with a brook link, which supports TCP and UDP. It can start a socks5 proxy, [src <-> socks5 <-> $ brook connect <-> $ brook server/wsserver/wssserver/quicserver <-> dst]

relay

Run a standalone relay, which supports TCP and UDP. Accessing [from address] is equal to accessing [to address], [src <-> from address <-> to address]

dnsserver

Run a standalone dns server

dnsclient

Send a dns query

dohserver

Run a standalone doh server

dohclient

Send a dns query

dhcpserver

Run a standalone dhcp server. Other running dhcp servers need to be stopped.

socks5

Run a standalone standard socks5 server, which supports TCP and UDP

socks5tohttp

Convert a socks5 proxy to a http proxy, [src <-> listen address(http proxy) <-> socks5 address <-> dst]

pac

Run a PAC server or save PAC to a file

testsocks5

Test a socks5 server to see if it works properly

testbrook

Test UDP and TCP of a brook server/wsserver/wssserver/quicserver connection. (The global-dial-parameter is ignored)

echoserver

Echo server, echo UDP and TCP address of routes

echoclient

Connect to echoserver, echo UDP and TCP address of routes

ipcountry

Get country of IP

completion

Generate shell completions

mdpage

Generate markdown page

manpage

Generate man.1 page

help, h

Shows a list of commands or help for one command

Examples

List some examples of common scene commands, pay attention to replace the parameters such as IP, port, password, domain name, certificate path, etc. in the example by yourself

Run brook server

brook server --listen :9999 --password hello

then

or get brook link

brook link --server 1.2.3.4:9999 --password hello --name 'my brook server'

or get brook link with --udpovertcp

brook link --server 1.2.3.4:9999 --password hello --udpovertcp --name 'my brook server'

Run brook wsserver

brook wsserver --listen :9999 --password hello

then

or get brook link

brook link --server ws://1.2.3.4:9999 --password hello --name 'my brook wsserver'

or get brook link with domain, even if that's not your domain

brook link --server ws://hello.com:9999 --password hello --address 1.2.3.4:9999 --name 'my brook wsserver'

Run brook wssserver: automatically certificate

Make sure your domain has been resolved to your server IP successfully. Automatic certificate issuance requires the use of port 80

brook wssserver --domainaddress domain.com:443 --password hello

then

or get brook link

brook link --server wss://domain.com:443 --password hello --name 'my brook wssserver'

Run brook wssserver Use a certificate issued by an existing trust authority

Make sure your domain has been resolved to your server IP successfully

brook wssserver --domainaddress domain.com:443 --password hello --cert /root/cert.pem --certkey /root/certkey.pem

then

or get brook link

brook link --server wss://domain.com:443 --password hello --name 'my brook wssserver'

Run brook wssserver issue untrusted certificates yourself, any domain

Install mad

nami install mad

Generate root ca

mad ca --ca /root/ca.pem --key /root/cakey.pem

Generate domain cert by root ca

mad cert --ca /root/ca.pem --ca_key /root/cakey.pem --cert /root/cert.pem --key /root/certkey.pem --domain domain.com

Run brook

brook wssserver --domainaddress domain.com:443 --password hello --cert /root/cert.pem --certkey /root/certkey.pem

get brook link with --insecure

brook link --server wss://domain.com:443 --password hello --name 'my brook wssserver' --address 1.2.3.4:443 --insecure

or get brook link with --ca

brook link --server wss://domain.com:443 --password hello --name 'my brook wssserver' --address 1.2.3.4:443 --ca /root/ca.pem

withoutBrookProtocol

Better performance, but data is not strongly encrypted using Brook protocol. So please use certificate encryption, and it is not recommended to use --withoutBrookProtocol and --insecure together

withoutBrookProtocol automatically certificate

Make sure your domain has been resolved to your server IP successfully. Automatic certificate issuance requires the use of port 80

brook wssserver --domainaddress domain.com:443 --password hello --withoutBrookProtocol

get brook link

brook link --server wss://domain.com:443 --password hello --withoutBrookProtocol

withoutBrookProtocol Use a certificate issued by an existing trust authority

Make sure your domain has been resolved to your server IP successfully

brook wssserver --domainaddress domain.com:443 --password hello --cert /root/cert.pem --certkey /root/certkey.pem --withoutBrookProtocol

get brook link

brook link --server wss://domain.com:443 --password hello --name 'my brook wssserver' --withoutBrookProtocol

withoutBrookProtocol issue untrusted certificates yourself, any domain

Install mad

nami install mad

Generate root ca

mad ca --ca /root/ca.pem --key /root/cakey.pem

Generate domain cert by root ca

mad cert --ca /root/ca.pem --ca_key /root/cakey.pem --cert /root/cert.pem --key /root/certkey.pem --domain domain.com

Run brook wssserver

brook wssserver --domainaddress domain.com:443 --password hello --cert /root/cert.pem --certkey /root/certkey.pem --withoutBrookProtocol

Get brook link

brook link --server wss://domain.com:443 --password hello --withoutBrookProtocol --address 1.2.3.4:443 --ca /root/ca.pem

Run brook socks5, A stand-alone standard socks5 server

brook socks5 --listen :1080 --socks5ServerIP 1.2.3.4

then

or get brook link

brook link --server socks5://1.2.3.4:1080

Run brook socks5 with username and password. A stand-alone standard socks5 server

brook socks5 --listen :1080 --socks5ServerIP 1.2.3.4 --username hello --password world

then

or get brook link

brook link --server socks5://1.2.3.4:1080 --username hello --password world

brook relayoverbrook can relay a local address to a remote address over brook, both TCP and UDP, it works with brook server wsserver wssserver.

brook relayoverbrook ... --from 127.0.0.1:5353 --to 8.8.8.8:53

brook dnsserveroverbrook can create a encrypted DNS server, both TCP and UDP, it works with brook server wsserver wssserver.

brook dnsserveroverbrook ... --listen 127.0.0.1:53

Brook OpenWRT Router: Perfectly supports IPv4/IPv6/TCP/UDP. Native IPv6

https://www.txthinking.com/talks/articles/brook-openwrt-en.article

Turn macOS into a Gateway with Brook

https://www.txthinking.com/talks/articles/brook-macos-gateway-en.article

Turn Windows into a Gateway with Brook

https://www.txthinking.com/talks/articles/brook-windows-gateway-en.article

Turn Linux into a Gateway with Brook

https://www.txthinking.com/talks/articles/brook-linux-gateway-en.article

brook relay can relay a address to a remote address. It can relay any tcp and udp server

brook relay --from :9999 --to 1.2.3.4:9999

brook socks5tohttp can convert a socks5 to a http proxy

brook socks5tohttp --socks5 127.0.0.1:1080 --listen 127.0.0.1:8010

There are countless examples; for more feature suggestions, it's best to look at the commands and parameters in the CLI documentation one by one, and blog, YouTube...

Resources

CLI Description
nami A clean and tidy decentralized package manager
joker Joker can turn process into daemon. Zero-Configuration
nico Nico can work with brook wsserver together
z z - process manager
tun2brook Proxy all traffic just one line command
mad Generate root CA and derivative certificate for any domains and any IPs
hancock Manage multiple remote servers and execute commands remotely
sshexec A command-line tool to execute remote command through ssh
jb write script in an easier way than bash
bash Many one-click scripts
docker docker run txthinking/brook
Resources Description
Protocol Brook Protocol
Blog Some articles you should read
YouTube Some videos you should watch
Telegram Ask questions here
Announce All news you should care
GitHub Other useful repos
Socks5 Configurator If you prefer CLI brook client
IPvBar See domain, IP and country in browser
TxThinking SSH A SSH Terminal
brook-user-system A Brook User System
TxThinking Everything