Skip to content

Commit

Permalink
Add LogDebug; Bug fix; Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Simple-Tracker committed Jan 16, 2024
1 parent 0b0ebc4 commit 7b28778
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
| BanByPUStartPrecent | 2% | 增强自动屏蔽/起始进度. 若客户端上传达到起始进度, 则允许屏蔽 Peer |
| BanByPUAntiErrorRatio | 5X | 增强自动屏蔽/滞后防误判倍率. 若 Peer 报告下载进度与倍率之乘积得到之下载量 比 客户端上传量 还低, 则允许屏蔽 Peer |
| longConnection | 启用 | 长连接. 启用可降低资源消耗 |
| logToFile | 启用 | 记录日志到文件. 启用后可用于分析及统计用途, 但不会记录调试模式的信息 |
| logToFile | 启用 | 记录普通日志到文件. 启用后可用于一般的分析及统计用途 |
| logDebug | 禁用 | 记录调试日志到文件 (须先启用 logToFile). 启用后可用于进阶的分析及统计用途, 但信息量较大 |
| qBURL | http://127.0.0.1:990 | qBittorrent Web UI 地址. 正确填入是使用客户端屏蔽器的前提条件 |
| qBUsername || qBittorrent Web UI 账号. 若启用 qBittorrent 内 "跳过本机客户端认证" 可默认留空 |
| qBPassword || qBittorrent Web UI 密码. 若启用 qBittorrent 内 "跳过本机客户端认证" 可默认留空 |
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"timeout": 6,
"longConnection": true,
"logToFile": true,
"logDebug": false,
"qBURL": "http://127.0.0.1:990",
"qBUsername": "",
"qBPassword": "",
Expand Down
50 changes: 30 additions & 20 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type ConfigStruct struct {
BanByPUAntiErrorRatio uint32
LongConnection bool
LogToFile bool
LogDebug bool
QBURL string
QBUsername string
QBPassword string
Expand Down Expand Up @@ -88,6 +89,7 @@ var config = ConfigStruct {
BanByPUAntiErrorRatio: 5,
LongConnection: true,
LogToFile: true,
LogDebug: false,
QBURL: "http://127.0.0.1:990",
QBUsername: "",
QBPassword: "",
Expand All @@ -98,11 +100,15 @@ var configLastMod int64 = 0
var logFile *os.File

func Log(module string, str string, logToFile bool, args ...interface {}) {
if !config.Debug && strings.HasPrefix(module, "Debug") {
return
if strings.HasPrefix(module, "Debug") {
if !config.Debug {
return
} else if config.LogDebug {
logToFile = true
}
}
logStr := fmt.Sprintf("[" + GetDateTime(true) + "][" + module + "] " + str + ".\n", args...)
if logToFile && config.LogToFile && logFile != nil {
if config.LogToFile && logToFile && logFile != nil {
if _, err := logFile.Write([]byte(logStr)); err != nil {
Log("Log", "写入日志时发生了错误: %s", false, err.Error())
}
Expand Down Expand Up @@ -157,6 +163,9 @@ func LoadConfig() bool {
if config.LogToFile {
os.Mkdir("logs", os.ModePerm)
LoadLog()
} else if logFile != nil {
logFile.Close()
logFile = nil
}
if config.Interval < 1 {
config.Interval = 1
Expand Down Expand Up @@ -274,27 +283,27 @@ func Login() bool {
func Fetch(url string) []byte {
response, err := httpClient.Get(url)
if err != nil {
Log("Fetch", "请求时发生了错误: %s", false, err.Error())
Log("Fetch", "请求时发生了错误: %s", true, err.Error())
return nil
}
if response.StatusCode == 403 && !Login() {
Log("Fetch", "请求时发生了错误: 认证失败", false)
Log("Fetch", "请求时发生了错误: 认证失败", true)
return nil
}
if response.StatusCode == 404 {
Log("Fetch", "请求时发生了错误: 资源不存在", false)
Log("Fetch", "请求时发生了错误: 资源不存在", true)
return nil
}
response, err = httpClient.Get(url)
if err != nil {
Log("Fetch", "请求时发生了错误: %s", false, err.Error())
Log("Fetch", "请求时发生了错误: %s", true, err.Error())
return nil
}
defer response.Body.Close()

responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
Log("Fetch", "读取时发生了错误: %s", false, err.Error())
Log("Fetch", "读取时发生了错误: %s", true, err.Error())
return nil
}

Expand All @@ -303,23 +312,23 @@ func Fetch(url string) []byte {
func Submit(url string, postdata string) []byte {
response, err := httpClient.Post(url, "application/x-www-form-urlencoded", strings.NewReader(postdata))
if err != nil {
Log("Submit", "请求时发生了错误: %s", false, err.Error())
Log("Submit", "请求时发生了错误: %s", true, err.Error())
return nil
}
if response.StatusCode == 403 && !Login() {
Log("Submit", "请求时发生了错误: 认证失败", false)
Log("Submit", "请求时发生了错误: 认证失败", true)
return nil
}
response, err = httpClient.Post(url, "application/x-www-form-urlencoded", strings.NewReader(postdata))
if err != nil {
Log("Submit", "请求时发生了错误: %s", false, err.Error())
Log("Submit", "请求时发生了错误: %s", true, err.Error())
return nil
}
defer response.Body.Close()

responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
Log("Submit", "读取时发生了错误", false)
Log("Submit", "读取时发生了错误", true)
return nil
}

Expand All @@ -328,13 +337,13 @@ func Submit(url string, postdata string) []byte {
func FetchMaindata() *MainDataStruct {
maindataResponseBody := Fetch(config.QBURL + "/api/v2/sync/maindata?rid=0")
if maindataResponseBody == nil {
Log("FetchMaindata", "发生错误", false)
Log("FetchMaindata", "发生错误", true)
return nil
}

var mainDataResult MainDataStruct
if err := json.Unmarshal(maindataResponseBody, &mainDataResult); err != nil {
Log("FetchMaindata", "解析时发生了错误: %s", false, err.Error())
Log("FetchMaindata", "解析时发生了错误: %s", true, err.Error())
return nil
}

Expand All @@ -345,13 +354,13 @@ func FetchMaindata() *MainDataStruct {
func FetchTorrentPeers(infoHash string) *TorrentPeersStruct {
torrentPeersResponseBody := Fetch(config.QBURL + "/api/v2/sync/torrentPeers?rid=0&hash=" + infoHash)
if torrentPeersResponseBody == nil {
Log("FetchTorrentPeers", "发生错误", false)
Log("FetchTorrentPeers", "发生错误", true)
return nil
}

var torrentPeersResult TorrentPeersStruct
if err := json.Unmarshal(torrentPeersResponseBody, &torrentPeersResult); err != nil {
Log("FetchTorrentPeers", "解析时发生了错误: %s", false, err.Error())
Log("FetchTorrentPeers", "解析时发生了错误: %s", true, err.Error())
return nil
}

Expand All @@ -363,7 +372,7 @@ func SubmitBlockPeers(banIPsStr string) {
banIPsStr = url.QueryEscape("{\"banned_IPs\": \"" + banIPsStr + "\"}")
banResponseBody := Submit(config.QBURL + "/api/v2/app/setPreferences", "json=" + banIPsStr)
if banResponseBody == nil {
Log("SubmitBlockPeers", "发生错误", false)
Log("SubmitBlockPeers", "发生错误", true)
}
}
func Task() {
Expand Down Expand Up @@ -416,9 +425,10 @@ func Task() {
}
Log("Debug-Task_CheckPeer", "%s %s", false, peerInfo.IP, peerInfo.Client)
if IsProgressNotMatchUploaded(torrentInfoArr.TotalSize, peerInfo.Progress, peerInfo.Uploaded) {
Log("Task_AddBlockPeer (Bad-Progess_Uploaded)", "%s %s (TorrentTotalSize: %d, Progress: %d, Uploaded: %d)", false, peerInfo.IP, peerInfo.Client, torrentInfoArr.TotalSize, peerInfo.Progress, peerInfo.Uploaded)
blockCount++
Log("Task_AddBlockPeer (Bad-Progess_Uploaded)", "%s %s (TorrentTotalSize: %d, Progress: %.2f%%, Uploaded: %d)", true, peerInfo.IP, peerInfo.Client, torrentInfoArr.TotalSize, (peerInfo.Progress * 100), peerInfo.Uploaded)
AddBlockPeer(peerInfo.IP, peerInfo.Client)
break
continue
}
for _, v := range blockListCompiled {
if v.MatchString(peerInfo.Client) {
Expand All @@ -445,7 +455,7 @@ func Task() {
}
func RunConsole() {
if !LoadConfig() {
Log("Main", "读取配置文件失败或不完整", true)
Log("Main", "读取配置文件失败或不完整", false)
}
if !Login() {
Log("Main", "认证失败", true)
Expand Down

0 comments on commit 7b28778

Please sign in to comment.