Skip to content

Commit 5867a44

Browse files
committed
Running at non root
1 parent eaff94d commit 5867a44

File tree

6 files changed

+108
-49
lines changed

6 files changed

+108
-49
lines changed

etc/ddns/aliyun/config.json renamed to .ddns/aliyun.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
{
55
"name": "akProfile",
66
"mode": "AK",
7-
"access_key_id": "<AccessKey ID>",
8-
"access_key_secret": "<Access Key Secret>",
7+
"access_key_id": "<ACCESS_KEY_ID>",
8+
"access_key_secret": "<ACCESS_KEY_SECRET>",
99
"sts_token": "",
1010
"ram_role_name": "",
1111
"ram_role_arn": "",

etc/ddns/ddns.conf renamed to .ddns/ddns.conf

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
ALIYUN_BIN="/usr/bin/aliyun"
33

44
# 阿里云 CLI 公共选项
5-
ALIYUN_OPTIONS="--config-path /etc/ddns/aliyun/config.json --profile akProfile"
5+
ALIYUN_OPTIONS="--profile akProfile"
66

77
# 公网 IP 查询时间间隔,单位:秒
88
QUERY_INTERVAL="30"
99

10+
# 阿里云 AccessKey
11+
ACCESS_KEY_ID="<AccessKey ID>"
12+
ACCESS_KEY_SECRET="<Access Key Secret>"
13+
1014
# 需要操作的域名
1115
RECORD_RR="www"
1216
RECORD_DOMAIN="example.com"

README.md

+16-25
Original file line numberDiff line numberDiff line change
@@ -26,58 +26,49 @@
2626
3. 克隆项目并运行 `install.sh` 进行安装
2727

2828
git clone [email protected]:hamflx/ddns-aliyun.git
29-
sudo ./install.sh
29+
cd ddns-aliyun
3030

31-
4. 配置 `AccessKey ID``Access Key Secret`
31+
# install.sh 将会把配置文件安装到此处指定的用户名 <USERNAME> 家目录下
32+
sudo ./install.sh install <USERNAME>
33+
34+
4. 配置 `~/.ddns/ddns.conf`
3235

3336
进入阿里云 accesskeys 管理页面,生成一个 `AccessKey`
3437

3538
![阿里云 accesskeys 管理页面入口](./assets/step1.png "阿里云 accesskeys 管理页面入口")
3639

3740
![阿里云 accesskeys 管理页面](./assets/step2.png "阿里云 accesskeys 管理页面")
3841

39-
sudo vim /etc/ddns/aliyun/config.json
40-
41-
编辑 `/etc/ddns/aliyun/config.json``<AccessKey ID>``<Access Key Secret>` 替换为在阿里云上生成的 `AccessKey``AccessKey ID``Access Key Secret`
42-
43-
{
44-
...
45-
"profiles": [
46-
{
47-
...
48-
"access_key_id": "<AccessKey ID>",
49-
"access_key_secret": "<Access Key Secret>",
50-
...
51-
}
52-
}
53-
54-
5. 配置 ddns.conf
42+
编辑 `~/.ddns/ddns.conf``ACCESS_KEY_ID``ACCESS_KEY_SECRET` 的值改为在阿里云上生成的 `AccessKey``AccessKey ID``Access Key Secret`
5543

56-
编辑 `RECORD_RR` 的值为需要解析的记录,`RECORD_DOMAIN` 顶级域名。
44+
sudo vim ~/.ddns/ddns.conf
5745

58-
sudo vim /etc/ddns/ddns.conf
46+
# 找到下面两句,将 <AccessKey ID> 与 <Access Key Secret> 修改为具体的值
47+
# ACCESS_KEY_ID="<AccessKey ID>"
48+
# ACCESS_KEY_SECRET="<Access Key Secret>"
5949

60-
例如
50+
修改 `RECORD_RR` 的值为需要解析的记录,`RECORD_DOMAIN` 顶级域名
6151

6252
# 需要操作的域名
6353
RECORD_RR="www"
6454
RECORD_DOMAIN="njstee.com"
6555

66-
6. 添加域名
56+
5. 添加域名
6757

6858
在阿里云控制台添加需要解析的域名记录,值为任意即可,在启动软件后会自动获取公网 IP 并进行设置。
6959

7060
![解析记录](./assets/record.png "解析记录")
7161

72-
7. 启动 ddns-aliyun
62+
6. 启动 ddns-aliyun
7363

7464
启动 ddns-aliyun 进行测试,若提示 `Updated successfully``Already latest` 则表示运行成功:
7565

76-
sudo /usr/bin/ddns.sh
66+
ddns.sh
7767

7868
启动 ddns-aliyun 并将其设置为随系统引导一起启动:
7969

80-
sudo systemctl enable --now ddns.service
70+
# 将 <USERNAME> 替换为此前运行 install.sh 时指定的用户名
71+
sudo systemctl enable --now ddns@<USERNAME>.service
8172

8273
## License
8374

bin/ddns.sh

+16-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,23 @@ function log () {
88
fi
99
}
1010

11-
. /etc/ddns/ddns.conf
11+
CONFIGURATION_DIR="$HOME/.ddns"
12+
CONFIGURATION_FILE="$HOME/.ddns/ddns.conf"
13+
ALIYUN_CONFIG_FILE="$HOME/.ddns/aliyun.json"
1214

15+
if [[ ! -f "$CONFIGURATION_FILE" ]]; then
16+
echo "Configuration file $CONFIGURATION_FILE not found."
17+
exit 1
18+
fi
19+
20+
. "$CONFIGURATION_FILE"
21+
22+
# 将配置文件中的 AccessKey 写入到阿里云的配置文件
23+
sed -i -e "s/\"access_key_id\": \"[^\"]*\"/\"access_key_id\": \"$ACCESS_KEY_ID\"/g" \
24+
-e "s/\"access_key_secret\": \"[^\"]*\"/\"access_key_secret\": \"$ACCESS_KEY_SECRET\"/g" "$ALIYUN_CONFIG_FILE"
25+
26+
# 将配置文件路径传入阿里云 CLI
27+
ALIYUN_OPTIONS="$ALIYUN_OPTIONS"" --config-path ""$ALIYUN_CONFIG_FILE"
1328
# 从阿里云获取域名解析记录
1429
RECORD="$($ALIYUN_BIN alidns $ALIYUN_OPTIONS DescribeSubDomainRecords --SubDomain $RECORD_RR.$RECORD_DOMAIN | jq -r '.DomainRecords.Record[0]' 2>/dev/null)"
1530
# 从域名信息中提取 RecordId

install.sh

+68-18
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,97 @@
11
#!/bin/bash
22

33
function do_install () {
4-
cp -r bin/* /usr/bin 2>/dev/null &&
5-
cp -r etc/* /etc 2>/dev/null &&
6-
cp -r lib/systemd/system/* /lib/systemd/system 2>/dev/null &&
7-
chmod 0755 /usr/bin/ddns.sh 2>/dev/null &&
8-
chmod 0660 /etc/ddns/aliyun/config.json 2>/dev/null
4+
HOME_DIR="$(cat /etc/passwd | grep -P ""^$1:"" | cut -d: -f6)"
5+
DDNS_CONF_DIR="$HOME_DIR/.ddns"
6+
SCRIPT_PATH="$(readlink -f ""$0"")"
7+
SOURCE_DIR="$(dirname ""$SCRIPT_PATH"")"
98

10-
return $?
9+
if [[ ! -d "$HOME_DIR" ]]; then
10+
echo "Home directory $HOME_DIR not found"
11+
return 1
12+
fi
13+
14+
mkdir -p "$DDNS_CONF_DIR" 2>/dev/null && \
15+
cp -r "$SOURCE_DIR/bin/"* /usr/bin 2>/dev/null && \
16+
cp -r "$SOURCE_DIR/.ddns/"* "$DDNS_CONF_DIR" 2>/dev/null && \
17+
cp -r "$SOURCE_DIR/lib/systemd/system/"* /lib/systemd/system 2>/dev/null && \
18+
chown -R "$1.$1" "$DDNS_CONF_DIR" 2>/dev/null && \
19+
chmod 0755 /usr/bin/ddns.sh 2>/dev/null && \
20+
chmod 0700 "$DDNS_CONF_DIR" 2>/dev/null && \
21+
chmod 0600 "$DDNS_CONF_DIR/"* 2>/dev/null
22+
23+
if [[ "$?" != "0" ]]; then
24+
echo 'Failed to copy files to system.'
25+
return 1
26+
fi
27+
28+
return 0
1129
}
1230

1331
function do_uninstall () {
32+
HOME_DIR="$(cat /etc/passwd | grep -P ""^$1:"" | cut -d: -f6)"
33+
DDNS_CONF_DIR="$HOME_DIR/.ddns"
34+
SCRIPT_PATH="$(readlink -f ""$0"")"
35+
SOURCE_DIR="$(dirname ""$SCRIPT_PATH"")"
36+
37+
if [[ ! -d "$HOME_DIR" ]]; then
38+
echo "Home directory $HOME_DIR not found"
39+
return 1
40+
fi
41+
1442
rm -f /lib/systemd/system/ddns.service 2>/dev/null
15-
rm -rf /etc/ddns 2>/dev/null
1643
rm -f /usr/bin/ddns.sh 2>/dev/null
44+
rm -f "$DDNS_CONF_DIR/"* 2>/dev/null
45+
rmdir "$DDNS_CONF_DIR" 2>/dev/null
1746

1847
return 0
1948
}
2049

21-
if [[ "$(id -u)" != "0" ]]; then
22-
echo "Require root permission!"
23-
exit 1
50+
function usage () {
51+
echo "Usage:"
52+
echo " ddns.sh install <username>"
53+
echo " ddns.sh uninstall <username>"
54+
}
55+
56+
if [[ "$#" != "2" ]]; then
57+
usage
58+
exit 0
2459
fi
2560

26-
if [[ "$1" == "uninstall" ]]; then
61+
COMMAND="$1"
62+
shift
63+
USERNAME="$1"
64+
shift
2765

28-
do_uninstall
66+
if [[ "$COMMAND" == "--help" || "$COMMAND" == "-h" ]]; then
67+
usage
68+
exit 0
69+
fi
2970

30-
echo "Uninstalled successfully"
71+
if ! id "$USERNAME" >/dev/null 2>&1; then
72+
echo "User $USERNAME not exists"
73+
exit 1
74+
fi
3175

32-
else
76+
if [[ "$(id -u)" != "0" ]]; then
77+
echo "Require root permission."
78+
exit 1
79+
fi
3380

34-
do_install
81+
if [[ "$COMMAND" == "uninstall" ]]; then
82+
do_uninstall "$USERNAME"
83+
echo "Uninstalled successfully"
84+
else
85+
do_install "$USERNAME"
3586

3687
if [[ "$?" == "0" ]]; then
3788
echo "Installed successfully"
3889
else
39-
echo "Failed to install!"
90+
echo "Failed to install."
4091
echo "Uninstalling ..."
4192

42-
do_uninstall
93+
do_uninstall "$USERNAME"
4394

4495
exit 1
4596
fi
46-
4797
fi
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ Type=simple
88
ExecStart=/bin/bash /usr/bin/ddns.sh
99
Restart=always
1010
RestartSec=30s
11-
User=root
12-
Group=root
11+
User=%i
1312

1413
[Install]
1514
WantedBy=multi-user.target

0 commit comments

Comments
 (0)