Skip to content

Commit 3779d4b

Browse files
committed
增加http异常处理,设置CGO_ENABLE为0,兼容低版本Linux
1 parent e562741 commit 3779d4b

File tree

5 files changed

+68
-13
lines changed

5 files changed

+68
-13
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
env:
5757
GOOS: ${{ matrix.goos }}
5858
GOARCH: ${{ matrix.arch }}
59-
CGO_ENABLED: 1
59+
CGO_ENABLED: 0
6060
- uses: svenstaro/upx-action@v2
6161
with:
6262
args: ${{ matrix.args }}

core/crackmodule/httpbasic.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/tls"
55
"cube/config"
66
"cube/gologger"
7+
"log"
78
"net/http"
89
"strings"
910
)
@@ -51,13 +52,32 @@ func (h HttpBasic) Exec() CrackResult {
5152
req.Header.Add("Connection", "close")
5253
req.SetBasicAuth(h.Auth.User, h.Auth.Password)
5354
res, err := clt.Do(req)
55+
5456
if err != nil {
55-
gologger.Error(err)
57+
// 处理请求错误
58+
log.Printf("Error making request: %v", err)
59+
// 应该在这里返回或处理错误
60+
return result
5661
}
57-
defer res.Body.Close()
58-
if res.StatusCode != 401 {
59-
result.Result = true
62+
if res != nil {
63+
defer func() {
64+
// 使用 defer 调用匿名函数来处理 Close 的错误
65+
if err := res.Body.Close(); err != nil {
66+
// 处理关闭 resp.Body 时的错误
67+
log.Printf("Error closing response body: %v", err)
68+
}
69+
}()
70+
if res.StatusCode != 401 {
71+
result.Result = true
72+
}
73+
} else {
74+
// 如果到这里,说明有严重的错误发生,resp2 应该不为 nil。
75+
log.Printf("Response is nil without a preceding error.")
6076
}
77+
78+
//if res.StatusCode != 401 {
79+
// result.Result = true
80+
//}
6181
return result
6282
}
6383

core/crackmodule/jenkins.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"cube/config"
77
"cube/gologger"
8+
"log"
89
"net/http"
910
"net/http/cookiejar"
1011
"net/url"
@@ -57,7 +58,10 @@ func (j Jenkins) Exec() CrackResult {
5758
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
5859
resp, err := clt.Do(req)
5960
if err != nil {
60-
panic(err)
61+
// 处理请求错误
62+
log.Printf("Error making request: %v", err)
63+
// 应该在这里返回或处理错误
64+
return result
6165
}
6266

6367
data := make([]byte, 20250)
@@ -100,8 +104,12 @@ func (j Jenkins) Exec() CrackResult {
100104

101105
r2, err := clt2.Do(req2)
102106
if err != nil {
103-
panic(err)
107+
// 处理请求错误
108+
log.Printf("Error making request: %v", err)
109+
// 应该在这里返回或处理错误
110+
return result
104111
}
112+
105113
defer r2.Body.Close()
106114
data2 := make([]byte, 10480)
107115
c2 := bufio.NewReader(r2.Body)

core/crackmodule/phpmyadmin.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/tls"
66
"cube/config"
77
"cube/gologger"
8+
"log"
89
"net/http"
910
"net/http/cookiejar"
1011
"net/url"
@@ -57,6 +58,7 @@ func (p Phpmyadmin) Exec() CrackResult {
5758
req.Header.Add("Accept-Charset", "utf-8")
5859
resp, err := clt.Do(req)
5960
if err != nil {
61+
log.Printf("Error making request: %v", err)
6062
return result
6163
}
6264

@@ -97,12 +99,29 @@ func (p Phpmyadmin) Exec() CrackResult {
9799
req2.Header.Add("Connection", "close")
98100
req2.Header.Set("Content-Type", "application/x-www-form-urlencoded")
99101

100-
resp2, _ := crackClt.Do(req2)
101-
//resp2, _ := crackClt.PostForm(task.Ip, urlValues)
102-
//resp2, _ := crackClt.Post(task.Ip, urlValues)
103-
defer resp2.Body.Close()
104-
if resp2.StatusCode == 302 {
105-
result.Result = true
102+
resp2, err := crackClt.Do(req2)
103+
if err != nil {
104+
// 处理请求错误
105+
log.Printf("Error making request: %v", err)
106+
// 应该在这里返回或处理错误
107+
return result
108+
}
109+
110+
if resp2 != nil {
111+
defer func() {
112+
// 使用 defer 调用匿名函数来处理 Close 的错误
113+
if err := resp2.Body.Close(); err != nil {
114+
// 处理关闭 resp.Body 时的错误
115+
log.Printf("Error closing response body: %v", err)
116+
}
117+
}()
118+
119+
if resp2.StatusCode == 302 {
120+
result.Result = true
121+
}
122+
} else {
123+
// 如果到这里,说明有严重的错误发生,resp2 应该不为 nil。
124+
log.Printf("Response is nil without a preceding error.")
106125
}
107126

108127
return result

core/crackmodule/zabbix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"cube/config"
66
"cube/gologger"
7+
"log"
78
"net/http"
89
"net/http/cookiejar"
910
"net/url"
@@ -92,6 +93,13 @@ func (z Zabbix) Exec() CrackResult {
9293
req2.Header.Set("Content-Type", "application/x-www-form-urlencoded")
9394

9495
resp2, _ := crackClt.Do(req2)
96+
if err != nil {
97+
// 处理请求错误
98+
log.Printf("Error making request: %v", err)
99+
// 应该在这里返回或处理错误
100+
return result
101+
}
102+
95103
defer resp2.Body.Close()
96104
if resp2.StatusCode == 302 {
97105
result.Result = true

0 commit comments

Comments
 (0)