Skip to content

Commit e2f1322

Browse files
committed
- Add -debug output more detailed information.
- Add `-smfix` now you can directly perform the fixes without to setup SMFix in the Post-processing Scipts(If it has already been set up, it doesn't matter as it won't process it again). By default, sm2up will search for a file that name starting with "smfix" in the same directory. However, you can also specify a separate path for it. 中文说明: - 添加参数 `-debug` 输出更多运行的细节 - 添加参数 `-smfix` 现在允许在上传文件的同时进行 SMFix,而不需要预先设置后处理脚本(Post-processing scripts),如果已经设置也没有关系,它并不会重复处理。默认的,sm2up将在同目录中查找smfix开头的文件,你也可以单独指定这个路径。
1 parent 30e6746 commit e2f1322

File tree

9 files changed

+276
-99
lines changed

9 files changed

+276
-99
lines changed

Makefile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ FLAGS = -ldflags="-w -s $(VERSION)"
99
CMD = go build -trimpath $(FLAGS)
1010
SRC = $(shell ls *.go | grep -v _test.go)
1111

12-
.PHONY: all clean dep darwin-arm64 darwin-amd64 linux-amd64 linux-arm7 win64
12+
.PHONY: all clean dep darwin-arm64 darwin-amd64 linux-amd64 linux-arm7 linux-arm6 win64 win32
1313

1414
darwin-arm64: $(SRC)
1515
GOOS=darwin GOARCH=arm64 $(CMD) -o $(DIST)$(NAME)-$@ $^
@@ -23,19 +23,25 @@ linux-amd64: $(SRC)
2323
linux-arm7: $(SRC)
2424
GOOS=linux GOARCH=arm GOARM=7 $(CMD) -o $(DIST)$(NAME)-$@ $^
2525

26+
linux-arm6: $(SRC)
27+
GOOS=linux GOARCH=arm GOARM=6 $(CMD) -o $(DIST)$(NAME)-$@ $^
28+
2629
win64: $(SRC)
2730
GOOS=windows GOARCH=amd64 $(CMD) -o $(DIST)$(NAME)-$@.exe $^
2831

32+
win32: $(SRC)
33+
GOOS=windows GOARCH=386 $(CMD) -o $(DIST)$(NAME)-$@.exe $^
34+
2935
dep: # Get the dependencies
3036
go mod download
3137

32-
all: dep darwin-arm64 darwin-amd64 linux-amd64 linux-arm7 win64
38+
all: dep darwin-arm64 darwin-amd64 linux-amd64 linux-arm7 linux-arm6 win64 win32
3339
@true
3440

3541
all-zip: all
36-
for p in darwin-arm64 darwin-amd64 linux-amd64 linux-arm7 win64.exe; do \
42+
for p in darwin-arm64 darwin-amd64 linux-amd64 linux-arm7 linux-arm6 win64.exe win32.exe; do \
3743
zip -j $(DIST)$(NAME)-$$p.zip $(DIST)$(NAME)-$$p README.md README.zh-cn.md LICENSE; \
38-
done
44+
done
3945

4046
clean:
4147
rm -f $(DIST)$(NAME)-*

connector_http.go

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,29 @@ func (hc *HTTPConnector) Connect() error {
4444
hc.client.DisableAllowGetMethodPayload()
4545
// hc.client.EnableDumpAllWithoutRequestBody()
4646

47-
type resp_data struct {
47+
result := struct {
4848
Token string `json:"token"`
49-
}
50-
result := &resp_data{}
51-
resp, err := hc.request().SetSuccessResult(result).Post(hc.URL("/connect"))
49+
}{}
50+
51+
req := hc.request().
52+
SetSuccessResult(&result).
53+
SetRetryCount(3).
54+
SetRetryFixedInterval(1 * time.Second).
55+
SetRetryCondition(func(r *req.Response, err error) bool {
56+
if Debug {
57+
log.Printf("-- retrying %s -> %d, token %s", r.Request.URL, r.StatusCode, hc.printer.Token)
58+
}
59+
60+
// token expired
61+
if r.StatusCode == 403 && hc.printer.Token != "" {
62+
hc.printer.Token = ""
63+
// reconnect with no token to get new one
64+
return true
65+
}
66+
return false
67+
})
68+
69+
resp, err := req.Post(hc.URL("/connect"))
5270
if err != nil {
5371
return err
5472
}
@@ -72,11 +90,13 @@ func (hc *HTTPConnector) Connect() error {
7290
return fmt.Errorf("access denied")
7391
}
7492
}
75-
} else if resp.StatusCode == 403 && hc.printer.Token != "" {
76-
// token expired
77-
hc.printer.Token = ""
78-
// reconnect with no token to get new one
79-
return hc.Connect()
93+
/*
94+
} else if resp.StatusCode == 403 && hc.printer.Token != "" {
95+
// token expired
96+
hc.printer.Token = ""
97+
// reconnect with no token to get new one
98+
return hc.Connect()
99+
*/
80100
}
81101

82102
return fmt.Errorf("connect error %d", resp.StatusCode)
@@ -90,45 +110,48 @@ func (hc *HTTPConnector) Disconnect() (err error) {
90110
}
91111

