From 379cda979c098955bafb2a61c5443526c6b472ba Mon Sep 17 00:00:00 2001 From: Damon To Date: Tue, 7 May 2024 15:04:18 +0800 Subject: [PATCH] feat: show advertising message --- .github/workflows/deploy.yml | 6 +----- README.md | 8 +++++--- internal/cloud/server.go | 6 ++++++ internal/config/config.go | 15 +++++++++++++++ main.go | 1 + scripts/deploy.sh | 11 +++++------ 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index cd08a3c..60a9038 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -37,14 +37,10 @@ jobs: exit 1 fi - # Save the private key to a file mkdir -p $HOME/.ssh echo "$SSH_PRIVATE_KEY" > $HOME/.ssh/id_ed25519 chmod 600 $HOME/.ssh/id_ed25519 ssh -i $HOME/.ssh/id_ed25519 -o StrictHostKeyChecking=no ${{ matrix.server.user }}@${{ matrix.server.host }} -p ${{ matrix.server.port }} "mkdir -p /opt/estkme-cloud" scp -i $HOME/.ssh/id_ed25519 -P ${{ matrix.server.port }} -r scripts/deploy.sh ${{ matrix.server.user }}@${{ matrix.server.host }}:/opt/estkme-cloud - ssh -i $HOME/.ssh/id_ed25519 -o StrictHostKeyChecking=no ${{ matrix.server.user }}@${{ matrix.server.host }} -p ${{ matrix.server.port }} "sudo /opt/estkme-cloud/deploy.sh && rm -f /opt/estkme-cloud/deploy.sh" - - - + ssh -i $HOME/.ssh/id_ed25519 -o StrictHostKeyChecking=no ${{ matrix.server.user }}@${{ matrix.server.host }} -p ${{ matrix.server.port }} "sudo /opt/estkme-cloud/deploy.sh ${{ matrix.server.ad }} && rm -f /opt/estkme-cloud/deploy.sh" diff --git a/README.md b/README.md index 0cddf1f..e1156f4 100644 --- a/README.md +++ b/README.md @@ -52,14 +52,16 @@ If you want to change the default port, lpac version or data directory, you can ./estkme-cloud --help Usage of estkme-cloud: + -advertising string + advertising message to show on the server (max: 100 characters) -data-dir string data directory (default "/home/user/workspace/estkme-cloud/data") + -dont-download + don't download lpac -listen-address string eSTK.me cloud enhance server listen address (default ":1888") -lpac-version string - lpac version (default "v2.0.0-beta.1") - -dont-download - don't download lpac + lpac version (default "v2.0.1") -verbose verbose mode ``` diff --git a/internal/cloud/server.go b/internal/cloud/server.go index a70c692..36c7202 100644 --- a/internal/cloud/server.go +++ b/internal/cloud/server.go @@ -10,6 +10,8 @@ import ( "os" "sync" "time" + + "github.com/damonto/estkme-cloud/internal/config" ) type Server interface { @@ -66,6 +68,10 @@ func (s *server) handleConn(tcpConn *net.TCPConn) { defer conn.Close() defer s.manager.Remove(id) + if config.C.Advertising != "" { + conn.Send(TagMessageBox, []byte(config.C.Advertising)) + } + for { tag, data, err := conn.Read() if err != nil { diff --git a/internal/config/config.go b/internal/config/config.go index caaf7d5..6ca2ed2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -11,6 +11,7 @@ type Config struct { LpacVersion string DataDir string DontDownload bool + Advertising string Verbose bool } @@ -18,6 +19,8 @@ var C = &Config{} var ( ErrLpacVersionRequired = errors.New("lpac version is required") + ErrAdvertisingTooLong = errors.New("advertising message is too long (max: 100 characters)") + ErrInvalidAdvertising = errors.New("advertising message contains non-printable ASCII characters") ) func (c *Config) IsValid() error { @@ -27,6 +30,15 @@ func (c *Config) IsValid() error { if c.LpacVersion == "" { return ErrLpacVersionRequired } + if len(c.Advertising) > 100 { + return ErrAdvertisingTooLong + } + // Advertising message is only allowed contain printable ASCII characters + for _, r := range c.Advertising { + if r < 32 || r > 126 { + return ErrInvalidAdvertising + } + } return nil } @@ -43,6 +55,9 @@ func (c *Config) LoadEnv() { if os.Getenv("ESTKME_CLOUD_DONT_DOWNLOAD") != "" { c.DontDownload = true } + if os.Getenv("ESTKME_CLOUD_ADVERTISING") != "" { + c.Advertising = os.Getenv("ESTKME_CLOUD_ADVERTISING") + } if os.Getenv("ESTKME_CLOUD_VERBOSE") != "" { c.Verbose = true } diff --git a/main.go b/main.go index e0009d1..5bd0ee0 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ func init() { flag.StringVar(&config.C.DataDir, "data-dir", filepath.Join(cwd, "data"), "data directory") flag.StringVar(&config.C.LpacVersion, "lpac-version", "v2.0.1", "lpac version") flag.BoolVar(&config.C.DontDownload, "dont-download", false, "don't download lpac") + flag.StringVar(&config.C.Advertising, "advertising", "", "advertising message to show on the server (max: 100 characters)") flag.BoolVar(&config.C.Verbose, "verbose", false, "verbose mode") flag.Parse() } diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 7df2544..244ca43 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -60,10 +60,6 @@ function build_from_source { echo "Downloading LPAC version $LPAC_VERSION" mkdir -p $DST_DIR/data -if [ -f $DST_DIR/data/lpac ]; then - rm -f $DST_DIR/data/lpac -fi - if [ "$(uname -m)" == "x86_64" ]; then download_binary else @@ -88,6 +84,10 @@ ESTKME_CLOUD_BINARY_URL="https://github.com/damonto/estkme-cloud/releases/downlo SYSTEMED_UNIT="estkme-cloud.service" SYSTEMED_UNIT_PATH="/etc/systemd/system/$SYSTEMED_UNIT" +START_CMD="/opt/estkme-cloud/estkme-cloud --data-dir=/opt/estkme-cloud/data --dont-download" +if [ -n "$1" ]; then + START_CMD="$START_CMD --advertising='$1'" +fi SYSTEMED_FILE=" [Unit] Description=eSTK.me Cloud Enhance Server @@ -96,7 +96,7 @@ After=network.target [Service] Type=simple Restart=on-failure -ExecStart=/opt/estkme-cloud/estkme-cloud --data-dir=/opt/estkme-cloud/data --dont-download +ExecStart=$START_CMD RestartSec=10s TimeoutStopSec=30s @@ -104,7 +104,6 @@ TimeoutStopSec=30s WantedBy=multi-user.target " -# Copy the binary to the destination directory if [ "$(systemctl is-active $SYSTEMED_UNIT)" == "active" ]; then systemctl stop $SYSTEMED_UNIT fi