From 404260187c3bc3997509db79ceb8968a7236992e Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Fri, 18 Oct 2024 15:08:51 +0800 Subject: [PATCH] grpc: update deprecated method (#74) * grpc: update deprecated method * grpc: add connection timeout * remove cf-ns.com * prolong connection timeout to 10s * remove connection timeout --- cmd/agent/main.go | 9 +++------ pkg/monitor/myip.go | 2 +- pkg/util/util.go | 4 ++-- pkg/utls/roundtripper.go | 4 ++-- pkg/utls/roundtripper_test.go | 2 +- 5 files changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 8ae9114..192df9d 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -125,7 +125,7 @@ func init() { http.DefaultClient.Timeout = time.Second * 30 httpClient.Transport = utlsx.NewUTLSHTTPRoundTripperWithProxy( utls.HelloChrome_Auto, new(utls.Config), - http.DefaultTransport, nil, headers, + http.DefaultTransport, nil, &headers, ) ex, err := os.Executable() @@ -248,7 +248,6 @@ func run() { } for { - timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) var securityOption grpc.DialOption if agentCliParam.TLS { if agentCliParam.InsecureTLS { @@ -259,17 +258,15 @@ func run() { } else { securityOption = grpc.WithTransportCredentials(insecure.NewCredentials()) } - conn, err = grpc.DialContext(timeOutCtx, agentCliParam.Server, securityOption, grpc.WithPerRPCCredentials(&auth)) + conn, err = grpc.NewClient(agentCliParam.Server, securityOption, grpc.WithPerRPCCredentials(&auth)) if err != nil { printf("与面板建立连接失败: %v", err) - cancel() retry() continue } - cancel() client = pb.NewNezhaServiceClient(conn) // 第一步注册 - timeOutCtx, cancel = context.WithTimeout(context.Background(), networkTimeOut) + timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut) _, err = client.ReportSystemInfo(timeOutCtx, monitor.GetHost().PB()) if err != nil { printf("上报系统信息失败: %v", err) diff --git a/pkg/monitor/myip.go b/pkg/monitor/myip.go index c488d03..5f6f398 100644 --- a/pkg/monitor/myip.go +++ b/pkg/monitor/myip.go @@ -15,7 +15,7 @@ var ( cfList = []string{ "https://blog.cloudflare.com/cdn-cgi/trace", "https://dash.cloudflare.com/cdn-cgi/trace", - "https://cf-ns.com/cdn-cgi/trace", // 有国内节点 + "https://developers.cloudflare.com/cdn-cgi/trace", } CachedIP, GeoQueryIP, CachedCountryCode string httpClientV4 = util.NewSingleStackHTTPClient(time.Second*20, time.Second*5, time.Second*10, false) diff --git a/pkg/util/util.go b/pkg/util/util.go index c0a59c3..5a5d2c0 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -34,8 +34,8 @@ func Printf(enabled bool, format string, v ...interface{}) { } } -func BrowserHeaders() *http.Header { - return &http.Header{ +func BrowserHeaders() http.Header { + return http.Header{ "Accept": {"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"}, "Accept-Language": {"en,zh-CN;q=0.9,zh;q=0.8"}, "User-Agent": {MacOSChromeUA}, diff --git a/pkg/utls/roundtripper.go b/pkg/utls/roundtripper.go index 6cf2bf5..626b118 100644 --- a/pkg/utls/roundtripper.go +++ b/pkg/utls/roundtripper.go @@ -128,7 +128,7 @@ func getPendingConnectionID(dest string, alpnIsH2 bool) pendingConnKey { func (r *uTLSHTTPRoundTripperImpl) putConn(addr string, alpnIsH2 bool, conn net.Conn) { connId := getPendingConnectionID(addr, alpnIsH2) - r.pendingConn[connId] = NewUnclaimedConnection(conn, time.Minute) + r.pendingConn[connId] = newUnclaimedConnection(conn, time.Minute) } func (r *uTLSHTTPRoundTripperImpl) getConn(addr string, alpnIsH2 bool) net.Conn { @@ -241,7 +241,7 @@ func (r *uTLSHTTPRoundTripperImpl) init() { } } -func NewUnclaimedConnection(conn net.Conn, expireTime time.Duration) *unclaimedConnection { +func newUnclaimedConnection(conn net.Conn, expireTime time.Duration) *unclaimedConnection { c := &unclaimedConnection{ Conn: conn, } diff --git a/pkg/utls/roundtripper_test.go b/pkg/utls/roundtripper_test.go index a3a6f9d..ef4ebbb 100644 --- a/pkg/utls/roundtripper_test.go +++ b/pkg/utls/roundtripper_test.go @@ -26,7 +26,7 @@ func TestCloudflareDetection(t *testing.T) { headers := util.BrowserHeaders() client.Transport = utlsx.NewUTLSHTTPRoundTripperWithProxy( utls.HelloChrome_Auto, new(utls.Config), - http.DefaultTransport, nil, headers, + http.DefaultTransport, nil, &headers, ) resp, err = doRequest(client, url) if err != nil {