Skip to content

Commit 06e9cf3

Browse files
committed
add support for user-specified rules file
1 parent 407db22 commit 06e9cf3

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,17 @@ sudo mv ssh-tunnel-swarm /usr/bin/
154154

155155
### Environment Variables
156156

157+
- **RULES_FILE**: This can be used to specify the path to the file containing the tunnel rules. If this variable is not set, the default file path is set to `rules.txt`.
158+
157159
- **LOG_ENABLED**: This acts as a master switch for the logger. If it's set to `1`, logging is enabled. A value of `0` disables logging. Default value if not set is `1`.
158160

159161
- **LOG_FILE**: This determines the output destination of the log messages. If set, log messages will be written to the specified file. If not set, logs will be printed to stdout.
160162

161-
- **LOG_LEVEL**: This determines the severity level of the messages to be logged. Messages with a severity level less than this will not be logged. For example, if `LOG_LEVEL` is set to `INFO`, then `DEBUG` messages won't be logged. Default value if not set is `DEBUG`.
163+
- **LOG_LEVEL**: This determines the severity level of the messages to be logged. Messages with a severity level less than this will not be logged. For example, if `LOG_LEVEL` is set to `INFO`, then `DEBUG` messages won't be logged. Default value if not set is `DEBUG`. You can find all of the [supported log levels here](#supported-log-levels).
162164

163165
### Tunnel Rules
164166

165-
SSH Tunnel Swarm reads the tunnel rules from a file named `rules.txt` which contains the rules for each host. Each entry should include the username, hostname, port, and the tunnels to establish. For instance:
167+
SSH Tunnel Swarm reads the tunnel rules for each host from a file specified by the `RULES_FILE` environment variable. Each entry within the file should include the username, hostname, port, and the tunnels to establish. For instance:
166168

167169
```
168170
aparker@host789:34567
@@ -215,7 +217,13 @@ Now guess what happens when you try to connect to localhost:6366.
215217

216218
## Usage
217219

218-
- coming up soon
220+
```shell
221+
RULES_FILE=/path/to/rules.txt \
222+
LOG_ENABLED=1 \
223+
LOG_FILE=/path/to/log/file \
224+
LOG_LEVEL=DEBUG \
225+
ssh-tunnel-swarm
226+
```
219227

220228
## Logging
221229

logger.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/bash
2-
# Set default log level to DEBUG
2+
# Set default log level to DEBUG if it's not already set
33
: "${LOG_LEVEL:=DEBUG}"
44
# Set default log enabled flag only if it's not been set before
55
: "${LOG_ENABLED:=1}"

rules.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ source logger.sh
55
# Load validator functions
66
source validators.sh
77

8+
# Set default rules file to rules.txt if it's not already set
9+
: "${RULES_FILE:=rules.txt}"
10+
811
declare -A RULES
912

10-
# This function loads the rules from rules.txt file into an associative array called RULES.
13+
# This function loads the rules from the rules file into an associative array called RULES.
1114
# The format of the rules file is:
1215
# <username>@<hostname/IP address>:<port>
1316
# <forward/reverse> <local address>:<local port>:<remote address>:<remote port>
@@ -18,13 +21,13 @@ declare -A RULES
1821
load_rules() {
1922
logger "DEBUG" "Starting load_rules function"
2023

21-
# Check if rules.txt exists and has content
22-
if [ ! -f "rules.txt" ]; then
23-
logger "FATAL" "rules.txt not found"
24+
# Check if the rules file exists and has content
25+
if [ ! -f "$RULES_FILE" ]; then
26+
logger "FATAL" "Rules file \"$RULES_FILE\" not found"
2427
fi
2528

26-
if [ ! -s "rules.txt" ]; then
27-
logger "FATAL" "rules.txt is empty"
29+
if [ ! -s "$RULES_FILE" ]; then
30+
logger "FATAL" "Rules file \"$RULES_FILE\" is empty"
2831
fi
2932

3033
# Variable to keep track of the current host being processed
@@ -82,7 +85,7 @@ load_rules() {
8285
# Log an error for invalid lines
8386
logger "FATAL" "Invalid line in rules file: $line in line $line_no"
8487
fi
85-
done <rules.txt
88+
done <"$RULES_FILE"
8689

8790
logger "DEBUG" "load_rules function finished"
8891
}

0 commit comments

Comments
 (0)