Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLAT-25704: Add megabridge login flows #77

Merged
merged 9 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

*.json
*.db
*.db-shm
*.db-wal
*.log
*.bak

/mautrix-meta
/mautrix-meta-v2
/start
2 changes: 1 addition & 1 deletion build-v2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ if [[ -z "$LIBRARY_PATH" && -d /opt/homebrew ]]; then
fi
export MAUTRIX_VERSION=$(cat go.mod | grep 'maunium.net/go/mautrix ' | awk '{ print $2 }')
export GO_LDFLAGS="-s -w -X main.Tag=$(git describe --exact-match --tags 2>/dev/null) -X main.Commit=$(git rev-parse HEAD) -X 'main.BuildTime=`date '+%b %_d %Y, %H:%M:%S'`' -X 'maunium.net/go/mautrix.GoModVersion=$MAUTRIX_VERSION'"
go build -ldflags "$GO_LDFLAGS" -o mautrix-meta ./cmd/mautrix-meta "$@"
go build -ldflags "$GO_LDFLAGS" -o mautrix-meta-v2 ./cmd/mautrix-meta-v2 "$@"
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.21
require (
github.com/google/go-querystring v1.1.0
github.com/google/uuid v1.6.0
github.com/gorilla/websocket v1.5.0
github.com/gorilla/websocket v1.5.3
github.com/lib/pq v1.10.9
github.com/mattn/go-colorable v0.1.13
github.com/mattn/go-sqlite3 v1.14.22
github.com/rs/zerolog v1.33.0
github.com/tidwall/gjson v1.17.1
go.mau.fi/libsignal v0.1.0
go.mau.fi/util v0.5.1-0.20240625181823-38eefa626984
go.mau.fi/util v0.5.1-0.20240629220711-4fa40bf64652
go.mau.fi/whatsmeow v0.0.0-20240625083845-6acab596dd8c
golang.org/x/crypto v0.24.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
Expand All @@ -26,14 +26,14 @@ require (
require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/yuin/goldmark v1.7.2 // indirect
github.com/yuin/goldmark v1.7.4 // indirect
go.mau.fi/zeroconfig v0.1.2 // indirect
golang.org/x/sys v0.21.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
Expand Down
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,18 @@ github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -43,16 +44,17 @@ github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=
github.com/tidwall/gjson v1.17.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
github.com/yuin/goldmark v1.7.2 h1:NjGd7lO7zrUn/A7eKwn5PEOt4ONYGqpxSEeZuduvgxc=
github.com/yuin/goldmark v1.7.2/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
go.mau.fi/libsignal v0.1.0 h1:vAKI/nJ5tMhdzke4cTK1fb0idJzz1JuEIpmjprueC+c=
go.mau.fi/libsignal v0.1.0/go.mod h1:R8ovrTezxtUNzCQE5PH30StOQWWeBskBsWE55vMfY9I=
go.mau.fi/util v0.5.1-0.20240625181823-38eefa626984 h1:63X00R1qL5G+m2LWxdKzAS8cyihFu5IaaOzJ7QHvkzs=
go.mau.fi/util v0.5.1-0.20240625181823-38eefa626984/go.mod h1:DsJzUrJAG53lCZnnYvq9/mOyLuPScWwYhvETiTrpdP4=
go.mau.fi/util v0.5.1-0.20240629220711-4fa40bf64652 h1:/wY7vpwOE6he5Qlf6lICHuOUs+nAQdsC7qwRsDbsh14=
go.mau.fi/util v0.5.1-0.20240629220711-4fa40bf64652/go.mod h1:DsJzUrJAG53lCZnnYvq9/mOyLuPScWwYhvETiTrpdP4=
go.mau.fi/whatsmeow v0.0.0-20240625083845-6acab596dd8c h1:yiULssyKHJcFA1fae2NJkwU7QW4EHQs7QEWoIqfqilA=
go.mau.fi/whatsmeow v0.0.0-20240625083845-6acab596dd8c/go.mod h1:0+65CYaE6r4dWzr0dN8i+UZKy0gIfJ79VuSqIl0nKRM=
go.mau.fi/zeroconfig v0.1.2 h1:DKOydWnhPMn65GbXZOafgkPm11BvFashZWLct0dGFto=
Expand Down
9 changes: 9 additions & 0 deletions messagix/cookies/cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ type Cookies struct {
IGWWWClaim string
}

func (c *Cookies) UpdateValues(newValues map[string]string) {
c.lock.Lock()
defer c.lock.Unlock()
c.values = make(map[MetaCookieName]string)
for k, v := range newValues {
c.values[MetaCookieName(k)] = v
}
}

func (c *Cookies) MarshalJSON() ([]byte, error) {
return json.Marshal(c.values)
}
Expand Down
39 changes: 31 additions & 8 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,37 @@ func (m *MetaConnector) GetCapabilities() *bridgev2.NetworkGeneralCapabilities {
}

func (s *MetaConnector) GetName() bridgev2.BridgeName {
// Would be nice if we could use MetaConfig.Mode here, but it's not available yet?
return bridgev2.BridgeName{
DisplayName: "Meta",
NetworkURL: "https://meta.com",
NetworkIcon: "mxc://maunium.net/JxjlbZUlCPULEeHZSwleUXQv", // Instagram icon
NetworkID: "meta",
BeeperBridgeType: "meta",
DefaultPort: 29319,
if s.Config == nil || s.Config.Mode == "" {
return bridgev2.BridgeName{
DisplayName: "Meta",
NetworkURL: "https://meta.com",
NetworkIcon: "mxc://maunium.net/DxpVrwwzPUwaUSazpsjXgcKB",
NetworkID: "meta",
BeeperBridgeType: "meta",
DefaultPort: 29319,
}
} else {
if s.Config.Mode == "instagram" {
return bridgev2.BridgeName{
DisplayName: "Instagram",
NetworkURL: "https://instagram.com",
NetworkIcon: "mxc://maunium.net/JxjlbZUlCPULEeHZSwleUXQv",
NetworkID: "instagram",
BeeperBridgeType: "meta",
DefaultPort: 29319,
}
} else if s.Config.Mode == "facebook" {
return bridgev2.BridgeName{
DisplayName: "Facebook",
NetworkURL: "https://www.facebook.com/messenger",
NetworkIcon: "mxc://maunium.net/ygtkteZsXnGJLJHRchUwYWak",
NetworkID: "facebook",
BeeperBridgeType: "meta",
DefaultPort: 29319,
}
} else {
panic("unknown mode")
}
}
}

Expand Down
1 change: 0 additions & 1 deletion pkg/connector/example-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# Which service is this bridge for? Either "facebook" or "instagram".
# Remember to change the appservice id, bot profile info and bridge username_template too.
mode: instagram
136 changes: 117 additions & 19 deletions pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,141 @@ import (
"fmt"

"maunium.net/go/mautrix/bridgev2"

"go.mau.fi/util/exslices"

"go.mau.fi/mautrix-meta/messagix"
"go.mau.fi/mautrix-meta/messagix/cookies"
"go.mau.fi/mautrix-meta/messagix/table"
"go.mau.fi/mautrix-meta/messagix/types"
)

const FlowIDFacebookCookies = "cookies-facebook"
const FlowIDInstagramCookies = "cookies-instagram"

func (m *MetaConnector) CreateLogin(ctx context.Context, user *bridgev2.User, flowID string) (bridgev2.LoginProcess, error) {
if flowID != "cookies" {
if flowID != FlowIDFacebookCookies && flowID != FlowIDInstagramCookies {
return nil, fmt.Errorf("unknown flow ID %s", flowID)
}

return &CookieLogin{}, nil
return &MetaCookieLogin{
Flow: flowID,
User: user,
Main: m,
}, nil
}

func (m *MetaConnector) GetLoginFlows() []bridgev2.LoginFlow {
return []bridgev2.LoginFlow{{
Name: "Cookies",
Description: "Login using cookies from a web browser",
ID: "cookies",
}}
facebook := bridgev2.LoginFlow{
Name: "Facebook Cookies",
Description: "Login using cookies from Facebook Messenger",
ID: FlowIDFacebookCookies,
}
instagram := bridgev2.LoginFlow{
Name: "Instagram Cookies",
Description: "Login using cookies from Instagram",
ID: FlowIDInstagramCookies,
}
if m.Config == nil || m.Config.Mode == "" {
return []bridgev2.LoginFlow{facebook, instagram}
} else if m.Config.Mode == "facebook" {
return []bridgev2.LoginFlow{facebook}
} else if m.Config.Mode == "instagram" {
return []bridgev2.LoginFlow{instagram}
} else {
panic("unknown mode in config")
}
}

type CookieLogin struct{}
type MetaCookieLogin struct {
Flow string
User *bridgev2.User
Main *MetaConnector
}

var _ bridgev2.LoginProcessCookies = (*CookieLogin)(nil)
var _ bridgev2.LoginProcessCookies = (*MetaCookieLogin)(nil)

func (p *CookieLogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) {
return &bridgev2.LoginStep{
Type: bridgev2.LoginStepTypeCookies,
StepID: "fi.mau.meta.cookies",
Instructions: "Please enter cookies from your browser",
CookiesParams: &bridgev2.LoginCookiesParams{},
}, nil
func (m *MetaCookieLogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) {
if m.Flow == FlowIDFacebookCookies {
return &bridgev2.LoginStep{
Type: bridgev2.LoginStepTypeCookies,
StepID: "fi.mau.meta.cookies",
Instructions: "Please enter cookies from your browser",
CookiesParams: &bridgev2.LoginCookiesParams{
URL: "https://www.facebook.com/",
UserAgent: messagix.UserAgent,
CookieDomain: "www.facebook.com",
CookieKeys: exslices.CastToString[string](cookies.FBRequiredCookies),
},
}, nil
} else if m.Flow == FlowIDInstagramCookies {
return &bridgev2.LoginStep{
Type: bridgev2.LoginStepTypeCookies,
StepID: "fi.mau.meta.cookies",
Instructions: "Please enter cookies from your browser",
CookiesParams: &bridgev2.LoginCookiesParams{
URL: "https://www.instagram.com/",
UserAgent: messagix.UserAgent,
CookieDomain: "www.instagram.com",
CookieKeys: exslices.CastToString[string](cookies.IGRequiredCookies),
},
}, nil
} else {
return nil, fmt.Errorf("unknown flow ID %s", m.Flow)
}
}

func (c *CookieLogin) Cancel() {}
func (m *MetaCookieLogin) Cancel() {}

func (m *MetaCookieLogin) getFBID(currentUser types.UserInfo, tbl *table.LSTable) int64 {
var newFBID int64
// TODO figure out why the contact IDs for self is different than the fbid in the ready event
for _, row := range tbl.LSVerifyContactRowExists {
if row.IsSelf && row.ContactId != newFBID {
if newFBID != 0 {
// Hopefully this won't happen
m.User.Log.Warn().Int64("prev_fbid", newFBID).Int64("new_fbid", row.ContactId).Msg("Got multiple fbids for self")
} else {
m.User.Log.Debug().Int64("fbid", row.ContactId).Msg("Found own fbid")
}
newFBID = row.ContactId
}
}
if newFBID == 0 {
newFBID = currentUser.GetFBID()
m.User.Log.Warn().Int64("fbid", newFBID).Msg("Own contact entry not found, falling back to fbid in current user object")
}
return newFBID
}

func (m *MetaCookieLogin) SubmitCookies(ctx context.Context, strCookies map[string]string) (*bridgev2.LoginStep, error) {
c := &cookies.Cookies{
Platform: types.Instagram,
}
if m.Flow == FlowIDFacebookCookies {
c.Platform = types.Facebook
}

c.UpdateValues(strCookies)

// Check if the cookies are valid
if !c.IsLoggedIn() {
return nil, fmt.Errorf("invalid cookies")
}

log := m.User.Log.With().Str("component", "messagix").Logger()
client := messagix.NewClient(c, log)

currentUser, initialTable, err := client.LoadMessagesPage()
if err != nil {
return nil, err
}

FBID := m.getFBID(currentUser, initialTable)

func (p *CookieLogin) SubmitCookies(ctx context.Context, cookies map[string]string) (*bridgev2.LoginStep, error) {
return &bridgev2.LoginStep{
Type: bridgev2.LoginStepTypeComplete,
StepID: "fi.mau.meta.complete",
Instructions: "Login successful",
Instructions: fmt.Sprintf("Logged in as %s (%d)", currentUser.GetName(), FBID),
}, nil
}
Loading