From 482ddbd4bb8c54472423057ab20c8f8547280f1a Mon Sep 17 00:00:00 2001 From: Ke Jie Date: Mon, 7 Aug 2023 10:56:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=AF=B9Stream?= =?UTF-8?q?=E9=80=9A=E9=81=93=E6=A0=87=E8=AF=86=E6=9C=AC=E5=9C=B0=E5=B1=80?= =?UTF-8?q?=E5=9F=9F=E7=BD=91IP=EF=BC=8C=E7=94=A8=E4=BA=8E=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E8=AF=8A=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ke Jie --- client/client.go | 3 +++ client/config.go | 2 +- payload/connection.go | 1 + utils/net.go | 25 +++++++++++++++++++++++++ utils/net_test.go | 12 ++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 utils/net.go create mode 100644 utils/net_test.go 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) +}