Skip to content

Commit

Permalink
grpc: update deprecated method (#74)
Browse files Browse the repository at this point in the history
* grpc: update deprecated method

* grpc: add connection timeout

* remove cf-ns.com

* prolong connection timeout to 10s

* remove connection timeout
  • Loading branch information
uubulb authored Oct 18, 2024
1 parent c6eb907 commit 4042601
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 12 deletions.
9 changes: 3 additions & 6 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -248,7 +248,6 @@ func run() {
}

for {
timeOutCtx, cancel := context.WithTimeout(context.Background(), networkTimeOut)
var securityOption grpc.DialOption
if agentCliParam.TLS {
if agentCliParam.InsecureTLS {
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitor/myip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
4 changes: 2 additions & 2 deletions pkg/utls/roundtripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/utls/roundtripper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

1 comment on commit 4042601

@joyoner
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个版本agent更新后,面板使用caddy 反代rpc端口的情况下,有些agent连不上了。

data.example.com:443 { # 你的 Agent 连接 Dashboard 的域名
    reverse_proxy {
        to localhost:5555
        transport http {
            versions h2c 2
        }
    }
}

Please sign in to comment.