Merge pull request #7 from chzealot/main

feat: 支持对Stream通道标识本地局域网IP,用于问题诊断
This commit is contained in:
hzjiangjian 2023-08-07 11:01:18 +08:00 committed by GitHub
commit 24b714f7ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 1 deletions

View File

@ -273,6 +273,9 @@ func (cli *StreamClient) GetConnectionEndpoint(ctx context.Context) (*payload.Co
UserAgent: cli.UserAgent.UserAgent, UserAgent: cli.UserAgent.UserAgent,
Subscriptions: make([]*payload.SubscriptionModel, 0), Subscriptions: make([]*payload.SubscriptionModel, 0),
} }
if localIp, err := utils.GetFirstLanIP(); err == nil {
requestModel.LocalIP = localIp
}
for ttype, subs := range cli.subscriptions { for ttype, subs := range cli.subscriptions {
for ttopic, _ := range subs { for ttopic, _ := range subs {

View File

@ -41,7 +41,7 @@ type UserAgentConfig struct {
func NewDingtalkGoSDKUserAgent() *UserAgentConfig { func NewDingtalkGoSDKUserAgent() *UserAgentConfig {
return &UserAgentConfig{ return &UserAgentConfig{
UserAgent: "dingtalk-sdk-go/v0.2.0", UserAgent: "dingtalk-sdk-go/v0.3.0",
} }
} }

View File

@ -18,6 +18,7 @@ type ConnectionEndpointRequest struct {
ClientSecret string `json:"clientSecret"` //自建应用appSecret; 三方应用suiteSecret ClientSecret string `json:"clientSecret"` //自建应用appSecret; 三方应用suiteSecret
Subscriptions []*SubscriptionModel `json:"subscriptions"` Subscriptions []*SubscriptionModel `json:"subscriptions"`
UserAgent string `json:"ua"` UserAgent string `json:"ua"`
LocalIP string `json:"localIp"`
} }
// 长连接接入点参数 // 长连接接入点参数

25
utils/net.go Normal file
View File

@ -0,0 +1,25 @@
package utils
import (
"errors"
"net"
)
func GetFirstLanIP() (string, error) {
addresses, err := net.InterfaceAddrs()
if err != nil {
return "", err
}
if len(addresses) == 0 {
return "", errors.New("empty interfaces")
}
for _, addr := range addresses {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String(), nil
}
}
}
return "", errors.New("no valid interfaces")
}

12
utils/net_test.go Normal file
View File

@ -0,0 +1,12 @@
package utils
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetFirstLanIP(t *testing.T) {
ip, err := GetFirstLanIP()
assert.Nil(t, err)
assert.NotEmpty(t, ip)
}