txthinking / brook

A cross-platform programmable network tool
https://brook.app
GNU General Public License v3.0
14.4k stars 2.39k forks source link

How to bypass local IPs? #1353

Closed donaldzou closed 2 months ago

donaldzou commented 2 months ago

Haven't use Brook in a long time and saw the huge changes and I loved it! Just wondering how can I bypass local IPs? I tried to write a module but it doesn't seem to be working:

address_handler := func(m) {
   if m.ipaddress {
        if m.ipaddress == "192.168.64.2" {
             return { "bypass": true }
           }
      } 
 }

 handler := func(){
    if in_address {
        return address_handler(in_address)
    }
}

handler()

Plz help :) Thank you!

donaldzou commented 2 months ago

Forgot to mention... The issue I'm having is, I have a VM running locally on 192.168.64.2 and is using bridge mode for internet connection. After I connected Brook GUI (macOS), I can no longer access the VM via IP

txthinking commented 2 months ago

There is a built-in module: Bypass Geo, it contains ZZ. All private IPs are ZZ. BTW, using m.ipaddress is also possible, but the definition of address(m.ipaddress or m.domainaddress) includes both the host and port. For example, 1.1.1.1:443, or google.com:443. Sorry for the lack of clarity in the documentation, I think I should improve it.

Test:

  1. Brook GUI: Enable Bypass Geo module, and Connect
  2. Ubuntu: start a simple http server nami install httpserver, httpserver -l :8080
  3. macOS: connect to the http server: curl -I http://192.168.3.160:8080, success.

I have no tested it on M1 Mac, because I have not installed VMware Fusion on my m1 mac, I believe the result should be the same?


Brook GUI

In Brook GUI, scripts are abstracted into modules, and it will 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`
    }
})

In your case, for example:

modules = append(modules, {
    address: func(m) {
        if m.ipaddress {
            text := import("text")
            if text.has_prefix(m.ipaddress, "192.168.64.2") { // because m.ipaddress contains port, such as 192.168.64.2:80
                return { bypass: true }
            }
        }
    }
})

tun2brook

If you are using tun2brook, you can 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
donaldzou commented 2 months ago

Yay! Thank you for the quick response, it works now. I think the tengo script above is working, it was just the network configuration I had was "Shared" but not "Bridge". Tysm!