diff --git a/client/client.go b/client/client.go index d3f93e0..f57a529 100644 --- a/client/client.go +++ b/client/client.go @@ -273,6 +273,9 @@ func (cli *StreamClient) GetConnectionEndpoint(ctx context.Context) (*payload.Co UserAgent: cli.UserAgent.UserAgent, Subscriptions: make([]*payload.SubscriptionModel, 0), } + if localIp, err := utils.GetFirstLanIP(); err == nil { + requestModel.LocalIP = localIp + } for ttype, subs := range cli.subscriptions { for ttopic, _ := range subs { diff --git a/client/config.go b/client/config.go index 4949948..829a129 100644 --- a/client/config.go +++ b/client/config.go @@ -41,7 +41,7 @@ type UserAgentConfig struct { func NewDingtalkGoSDKUserAgent() *UserAgentConfig { return &UserAgentConfig{ - UserAgent: "dingtalk-sdk-go/v0.2.0", + UserAgent: "dingtalk-sdk-go/v0.3.0", } } diff --git a/payload/connection.go b/payload/connection.go index de20f28..ff73113 100644 --- a/payload/connection.go +++ b/payload/connection.go @@ -18,6 +18,7 @@ type ConnectionEndpointRequest struct { ClientSecret string `json:"clientSecret"` //自建应用appSecret; 三方应用suiteSecret Subscriptions []*SubscriptionModel `json:"subscriptions"` UserAgent string `json:"ua"` + LocalIP string `json:"localIp"` } // 长连接接入点参数 diff --git a/utils/net.go b/utils/net.go new file mode 100644 index 0000000..69e261e --- /dev/null +++ b/utils/net.go @@ -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") +} diff --git a/utils/net_test.go b/utils/net_test.go new file mode 100644 index 0000000..1bb5d6a --- /dev/null +++ b/utils/net_test.go @@ -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) +}