main.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. _ "embed"
  19. "errors"
  20. "fmt"
  21. "net/http"
  22. "os"
  23. "os/signal"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "syscall"
  28. "time"
  29. "google.golang.org/protobuf/proto"
  30. "go.mau.fi/whatsmeow"
  31. waProto "go.mau.fi/whatsmeow/binary/proto"
  32. "go.mau.fi/whatsmeow/store"
  33. "go.mau.fi/whatsmeow/store/sqlstore"
  34. "go.mau.fi/whatsmeow/types"
  35. flag "maunium.net/go/mauflag"
  36. log "maunium.net/go/maulogger/v2"
  37. "maunium.net/go/mautrix"
  38. "maunium.net/go/mautrix/appservice"
  39. "maunium.net/go/mautrix/event"
  40. "maunium.net/go/mautrix/id"
  41. "maunium.net/go/mautrix-whatsapp/config"
  42. "maunium.net/go/mautrix-whatsapp/database"
  43. "maunium.net/go/mautrix-whatsapp/database/upgrades"
  44. )
  45. // The name and repo URL of the bridge.
  46. var (
  47. Name = "mautrix-whatsapp"
  48. URL = "https://github.com/mautrix/whatsapp"
  49. )
  50. // Information to find out exactly which commit the bridge was built from.
  51. // These are filled at build time with the -X linker flag.
  52. var (
  53. Tag = "unknown"
  54. Commit = "unknown"
  55. BuildTime = "unknown"
  56. )
  57. var (
  58. // Version is the version number of the bridge. Changed manually when making a release.
  59. Version = "0.3.0"
  60. // WAVersion is the version number exposed to WhatsApp. Filled in init()
  61. WAVersion = ""
  62. // VersionString is the bridge version, plus commit information. Filled in init() using the build-time values.
  63. VersionString = ""
  64. )
  65. //go:embed example-config.yaml
  66. var ExampleConfig string
  67. func init() {
  68. if len(Tag) > 0 && Tag[0] == 'v' {
  69. Tag = Tag[1:]
  70. }
  71. if Tag != Version {
  72. suffix := ""
  73. if !strings.HasSuffix(Version, "+dev") {
  74. suffix = "+dev"
  75. }
  76. if len(Commit) > 8 {
  77. Version = fmt.Sprintf("%s%s.%s", Version, suffix, Commit[:8])
  78. } else {
  79. Version = fmt.Sprintf("%s%s.unknown", Version, suffix)
  80. }
  81. }
  82. mautrix.DefaultUserAgent = fmt.Sprintf("mautrix-whatsapp/%s %s", Version, mautrix.DefaultUserAgent)
  83. WAVersion = strings.FieldsFunc(Version, func(r rune) bool { return r == '-' || r == '+' })[0]
  84. VersionString = fmt.Sprintf("%s %s (%s)", Name, Version, BuildTime)
  85. config.ExampleConfig = ExampleConfig
  86. }
  87. var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String()
  88. var dontSaveConfig = flag.MakeFull("n", "no-update", "Don't save updated config to disk.", "false").Bool()
  89. var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String()
  90. var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool()
  91. var version = flag.MakeFull("v", "version", "View bridge version and quit.", "false").Bool()
  92. var ignoreUnsupportedDatabase = flag.Make().LongKey("ignore-unsupported-database").Usage("Run even if the database schema is too new").Default("false").Bool()
  93. var ignoreForeignTables = flag.Make().LongKey("ignore-foreign-tables").Usage("Run even if the database contains tables from other programs (like Synapse)").Default("false").Bool()
  94. var migrateFrom = flag.Make().LongKey("migrate-db").Usage("Source database type and URI to migrate from.").Bool()
  95. var wantHelp, _ = flag.MakeHelpFlag()
  96. func (bridge *Bridge) GenerateRegistration() {
  97. if *dontSaveConfig {
  98. // We need to save the generated as_token and hs_token in the config
  99. _, _ = fmt.Fprintln(os.Stderr, "--no-update is not compatible with --generate-registration")
  100. os.Exit(5)
  101. }
  102. reg, err := bridge.Config.NewRegistration()
  103. if err != nil {
  104. _, _ = fmt.Fprintln(os.Stderr, "Failed to generate registration:", err)
  105. os.Exit(20)
  106. }
  107. err = reg.Save(*registrationPath)
  108. if err != nil {
  109. _, _ = fmt.Fprintln(os.Stderr, "Failed to save registration:", err)
  110. os.Exit(21)
  111. }
  112. err = config.Mutate(*configPath, func(helper *config.UpgradeHelper) {
  113. helper.Set(config.Str, bridge.Config.AppService.ASToken, "appservice", "as_token")
  114. helper.Set(config.Str, bridge.Config.AppService.HSToken, "appservice", "hs_token")
  115. })
  116. if err != nil {
  117. _, _ = fmt.Fprintln(os.Stderr, "Failed to save config:", err)
  118. os.Exit(22)
  119. }
  120. fmt.Println("Registration generated. Add the path to the registration to your Synapse config, restart it, then start the bridge.")
  121. os.Exit(0)
  122. }
  123. func (bridge *Bridge) MigrateDatabase() {
  124. oldDB, err := database.New(config.DatabaseConfig{Type: flag.Arg(0), URI: flag.Arg(1)}, log.DefaultLogger)
  125. if err != nil {
  126. fmt.Println("Failed to open old database:", err)
  127. os.Exit(30)
  128. }
  129. err = oldDB.Init()
  130. if err != nil {
  131. fmt.Println("Failed to upgrade old database:", err)
  132. os.Exit(31)
  133. }
  134. newDB, err := database.New(bridge.Config.AppService.Database, log.DefaultLogger)
  135. if err != nil {
  136. fmt.Println("Failed to open new database:", err)
  137. os.Exit(32)
  138. }
  139. err = newDB.Init()
  140. if err != nil {
  141. fmt.Println("Failed to upgrade new database:", err)
  142. os.Exit(33)
  143. }
  144. database.Migrate(oldDB, newDB)
  145. }
  146. type Bridge struct {
  147. AS *appservice.AppService
  148. EventProcessor *appservice.EventProcessor
  149. MatrixHandler *MatrixHandler
  150. Config *config.Config
  151. DB *database.Database
  152. Log log.Logger
  153. StateStore *database.SQLStateStore
  154. Provisioning *ProvisioningAPI
  155. Bot *appservice.IntentAPI
  156. Formatter *Formatter
  157. Crypto Crypto
  158. Metrics *MetricsHandler
  159. WAContainer *sqlstore.Container
  160. usersByMXID map[id.UserID]*User
  161. usersByUsername map[string]*User
  162. usersLock sync.Mutex
  163. spaceRooms map[id.RoomID]*User
  164. spaceRoomsLock sync.Mutex
  165. managementRooms map[id.RoomID]*User
  166. managementRoomsLock sync.Mutex
  167. portalsByMXID map[id.RoomID]*Portal
  168. portalsByJID map[database.PortalKey]*Portal
  169. portalsLock sync.Mutex
  170. puppets map[types.JID]*Puppet
  171. puppetsByCustomMXID map[id.UserID]*Puppet
  172. puppetsLock sync.Mutex
  173. }
  174. type Crypto interface {
  175. HandleMemberEvent(*event.Event)
  176. Decrypt(*event.Event) (*event.Event, error)
  177. Encrypt(id.RoomID, event.Type, event.Content) (*event.EncryptedEventContent, error)
  178. WaitForSession(id.RoomID, id.SenderKey, id.SessionID, time.Duration) bool
  179. RequestSession(id.RoomID, id.SenderKey, id.SessionID, id.UserID, id.DeviceID)
  180. ResetSession(id.RoomID)
  181. Init() error
  182. Start()
  183. Stop()
  184. }
  185. func (bridge *Bridge) ensureConnection() {
  186. for {
  187. resp, err := bridge.Bot.Whoami()
  188. if err != nil {
  189. if errors.Is(err, mautrix.MUnknownToken) {
  190. bridge.Log.Fatalln("The as_token was not accepted. Is the registration file installed in your homeserver correctly?")
  191. os.Exit(16)
  192. } else if errors.Is(err, mautrix.MExclusive) {
  193. bridge.Log.Fatalln("The as_token was accepted, but the /register request was not. Are the homeserver domain and username template in the config correct, and do they match the values in the registration?")
  194. os.Exit(16)
  195. }
  196. bridge.Log.Errorfln("Failed to connect to homeserver: %v. Retrying in 10 seconds...", err)
  197. time.Sleep(10 * time.Second)
  198. } else if resp.UserID != bridge.Bot.UserID {
  199. bridge.Log.Fatalln("Unexpected user ID in whoami call: got %s, expected %s", resp.UserID, bridge.Bot.UserID)
  200. os.Exit(17)
  201. } else {
  202. break
  203. }
  204. }
  205. }
  206. func (bridge *Bridge) Init() {
  207. var err error
  208. bridge.AS, err = bridge.Config.MakeAppService()
  209. if err != nil {
  210. _, _ = fmt.Fprintln(os.Stderr, "Failed to initialize AppService:", err)
  211. os.Exit(11)
  212. }
  213. _, _ = bridge.AS.Init()
  214. bridge.Log = log.Create()
  215. bridge.Config.Logging.Configure(bridge.Log)
  216. log.DefaultLogger = bridge.Log.(*log.BasicLogger)
  217. if len(bridge.Config.Logging.FileNameFormat) > 0 {
  218. err = log.OpenFile()
  219. if err != nil {
  220. _, _ = fmt.Fprintln(os.Stderr, "Failed to open log file:", err)
  221. os.Exit(12)
  222. }
  223. }
  224. bridge.AS.Log = log.Sub("Matrix")
  225. bridge.Bot = bridge.AS.BotIntent()
  226. bridge.Log.Infoln("Initializing", VersionString)
  227. bridge.Log.Debugln("Initializing database connection")
  228. bridge.DB, err = database.New(bridge.Config.AppService.Database, bridge.Log)
  229. if err != nil {
  230. bridge.Log.Fatalln("Failed to initialize database connection:", err)
  231. os.Exit(14)
  232. }
  233. bridge.Log.Debugln("Initializing state store")
  234. bridge.StateStore = database.NewSQLStateStore(bridge.DB)
  235. bridge.AS.StateStore = bridge.StateStore
  236. bridge.WAContainer = sqlstore.NewWithDB(bridge.DB.DB, bridge.Config.AppService.Database.Type, nil)
  237. ss := bridge.Config.AppService.Provisioning.SharedSecret
  238. if len(ss) > 0 && ss != "disable" {
  239. bridge.Provisioning = &ProvisioningAPI{bridge: bridge}
  240. }
  241. bridge.Log.Debugln("Initializing Matrix event processor")
  242. bridge.EventProcessor = appservice.NewEventProcessor(bridge.AS)
  243. bridge.Log.Debugln("Initializing Matrix event handler")
  244. bridge.MatrixHandler = NewMatrixHandler(bridge)
  245. bridge.Formatter = NewFormatter(bridge)
  246. bridge.Crypto = NewCryptoHelper(bridge)
  247. bridge.Metrics = NewMetricsHandler(bridge.Config.Metrics.Listen, bridge.Log.Sub("Metrics"), bridge.DB)
  248. store.BaseClientPayload.UserAgent.OsVersion = proto.String(WAVersion)
  249. store.BaseClientPayload.UserAgent.OsBuildNumber = proto.String(WAVersion)
  250. store.CompanionProps.Os = proto.String(bridge.Config.WhatsApp.OSName)
  251. store.CompanionProps.RequireFullSync = proto.Bool(bridge.Config.Bridge.HistorySync.RequestFullSync)
  252. versionParts := strings.Split(WAVersion, ".")
  253. if len(versionParts) > 2 {
  254. primary, _ := strconv.Atoi(versionParts[0])
  255. secondary, _ := strconv.Atoi(versionParts[1])
  256. tertiary, _ := strconv.Atoi(versionParts[2])
  257. store.CompanionProps.Version.Primary = proto.Uint32(uint32(primary))
  258. store.CompanionProps.Version.Secondary = proto.Uint32(uint32(secondary))
  259. store.CompanionProps.Version.Tertiary = proto.Uint32(uint32(tertiary))
  260. }
  261. platformID, ok := waProto.CompanionProps_CompanionPropsPlatformType_value[strings.ToUpper(bridge.Config.WhatsApp.BrowserName)]
  262. if ok {
  263. store.CompanionProps.PlatformType = waProto.CompanionProps_CompanionPropsPlatformType(platformID).Enum()
  264. }
  265. }
  266. func (bridge *Bridge) Start() {
  267. bridge.Log.Debugln("Running database upgrades")
  268. err := bridge.DB.Init()
  269. if err != nil && (!errors.Is(err, upgrades.ErrUnsupportedDatabaseVersion) || !*ignoreUnsupportedDatabase) {
  270. bridge.Log.Fatalln("Failed to initialize database:", err)
  271. if errors.Is(err, upgrades.ErrForeignTables) {
  272. bridge.Log.Infoln("You can use --ignore-foreign-tables to ignore this error")
  273. } else if errors.Is(err, upgrades.ErrNotOwned) {
  274. bridge.Log.Infoln("Sharing the same database with different programs is not supported")
  275. } else if errors.Is(err, upgrades.ErrUnsupportedDatabaseVersion) {
  276. bridge.Log.Infoln("Downgrading the bridge is not supported")
  277. }
  278. os.Exit(15)
  279. }
  280. bridge.Log.Debugln("Checking connection to homeserver")
  281. bridge.ensureConnection()
  282. if bridge.Crypto != nil {
  283. err = bridge.Crypto.Init()
  284. if err != nil {
  285. bridge.Log.Fatalln("Error initializing end-to-bridge encryption:", err)
  286. os.Exit(19)
  287. }
  288. }
  289. if bridge.Provisioning != nil {
  290. bridge.Log.Debugln("Initializing provisioning API")
  291. bridge.Provisioning.Init()
  292. }
  293. bridge.Log.Debugln("Starting application service HTTP server")
  294. go bridge.AS.Start()
  295. bridge.Log.Debugln("Starting event processor")
  296. go bridge.EventProcessor.Start()
  297. go bridge.CheckWhatsAppUpdate()
  298. go bridge.UpdateBotProfile()
  299. if bridge.Crypto != nil {
  300. go bridge.Crypto.Start()
  301. }
  302. go bridge.StartUsers()
  303. if bridge.Config.Metrics.Enabled {
  304. go bridge.Metrics.Start()
  305. }
  306. if bridge.Config.Bridge.ResendBridgeInfo {
  307. go bridge.ResendBridgeInfo()
  308. }
  309. go bridge.Loop()
  310. bridge.AS.Ready = true
  311. }
  312. func (bridge *Bridge) CheckWhatsAppUpdate() {
  313. bridge.Log.Debugfln("Checking for WhatsApp web update")
  314. resp, err := whatsmeow.CheckUpdate(http.DefaultClient)
  315. if err != nil {
  316. bridge.Log.Warnfln("Failed to check for WhatsApp web update: %v", err)
  317. return
  318. }
  319. if store.GetWAVersion() == resp.ParsedVersion {
  320. bridge.Log.Debugfln("Bridge is using latest WhatsApp web protocol")
  321. } else if store.GetWAVersion().LessThan(resp.ParsedVersion) {
  322. if resp.IsBelowHard || resp.IsBelowSoft || resp.IsBroken {
  323. bridge.Log.Warnfln("Bridge is using outdated WhatsApp web protocol and may no longer function (%s, latest is %s)", store.GetWAVersion(), resp.ParsedVersion)
  324. } else {
  325. bridge.Log.Debugfln("Bridge is using outdated WhatsApp web protocol (%s, latest is %s)", store.GetWAVersion(), resp.ParsedVersion)
  326. }
  327. } else {
  328. bridge.Log.Debugfln("Bridge is using newer than latest WhatsApp web protocol")
  329. }
  330. }
  331. func (bridge *Bridge) Loop() {
  332. for {
  333. bridge.SleepAndDeleteUpcoming()
  334. time.Sleep(1 * time.Hour)
  335. bridge.WarnUsersAboutDisconnection()
  336. }
  337. }
  338. func (bridge *Bridge) WarnUsersAboutDisconnection() {
  339. bridge.usersLock.Lock()
  340. for _, user := range bridge.usersByUsername {
  341. if user.IsConnected() && !user.PhoneRecentlySeen(true) {
  342. go user.sendPhoneOfflineWarning()
  343. }
  344. }
  345. bridge.usersLock.Unlock()
  346. }
  347. func (bridge *Bridge) ResendBridgeInfo() {
  348. if *dontSaveConfig {
  349. bridge.Log.Warnln("Not setting resend_bridge_info to false in config due to --no-update flag")
  350. } else {
  351. err := config.Mutate(*configPath, func(helper *config.UpgradeHelper) {
  352. helper.Set(config.Bool, "false", "bridge", "resend_bridge_info")
  353. })
  354. if err != nil {
  355. bridge.Log.Errorln("Failed to save config after setting resend_bridge_info to false:", err)
  356. }
  357. }
  358. bridge.Log.Infoln("Re-sending bridge info state event to all portals")
  359. for _, portal := range bridge.GetAllPortals() {
  360. portal.UpdateBridgeInfo()
  361. }
  362. bridge.Log.Infoln("Finished re-sending bridge info state events")
  363. }
  364. func (bridge *Bridge) UpdateBotProfile() {
  365. bridge.Log.Debugln("Updating bot profile")
  366. botConfig := &bridge.Config.AppService.Bot
  367. var err error
  368. var mxc id.ContentURI
  369. if botConfig.Avatar == "remove" {
  370. err = bridge.Bot.SetAvatarURL(mxc)
  371. } else if len(botConfig.Avatar) > 0 {
  372. mxc, err = id.ParseContentURI(botConfig.Avatar)
  373. if err == nil {
  374. err = bridge.Bot.SetAvatarURL(mxc)
  375. }
  376. botConfig.ParsedAvatar = mxc
  377. }
  378. if err != nil {
  379. bridge.Log.Warnln("Failed to update bot avatar:", err)
  380. }
  381. if botConfig.Displayname == "remove" {
  382. err = bridge.Bot.SetDisplayName("")
  383. } else if len(botConfig.Displayname) > 0 {
  384. err = bridge.Bot.SetDisplayName(botConfig.Displayname)
  385. }
  386. if err != nil {
  387. bridge.Log.Warnln("Failed to update bot displayname:", err)
  388. }
  389. }
  390. func (bridge *Bridge) StartUsers() {
  391. bridge.Log.Debugln("Starting users")
  392. foundAnySessions := false
  393. for _, user := range bridge.GetAllUsers() {
  394. if !user.JID.IsEmpty() {
  395. foundAnySessions = true
  396. }
  397. go user.Connect()
  398. }
  399. if !foundAnySessions {
  400. bridge.sendGlobalBridgeState(BridgeState{StateEvent: StateUnconfigured}.fill(nil))
  401. }
  402. bridge.Log.Debugln("Starting custom puppets")
  403. for _, loopuppet := range bridge.GetAllPuppetsWithCustomMXID() {
  404. go func(puppet *Puppet) {
  405. puppet.log.Debugln("Starting custom puppet", puppet.CustomMXID)
  406. err := puppet.StartCustomMXID(true)
  407. if err != nil {
  408. puppet.log.Errorln("Failed to start custom puppet:", err)
  409. }
  410. }(loopuppet)
  411. }
  412. }
  413. func (bridge *Bridge) Stop() {
  414. if bridge.Crypto != nil {
  415. bridge.Crypto.Stop()
  416. }
  417. bridge.AS.Stop()
  418. bridge.Metrics.Stop()
  419. bridge.EventProcessor.Stop()
  420. for _, user := range bridge.usersByUsername {
  421. if user.Client == nil {
  422. continue
  423. }
  424. bridge.Log.Debugln("Disconnecting", user.MXID)
  425. user.Client.Disconnect()
  426. close(user.historySyncs)
  427. }
  428. }
  429. func (bridge *Bridge) Main() {
  430. configData, upgraded, err := config.Upgrade(*configPath, !*dontSaveConfig)
  431. if err != nil {
  432. _, _ = fmt.Fprintln(os.Stderr, "Error updating config:", err)
  433. if configData == nil {
  434. os.Exit(10)
  435. }
  436. }
  437. bridge.Config, err = config.Load(configData, upgraded)
  438. if err != nil {
  439. _, _ = fmt.Fprintln(os.Stderr, "Failed to parse config:", err)
  440. os.Exit(10)
  441. }
  442. if *generateRegistration {
  443. bridge.GenerateRegistration()
  444. return
  445. } else if *migrateFrom {
  446. bridge.MigrateDatabase()
  447. return
  448. }
  449. bridge.Init()
  450. bridge.Log.Infoln("Bridge initialization complete, starting...")
  451. bridge.Start()
  452. bridge.Log.Infoln("Bridge started!")
  453. c := make(chan os.Signal)
  454. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  455. <-c
  456. bridge.Log.Infoln("Interrupt received, stopping...")
  457. bridge.Stop()
  458. bridge.Log.Infoln("Bridge stopped.")
  459. os.Exit(0)
  460. }
  461. func main() {
  462. flag.SetHelpTitles(
  463. "mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.",
  464. "mautrix-whatsapp [-h] [-c <path>] [-r <path>] [-g] [--migrate-db <source type> <source uri>]")
  465. err := flag.Parse()
  466. if err != nil {
  467. _, _ = fmt.Fprintln(os.Stderr, err)
  468. flag.PrintHelp()
  469. os.Exit(1)
  470. } else if *wantHelp {
  471. flag.PrintHelp()
  472. os.Exit(0)
  473. } else if *version {
  474. fmt.Println(VersionString)
  475. return
  476. }
  477. upgrades.IgnoreForeignTables = *ignoreForeignTables
  478. (&Bridge{
  479. usersByMXID: make(map[id.UserID]*User),
  480. usersByUsername: make(map[string]*User),
  481. spaceRooms: make(map[id.RoomID]*User),
  482. managementRooms: make(map[id.RoomID]*User),
  483. portalsByMXID: make(map[id.RoomID]*Portal),
  484. portalsByJID: make(map[database.PortalKey]*Portal),
  485. puppets: make(map[types.JID]*Puppet),
  486. puppetsByCustomMXID: make(map[id.UserID]*Puppet),
  487. }).Main()
  488. }