Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
180 lines (156 loc) · 5.68 KB
/
Copy pathclient.go
File metadata and controls
180 lines (156 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// mautrix-linkedin - A Matrix-LinkedIn puppeting bridge.
// Copyright (C) 2025 Sumner Evans
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package linkedingo
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"sync"
"sync/atomic"
"github.com/google/uuid"
"github.com/rs/zerolog"
"go.mau.fi/util/random"
)
const BrowserName = "Chrome"
const ChromeVersion = "141"
const UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/" + ChromeVersion + ".0.0.0 Safari/537.36"
const SecCHUserAgent = `"Chromium";v="` + ChromeVersion + `", "Google Chrome";v="` + ChromeVersion + `", "Not-A.Brand";v="99"`
const OSName = "Linux"
const SecCHPlatform = `"` + OSName + `"`
const SecCHMobile = "?0"
const SecCHPrefersColorScheme = "light"
const ServiceVersion = "1.13.40953"
const defaultXLiTrack = `{"clientVersion":"` + ServiceVersion + `","mpVersion":"` + ServiceVersion + `","osName":"web","deviceFormFactor":"DESKTOP","mpName":"voyager-web","displayDensity":2,"displayWidth":2880,"displayHeight":1800}`
var defaultBrowserHeaders = http.Header{
"User-Agent": {UserAgent},
"Sec-Ch-Ua": {SecCHUserAgent},
"Sec-Ch-Ua-Platform": {SecCHPlatform},
"Sec-Ch-Ua-Mobile": {SecCHMobile},
"Sec-Ch-Prefers-Color-Scheme": {SecCHPrefersColorScheme},
}
type Client struct {
http *http.Client
browserHeaders http.Header
jar *StringCookieJar
userEntityURN URN
realtimeSessionID uuid.UUID
realtimeCancelFn context.CancelFunc
realtimeWaitGroup sync.WaitGroup
handlers Handlers
loggedOut atomic.Bool
pageInstance string
xLITrack string
serviceVersion string
conversationsSyncToken string
}
func NewClient(ctx context.Context, userEntityURN URN, jar *StringCookieJar, pageInstance, xLiTrack, conversationsSyncToken string, handlers Handlers) *Client {
log := zerolog.Ctx(ctx)
if xLiTrack == "" {
log.Warn().Msg("x-li-track is empty, using default")
xLiTrack = defaultXLiTrack
}
if pageInstance == "" {
log.Warn().Msg("pageInstance is empty, using default")
pageInstance = "urn:li:page:messaging_thread;5accf988-7540-4d0a-8a28-a0732bf6de20"
}
trackingData := map[string]any{}
if err := json.Unmarshal([]byte(xLiTrack), &trackingData); err != nil {
log.Warn().Err(err).Msg("failed to parse x-li-track")
}
serviceVersion, _ := trackingData["mpVersion"].(string)
if serviceVersion == "" {
log.Warn().Msg("mpVersion is empty, using default")
serviceVersion = ServiceVersion
}
// Possible workaround for a/b testing where the frontend appears to be a completely different version
mpName, _ := trackingData["mpName"].(string)
if mpName != "voyager-web" {
log.Warn().Msg("mpName is not voyager-web, using default xLiTrack")
xLiTrack = defaultXLiTrack
pageInstance = "urn:li:page:d_flagship3_messaging_conversation_detail;" + base64.StdEncoding.EncodeToString(random.Bytes(16))
}
cli := &Client{
browserHeaders: defaultBrowserHeaders.Clone(),
userEntityURN: userEntityURN,
jar: jar,
pageInstance: pageInstance,
xLITrack: xLiTrack,
serviceVersion: serviceVersion,
realtimeSessionID: uuid.New(),
handlers: handlers,
conversationsSyncToken: conversationsSyncToken,
}
cli.http = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
},
Jar: jar,
CheckRedirect: cli.checkHTTPRedirect,
}
return cli
}
func (c *Client) IsLoggedIn() bool {
return !c.loggedOut.Load() && c.jar.GetCookie(LinkedInCookieJSESSIONID) != ""
}
func (c *Client) onAuthRejected(ctx context.Context, err error) {
if c.loggedOut.CompareAndSwap(false, true) {
c.handlers.onBadCredentials(ctx, err)
}
}
func (c *Client) SetBrowserHeaders(headers http.Header) {
if headers.Get("User-Agent") != "" {
c.browserHeaders = headers.Clone()
}
}
type Handlers struct {
Heartbeat func(context.Context)
ClientConnection func(context.Context, *ClientConnection)
TransientDisconnect func(context.Context, error)
BadCredentials func(context.Context, error)
DecoratedEvent func(context.Context, *DecoratedEvent)
ConversationsSyncToken func(context.Context, string)
}
func (h Handlers) onHeartbeat(ctx context.Context) {
if h.Heartbeat != nil {
h.Heartbeat(ctx)
}
}
func (h Handlers) onClientConnection(ctx context.Context, conn *ClientConnection) {
if h.ClientConnection != nil {
h.ClientConnection(ctx, conn)
}
}
func (h Handlers) onTransientDisconnect(ctx context.Context, err error) {
if h.TransientDisconnect != nil {
h.TransientDisconnect(ctx, err)
}
}
func (h Handlers) onBadCredentials(ctx context.Context, err error) {
if h.BadCredentials != nil {
h.BadCredentials(ctx, err)
}
}
func (h Handlers) onDecoratedEvent(ctx context.Context, decoratedEvent *DecoratedEvent) {
if h.DecoratedEvent != nil {
h.DecoratedEvent(ctx, decoratedEvent)
}
}
func (h Handlers) onConversationsSyncToken(ctx context.Context, conversationsSyncToken string) {
if h.ConversationsSyncToken != nil {
h.ConversationsSyncToken(ctx, conversationsSyncToken)
}
}