Skip to content

Commit ec40ccf

Browse files
author
Simple-Tracker
committed
Improve proxy logic
1 parent 180cd93 commit ec40ccf

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

README.en.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Docker version is configured through the same name variable configuration, which
147147
| ignoreFailureExit | bool | false | Ignore failure exit. If enabled, it will continue to retry after first detection of the client fails or authentication fails |
148148
| sleepTime | uint32 | 20 (MicroSec) | Query waiting time of each Torrent Peers. Short interval can make blocking Peer faster but may cause client lag, Long interval can help average CPU usage |
149149
| timeout | uint32 | 6 (MillSec) | Request timeout. If interval is too short, peer may not be properly blocked. If interval is too long, timeout request will affect blocking other peer |
150-
| proxy | string | Auto | Use proxy. Set to empty to disable this behavior but still automatically detect proxy on first load |
150+
| proxy | string | Auto | Use proxy. Proxy will still automatically detect proxy on first load. Empty: Disable proxy; Auto: Automatic (use proxy only for external resources); All: Use proxy |
151151
| longConnection | bool | true | Long connection. Enable to reduce resource consumption |
152152
| logPath | string | logs | Log path. Must enable logToFile |
153153
| logToFile | bool | true | Log general information to file. If enabled, it can be used for general analysis and statistical purposes |

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Docker 版本通过相同名称的环境变量配置, 通过自动转换环境
147147
| ignoreFailureExit | bool | false (禁用) | 忽略失败退出. 启用后会使得首次检测客户端失败或认证失败后继续重试 |
148148
| sleepTime | uint32 | 20 (毫秒) | 查询每个 Torrent Peers 的等待时间. 短间隔可使屏蔽 Peer 更快但可能造成客户端卡顿, 长间隔有助于平均 CPU 资源占用 |
149149
| timeout | uint32 | 6 (秒) | 请求超时. 过短间隔可能会造成无法正确屏蔽 Peer, 过长间隔会使超时请求影响屏蔽其它 Peer 的性能 |
150-
| proxy | string | Auto (自动) | 使用代理. 设置为空可以禁止此行为, 但仍会在首次加载配置文件时自动检测代理 |
150+
| proxy | string | Auto (自动) | 使用代理. 仍会在首次加载配置文件时自动检测代理. 空: 禁止使用代理; Auto: 自动 (仅对外部资源使用代理); All: 使用代理 |
151151
| longConnection | bool | true (启用) | 长连接. 启用可降低资源消耗 |
152152
| logPath | string | logs | 日志目录. 须先启用 logToFile |
153153
| logToFile | bool | true (启用) | 记录普通信息到日志. 启用后可用于一般的分析及统计用途 |

config.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ var httpTransport = &http.Transport{
126126
}
127127

128128
var httpClient http.Client
129-
var httpClientWithoutCookie http.Client
129+
var httpClientExternal http.Client // 没有 Cookie.
130130

131131
var httpServer = http.Server{
132132
ReadTimeout: 30,
@@ -499,14 +499,20 @@ func InitConfig() {
499499
httpTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: false}
500500
}
501501

502+
httpTransportExternal := httpTransport.Clone()
503+
502504
if config.Proxy == "Auto" {
505+
// Aka default. 仅对外部资源使用代理.
506+
httpTransport.Proxy = nil
507+
httpTransportExternal.Proxy = GetProxy
508+
} else if config.Proxy == "All" {
503509
httpTransport.Proxy = GetProxy
510+
httpTransportExternal.Proxy = GetProxy
504511
} else {
505512
httpTransport.Proxy = nil
513+
httpTransportExternal.Proxy = nil
506514
}
507515

508-
httpTransportWithoutCookie := httpTransport.Clone()
509-
510516
if config.LongConnection {
511517
httpTransport.DisableKeepAlives = false
512518
}
@@ -522,9 +528,9 @@ func InitConfig() {
522528
},
523529
}
524530

525-
httpClientWithoutCookie = http.Client{
531+
httpClientExternal = http.Client{
526532
Timeout: currentTimeout,
527-
Transport: httpTransportWithoutCookie,
533+
Transport: httpTransportExternal,
528534
CheckRedirect: func(req *http.Request, via []*http.Request) error {
529535
return http.ErrUseLastResponse
530536
},

console.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func Stop() {
351351
DeleteIPFilter()
352352
SubmitBlockPeer(nil)
353353
httpClient.CloseIdleConnections()
354-
httpClientWithoutCookie.CloseIdleConnections()
354+
httpClientExternal.CloseIdleConnections()
355355
StopServer()
356356
Platform_Stop()
357357

request.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
var fetchFailedCount = 0
1010

11-
func NewRequest(isPOST bool, url string, postdata string, withAuth bool, withHeader *map[string]string) *http.Request {
11+
func NewRequest(isPOST bool, url string, postdata string, external bool, withHeader *map[string]string) *http.Request {
1212
var request *http.Request
1313
var err error
1414

@@ -47,29 +47,29 @@ func NewRequest(isPOST bool, url string, postdata string, withAuth bool, withHea
4747
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
4848
}
4949

50-
if currentClientType == "Transmission" && withAuth && Tr_csrfToken != "" {
50+
if currentClientType == "Transmission" && external && Tr_csrfToken != "" {
5151
request.Header.Set("X-Transmission-Session-Id", Tr_csrfToken)
5252
}
5353

54-
if withAuth && config.UseBasicAuth && config.ClientUsername != "" {
54+
if external && config.UseBasicAuth && config.ClientUsername != "" {
5555
request.SetBasicAuth(config.ClientUsername, config.ClientPassword)
5656
}
5757

5858
return request
5959
}
60-
func Fetch(url string, tryLogin bool, withCookie bool, withHeader *map[string]string) (int, http.Header, []byte) {
61-
request := NewRequest(false, url, "", withCookie, withHeader)
60+
func Fetch(url string, tryLogin bool, external bool, withHeader *map[string]string) (int, http.Header, []byte) {
61+
request := NewRequest(false, url, "", external, withHeader)
6262
if request == nil {
6363
return -1, nil, nil
6464
}
6565

6666
var response *http.Response
6767
var err error
6868

69-
if withCookie {
69+
if external {
7070
response, err = httpClient.Do(request)
7171
} else {
72-
response, err = httpClientWithoutCookie.Do(request)
72+
response, err = httpClientExternal.Do(request)
7373
}
7474

7575
if err != nil {
@@ -141,19 +141,19 @@ func Fetch(url string, tryLogin bool, withCookie bool, withHeader *map[string]st
141141

142142
return response.StatusCode, response.Header, responseBody
143143
}
144-
func Submit(url string, postdata string, tryLogin bool, withCookie bool, withHeader *map[string]string) (int, http.Header, []byte) {
145-
request := NewRequest(true, url, postdata, withCookie, withHeader)
144+
func Submit(url string, postdata string, tryLogin bool, external bool, withHeader *map[string]string) (int, http.Header, []byte) {
145+
request := NewRequest(true, url, postdata, external, withHeader)
146146
if request == nil {
147147
return -1, nil, nil
148148
}
149149

150150
var response *http.Response
151151
var err error
152152

153-
if withCookie {
153+
if external {
154154
response, err = httpClient.Do(request)
155155
} else {
156-
response, err = httpClientWithoutCookie.Do(request)
156+
response, err = httpClientExternal.Do(request)
157157
}
158158

159159
if err != nil {

0 commit comments

Comments
 (0)