From 19cbd755299bb754a7abb4ff9794df29195dadc9 Mon Sep 17 00:00:00 2001 From: ihexon <14349453+ihexon@users.noreply.github.com> Date: Tue, 13 May 2025 16:47:54 +0800 Subject: [PATCH 1/4] refactor(caller): re-write caller --- .github/workflows/build.yaml | 12 ++------ Makefile | 12 +++----- pkg/handler/ffmpeg/handler.go | 2 +- pkg/logger/log.go | 2 +- scripts/caller.go | 57 +++++++++++++++++++++++++++++++++++ scripts/caller.sh | 37 ----------------------- scripts/installer.sh | 11 +------ 7 files changed, 67 insertions(+), 66 deletions(-) create mode 100644 scripts/caller.go delete mode 100755 scripts/caller.sh diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 4d5e1d2..0758e92 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -34,11 +34,7 @@ jobs: uses: 'actions/upload-artifact@v4' with: name: sshexec - path: | - out/sshexec-amd64 - out/sshexec-arm64 - out/installer.sh - out/caller.sh + path: out/ if-no-files-found: error overwrite: true @@ -48,10 +44,8 @@ jobs: with: generate_release_notes: true files: | - out/sshexec-amd64 - out/sshexec-arm64 - out/installer.sh - out/caller.sh + out/sshexec* + out/caller* append_body: true draft: false prerelease: false diff --git a/Makefile b/Makefile index 1abcc91..cb24524 100644 --- a/Makefile +++ b/Makefile @@ -7,13 +7,16 @@ BINARY_NAME=sshexec # All target all: build -build: build-arm64 build-amd64 script +build: build-arm64 build-amd64 build-arm64: GOARCH=arm64 $(GOBUILD) -o out/$(BINARY_NAME)-arm64 -v sshd/cmd/ + GOOS=linux GOARCH=arm64 $(GOBUILD) -o out/caller-arm64 -v scripts/caller.go + build-amd64: GOARCH=amd64 $(GOBUILD) -o out/$(BINARY_NAME)-amd64 -v sshd/cmd/ + GOOS=linux GOARCH=amd64 $(GOBUILD) -o out/caller-amd64 -v scripts/caller.go lint: golangci-lint run @@ -23,13 +26,6 @@ clean: $(GOCLEAN) rm -f $(BINARY_NAME) -# Format the code -script: - cp scripts/caller.sh out/ - chmod +x out/caller.sh - cp scripts/installer.sh out/ - chmod +x out/installer.sh - fmt: $(GOCMD) fmt ./... diff --git a/pkg/handler/ffmpeg/handler.go b/pkg/handler/ffmpeg/handler.go index 20642b1..3551960 100644 --- a/pkg/handler/ffmpeg/handler.go +++ b/pkg/handler/ffmpeg/handler.go @@ -16,7 +16,7 @@ func Run(next ssh.Handler) ssh.Handler { return func(s ssh.Session) { targetBin := s.Command()[0] if targetBin == define.FFPROBEBin || targetBin == define.FFMPEGBin { - logrus.Infof("run middleware: %q\r\n", RunFFMPEGStage) + logrus.Infof("run middleware: %q\n", RunFFMPEGStage) stubber := ffmpeg.NewVersion6(s) args, err := exec.DoArgsSanitizers(s.Command()[1:]) if err != nil { diff --git a/pkg/logger/log.go b/pkg/logger/log.go index a0cc3b4..bf95170 100644 --- a/pkg/logger/log.go +++ b/pkg/logger/log.go @@ -33,7 +33,7 @@ func SetupLogger() error { logrus.SetFormatter(&logrus.TextFormatter{ FullTimestamp: true, ForceColors: false, - DisableColors: true, + DisableColors: false, TimestampFormat: "2006-01-02 15:04:05.000", }) homeDir, err := os.UserHomeDir() diff --git a/scripts/caller.go b/scripts/caller.go new file mode 100644 index 0000000..425e3af --- /dev/null +++ b/scripts/caller.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "github.com/sirupsen/logrus" + "os" + "os/exec" + "path/filepath" + "strings" +) + +func init() { + logrus.SetFormatter(&logrus.TextFormatter{ + FullTimestamp: true, + ForceColors: false, + DisableColors: true, + TimestampFormat: "2006-01-02 15:04:05.000", + }) + logrus.SetOutput(os.Stderr) +} + +const ( + addr = "127.0.0.1" + user = "oomol" + port = "5322" + endpoint = user + "@" + addr +) + +func main() { + targetName := filepath.Base(os.Args[0]) + + fullFFMPEGArgsList := append([]string{targetName}, os.Args[1:]...) + + var argsBuilder strings.Builder + // Wrap the arguments with single quotes + for _, arg := range fullFFMPEGArgsList { + str := fmt.Sprintf("%s%s%s", "'", arg, "'") + argsBuilder.WriteString(str) + argsBuilder.WriteString(" ") + } + fullFFMPEGArgString := argsBuilder.String() + _, _ = fmt.Fprintf(os.Stderr, "ffmpeg cmdline: %q\n", fullFFMPEGArgString) + + var finalArgs strings.Builder + finalArgs.WriteString(fullFFMPEGArgString) + fullArgs := finalArgs.String() + + cmd := exec.Command("ssh", "-q", "-o", "StrictHostKeyChecking=no", + "-p", port, endpoint, fullArgs) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + _, _ = fmt.Fprintf(os.Stderr, "full cmdline: %q\n", cmd.Args) + if err := cmd.Run(); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "%v", err) + } +} diff --git a/scripts/caller.sh b/scripts/caller.sh deleted file mode 100755 index 04fbd4f..0000000 --- a/scripts/caller.sh +++ /dev/null @@ -1,37 +0,0 @@ -#! /usr/bin/env bash -set -e -set -u -cmd_file="/tmp/.cmd_$(uuidgen)" -# Default is oomol -USER=oomol -# https://github.com/containers/gvisor-tap-vsock/blob/f0f18025e5b7c7c281a11dfd81034641b40efe18/cmd/gvproxy/main.go#L56 -IP_ADDR=192.168.127.254 -# https://github.com/oomol/sshexec/blob/f6e0e1583fc874727d68cc5cc3213dff6867dd0e/pkg/define/const.go#L21 -PORT=5322 - -arg0="$(basename "$0")" - -expand-q() { - for i; do echo -n " ${i@Q} "; done -} - -output_args() { - echo -n \" - expand-q "$@" - echo -n \" - echo -} - -write_cmd() { - echo "#! /usr/bin/env bash" - echo -n 'exec' - echo -n ' ' - echo -n ssh -q -o StrictHostKeyChecking=no $USER@$IP_ADDR -p $PORT - echo -n ' ' - output_args "$arg0" "$@" -} -write_cmd "$@" >"$cmd_file" - -chmod +x "$cmd_file" -sync -exec "$cmd_file" \ No newline at end of file diff --git a/scripts/installer.sh b/scripts/installer.sh index cd4d78b..56f7de3 100644 --- a/scripts/installer.sh +++ b/scripts/installer.sh @@ -48,16 +48,7 @@ get_platform() { # Setup ffmpeg binaries logic setup_ffmpeg_for_macos_aarch64() { - caller_script="/usr/bin/caller.sh" - wget https://github.com/oomol/sshexec/raw/refs/heads/main/scripts/caller.sh --output-document "$caller_script" - chmod +x "$caller_script" - - ln -sf "$caller_script" /usr/bin/install_ffmpeg # used to install_ffmpeg - /usr/bin/install_ffmpeg # do install ffmpeg - - # Link caller.sh to - ln -sf "$caller_script" /usr/bin/ffmpeg - ln -sf "$caller_script" /usr/bin/ffprobe + echo "skip" } setup_ffmpeg_for_wsl2_x86_64() { From 9a4ca107ec75a4f7464450f70076f64ba78e37c5 Mon Sep 17 00:00:00 2001 From: CAT <14349453+ihexon@users.noreply.github.com> Date: Tue, 13 May 2025 17:08:54 +0800 Subject: [PATCH 2/4] refactor(installer): re-write installer.sh (#24) --- scripts/installer.sh | 96 ++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 47 deletions(-) diff --git a/scripts/installer.sh b/scripts/installer.sh index 56f7de3..79f232d 100644 --- a/scripts/installer.sh +++ b/scripts/installer.sh @@ -3,79 +3,81 @@ set -e set -u log() { - msg="LOG: $S_NAME> $*" - echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2 + msg="LOG: $S_NAME> $*" + echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2 } err() { - msg="ERROR: $S_NAME> $*" - echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2 - exit 100 + msg="ERROR: $S_NAME> $*" + echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2 + exit 100 } warn() { - msg="WARN: $S_NAME> $*" - echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2 + msg="WARN: $S_NAME> $*" + echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2 } get_platform() { - arch=$(uname -m) - platform=unknown + arch=$(uname -m) + platform=unknown - if [[ -z "$arch" ]]; then - warn "uname -m return empty" - return - fi + if [[ -z "$arch" ]]; then + warn "uname -m return empty" + return + fi - # For wsl2 - if [[ "$arch" == x86_64 ]] && [[ -d "/usr/lib/wsl" ]]; then - platform="wsl2-$arch" - return - fi + # For wsl2 + if [[ "$arch" == x86_64 ]] && [[ -d "/usr/lib/wsl" ]]; then + platform="wsl2-$arch" + return + fi - # For MacOS-x86_64 - if [[ "$arch" == x86_64 ]]; then - platform="macos-$arch" - return - fi + # For MacOS-x86_64 + if [[ "$arch" == x86_64 ]]; then + platform="macos-$arch" + return + fi - # For MacOS-aarch64 - if [[ "$arch" == aarch64 ]] || [[ $arch == arm64 ]]; then - platform="macos-$arch" - return - fi + # For MacOS-aarch64 + if [[ "$arch" == aarch64 ]] || [[ $arch == arm64 ]]; then + platform="macos-$arch" + return + fi } # Setup ffmpeg binaries logic setup_ffmpeg_for_macos_aarch64() { - echo "skip" + wget https://github.com/oomol/sshexec/releases/download/v1.0.6/caller-arm64 --output-document=/usr/bin/caller + ln -sf /usr/bin/caller /usr/bin/ffmpeg + ln -sf /usr/bin/caller /usr/bin/ffprobe } setup_ffmpeg_for_wsl2_x86_64() { - wget https://github.com/oomol/builded/releases/download/v1.7/ffmpeg-wsl2_x86_64.tar.xz --output-document=/tmp/ffmpeg-wsl2_x86_64.tar.xz - tar -xvf /tmp/ffmpeg-wsl2_x86_64.tar.xz -C /tmp/ - echo "Install ffmpeg" - cp /tmp/ffmpeg/ffmpeg /usr/bin/ - cp /tmp/ffmpeg/ffprobe /usr/bin/ - echo "Install ffmpeg done" + wget https://github.com/oomol/builded/releases/download/v1.7/ffmpeg-wsl2_x86_64.tar.xz --output-document=/tmp/ffmpeg-wsl2_x86_64.tar.xz + tar -xvf /tmp/ffmpeg-wsl2_x86_64.tar.xz -C /tmp/ + echo "Install ffmpeg" + cp /tmp/ffmpeg/ffmpeg /usr/bin/ + cp /tmp/ffmpeg/ffprobe /usr/bin/ + echo "Install ffmpeg done" } setup_ffmpeg() { - if [[ "$platform" == macos-aarch64 ]]; then - setup_ffmpeg_for_macos_aarch64 - elif [[ "$platform" == wsl2-x86_64 ]]; then - setup_ffmpeg_for_wsl2_x86_64 - else - err "unsupport platform: $platform" - fi + if [[ "$platform" == macos-aarch64 ]]; then + setup_ffmpeg_for_macos_aarch64 + elif [[ "$platform" == wsl2-x86_64 ]]; then + setup_ffmpeg_for_wsl2_x86_64 + else + err "unsupport platform: $platform" + fi } main() { - get_platform - if [[ "$platform" == "unknown" ]]; then - err "unknown platform" - fi - setup_ffmpeg + get_platform + if [[ "$platform" == "unknown" ]]; then + err "unknown platform" + fi + setup_ffmpeg } main From 3aa4b6830ab3973cf65838249d98724a0467406b Mon Sep 17 00:00:00 2001 From: ihexon <14349453+ihexon@users.noreply.github.com> Date: Tue, 13 May 2025 17:31:29 +0800 Subject: [PATCH 3/4] fix: caller wrong ip --- scripts/caller.go | 2 +- scripts/installer.sh | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/caller.go b/scripts/caller.go index 425e3af..3ba63c3 100644 --- a/scripts/caller.go +++ b/scripts/caller.go @@ -20,7 +20,7 @@ func init() { } const ( - addr = "127.0.0.1" + addr = "192.168.127.254" user = "oomol" port = "5322" endpoint = user + "@" + addr diff --git a/scripts/installer.sh b/scripts/installer.sh index 79f232d..b9d86fc 100644 --- a/scripts/installer.sh +++ b/scripts/installer.sh @@ -46,11 +46,12 @@ get_platform() { fi } -# Setup ffmpeg binaries logic setup_ffmpeg_for_macos_aarch64() { wget https://github.com/oomol/sshexec/releases/download/v1.0.6/caller-arm64 --output-document=/usr/bin/caller ln -sf /usr/bin/caller /usr/bin/ffmpeg ln -sf /usr/bin/caller /usr/bin/ffprobe + ln -sf /usr/bin/caller /usr/bin/install_ffmpeg_6 + /usr/bin/install_ffmpeg_6 } setup_ffmpeg_for_wsl2_x86_64() { From 2ef6ab5f563e20a0b38701c3f6fe20b1fd44d67c Mon Sep 17 00:00:00 2001 From: ihexon <14349453+ihexon@users.noreply.github.com> Date: Tue, 13 May 2025 17:37:52 +0800 Subject: [PATCH 4/4] fix(caller): chmod +x caller --- scripts/installer.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/installer.sh b/scripts/installer.sh index b9d86fc..533aa40 100644 --- a/scripts/installer.sh +++ b/scripts/installer.sh @@ -48,6 +48,7 @@ get_platform() { setup_ffmpeg_for_macos_aarch64() { wget https://github.com/oomol/sshexec/releases/download/v1.0.6/caller-arm64 --output-document=/usr/bin/caller + chmod +x /usr/bin/caller ln -sf /usr/bin/caller /usr/bin/ffmpeg ln -sf /usr/bin/caller /usr/bin/ffprobe ln -sf /usr/bin/caller /usr/bin/install_ffmpeg_6