From 573f158f7f42d4238c198bb12306343094f44cc0 Mon Sep 17 00:00:00 2001
From: Nelson Chan <chakflying@hotmail.com>
Date: Thu, 27 Jul 2023 17:42:22 +0800
Subject: [PATCH 1/2] Fix: incorrect radius error handling

---
 server/model/monitor.js | 36 +++++++++++++-----------------------
 server/util-server.js   |  7 +++++++
 2 files changed, 20 insertions(+), 23 deletions(-)

diff --git a/server/model/monitor.js b/server/model/monitor.js
index 06f6a7d5..ff9f34a6 100644
--- a/server/model/monitor.js
+++ b/server/model/monitor.js
@@ -759,29 +759,19 @@ class Monitor extends BeanModel {
                         port = this.port;
                     }
 
-                    try {
-                        const resp = await radius(
-                            this.hostname,
-                            this.radiusUsername,
-                            this.radiusPassword,
-                            this.radiusCalledStationId,
-                            this.radiusCallingStationId,
-                            this.radiusSecret,
-                            port,
-                            this.interval * 1000 * 0.8,
-                        );
-                        if (resp.code) {
-                            bean.msg = resp.code;
-                        }
-                        bean.status = UP;
-                    } catch (error) {
-                        bean.status = DOWN;
-                        if (error.response?.code) {
-                            bean.msg = error.response.code;
-                        } else {
-                            bean.msg = error.message;
-                        }
-                    }
+                    const resp = await radius(
+                        this.hostname,
+                        this.radiusUsername,
+                        this.radiusPassword,
+                        this.radiusCalledStationId,
+                        this.radiusCallingStationId,
+                        this.radiusSecret,
+                        port,
+                        this.interval * 1000 * 0.4,
+                    );
+
+                    bean.msg = resp.code;
+                    bean.status = UP;
                     bean.ping = dayjs().valueOf() - startTime;
                 } else if (this.type === "redis") {
                     let startTime = dayjs().valueOf();
diff --git a/server/util-server.js b/server/util-server.js
index 031d8b67..ece0456d 100644
--- a/server/util-server.js
+++ b/server/util-server.js
@@ -486,6 +486,7 @@ exports.radius = function (
         host: hostname,
         hostPort: port,
         timeout: timeout,
+        retries: 1,
         dictionaries: [ file ],
     });
 
@@ -497,6 +498,12 @@ exports.radius = function (
             [ attributes.CALLING_STATION_ID, callingStationId ],
             [ attributes.CALLED_STATION_ID, calledStationId ],
         ],
+    }).catch((error) => {
+        if (error.response?.code) {
+            throw Error(error.response.code);
+        } else {
+            throw Error(error.message);
+        }
     });
 };
 

From c0eb0cb42cdd5c7b612de828b54e6ca06d123085 Mon Sep 17 00:00:00 2001
From: Nelson Chan <chakflying@hotmail.com>
Date: Thu, 27 Jul 2023 17:43:33 +0800
Subject: [PATCH 2/2] Chore: Add test radius dockerfile

---
 test/test-radius.dockerfile | 13 +++++++++++++
 1 file changed, 13 insertions(+)
 create mode 100644 test/test-radius.dockerfile

diff --git a/test/test-radius.dockerfile b/test/test-radius.dockerfile
new file mode 100644
index 00000000..3f577ede
--- /dev/null
+++ b/test/test-radius.dockerfile
@@ -0,0 +1,13 @@
+# Container running a test radius server
+# More instructions in https://github.com/louislam/uptime-kuma/pull/1635
+
+FROM freeradius/freeradius-server:latest
+
+RUN mkdir -p /etc/raddb/mods-config/files/
+
+RUN echo "client net {"                 > /etc/raddb/clients.conf
+RUN echo "    ipaddr = 172.17.0.0/16"  >> /etc/raddb/clients.conf
+RUN echo "    secret = testing123"     >> /etc/raddb/clients.conf
+RUN echo "}"                           >> /etc/raddb/clients.conf
+
+RUN echo "bob Cleartext-Password := \"testpw\"" > /etc/raddb/mods-config/files/authorize