Set Your Proxy Client On Synology NAS Through cli

You want to be able to configure your proxy client settings with the terminal but don’t know how…

Challenge

I use a couple of different proxy servers I’ve created with docker and I can (de)activate them through the
command line at will. The trouble came when I tried to do the same with the proxy client settings as shown in the screen shot.

Normally on linux machines you can just set environment variables like http_proxy and https_proxy but it
seems to go a bit different on a Synology NAS.

So I was able to start and stop the docker proxies easily on my iPhone with a simple ssh app but not the proxy client of Synology itself.

Finally I got tired of it and dug my heels in and did some reverse engineering :-)

Solution

Do it with the synowebapi.

There is an /etc/proxy.conf file where DSM saves its data to be retrieved when needed, but when I tried
to just edit that file and change some values DSM did not pick it up, hmmm not what I wanted.

Ok lets do it through the web interface and use the developers view to see the network traffic…
Yup that seemed to work and that resulted in the script below.

~/bin/set-proxy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/sh
PORT=8118
HOST=localhost
ENABLE=true
while getopts ":p:h:ad?" opt; do
case ${opt} in
h) HOST=$OPTARG
;;
p) PORT=$OPTARG
;;
a) ENABLE=true
;;
d) ENABLE=false
;;
\?) echo "Usage: set-proxy [-p PORT_NUMBER] [-h HOSTNAME] [-a] [-d] [-?]"
echo " -p PORT_NUMBER : sets the portnumber (default: 8118)"
echo " -h HOSTNAME : sets the hostname (default: localhost)"
echo " -a : activates the proxy client (=default)"
echo " -d : disables the proxy client"
echo " -? : this help message"
exit 0
;;
esac
done
/usr/syno/bin/synowebapi --exec \
"api"="SYNO.Core.Network.Proxy" \
"method"="set" \
"version"="1" \
"enable"=${ENABLE:-true} \
"http_host"="${HOST}" \
"http_port"="${PORT}" \
"enable_different_host"=false \
"enable_auth"=false \
"enable_bypass"=false \
"https_host"="${HOST}" \
"https_port"="${PORT}"
/usr/syno/bin/synowebapi --exec \
"api"="SYNO.Core.Network.Proxy" \
"method"="get"

Note that I have my ~/bin folder always on my PATH

When I saved the above script in set-proxy and ran it without params

1
set-proxy

the result was something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Line 259] Exec WebAPI: api=SYNO.Core.Network.Proxy, version=1, method=set, param={"enable":true,"enable_auth":false,"enable_bypass":false,"enable_different_host":false,"http_host":"localhost","http_port":8118,"https_host":"localhost","https_port":8118}, runner=
{
"httpd_restart" : false,
"success" : true
}
[Line 259] Exec WebAPI: api=SYNO.Core.Network.Proxy, version=1, method=get, param={}, runner=
{
"data" : {
"enable" : true,
"enable_auth" : false,
"enable_bypass" : false,
"enable_different_host" : false,
"http_host" : "localhost",
"http_port" : "8118",
"https_host" : "localhost",
"https_port" : "8118",
"password" : "\t\t\t\t\t\t\t\t",
"username" : ""
},
"httpd_restart" : false,
"success" : true
}

Other command examples:

Use localhost with port 3128 and enable it

1
set-proxy -p 3128 -a

disable the proxy:

1
set-proxy -d

Use some other host on port 92000 and enable it

1
set-proxy -a -p 92000 -h www.example.com

Note that you can add true but as it is the default you can drop it too.

testing

I did quite a few tests with this and it seems to work just fine.

With the following command you can see your current external ip address

1
curl -s https://ipecho.net/plain

Start with the above command to see what your IP is before setting a proxy and then start the proxy and try again…

An example test was to run my ivonet/nordvpn-tor-provoxy docker image on port 8118 and to set-proxy to it and then do
the curl as described above.

It all worked just as hoped.

Goal

Well my goal to be able to configure the proxy client through a cli interface has worked.
The commands need to be executed as root (sudo) and that makes scripting it a bit more difficult but not
insurmountable as I have already written a blog about how to Run A Script As root Without sudo

If you have questions of comments leave them below.
They are always welcome.