Skip to content

Commit cd34aa3

Browse files
authored
refactor: re-write caller and installer (#25)
* refactor(caller): re-write caller * refactor(installer): re-write installer.sh (#24) * fix: caller wrong ip * fix(caller): chmod +x caller
1 parent 45f8a90 commit cd34aa3

7 files changed

Lines changed: 118 additions & 113 deletions

File tree

.github/workflows/build.yaml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ jobs:
3434
uses: 'actions/upload-artifact@v4'
3535
with:
3636
name: sshexec
37-
path: |
38-
out/sshexec-amd64
39-
out/sshexec-arm64
40-
out/installer.sh
41-
out/caller.sh
37+
path: out/
4238
if-no-files-found: error
4339
overwrite: true
4440

@@ -48,10 +44,8 @@ jobs:
4844
with:
4945
generate_release_notes: true
5046
files: |
51-
out/sshexec-amd64
52-
out/sshexec-arm64
53-
out/installer.sh
54-
out/caller.sh
47+
out/sshexec*
48+
out/caller*
5549
append_body: true
5650
draft: false
5751
prerelease: false

Makefile

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ BINARY_NAME=sshexec
77
# All target
88
all: build
99

10-
build: build-arm64 build-amd64 script
10+
build: build-arm64 build-amd64
1111

1212
build-arm64:
1313
GOARCH=arm64 $(GOBUILD) -o out/$(BINARY_NAME)-arm64 -v sshd/cmd/
14+
GOOS=linux GOARCH=arm64 $(GOBUILD) -o out/caller-arm64 -v scripts/caller.go
15+
1416

1517
build-amd64:
1618
GOARCH=amd64 $(GOBUILD) -o out/$(BINARY_NAME)-amd64 -v sshd/cmd/
19+
GOOS=linux GOARCH=amd64 $(GOBUILD) -o out/caller-amd64 -v scripts/caller.go
1720

1821
lint:
1922
golangci-lint run
@@ -23,13 +26,6 @@ clean:
2326
$(GOCLEAN)
2427
rm -f $(BINARY_NAME)
2528

26-
# Format the code
27-
script:
28-
cp scripts/caller.sh out/
29-
chmod +x out/caller.sh
30-
cp scripts/installer.sh out/
31-
chmod +x out/installer.sh
32-
3329
fmt:
3430
$(GOCMD) fmt ./...
3531

pkg/handler/ffmpeg/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func Run(next ssh.Handler) ssh.Handler {
1616
return func(s ssh.Session) {
1717
targetBin := s.Command()[0]
1818
if targetBin == define.FFPROBEBin || targetBin == define.FFMPEGBin {
19-
logrus.Infof("run middleware: %q\r\n", RunFFMPEGStage)
19+
logrus.Infof("run middleware: %q\n", RunFFMPEGStage)
2020
stubber := ffmpeg.NewVersion6(s)
2121
args, err := exec.DoArgsSanitizers(s.Command()[1:])
2222
if err != nil {

pkg/logger/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func SetupLogger() error {
3333
logrus.SetFormatter(&logrus.TextFormatter{
3434
FullTimestamp: true,
3535
ForceColors: false,
36-
DisableColors: true,
36+
DisableColors: false,
3737
TimestampFormat: "2006-01-02 15:04:05.000",
3838
})
3939
homeDir, err := os.UserHomeDir()

scripts/caller.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/sirupsen/logrus"
6+
"os"
7+
"os/exec"
8+
"path/filepath"
9+
"strings"
10+
)
11+
12+
func init() {
13+
logrus.SetFormatter(&logrus.TextFormatter{
14+
FullTimestamp: true,
15+
ForceColors: false,
16+
DisableColors: true,
17+
TimestampFormat: "2006-01-02 15:04:05.000",
18+
})
19+
logrus.SetOutput(os.Stderr)
20+
}
21+
22+
const (
23+
addr = "192.168.127.254"
24+
user = "oomol"
25+
port = "5322"
26+
endpoint = user + "@" + addr
27+
)
28+
29+
func main() {
30+
targetName := filepath.Base(os.Args[0])
31+
32+
fullFFMPEGArgsList := append([]string{targetName}, os.Args[1:]...)
33+
34+
var argsBuilder strings.Builder
35+
// Wrap the arguments with single quotes
36+
for _, arg := range fullFFMPEGArgsList {
37+
str := fmt.Sprintf("%s%s%s", "'", arg, "'")
38+
argsBuilder.WriteString(str)
39+
argsBuilder.WriteString(" ")
40+
}
41+
fullFFMPEGArgString := argsBuilder.String()
42+
_, _ = fmt.Fprintf(os.Stderr, "ffmpeg cmdline: %q\n", fullFFMPEGArgString)
43+
44+
var finalArgs strings.Builder
45+
finalArgs.WriteString(fullFFMPEGArgString)
46+
fullArgs := finalArgs.String()
47+
48+
cmd := exec.Command("ssh", "-q", "-o", "StrictHostKeyChecking=no",
49+
"-p", port, endpoint, fullArgs)
50+
cmd.Stdout = os.Stdout
51+
cmd.Stderr = os.Stderr
52+
cmd.Stdin = os.Stdin
53+
_, _ = fmt.Fprintf(os.Stderr, "full cmdline: %q\n", cmd.Args)
54+
if err := cmd.Run(); err != nil {
55+
_, _ = fmt.Fprintf(os.Stderr, "%v", err)
56+
}
57+
}

scripts/caller.sh

Lines changed: 0 additions & 37 deletions
This file was deleted.

scripts/installer.sh

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,83 @@ set -e
33
set -u
44

55
log() {
6-
msg="LOG: $S_NAME> $*"
7-
echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2
6+
msg="LOG: $S_NAME> $*"
7+
echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2
88
}
99

1010
err() {
11-
msg="ERROR: $S_NAME> $*"
12-
echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2
13-
exit 100
11+
msg="ERROR: $S_NAME> $*"
12+
echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2
13+
exit 100
1414
}
1515

1616
warn() {
17-
msg="WARN: $S_NAME> $*"
18-
echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2
17+
msg="WARN: $S_NAME> $*"
18+
echo "$(date +'%Y-%m-%dT%H:%M:%S.%3N%z'):" "$msg" >&2
1919
}
2020

2121
get_platform() {
22-
arch=$(uname -m)
23-
platform=unknown
22+
arch=$(uname -m)
23+
platform=unknown
2424

25-
if [[ -z "$arch" ]]; then
26-
warn "uname -m return empty"
27-
return
28-
fi
25+
if [[ -z "$arch" ]]; then
26+
warn "uname -m return empty"
27+
return
28+
fi
2929

30-
# For wsl2
31-
if [[ "$arch" == x86_64 ]] && [[ -d "/usr/lib/wsl" ]]; then
32-
platform="wsl2-$arch"
33-
return
34-
fi
30+
# For wsl2
31+
if [[ "$arch" == x86_64 ]] && [[ -d "/usr/lib/wsl" ]]; then
32+
platform="wsl2-$arch"
33+
return
34+
fi
3535

36-
# For MacOS-x86_64
37-
if [[ "$arch" == x86_64 ]]; then
38-
platform="macos-$arch"
39-
return
40-
fi
36+
# For MacOS-x86_64
37+
if [[ "$arch" == x86_64 ]]; then
38+
platform="macos-$arch"
39+
return
40+
fi
4141

42-
# For MacOS-aarch64
43-
if [[ "$arch" == aarch64 ]] || [[ $arch == arm64 ]]; then
44-
platform="macos-$arch"
45-
return
46-
fi
42+
# For MacOS-aarch64
43+
if [[ "$arch" == aarch64 ]] || [[ $arch == arm64 ]]; then
44+
platform="macos-$arch"
45+
return
46+
fi
4747
}
4848

49-
# Setup ffmpeg binaries logic
5049
setup_ffmpeg_for_macos_aarch64() {
51-
caller_script="/usr/bin/caller.sh"
52-
wget https://github.com/oomol/sshexec/raw/refs/heads/main/scripts/caller.sh --output-document "$caller_script"
53-
chmod +x "$caller_script"
54-
55-
ln -sf "$caller_script" /usr/bin/install_ffmpeg # used to install_ffmpeg
56-
/usr/bin/install_ffmpeg # do install ffmpeg
57-
58-
# Link caller.sh to <binary_name>
59-
ln -sf "$caller_script" /usr/bin/ffmpeg
60-
ln -sf "$caller_script" /usr/bin/ffprobe
50+
wget https://github.com/oomol/sshexec/releases/download/v1.0.6/caller-arm64 --output-document=/usr/bin/caller
51+
chmod +x /usr/bin/caller
52+
ln -sf /usr/bin/caller /usr/bin/ffmpeg
53+
ln -sf /usr/bin/caller /usr/bin/ffprobe
54+
ln -sf /usr/bin/caller /usr/bin/install_ffmpeg_6
55+
/usr/bin/install_ffmpeg_6
6156
}
6257

6358
setup_ffmpeg_for_wsl2_x86_64() {
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
65-
tar -xvf /tmp/ffmpeg-wsl2_x86_64.tar.xz -C /tmp/
66-
echo "Install ffmpeg"
67-
cp /tmp/ffmpeg/ffmpeg /usr/bin/
68-
cp /tmp/ffmpeg/ffprobe /usr/bin/
69-
echo "Install ffmpeg done"
59+
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
60+
tar -xvf /tmp/ffmpeg-wsl2_x86_64.tar.xz -C /tmp/
61+
echo "Install ffmpeg"
62+
cp /tmp/ffmpeg/ffmpeg /usr/bin/
63+
cp /tmp/ffmpeg/ffprobe /usr/bin/
64+
echo "Install ffmpeg done"
7065
}
7166

7267
setup_ffmpeg() {
73-
if [[ "$platform" == macos-aarch64 ]]; then
74-
setup_ffmpeg_for_macos_aarch64
75-
elif [[ "$platform" == wsl2-x86_64 ]]; then
76-
setup_ffmpeg_for_wsl2_x86_64
77-
else
78-
err "unsupport platform: $platform"
79-
fi
68+
if [[ "$platform" == macos-aarch64 ]]; then
69+
setup_ffmpeg_for_macos_aarch64
70+
elif [[ "$platform" == wsl2-x86_64 ]]; then
71+
setup_ffmpeg_for_wsl2_x86_64
72+
else
73+
err "unsupport platform: $platform"
74+
fi
8075
}
8176

8277
main() {
83-
get_platform
84-
if [[ "$platform" == "unknown" ]]; then
85-
err "unknown platform"
86-
fi
87-
setup_ffmpeg
78+
get_platform
79+
if [[ "$platform" == "unknown" ]]; then
80+
err "unknown platform"
81+
fi
82+
setup_ffmpeg
8883
}
8984

9085
main

0 commit comments

Comments
 (0)