92112
func (hc *HTTPConnector) Upload(fname string, content []byte) (err error) {
93-
finished := make(chan bool, 1)
113+
finished := make(chan empty, 1)
94114
defer func() {
95-
finished <- true
115+
finished <- empty{}
96116
}()
97117
go func() {
98-
ticker := time.NewTicker(3 * time.Second)
118+
ticker := time.NewTicker(1 * time.Second)
99119
for {
100120
select {
101121
case <-ticker.C:
102122
hc.checkStatus()
103123
case <-finished:
124+
if Debug {
125+
log.Printf("-- heartbeat stopped")
126+
}
104127
ticker.Stop()
105128
return
106129
}
107130
}
108131
}()
109132

133+
w := uilive.New()
134+
w.Start()
135+
log.SetOutput(w)
136+
defer func() {
137+
w.Stop()
138+
log.SetOutput(os.Stderr)
139+
}()
140+
110141
file := req.FileUpload{
111142
ParamName: "file",
112143
FileName: fname,
113144
GetFileContent: func() (io.ReadCloser, error) {
114-
return io.NopCloser(bytes.NewReader(content)), nil
145+
return io.NopCloser(bytes.NewBuffer(content)), nil
115146
},
116-
FileSize: int64(len(content)),
147+
FileSize: int64(len(content)),
148+
ContentType: "application/octet-stream",
117149
}
118-
119-
w := uilive.New()
120-
w.Start()
121-
defer w.Stop()
122-
req := hc.request(0).SetFileUpload(file).SetUploadCallback(func(info req.UploadInfo) {
123-
log.SetOutput(w)
124-
defer log.SetOutput(os.Stderr)
125-
126-
perc := float64(info.UploadedSize) / float64(info.FileSize) * 100.0
150+
req := hc.request(0).SetFileUpload(file).SetUploadCallbackWithInterval(func(info req.UploadInfo) {
151+
perc := float64(info.UploadedSize/info.FileSize) * 100.0
127152
log.Printf(" - HTTP sending %.1f%%", perc)
128-
})
153+
}, 35*time.Millisecond)
129154

130-
// disable chucked to make Content-Length
131-
// req.DisableForceChunkedEncoding()
132155
_, err = req.Post(hc.URL("/upload"))
133156
return
134157
}
@@ -150,6 +173,9 @@ func (hc *HTTPConnector) request(timeout ...int) *req.Request {
150173

151174
func (hc *HTTPConnector) checkStatus() (status int) {
152175
r, err := hc.request().Get(hc.URL("/status"))
176+
if Debug {
177+
log.Printf("-- heartbeat: %d, err(%s)", r.StatusCode, err)
178+
}
153179
if err == nil {
154180
switch r.StatusCode {
155181
case 200:

discover.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"log"
45
"net"
56
"time"
67
)
@@ -50,6 +51,10 @@ func Discover(timeout time.Duration) ([]*Printer, error) {
5051
return nil, err
5152
}
5253

54+
if Debug {
55+
log.Printf("-- Discover got %d bytes %s", n, buf[:n])
56+
}
57+
5358
// Parse the response into a Printer object
5459
printer, err := NewPrinter(buf[:n])
5560
if err != nil {

go.mod

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,34 @@ go 1.20
44

55
require (
66
github.com/gosuri/uilive v0.0.4
7-
github.com/imroc/req/v3 v3.33.2
7+
github.com/imroc/req/v3 v3.37.1
88
github.com/manifoldco/promptui v0.9.0
99
gopkg.in/yaml.v3 v3.0.1
1010
)
1111

1212
require (
13-
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
14-
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
13+
github.com/andybalholm/brotli v1.0.4 // indirect
14+
github.com/chzyer/readline v1.5.1 // indirect
15+
github.com/gaukas/godicttls v0.0.3 // indirect
16+
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
1517
github.com/golang/mock v1.6.0 // indirect
16-
github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect
18+
github.com/google/pprof v0.0.0-20230602150820-91b7bce49751 // indirect
1719
github.com/hashicorp/errwrap v1.1.0 // indirect
1820
github.com/hashicorp/go-multierror v1.1.1 // indirect
21+
github.com/klauspost/compress v1.15.15 // indirect
1922
github.com/kr/pretty v0.3.1 // indirect
20-
github.com/mattn/go-isatty v0.0.18 // indirect
21-
github.com/onsi/ginkgo/v2 v2.2.0 // indirect
23+
github.com/mattn/go-isatty v0.0.19 // indirect
24+
github.com/onsi/ginkgo/v2 v2.10.0 // indirect
2225
github.com/quic-go/qpack v0.4.0 // indirect
23-
github.com/quic-go/qtls-go1-18 v0.2.0 // indirect
24-
github.com/quic-go/qtls-go1-19 v0.2.0 // indirect
25-
github.com/quic-go/qtls-go1-20 v0.1.0 // indirect
26-
github.com/quic-go/quic-go v0.32.0 // indirect
27-
golang.org/x/crypto v0.4.0 // indirect
28-
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
29-
golang.org/x/mod v0.6.0 // indirect
30-
golang.org/x/net v0.5.0 // indirect
31-
golang.org/x/sys v0.6.0 // indirect
32-
golang.org/x/text v0.6.0 // indirect
33-
golang.org/x/tools v0.2.0 // indirect
26+
github.com/quic-go/qtls-go1-19 v0.3.2 // indirect
27+
github.com/quic-go/qtls-go1-20 v0.2.2 // indirect
28+
github.com/quic-go/quic-go v0.35.1 // indirect
29+
github.com/refraction-networking/utls v1.3.2 // indirect
30+
golang.org/x/crypto v0.10.0 // indirect
31+
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
32+
golang.org/x/mod v0.11.0 // indirect
33+
golang.org/x/net v0.11.0 // indirect
34+
golang.org/x/sys v0.9.0 // indirect
35+
golang.org/x/text v0.10.0 // indirect
36+
golang.org/x/tools v0.10.0 // indirect
3437
)

0 commit comments

Comments
 (0)