Skip to content

Commit

Permalink
feat: show advertising message
Browse files Browse the repository at this point in the history
  • Loading branch information
damonto committed May 7, 2024
1 parent b6c81c6 commit 379cda9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
6 changes: 6 additions & 0 deletions internal/cloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"os"
"sync"
"time"

"github.com/damonto/estkme-cloud/internal/config"
)

type Server interface {
Expand Down Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ type Config struct {
LpacVersion string
DataDir string
DontDownload bool
Advertising string
Verbose bool
}

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 {
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
11 changes: 5 additions & 6 deletions scripts/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -96,15 +96,14 @@ 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
[Install]
WantedBy=multi-user.target
"

# Copy the binary to the destination directory
if [ "$(systemctl is-active $SYSTEMED_UNIT)" == "active" ]; then
systemctl stop $SYSTEMED_UNIT
fi
Expand Down

0 comments on commit 379cda9

Please sign in to comment.