# Network Optimization

If the download package is slow or fails due to an unstable network, we can use the following methods to resolve it.

## Manual Download

By default, xmake will call curl, wget, and other tools to download. Users can also manually download with their own downloader (you can also use a proxy), and put the downloaded package in their own directory, for example: `/download/packages/zlib-v1.0.tar.gz`

Then use the following command to set the search directory for package downloads:

```sh
$ xmake g --pkg_searchdirs="/download/packages"
```

Then re-execute xmake to compile. Xmake will first look for the source package from `/download/packages`, and then use it directly, no longer downloading it itself.

As for the package name you are looking for, you can check it by the following command:

```sh
$ xmake require --info zlib
-> searchdirs: /download/packages
-> searchnames: zlib-1.2.11.tar.gz
```

We can see the corresponding search directory and the searched package name.

## Use Proxy

If manual downloading is still troublesome, we can also let xmake go directly to the proxy.

```sh
$ xmake g --proxy="socks5://127.0.0.1:1086"
$ xmake g --help
    -x PROXY, --proxy=PROXY Use proxy on given port. [PROTOCOL://]HOST[:PORT]
                                 e.g.
                                 -xmake g --proxy='http://host:port'
                                 -xmake g --proxy='https://host:port'
                                 -xmake g --proxy='socks5://host:port'
```

The `--proxy` parameter specifies the proxy protocol and address. The specific syntax can refer to curl. Usually, it can support http, https, socks5, and other protocols, but the actual support depends on curl, wget, and git. For example, wget does not support the socks5 protocol.

We can use the following parameters to specify which hosts go to the proxy. If not set, the default is to go global.

```sh
--proxy_hosts=PROXY_HOSTS Only enable proxy for the given hosts list, it will enable all if be unset,
                             and we can pass match pattern to list:
                                 e.g.
                                 -xmake g --proxy_hosts='github.com,gitlab.*,*.xmake.io'
```

If the hosts list is set, then the matching hosts in this list will go to the proxy.

`--proxy_host` supports multiple host settings, separated by commas, and supports basic pattern matching like *.github.com, and other lua pattern matching rules are also supported.

If we feel that the above hosts mode configuration is not flexible enough, we can also follow pac's automatic proxy configuration rules:

```sh
--proxy_pac=PROXY_PAC Set the auto proxy configuration file. (default: pac.lua)
                                     e.g.
                                     -xmake g --proxy_pac=pac.lua (in /Users/ruki/.xmake or absolute path)
                                     -function main(url, host)
                                           if host =='github.com' then
                                                return true
                                           end
                                       end
```

::: tip NOTE
If there are proxy_hosts, the host configuration is preferred, otherwise, the pac configuration can be used.
:::

The default path of pac: ~/.xmake/pac.lua. If --proxy is set, and this file exists, it will automatically go to pac. If it does not exist, and there are no hosts, then the proxy will take effect globally.

You can also manually specify the pac full path

```sh
$ xmake g --proxy_pac=/xxxx/xxxxx_pac.lua
```

Configuration rule description:

```lua
function main(url, host)
    if host:find("bintray.com") then
        return true
    end
end
```

If it returns true, then the url and host are the proxy to go, not to return or return false, it is not to proxy.

For specific details of this, see: https://github.com/xmake-io/xmake/issues/854

## Configure Mirror Proxy

After v2.5.4, mirror proxy rules can also be configured in the pac.lua configuration. For example, access to all github.com domain names is switched to the hub.fastgit.org domain name to achieve accelerated downloading of packages.

```lua
function mirror(url)
    return url:gsub("github.com", "hub.fastgit.org")
end
```

```sh
$ xrepo install libpng
> curl https://hub.fastgit.org/glennrp/libpng/archive/v1.6.37.zip -o v1.6.37.zip
```

