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
This commit is contained in:
UUBulb 2024-10-18 15:08:51 +08:00 committed by GitHub
parent c6eb90708e
commit 404260187c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 9 additions and 12 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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},

View File

@ -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,
}

View File

@ -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 {