Skip to content

Commit

Permalink
init & build arm64
Browse files Browse the repository at this point in the history
  • Loading branch information
i36lib committed May 24, 2024
0 parents commit cdcc98f
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 0 deletions.
Binary file added Country.mmdb
Binary file not shown.
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM alpine:latest

LABEL org.opencontainers.image.authors="[email protected]"

ARG ARCH_TYPE=unsupport
ARG PREMIUM=unsupport

WORKDIR /var/app

COPY Country.mmdb .
COPY ${ARCH_TYPE}/clash${PREMIUM} /usr/local/bin/clash
COPY reload.sh /usr/local/bin/reload.sh
COPY entry.sh /usr/local/bin/entry.sh

RUN chmod +x /usr/local/bin/clash \
/usr/local/bin/reload.sh \
/usr/local/bin/entry.sh \
&& apk add --no-cache curl shadow

EXPOSE 7890 9090
VOLUME ["/config"]

ENTRYPOINT ["/bin/sh", "/usr/local/bin/entry.sh"]
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: build

build:
sh build.sh
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## clashx

```shell
$ docker run -p 7890:7890 -p 9090:9090 -v ./config:/config i36lib/clashx:latest

$ ./reload.sh config http://127.0.0.1:9090
```
Binary file added arm64/clash
Binary file not shown.
Binary file added arm64/clash-premium
Binary file not shown.
25 changes: 25 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/sh

ARCH=$(arch)

if [ "$ARCH" = "i386" ] || [ "$ARCH" = "x86" ]; then
ARCH_VALUE="amd64"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "armv8" ]; then
ARCH_VALUE="arm64"
else
echo "Error: Unsupported architecture $ARCH" >&2
exit 1
fi

docker build --build-arg PREMIUM='' \
--build-arg ARCH_TYPE=$ARCH_VALUE \
-t i36lib/clashx:$ARCH_VALUE-latest .
docker push i36lib/clashx:$ARCH_VALUE-latest

echo 'build premium ...'

docker build --build-arg PREMIUM='-premium' \
--build-arg ARCH_TYPE=$ARCH_VALUE \
-t i36lib/clashx:premium-$ARCH_VALUE-latest .
docker push i36lib/clashx:premium-$ARCH_VALUE-latest

52 changes: 52 additions & 0 deletions entry.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/sh

# 如果未设置 PUID 和 PGID 环境变量,则设置默认值
USER_UID=${PUID:-1000}
USER_GID=${PGID:-1000}
USER_NAME="appuser"
GROUP_NAME="appgroup"

# 检查 GID 是否已经存在
if getent group "$USER_GID" > /dev/null 2>&1; then
# 如果 GID 已存在,使用现有组名
GROUP_NAME=$(getent group "$USER_GID" | cut -d: -f1)
else
# 创建新的组
addgroup -g "$USER_GID" "$GROUP_NAME"
fi

# 检查 UID 是否已经存在
if id -u "$USER_UID" > /dev/null 2>&1; then
# 如果 UID 已存在,使用现有用户名
USER_NAME=$(getent passwd "$USER_UID" | cut -d: -f1)
else
# 创建新的用户,并加入到组
adduser -u "$USER_UID" -G "$GROUP_NAME" -D "$USER_NAME"
fi

# 确保 /config 目录属于 appuser:appgroup
chown "$USER_NAME:$GROUP_NAME" /config

# 复制 Country.mmdb 到 /config
cp /var/app/Country.mmdb /config

# Function to handle SIGTERM
terminate() {
echo "Entrypoint received SIGTERM, forwarding to child processes..."
kill -SIGTERM $(jobs -p)
wait
exit 0
}

# Trap SIGTERM and call terminate function
trap terminate SIGTERM

# Start reload.sh in the background as appuser
su "$USER_NAME" -c "/bin/sh /usr/local/bin/reload.sh &"

# Execute /usr/local/bin/clash as appuser
exec su "$USER_NAME" -c "/usr/local/bin/clash -d /config"

# Wait for child processes to finish
wait

62 changes: 62 additions & 0 deletions reload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/bash

# Function to handle SIGTERM
terminate() {
echo "Received SIGTERM, terminating..."
exit 0
}

# Trap SIGTERM signal
trap terminate SIGTERM

# Default sleep time in minutes
default_sleep_minutes=15

# Check if the sleep time is specified in the environment variable, otherwise use the default
sleep_minutes=${REFRESH_CONFIG_INTERVAL:-$default_sleep_minutes}

# Convert minutes to seconds
sleep_seconds=$((sleep_minutes * 60))

# Loop indefinitely
while true; do
# Read the first line of /config/rss_url and trim whitespace
touch /config/rss_url
url=$(head -n 1 /config/rss_url | xargs)

if [ -n "$url" ]; then
# Download the content from the URL and save it as config.yaml
curl_output=$(curl -sS -m 5 -w "\n%{http_code}\n" -o /config/config.yaml "$url")

# Extract the HTTP status code from the response
http_code=$(echo "$curl_output" | tail -n 1)

# Check if the HTTP status code is not 200
if [ "$http_code" -ne 200 ]; then
echo "Failed to download config file: HTTP $http_code"
continue
fi

# Perform a PUT request to the specified address and capture response and HTTP code
response=$(curl -sS -m 5 -w "\n%{http_code}\n" -X PUT -H "Content-Type: application/json" \
-d '{"path": "/config/config.yaml"}' \
http://127.0.0.1:9090/configs)

# Extract the HTTP status code from the response
http_code=$(echo "$response" | tail -n 1)

if [ "$http_code" -eq 204 ]; then
echo "Config reloaded."
else
# Print the response and the HTTP code for debugging
echo "Failed to reload config: HTTP $http_code"
echo "$response"
fi
else
echo "URL is empty or not found"
fi

# Sleep for a specified interval before the next iteration
sleep "$sleep_seconds"
done

0 comments on commit cdcc98f

Please sign in to comment.