feat: 实现联通话费余量只读面板
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package unicom
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// debugEnabled 为调试总开关。关闭后调试函数直接返回。
|
||||
const debugEnabled = false
|
||||
|
||||
func DebugOn() bool { return debugEnabled }
|
||||
|
||||
func Debugf(format string, args ...any) {
|
||||
debugf(format, args...)
|
||||
}
|
||||
|
||||
func debugf(format string, args ...any) {
|
||||
if !debugEnabled {
|
||||
return
|
||||
}
|
||||
log.Printf("调试 "+format, args...)
|
||||
}
|
||||
|
||||
func debugHTTP(req *http.Request, resp *http.Response, buf []byte, reqErr error) {
|
||||
if !debugEnabled || req == nil || req.URL == nil {
|
||||
return
|
||||
}
|
||||
if reqErr != nil {
|
||||
debugf("%s %s 请求失败 %v", req.Method, req.URL.String(), reqErr)
|
||||
return
|
||||
}
|
||||
if resp == nil {
|
||||
return
|
||||
}
|
||||
debugf("%s %s -> %d %s", req.Method, req.URL.String(), resp.StatusCode, resp.Header.Get("Content-Type"))
|
||||
if debugSkipBody(req, resp.Header.Get("Content-Type"), buf) {
|
||||
debugf("%s 响应 %d 字节", req.URL.Path, len(buf))
|
||||
return
|
||||
}
|
||||
const limit = 8000
|
||||
if len(buf) > limit {
|
||||
debugf("%s 响应 %d 字节 %s", req.URL.RequestURI(), len(buf), string(buf[:limit]))
|
||||
return
|
||||
}
|
||||
debugf("%s 响应 %s", req.URL.RequestURI(), string(buf))
|
||||
}
|
||||
|
||||
func debugSkipBody(req *http.Request, ctype string, buf []byte) bool {
|
||||
ct := strings.ToLower(ctype)
|
||||
path := strings.ToLower(req.URL.Path)
|
||||
if strings.Contains(ct, "image") || strings.Contains(ct, "javascript") || strings.Contains(ct, "text/css") {
|
||||
return true
|
||||
}
|
||||
switch {
|
||||
case strings.HasSuffix(path, ".js"), strings.HasSuffix(path, ".css"), strings.HasSuffix(path, ".png"),
|
||||
strings.HasSuffix(path, ".jpg"), strings.HasSuffix(path, ".jpeg"), strings.HasSuffix(path, ".gif"),
|
||||
strings.HasSuffix(path, ".html"):
|
||||
return true
|
||||
}
|
||||
trim := strings.TrimSpace(string(buf))
|
||||
return strings.HasPrefix(trim, "<")
|
||||
}
|
||||
Reference in New Issue
Block a user