Skip to content

added gotty #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.18.6

EXPOSE 22 80 443 1180 11443
EXPOSE 22 80 443 1180 11443 8080

# Install some tools in the container and generate self-signed SSL certificates.
# Packages are listed in alphabetical order, for ease of readability and ease of maintenance.
Expand Down Expand Up @@ -47,22 +47,22 @@ COPY nginx.conf /etc/nginx/nginx.conf
COPY .bashrc /root/.bashrc
RUN echo 'if [ -f ~/.bashrc ]; then . ~/.bashrc; fi' > /root/.bash_profile

# Install GoTTY binary only - NOT starting it automatically
RUN wget -q -O /tmp/gotty.tar.gz https://github.com/sorenisanerd/gotty/releases/download/v1.5.0/gotty_v1.5.0_linux_amd64.tar.gz && \
tar -zxf /tmp/gotty.tar.gz -C /usr/local/bin && \
rm /tmp/gotty.tar.gz && \
chmod +x /usr/local/bin/gotty

# Create directories for GoTTY service
RUN mkdir -p /var/run/gotty /var/log/gotty

COPY gotty-service /usr/local/bin/gotty-service
RUN chmod +x /usr/local/bin/gotty-service

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's make it multi arch friendly by introducing a build image for gotty that uses go install way of installing gotty
then you just copy the binary from this build step to the target system

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it so its installed during a build stage and copied from it

COPY entrypoint.sh /docker/entrypoint.sh

# Start nginx in foreground (pass CMD to docker entrypoint.sh):
CMD ["/usr/sbin/nginx", "-g", "daemon off;"]

# Note: If you have not included the "bash" package, then it is "mandatory" to add "/bin/sh"
# in the ENTNRYPOINT instruction.
# Otherwise you will get strange errors when you try to run the container.
# Such as:
# standard_init_linux.go:219: exec user process caused: no such file or directory

# Run the startup script as ENTRYPOINT, which does few things and then starts nginx.
ENTRYPOINT ["/bin/sh", "/docker/entrypoint.sh"]






ENTRYPOINT ["/bin/sh", "/docker/entrypoint.sh"]
150 changes: 150 additions & 0 deletions gotty-service
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#!/bin/bash

# GoTTY service management script
# Usage: gotty-service {start|stop|status|restart} [PORT] [USERNAME] [PASSWORD] [SHELL]

# Default configuration
PORT=${2:-8080}
USERNAME=${3:-admin}
PASSWORD=${4:-admin}
SHELL=${5:-bash}
TITLE="Network Multitool Terminal"

# Paths
PID_FILE="/var/run/gotty/gotty-${PORT}.pid"
LOG_FILE="/var/log/gotty/gotty-${PORT}.log"

# Ensure directories exist
mkdir -p /var/run/gotty /var/log/gotty

check_port() {
if netstat -tuln | grep -q ":$PORT "; then
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
# Our GoTTY service is using this port
return 0
fi
fi
# Another service is using this port
echo "Error: Port $PORT is already in use by another service."
return 1
fi
return 0
}

start_gotty() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
echo "GoTTY is already running on port $PORT (PID: $PID)"
return 0
else
# Stale PID file
rm "$PID_FILE"
fi
fi

check_port || return 1

echo "Starting GoTTY service on port $PORT..."
gotty -w -p "$PORT" -c "$USERNAME:$PASSWORD" --title-format "$TITLE" "$SHELL" > "$LOG_FILE" 2>&1 &

PID=$!
echo $PID > "$PID_FILE"

# Check if process is still running after a short delay
sleep 1
if ps -p "$PID" > /dev/null; then
echo "GoTTY service started successfully (PID: $PID)"
echo "Access web terminal at: http://<host-ip>:$PORT"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"
return 0
else
echo "Failed to start GoTTY service. Check logs at $LOG_FILE"
rm -f "$PID_FILE"
return 1
fi
}

stop_gotty() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
echo "Stopping GoTTY service on port $PORT (PID: $PID)..."
kill "$PID"

# Wait for process to terminate
for i in {1..5}; do
if ! ps -p "$PID" > /dev/null; then
break
fi
sleep 1
done

# Check if process is still running
if ps -p "$PID" > /dev/null; then
echo "GoTTY service did not stop gracefully, forcing termination..."
kill -9 "$PID"
fi

rm -f "$PID_FILE"
echo "GoTTY service stopped"
else
echo "GoTTY service is not running on port $PORT (stale PID file)"
rm -f "$PID_FILE"
fi
else
echo "GoTTY service is not running on port $PORT"
fi
}

check_status() {
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ps -p "$PID" > /dev/null; then
echo "GoTTY service is running on port $PORT (PID: $PID)"
echo "Access web terminal at: http://<host-ip>:$PORT"
echo "Username: $USERNAME"
echo "Password: $PASSWORD"
return 0
else
echo "GoTTY service is not running on port $PORT (stale PID file)"
rm -f "$PID_FILE"
return 1
fi
else
echo "GoTTY service is not running on port $PORT"
return 1
fi
}

restart_gotty() {
stop_gotty
sleep 2
start_gotty
}

# Main logic
case "$1" in
start)
start_gotty
;;
stop)
stop_gotty
;;
status)
check_status
;;
restart)
restart_gotty
;;
*)
echo "Usage: $0 {start|stop|status|restart} [PORT] [USERNAME] [PASSWORD] [SHELL]"
echo "Default: PORT=8080, USERNAME=admin, PASSWORD=admin, SHELL=bash"
exit 1
;;
esac

exit 0