├── .editorconfig ├── .gitignore ├── .gitlab-ci.yml ├── Dockerfile ├── Dockerfile.ci ├── LICENSE ├── README.md ├── api.go ├── database.go ├── go.mod ├── go.sum ├── main.go ├── sendtxn.go ├── sync.go └── targets.go /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.{yaml,yml}] 12 | indent_style = space 13 | 14 | [*.md] 15 | max_line_length = 80 16 | 17 | [.gitlab-ci.yml] 18 | indent_size = 2 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.db 2 | -------------------------------------------------------------------------------- /.gitlab-ci.yml: -------------------------------------------------------------------------------- 1 | stages: 2 | - build 3 | - build docker 4 | - manifest 5 | 6 | .build: &build 7 | stage: build 8 | cache: 9 | paths: 10 | - .cache 11 | before_script: 12 | - mkdir -p .cache 13 | - export GOPATH="$CI_PROJECT_DIR/.cache" 14 | - export GOCACHE="$CI_PROJECT_DIR/.cache/build" 15 | - export GO_LDFLAGS="-linkmode external -extldflags -static" 16 | - export CGO_ENABLED=1 17 | script: 18 | - go build -ldflags "$GO_LDFLAGS" -o mautrix-syncproxy 19 | artifacts: 20 | paths: 21 | - mautrix-syncproxy 22 | 23 | build amd64: 24 | <<: *build 25 | image: dock.mau.dev/tulir/gomuks-build-docker:linux-amd64 26 | 27 | build arm64: 28 | <<: *build 29 | image: dock.mau.dev/tulir/gomuks-build-docker:linux-arm64 30 | 31 | build arm: 32 | <<: *build 33 | image: dock.mau.dev/tulir/gomuks-build-docker:linux-arm 34 | 35 | .build-docker: &build-docker 36 | image: docker:stable 37 | stage: build docker 38 | before_script: 39 | - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY 40 | script: 41 | - docker pull $CI_REGISTRY_IMAGE:latest || true 42 | - docker build --pull --cache-from $CI_REGISTRY_IMAGE:latest --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-$DOCKER_ARCH . --file Dockerfile.ci 43 | - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-$DOCKER_ARCH 44 | - docker rmi $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-$DOCKER_ARCH 45 | 46 | build docker amd64: 47 | <<: *build-docker 48 | tags: 49 | - amd64 50 | dependencies: 51 | - build amd64 52 | needs: 53 | - build amd64 54 | variables: 55 | DOCKER_ARCH: amd64 56 | 57 | build docker arm64: 58 | <<: *build-docker 59 | tags: 60 | - arm64 61 | dependencies: 62 | - build arm64 63 | needs: 64 | - build arm64 65 | variables: 66 | DOCKER_ARCH: arm64 67 | 68 | manifest: 69 | stage: manifest 70 | variables: 71 | GIT_STRATEGY: none 72 | before_script: 73 | - "mkdir -p $HOME/.docker && echo '{\"experimental\": \"enabled\"}' > $HOME/.docker/config.json" 74 | - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY 75 | needs: 76 | - build docker amd64 77 | - build docker arm64 78 | script: 79 | - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 80 | - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 81 | - if [ "$CI_COMMIT_BRANCH" = "master" ]; then docker manifest create $CI_REGISTRY_IMAGE:latest $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 && docker manifest push $CI_REGISTRY_IMAGE:latest; fi 82 | - if [ "$CI_COMMIT_BRANCH" != "master" ]; then docker manifest create $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 && docker manifest push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME; fi 83 | - docker rmi $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-amd64 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-arm64 84 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1-alpine AS builder 2 | 3 | COPY . /build 4 | WORKDIR /build 5 | RUN CGO_ENABLED=0 go build -o /usr/bin/mautrix-syncproxy 6 | 7 | FROM scratch 8 | 9 | COPY --from=builder /usr/bin/mautrix-syncproxy /usr/bin/mautrix-syncproxy 10 | 11 | ENV LISTEN_ADDRESS=:29332 12 | 13 | CMD ["/usr/bin/mautrix-syncproxy", "-config", "env"] 14 | -------------------------------------------------------------------------------- /Dockerfile.ci: -------------------------------------------------------------------------------- 1 | FROM scratch 2 | 3 | ARG EXECUTABLE=./mautrix-syncproxy 4 | COPY $EXECUTABLE /usr/bin/mautrix-syncproxy 5 | 6 | ENV LISTEN_ADDRESS=:29332 7 | 8 | CMD ["/usr/bin/mautrix-syncproxy", "-config", "env"] 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published by 637 | the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mautrix-syncproxy 2 | A "proxy" microservice that runs Matrix C-S `/sync` on the server and pushes 3 | to-device events and other encryption-related things through the usual 4 | appservice transaction system. 5 | 6 | This was designed for use with [mautrix-wsproxy] and the [android-sms] bridge 7 | to significantly reduce battery usage of the persistent foreground process 8 | (an idle websocket doesn't take much battery, but a HTTP request every 30 9 | seconds does). 10 | 11 | This partially implements [MSC3202] and the to-device part of [MSC2409]. 12 | 13 | [MSC2409]: https://github.com/matrix-org/matrix-doc/pull/2409 14 | [MSC3202]: https://github.com/matrix-org/matrix-doc/pull/3202 15 | [android-sms]: https://gitlab.com/beeper/android-sms 16 | [mautrix-wsproxy]: https://github.com/mautrix/wsproxy 17 | 18 | ## Setup 19 | You can download a prebuilt executable from [the CI] or [GitHub releases]. The 20 | executables are statically compiled and have no dependencies. Alternatively, 21 | you can build from source: 22 | 23 | [the CI]: https://mau.dev/mautrix/syncproxy/-/pipelines 24 | [GitHub releases]: https://github.com/mautrix/syncproxy/releases 25 | 26 | 0. Have [Go](https://golang.org/) 1.16 or higher installed. 27 | 1. Clone the repository (`git clone https://github.com/mautrix/syncproxy.git`). 28 | 2. Build with `go build -o mautrix-syncproxy`. The resulting executable will be 29 | in the current directory named `mautrix-syncproxy`. 30 | 31 | Configuring is done via environment variables. 32 | 33 | * `LISTEN_ADDRESS` - The address where to listen. 34 | * `HOMESERVER_URL` - The address to Synapse. If using workers, it is sufficient 35 | to have access to the `GET /sync` and `POST /user/{userId}/filter` endpoints. 36 | * `DATABASE_URL` - Database for storing sync tokens. SQLite and Postgres are 37 | supported: `sqlite:///mautrix-syncproxy.db` or `postgres://user:pass@host/db` 38 | * `SHARED_SECRET` - The shared secret for adding new sync targets. 39 | You should generate a random string here, e.g. `pwgen -snc 50 1` 40 | * `DEBUG` - If set, debug logs will be enabled. 41 | 42 | Since this is most useful with mautrix-wsproxy, the docker-compose instructions 43 | can be found in the [mautrix-wsproxy] readme. 44 | -------------------------------------------------------------------------------- /api.go: -------------------------------------------------------------------------------- 1 | // mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices. 2 | // Copyright (C) 2021 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 . 16 | 17 | package main 18 | 19 | import ( 20 | "encoding/json" 21 | "fmt" 22 | "net/http" 23 | "strings" 24 | 25 | "github.com/gorilla/mux" 26 | log "maunium.net/go/maulogger/v2" 27 | 28 | "maunium.net/go/mautrix/appservice" 29 | ) 30 | 31 | var ( 32 | errTargetNotFound = appservice.Error{ 33 | HTTPStatus: http.StatusNotFound, 34 | ErrorCode: "M_NOT_FOUND", 35 | Message: "No appservice found with that ID", 36 | } 37 | errTargetNotActive = appservice.Error{ 38 | HTTPStatus: http.StatusNotFound, 39 | ErrorCode: "FI.MAU.SYNCPROXY.NOT_ACTIVE", 40 | Message: "That appservice is not active", 41 | } 42 | errUpsertFailed = appservice.Error{ 43 | HTTPStatus: http.StatusInternalServerError, 44 | ErrorCode: "FI.MAU.SYNCPROXY.UPSERT_FAILED", 45 | Message: "Failed to insert appservice details into database", 46 | } 47 | ) 48 | 49 | func startSync(w http.ResponseWriter, r *http.Request) { 50 | if !checkAuth(w, r) { 51 | return 52 | } 53 | vars := mux.Vars(r) 54 | appserviceID := vars["appserviceID"] 55 | 56 | switch r.Method { 57 | case http.MethodPut: 58 | var req SyncTarget 59 | if !getJSON(w, r, &req) { 60 | return 61 | } 62 | log.Debugfln("Received PUT request for appservice %s (user: %s, device: %s, address: %s, proxy: %t)", req.AppserviceID, req.UserID, req.DeviceID, req.Address, req.IsProxy) 63 | req.AppserviceID = appserviceID 64 | target := GetOrSetTarget(appserviceID, &req) 65 | changed := true 66 | if target == nil { 67 | target = &req 68 | err := target.Init() 69 | if err != nil { 70 | target.log.Warnln("Failed to initialize new target:", err) 71 | appservice.Error{ 72 | HTTPStatus: http.StatusNotFound, 73 | ErrorCode: "FI.MAU.SYNCPROXY.INVALID_ADDRESS", 74 | Message: fmt.Sprintf("Failed to initialize target: %v", err), 75 | }.Write(w) 76 | return 77 | } 78 | } else if target.BotAccessToken != req.BotAccessToken || target.HSToken != req.HSToken || 79 | target.Address != req.Address || target.UserID != req.UserID || target.DeviceID != req.DeviceID { 80 | target.BotAccessToken = req.BotAccessToken 81 | target.HSToken = req.HSToken 82 | target.Address = req.Address 83 | target.UserID = req.UserID 84 | target.DeviceID = req.DeviceID 85 | if target.client != nil { 86 | target.client.AccessToken = target.BotAccessToken 87 | target.client.UserID = target.UserID 88 | target.client.DeviceID = target.DeviceID 89 | } 90 | } else { 91 | changed = false 92 | } 93 | if changed { 94 | target.log.Debugln("Upserting target for PUT request") 95 | err := target.Upsert() 96 | if err != nil { 97 | target.log.Warnln("Failed to upsert target:", err) 98 | errUpsertFailed.Write(w) 99 | return 100 | } 101 | } 102 | target.log.Debugln("Starting target for PUT request") 103 | go target.Start() 104 | appservice.WriteBlankOK(w) 105 | case http.MethodDelete: 106 | target := GetOrSetTarget(appserviceID, nil) 107 | if target == nil { 108 | log.Debugln("Client requested stopping unknown appservice", appserviceID) 109 | errTargetNotFound.Write(w) 110 | return 111 | } else if !target.Active { 112 | log.Debugln("Client requested stopping inactive appservice", appserviceID) 113 | errTargetNotActive.Write(w) 114 | return 115 | } 116 | target.Stop() 117 | target.log.Debugln("Waiting for syncing to stop") 118 | target.wg.Wait() 119 | target.log.Infoln("Target stopped after DELETE request") 120 | w.WriteHeader(http.StatusNoContent) 121 | default: 122 | w.WriteHeader(http.StatusMethodNotAllowed) 123 | } 124 | } 125 | 126 | func checkAuth(w http.ResponseWriter, r *http.Request) bool { 127 | var token string 128 | authHeader := r.Header.Get("Authorization") 129 | if !strings.HasPrefix(authHeader, "Bearer ") { 130 | token = r.URL.Query().Get("access_token") 131 | } else { 132 | token = authHeader[len("Bearer "):] 133 | } 134 | w.Header().Add("Content-Type", "application/json") 135 | if len(token) == 0 { 136 | appservice.Error{ 137 | HTTPStatus: http.StatusUnauthorized, 138 | ErrorCode: "M_MISSING_TOKEN", 139 | Message: "Missing authorization header", 140 | }.Write(w) 141 | return false 142 | } 143 | if token != cfg.SharedSecret { 144 | appservice.Error{ 145 | HTTPStatus: http.StatusUnauthorized, 146 | ErrorCode: "M_UNKNOWN_TOKEN", 147 | Message: "Unknown authorization token", 148 | }.Write(w) 149 | return false 150 | } 151 | return true 152 | } 153 | 154 | func getJSON(w http.ResponseWriter, r *http.Request, into interface{}) bool { 155 | err := json.NewDecoder(r.Body).Decode(&into) 156 | if err != nil { 157 | appservice.Error{ 158 | HTTPStatus: http.StatusBadRequest, 159 | ErrorCode: "M_BAD_JSON", 160 | Message: "Failed to decode request JSON", 161 | }.Write(w) 162 | return false 163 | } 164 | return true 165 | } 166 | -------------------------------------------------------------------------------- /database.go: -------------------------------------------------------------------------------- 1 | // mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices. 2 | // Copyright (C) 2021 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 . 16 | 17 | package main 18 | 19 | import ( 20 | "database/sql" 21 | "fmt" 22 | "net/url" 23 | "strings" 24 | 25 | _ "github.com/jackc/pgx/v4/stdlib" 26 | _ "github.com/mattn/go-sqlite3" 27 | 28 | log "maunium.net/go/maulogger/v2" 29 | ) 30 | 31 | type Database struct { 32 | conn *sql.DB 33 | scheme string 34 | } 35 | 36 | var knownSchemes = map[string]string{ 37 | "sqlite": "sqlite3", 38 | "sqlite3": "sqlite3", 39 | "postgres": "pgx", 40 | "postgresql": "pgx", 41 | "pgx": "pgx", 42 | } 43 | 44 | type DatabaseOpts struct { 45 | MaxOpenConns int `yaml:"max_open_conns"` 46 | MaxIdleConns int `yaml:"max_idle_conns"` 47 | } 48 | 49 | // Connect creates a new pgx connection pool. 50 | func Connect(dbURL string, opts DatabaseOpts) (*Database, error) { 51 | var localDB Database 52 | parsedURL, err := url.Parse(dbURL) 53 | if err != nil { 54 | return nil, err 55 | } 56 | var ok bool 57 | localDB.scheme, ok = knownSchemes[parsedURL.Scheme] 58 | if !ok { 59 | return nil, fmt.Errorf("unsupported database scheme '%s'", parsedURL.Scheme) 60 | } 61 | if localDB.scheme == "sqlite3" { 62 | newDBURL := strings.TrimPrefix(parsedURL.Path, "/") 63 | if len(newDBURL) == 0 { 64 | return nil, fmt.Errorf("invalid database URL '%s', missing a slash?", dbURL) 65 | } 66 | dbURL = newDBURL 67 | } 68 | localDB.conn, err = sql.Open(localDB.scheme, dbURL) 69 | localDB.conn.SetMaxOpenConns(opts.MaxOpenConns) 70 | localDB.conn.SetMaxIdleConns(opts.MaxIdleConns) 71 | return &localDB, err 72 | } 73 | 74 | type Upgrade struct { 75 | Message string 76 | Func func(conn *sql.Tx) error 77 | } 78 | 79 | var upgrades = []Upgrade{{ 80 | "Initial version", 81 | func(conn *sql.Tx) error { 82 | _, err := conn.Exec(` 83 | CREATE TABLE targets ( 84 | appservice_id TEXT PRIMARY KEY, 85 | bot_access_token TEXT NOT NULL, 86 | hs_token TEXT NOT NULL, 87 | address TEXT NOT NULL, 88 | user_id TEXT NOT NULL, 89 | device_id TEXT NOT NULL, 90 | is_proxy BOOLEAN NOT NULL, 91 | next_batch TEXT NOT NULL, 92 | active BOOLEAN DEFAULT false 93 | ); 94 | `) 95 | return err 96 | }, 97 | }} 98 | 99 | func setVersion(conn *sql.Tx, version int) error { 100 | _, err := conn.Exec("DELETE FROM version") 101 | if err != nil { 102 | return fmt.Errorf("failed to delete current version row: %w", err) 103 | } 104 | _, err = conn.Exec("INSERT INTO version VALUES ($1)", version) 105 | if err != nil { 106 | return fmt.Errorf("failed to insert new version row: %w", err) 107 | } 108 | return nil 109 | } 110 | 111 | // Upgrade updates the database schema to the latest version. 112 | func (db *Database) Upgrade() error { 113 | _, err := db.conn.Exec("CREATE TABLE IF NOT EXISTS version (version INTEGER PRIMARY KEY)") 114 | if err != nil { 115 | return fmt.Errorf("failed to ensure version table exists: %w", err) 116 | } 117 | var version int 118 | err = db.conn.QueryRow("SELECT version FROM version").Scan(&version) 119 | if err != nil && err != sql.ErrNoRows { 120 | return fmt.Errorf("failed to get current database schema version: %w", err) 121 | } 122 | 123 | if len(upgrades) > version { 124 | for index, upgrade := range upgrades[version:] { 125 | newVersion := version + index + 1 126 | log.Infofln("Updating database schema to v%d: %s", newVersion, upgrade.Message) 127 | var tx *sql.Tx 128 | if tx, err = db.conn.Begin(); err != nil { 129 | return fmt.Errorf("failed to begin transaction to upgrade database schema to v%d: %w", newVersion, err) 130 | } else if err = upgrade.Func(tx); err != nil { 131 | return fmt.Errorf("failed to upgrade database schema to v%d: %w", newVersion, err) 132 | } else if err = setVersion(tx, newVersion); err != nil { 133 | return fmt.Errorf("failed to store new version v%d in database: %w", newVersion, err) 134 | } else if err = tx.Commit(); err != nil { 135 | return fmt.Errorf("failed to commit upgrade of database schema to v%d: %w", newVersion, err) 136 | } 137 | } 138 | log.Infofln("Database schema update to v%d", len(upgrades)) 139 | } 140 | return nil 141 | } 142 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module go.mau.fi/mautrix-syncproxy 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/gorilla/mux v1.8.0 7 | github.com/jackc/pgx/v4 v4.13.0 8 | github.com/mattn/go-sqlite3 v1.14.8 9 | github.com/prometheus/client_golang v1.11.0 10 | maunium.net/go/maulogger/v2 v2.3.0 11 | maunium.net/go/mautrix v0.9.22 12 | ) 13 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= 2 | github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= 3 | github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= 4 | github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= 5 | github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= 6 | github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 7 | github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= 8 | github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 9 | github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= 10 | github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= 11 | github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= 12 | github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= 13 | github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= 14 | github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= 15 | github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= 16 | github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= 17 | github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= 18 | github.com/btcsuite/btcutil v1.0.2 h1:9iZ1Terx9fMIOtq1VrwdqfsATL9MC2l8ZrUY6YZ2uts= 19 | github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts= 20 | github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= 21 | github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= 22 | github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= 23 | github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= 24 | github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs= 25 | github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= 26 | github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= 27 | github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I= 28 | github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ= 29 | github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= 30 | github.com/coreos/go-systemd v0.0.0-20190719114852-fd7a80b32e1f/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= 31 | github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= 32 | github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 33 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 34 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 35 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 36 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 37 | github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 38 | github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= 39 | github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= 40 | github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= 41 | github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= 42 | github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= 43 | github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= 44 | github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw= 45 | github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= 46 | github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= 47 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 48 | github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 49 | github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 50 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 51 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 52 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 53 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 54 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 55 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 56 | github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= 57 | github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 58 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 59 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 60 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 61 | github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 62 | github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= 63 | github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 64 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 65 | github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= 66 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 67 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 68 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= 69 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 70 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 71 | github.com/jackc/chunkreader v1.0.0 h1:4s39bBR8ByfqH+DKm8rQA3E1LHZWB9XWcrz8fqaZbe0= 72 | github.com/jackc/chunkreader v1.0.0/go.mod h1:RT6O25fNZIuasFJRyZ4R/Y2BbhasbmZXF9QQ7T3kePo= 73 | github.com/jackc/chunkreader/v2 v2.0.0/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= 74 | github.com/jackc/chunkreader/v2 v2.0.1 h1:i+RDz65UE+mmpjTfyz0MoVTnzeYxroil2G82ki7MGG8= 75 | github.com/jackc/chunkreader/v2 v2.0.1/go.mod h1:odVSm741yZoC3dpHEUXIqA9tQRhFrgOHwnPIn9lDKlk= 76 | github.com/jackc/pgconn v0.0.0-20190420214824-7e0022ef6ba3/go.mod h1:jkELnwuX+w9qN5YIfX0fl88Ehu4XC3keFuOJJk9pcnA= 77 | github.com/jackc/pgconn v0.0.0-20190824142844-760dd75542eb/go.mod h1:lLjNuW/+OfW9/pnVKPazfWOgNfH2aPem8YQ7ilXGvJE= 78 | github.com/jackc/pgconn v0.0.0-20190831204454-2fabfa3c18b7/go.mod h1:ZJKsE/KZfsUgOEh9hBm+xYTstcNHg7UPMVJqRfQxq4s= 79 | github.com/jackc/pgconn v1.8.0/go.mod h1:1C2Pb36bGIP9QHGBYCjnyhqu7Rv3sGshaQUvmfGIB/o= 80 | github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8/2JY= 81 | github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= 82 | github.com/jackc/pgconn v1.10.0 h1:4EYhlDVEMsJ30nNj0mmgwIUXoq7e9sMJrVC2ED6QlCU= 83 | github.com/jackc/pgconn v1.10.0/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI= 84 | github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE= 85 | github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8= 86 | github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE= 87 | github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c= 88 | github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65 h1:DadwsjnMwFjfWc9y5Wi/+Zz7xoE5ALHsRQlOctkOiHc= 89 | github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak= 90 | github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= 91 | github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= 92 | github.com/jackc/pgproto3 v1.1.0 h1:FYYE4yRw+AgI8wXIinMlNjBbp/UitDJwfj5LqqewP1A= 93 | github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78= 94 | github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA= 95 | github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190609003834-432c2951c711/go.mod h1:uH0AWtUmuShn0bcesswc4aBTWGvw0cAxIJp+6OB//Wg= 96 | github.com/jackc/pgproto3/v2 v2.0.0-rc3/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= 97 | github.com/jackc/pgproto3/v2 v2.0.0-rc3.0.20190831210041-4c03ce451f29/go.mod h1:ryONWYqW6dqSg1Lw6vXNMXoBJhpzvWKnT95C46ckYeM= 98 | github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= 99 | github.com/jackc/pgproto3/v2 v2.1.1 h1:7PQ/4gLoqnl87ZxL7xjO0DR5gYuviDCZxQJsUlFW1eI= 100 | github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= 101 | github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= 102 | github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= 103 | github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= 104 | github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= 105 | github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= 106 | github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM= 107 | github.com/jackc/pgtype v1.8.1 h1:9k0IXtdJXHJbyAWQgbWr1lU+MEhPXZz6RIXxfR5oxXs= 108 | github.com/jackc/pgtype v1.8.1/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4= 109 | github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y= 110 | github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM= 111 | github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc= 112 | github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= 113 | github.com/jackc/pgx/v4 v4.13.0 h1:JCjhT5vmhMAf/YwBHLvrBn4OGdIQBiFG6ym8Zmdx570= 114 | github.com/jackc/pgx/v4 v4.13.0/go.mod h1:9P4X524sErlaxj0XSGZk7s+LD0eOyu1ZDUrrpznYDF0= 115 | github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= 116 | github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= 117 | github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= 118 | github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= 119 | github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= 120 | github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ= 121 | github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= 122 | github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 123 | github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 124 | github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= 125 | github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= 126 | github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= 127 | github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4= 128 | github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 129 | github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 130 | github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= 131 | github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= 132 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 133 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 134 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 135 | github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= 136 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 137 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 138 | github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 139 | github.com/lib/pq v1.1.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 140 | github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 141 | github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= 142 | github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8= 143 | github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= 144 | github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= 145 | github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= 146 | github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 147 | github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 148 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 149 | github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= 150 | github.com/mattn/go-sqlite3 v1.14.8 h1:gDp86IdQsN/xWjIEmr9MF6o9mpksUgh0fu+9ByFxzIU= 151 | github.com/mattn/go-sqlite3 v1.14.8/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= 152 | github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= 153 | github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= 154 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 155 | github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 156 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 157 | github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 158 | github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 159 | github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= 160 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 161 | github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 162 | github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= 163 | github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 164 | github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 165 | github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= 166 | github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 167 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 168 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 169 | github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= 170 | github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= 171 | github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= 172 | github.com/prometheus/client_golang v1.11.0 h1:HNkLOAEQMIDv/K+04rukrLx6ch7msSRwf3/SASFAGtQ= 173 | github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= 174 | github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= 175 | github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 176 | github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= 177 | github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= 178 | github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= 179 | github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= 180 | github.com/prometheus/common v0.26.0 h1:iMAkS2TDoNWnKM+Kopnx/8tnEStIfpYA0ur0xQzzhMQ= 181 | github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= 182 | github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= 183 | github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= 184 | github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= 185 | github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4= 186 | github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= 187 | github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= 188 | github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= 189 | github.com/rs/zerolog v1.13.0/go.mod h1:YbFCdg8HfsridGWAh22vktObvhZbQsZXe4/zB0OKkWU= 190 | github.com/rs/zerolog v1.15.0/go.mod h1:xYTKnLHcpfU2225ny5qZjxnj9NvkumZYjJHlAThCjNc= 191 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 192 | github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= 193 | github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24/go.mod h1:M+9NzErvs504Cn4c5DxATwIqPbtswREoFCre64PpcG4= 194 | github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= 195 | github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= 196 | github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= 197 | github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= 198 | github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= 199 | github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= 200 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 201 | github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 202 | github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= 203 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 204 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 205 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 206 | github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= 207 | github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 208 | github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= 209 | github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 210 | github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= 211 | github.com/tidwall/match v1.0.3/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= 212 | github.com/tidwall/pretty v1.0.2/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= 213 | github.com/tidwall/sjson v1.1.5/go.mod h1:VuJzsZnTowhSxWdOgsAnb886i4AjEyTkk7tNtsL7EYE= 214 | github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q= 215 | go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= 216 | go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= 217 | go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= 218 | go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= 219 | go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= 220 | go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4= 221 | go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= 222 | go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= 223 | go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= 224 | go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= 225 | go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM= 226 | golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 227 | golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= 228 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 229 | golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE= 230 | golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 231 | golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 232 | golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= 233 | golang.org/x/crypto v0.0.0-20200115085410-6d4e4cb37c7d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 234 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 235 | golang.org/x/crypto v0.0.0-20201203163018-be400aefbc4c/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= 236 | golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= 237 | golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 238 | golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= 239 | golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 240 | golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= 241 | golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= 242 | golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= 243 | golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 244 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 245 | golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 246 | golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 247 | golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 248 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 249 | golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 250 | golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 251 | golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 252 | golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= 253 | golang.org/x/net v0.0.0-20210220033124-5f55cee0dc0d/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 254 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= 255 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 256 | golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= 257 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 258 | golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 259 | golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 260 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 261 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 262 | golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 263 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 264 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 265 | golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 266 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 267 | golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 268 | golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 269 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 270 | golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 271 | golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 272 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 273 | golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 274 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 275 | golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 276 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 277 | golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 278 | golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 279 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 280 | golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 281 | golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 282 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= 283 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 284 | golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= 285 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 286 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 287 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 288 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 289 | golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 290 | golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= 291 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 292 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 293 | golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= 294 | golang.org/x/tools v0.0.0-20190425163242-31fd60d6bfdc/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= 295 | golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= 296 | golang.org/x/tools v0.0.0-20190823170909-c4a336ef6a2f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 297 | golang.org/x/tools v0.0.0-20191029041327-9cc4af7d6b2c/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 298 | golang.org/x/tools v0.0.0-20191029190741-b9c20aec41a5/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= 299 | golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= 300 | golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 301 | golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 302 | golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 303 | golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 304 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 305 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= 306 | golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 307 | google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= 308 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 309 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 310 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 311 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 312 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 313 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 314 | google.golang.org/protobuf v1.26.0-rc.1 h1:7QnIQpGRHE5RnLKnESfDoxm2dTapTZua5a0kS0A+VXQ= 315 | google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= 316 | gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= 317 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 318 | gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 319 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 320 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 321 | gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= 322 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 323 | gopkg.in/inconshreveable/log15.v2 v2.0.0-20180818164646-67afb5ed74ec/go.mod h1:aPpfJ7XW+gOuirDoZ8gHhLh3kZ1B08FtV2bbmy7Jv3s= 324 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 325 | gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 326 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 327 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 328 | gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 329 | gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= 330 | gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 331 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= 332 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 333 | honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= 334 | maunium.net/go/maulogger/v2 v2.2.4/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= 335 | maunium.net/go/maulogger/v2 v2.3.0 h1:TMCcO65fLk6+pJXo7sl38tzjzW0KBFgc6JWJMBJp4GE= 336 | maunium.net/go/maulogger/v2 v2.3.0/go.mod h1:TYWy7wKwz/tIXTpsx8G3mZseIRiC5DoMxSZazOHy68A= 337 | maunium.net/go/mautrix v0.9.22 h1:5O8brMUbZ1MRcAmJ9yYzRUj57hMEypcGok1pVOTsf8A= 338 | maunium.net/go/mautrix v0.9.22/go.mod h1:7IzKfWvpQtN+W2Lzxc0rLvIxFM3ryKX6Ys3S/ZoWbg8= 339 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | // mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices. 2 | // Copyright (C) 2021 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 . 16 | 17 | package main 18 | 19 | import ( 20 | "context" 21 | "net/http" 22 | "os" 23 | "os/signal" 24 | "strconv" 25 | "syscall" 26 | "time" 27 | 28 | "github.com/gorilla/mux" 29 | "github.com/prometheus/client_golang/prometheus/promhttp" 30 | log "maunium.net/go/maulogger/v2" 31 | ) 32 | 33 | type Config struct { 34 | ListenAddress string `yaml:"listen_address"` 35 | DatabaseURL string `yaml:"database_url"` 36 | HomeserverURL string `yaml:"homeserver_url"` 37 | SharedSecret string `yaml:"shared_secret"` 38 | ExpectSynchronous bool `yaml:"expect_synchronous"` 39 | Debug bool `yaml:"debug"` 40 | 41 | DatabaseOpts DatabaseOpts `yaml:"database_opts"` 42 | } 43 | 44 | var cfg Config 45 | var db *Database 46 | 47 | func getIntEnv(key string, defVal int) int { 48 | strVal, ok := os.LookupEnv(key) 49 | if !ok { 50 | return defVal 51 | } 52 | val, err := strconv.Atoi(strVal) 53 | if err != nil { 54 | return defVal 55 | } 56 | return val 57 | } 58 | 59 | func readConfig() { 60 | cfg.ListenAddress = os.Getenv("LISTEN_ADDRESS") 61 | cfg.DatabaseURL = os.Getenv("DATABASE_URL") 62 | cfg.DatabaseOpts.MaxOpenConns = getIntEnv("DATABASE_MAX_OPEN_CONNS", 4) 63 | cfg.DatabaseOpts.MaxIdleConns = getIntEnv("DATABASE_MAX_IDLE_CONNS", 2) 64 | cfg.HomeserverURL = os.Getenv("HOMESERVER_URL") 65 | cfg.SharedSecret = os.Getenv("SHARED_SECRET") 66 | cfg.ExpectSynchronous = len(os.Getenv("EXPECT_SYNCHRONOUS")) > 0 67 | cfg.Debug = len(os.Getenv("DEBUG")) > 0 68 | 69 | if len(cfg.ListenAddress) == 0 { 70 | log.Fatalln("LISTEN_ADDRESS environment variable is not set") 71 | } else if len(cfg.DatabaseURL) == 0 { 72 | log.Fatalln("DATABASE_URL environment variable is not set") 73 | } else if len(cfg.HomeserverURL) == 0 { 74 | log.Fatalln("HOMESERVER_URL environment variable is not set") 75 | } else if len(cfg.SharedSecret) == 0 { 76 | log.Fatalln("SHARED_SECRET environment variable is not set") 77 | } else { 78 | return 79 | } 80 | 81 | os.Exit(2) 82 | } 83 | 84 | func main() { 85 | log.DefaultLogger.TimeFormat = "Jan _2, 2006 15:04:05" 86 | readConfig() 87 | if cfg.Debug { 88 | log.DefaultLogger.PrintLevel = log.LevelDebug.Severity 89 | } 90 | if localDB, err := Connect(cfg.DatabaseURL, cfg.DatabaseOpts); err != nil { 91 | log.Fatalln("Failed to connect to database:", err) 92 | os.Exit(3) 93 | } else { 94 | db = localDB 95 | } 96 | 97 | if err := db.Upgrade(); err != nil { 98 | log.Fatalln("Failed to upgrade database:", err) 99 | os.Exit(4) 100 | } else if err = LoadTargets(); err != nil { 101 | log.Fatalln("Failed to load old targets from database:", err) 102 | os.Exit(5) 103 | } 104 | 105 | log.Infoln("Starting old active targets") 106 | startedCount := 0 107 | for _, target := range targets { 108 | if target.Active { 109 | go target.Start() 110 | startedCount += 1 111 | } 112 | } 113 | log.Infofln("Started %d active targets out of %d total old targets", startedCount, len(targets)) 114 | 115 | router := mux.NewRouter() 116 | router.HandleFunc("/_matrix/client/unstable/fi.mau.syncproxy/{appserviceID}", startSync).Methods(http.MethodPut, http.MethodDelete) 117 | router.Handle("/metrics", promhttp.Handler()) 118 | server := &http.Server{ 119 | Addr: cfg.ListenAddress, 120 | Handler: router, 121 | } 122 | go func() { 123 | log.Infoln("Starting to listen on", cfg.ListenAddress) 124 | err := server.ListenAndServe() 125 | if err != nil && err != http.ErrServerClosed { 126 | log.Fatalln("Error in listener:", err) 127 | os.Exit(6) 128 | } 129 | }() 130 | 131 | c := make(chan os.Signal) 132 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) 133 | <-c 134 | 135 | ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 136 | defer cancel() 137 | if err := server.Shutdown(ctx); err != nil { 138 | log.Errorln("Failed to close server:", err) 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /sendtxn.go: -------------------------------------------------------------------------------- 1 | // mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices. 2 | // Copyright (C) 2021 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 . 16 | 17 | package main 18 | 19 | import ( 20 | "bytes" 21 | "context" 22 | "encoding/json" 23 | "errors" 24 | "fmt" 25 | "io" 26 | "net/http" 27 | "net/url" 28 | "sync/atomic" 29 | "time" 30 | 31 | "maunium.net/go/maulogger/v2" 32 | 33 | "maunium.net/go/mautrix" 34 | "maunium.net/go/mautrix/appservice" 35 | ) 36 | 37 | const txnIDFormat = "fi.mau.syncproxy_%d_%d" 38 | const wrapperTxnIDFormat = "fi.mau.syncproxy.wrapper_%d_%d" 39 | 40 | const initialTransactionRetrySleep = 2 * time.Second 41 | const maxTransactionRetryInterval = 120 * time.Second 42 | 43 | var errFiMauWsNotConnected = mautrix.RespError{ErrCode: "FI.MAU.WS_NOT_CONNECTED"} 44 | var errWebsocketNotConnected = fmt.Errorf("server said the transaction websocket is not connected") 45 | 46 | type SendStatus string 47 | 48 | const ( 49 | SendStatusOK SendStatus = "ok" 50 | SendStatusWebsocketNotConnected SendStatus = "websocket-not-connected" 51 | ) 52 | 53 | type transactionRequest struct { 54 | *appservice.Transaction 55 | WrappedTxnID string `json:"fi.mau.syncproxy.transaction_id,omitempty"` 56 | SynchronousTo []string `json:"com.beeper.asmux.synchronous_to,omitempty"` 57 | } 58 | 59 | type ProxyError string 60 | 61 | const ( 62 | ProxyErrorLoggedOut ProxyError = "FI.MAU.CLIENT_LOGGED_OUT" 63 | ProxyErrorUnknown ProxyError = "M_UNKNOWN" 64 | ) 65 | 66 | type errorRequest struct { 67 | Error ProxyError `json:"errcode"` 68 | Message string `json:"error"` 69 | WrappedTxnID string `json:"fi.mau.syncproxy.transaction_id,omitempty"` 70 | } 71 | 72 | type transactionResponse struct { 73 | Synchronous bool `json:"com.beeper.asmux.synchronous"` 74 | SentTo map[string]SendStatus `json:"com.beeper.asmux.sent_to,omitempty"` 75 | } 76 | 77 | var lastTxnID uint64 78 | 79 | func nextTxnID(format string) (uint64, string) { 80 | txnIDCounter := atomic.AddUint64(&lastTxnID, 1) 81 | return txnIDCounter, fmt.Sprintf(format, 82 | time.Now().UnixNano(), 83 | txnIDCounter) 84 | } 85 | 86 | func (target *SyncTarget) tryPostTransaction(ctx context.Context, txn *appservice.Transaction, error *errorRequest) error { 87 | counter, txnID := nextTxnID(txnIDFormat) 88 | txnLog := ctx.Value(logContextKey).(maulogger.Logger).Sub(fmt.Sprintf("Txn-%d", counter)) 89 | ctx = context.WithValue(ctx, logContextKey, txnLog) 90 | 91 | if txn != nil { 92 | deviceListChanges := 0 93 | if txn.DeviceLists != nil { 94 | deviceListChanges = len(txn.DeviceLists.Changed) 95 | } 96 | txnLog.Debugfln("Sending %d to-device events, %d device list changes and %d OTK counts to %s in transaction %s", 97 | len(txn.EphemeralEvents), deviceListChanges, len(txn.DeviceOTKCount), target.AppserviceID, txnID) 98 | } else { 99 | txnLog.Debugfln("Sending error '%s' to %s in transaction %s", error.Error, target.AppserviceID, txnID) 100 | } 101 | 102 | retryIn := initialTransactionRetrySleep 103 | attemptNo := 1 104 | for { 105 | err := target.postTransaction(ctx, txn, error, txnID, attemptNo) 106 | attemptNo += 1 107 | if err == nil { 108 | return nil 109 | } else if ctx.Err() != nil { 110 | if err != ctx.Err() { 111 | txnLog.Debugfln("Sending transaction %s returned error %v, but context had different error %v", txnID, err, ctx.Err()) 112 | } 113 | return ctx.Err() 114 | } else if errors.Is(err, errWebsocketNotConnected) { 115 | // Assume that the server will ask as to restart syncing when the websocket does connect again. 116 | return err 117 | } 118 | 119 | txnLog.Warnfln("Failed to send transaction %s: %v. Retrying in %v", txnID, err, retryIn) 120 | select { 121 | case <-time.After(retryIn): 122 | case <-ctx.Done(): 123 | txnLog.Debugfln("Context returned error while waiting to retry transaction %s", txnID) 124 | return ctx.Err() 125 | } 126 | retryIn *= 2 127 | if retryIn > maxTransactionRetryInterval { 128 | retryIn = maxTransactionRetryInterval 129 | } 130 | } 131 | } 132 | 133 | func createTxnURL(address, appserviceID, txnID string, isError bool) (string, error) { 134 | parsedURL, err := url.Parse(address) 135 | if err != nil { 136 | return "", fmt.Errorf("failed to parse target URL: %w", err) 137 | } 138 | if isError { 139 | parsedURL.Path = fmt.Sprintf("/_matrix/app/unstable/fi.mau.syncproxy/error/%s", txnID) 140 | } else { 141 | parsedURL.Path = fmt.Sprintf("/_matrix/app/v1/transactions/%s", txnID) 142 | } 143 | q := parsedURL.Query() 144 | q.Add("appservice_id", appserviceID) 145 | parsedURL.RawQuery = q.Encode() 146 | return parsedURL.String(), nil 147 | } 148 | 149 | func closeBody(body io.ReadCloser) { 150 | _ = body.Close() 151 | } 152 | 153 | func (target *SyncTarget) postTransaction(ctx context.Context, txn *appservice.Transaction, error *errorRequest, txnID string, attemptNo int) error { 154 | txnLog := ctx.Value(logContextKey).(maulogger.Logger) 155 | var buf bytes.Buffer 156 | var req *http.Request 157 | var resp *http.Response 158 | var respData transactionResponse 159 | var txnData interface{} 160 | if txn != nil { 161 | txnData = &transactionRequest{ 162 | Transaction: txn, 163 | WrappedTxnID: txnID, 164 | SynchronousTo: []string{target.AppserviceID}, 165 | } 166 | } else { 167 | error.WrappedTxnID = txnID 168 | txnData = error 169 | } 170 | 171 | pathTxnID := txnID 172 | if target.IsProxy { 173 | _, pathTxnID = nextTxnID(wrapperTxnIDFormat) 174 | } 175 | txnLog.Debugfln("Attempt #%d for transaction %s (path: %s)", attemptNo, txnID, pathTxnID) 176 | 177 | if txnURL, err := createTxnURL(target.Address, target.AppserviceID, pathTxnID, error != nil); err != nil { 178 | return fmt.Errorf("failed to form transaction URL: %w", err) 179 | } else if err = json.NewEncoder(&buf).Encode(txnData); err != nil { 180 | return fmt.Errorf("failed to encode transaction JSON: %w", err) 181 | } else if req, err = http.NewRequestWithContext(ctx, http.MethodPut, txnURL, &buf); err != nil { 182 | return fmt.Errorf("failed to create request: %w", err) 183 | } else if req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", target.HSToken)); len(target.HSToken) == 0 { 184 | return fmt.Errorf("target is missing hs_token") 185 | } else if resp, err = http.DefaultClient.Do(req); err != nil { 186 | return fmt.Errorf("failed to send transaction: %w", err) 187 | } 188 | defer closeBody(resp.Body) 189 | if resp.StatusCode >= 300 || resp.StatusCode < 200 { 190 | var respErr mautrix.RespError 191 | if err := json.NewDecoder(resp.Body).Decode(&respErr); err != nil { 192 | return fmt.Errorf("transaction returned HTTP %d and non-JSON body", resp.StatusCode) 193 | } else if errors.Is(respErr, errFiMauWsNotConnected) { 194 | return errWebsocketNotConnected 195 | } else { 196 | return fmt.Errorf("transaction returned HTTP %d: %w", resp.StatusCode, err) 197 | } 198 | } else if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil { 199 | return fmt.Errorf("transaction returned HTTP %d, but had non-JSON body: %v", resp.StatusCode, err) 200 | } else if !respData.Synchronous && cfg.ExpectSynchronous { 201 | return fmt.Errorf("transaction returned HTTP %d, but EXPECT_SYNCHRONOUS is set and server didn't confirm support for synchronous delivery", resp.StatusCode) 202 | } else if respData.Synchronous && respData.SentTo == nil { 203 | return fmt.Errorf("transaction returned HTTP %d, but synchronous delivery confirmation was missing `com.beeper.asmux.sent_to` field", resp.StatusCode) 204 | } else if respData.Synchronous { 205 | status, ok := respData.SentTo[target.AppserviceID] 206 | if status == SendStatusOK { 207 | txnLog.Debugfln("Successfully sent transaction %s with synchronous delivery confirmation for %s on attempt #%d", txnID, target.AppserviceID, attemptNo) 208 | return nil 209 | } else if status == SendStatusWebsocketNotConnected { 210 | return errWebsocketNotConnected 211 | } else if ok { 212 | return fmt.Errorf("transaction returned HTTP %d, but server said it didn't reach the appservice (status %s)", resp.StatusCode, status) 213 | } else { 214 | return fmt.Errorf("transaction returned HTTP %d, but server didn't confirm synchronous delivery", resp.StatusCode) 215 | } 216 | } else { 217 | txnLog.Debugfln("Successfully sent transaction %s on attempt #%d", txnID, attemptNo) 218 | return nil 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /sync.go: -------------------------------------------------------------------------------- 1 | // mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices. 2 | // Copyright (C) 2021 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 . 16 | 17 | package main 18 | 19 | import ( 20 | "context" 21 | "errors" 22 | "fmt" 23 | "time" 24 | 25 | "maunium.net/go/maulogger/v2" 26 | 27 | "maunium.net/go/mautrix" 28 | "maunium.net/go/mautrix/appservice" 29 | "maunium.net/go/mautrix/event" 30 | "maunium.net/go/mautrix/id" 31 | ) 32 | 33 | var everything = []event.Type{{Type: "*"}} 34 | var nothing = mautrix.FilterPart{NotTypes: everything} 35 | var syncFilter = &mautrix.Filter{ 36 | Presence: nothing, 37 | AccountData: nothing, 38 | Room: mautrix.RoomFilter{ 39 | IncludeLeave: false, 40 | Ephemeral: nothing, 41 | AccountData: nothing, 42 | State: nothing, 43 | Timeline: nothing, 44 | }, 45 | } 46 | 47 | const initialSyncRetrySleep = 2 * time.Second 48 | const maxSyncRetryInterval = 120 * time.Second 49 | 50 | func (target *SyncTarget) sync(ctx context.Context) error { 51 | var filterID string 52 | if resp, err := target.client.CreateFilter(syncFilter); err != nil { 53 | return fmt.Errorf("failed to create filter: %w", err) 54 | } else { 55 | filterID = resp.FilterID 56 | } 57 | 58 | var otkCountSent bool 59 | var prevOTKCount mautrix.OTKCount 60 | syncLog := ctx.Value(logContextKey).(maulogger.Logger) 61 | retryIn := initialSyncRetrySleep 62 | 63 | for { 64 | resp, err := target.client.SyncRequest(30000, target.NextBatch, filterID, false, event.PresenceOffline, ctx) 65 | if err != nil { 66 | if errors.Is(err, mautrix.MUnknownToken) { 67 | return err 68 | } else if ctx.Err() != nil { 69 | if err != ctx.Err() { 70 | syncLog.Debugfln("Sync returned error %v, but context had different error %v", err, ctx.Err()) 71 | } 72 | return ctx.Err() 73 | } 74 | syncLog.Warnfln("Error syncing: %v. Retrying in %v", err, retryIn) 75 | select { 76 | case <-time.After(retryIn): 77 | case <-ctx.Done(): 78 | syncLog.Debugfln("Context returned error while waiting to retry sync") 79 | return ctx.Err() 80 | } 81 | retryIn *= 2 82 | if retryIn > maxSyncRetryInterval { 83 | retryIn = maxSyncRetryInterval 84 | } 85 | continue 86 | } 87 | retryIn = initialTransactionRetrySleep 88 | if len(resp.ToDevice.Events) > 0 || resp.DeviceOTKCount != prevOTKCount || !otkCountSent || len(resp.DeviceLists.Changed) > 0 { 89 | txn := syncToTransaction(resp, target.UserID, target.DeviceID, resp.DeviceOTKCount != prevOTKCount || !otkCountSent) 90 | prevOTKCount = resp.DeviceOTKCount 91 | otkCountSent = true 92 | err = target.tryPostTransaction(ctx, txn, nil) 93 | if err != nil { 94 | return fmt.Errorf("error sending transaction: %w", err) 95 | } 96 | } 97 | syncLog.Debugln("Storing new next batch token:", resp.NextBatch) 98 | err = target.SetNextBatch(resp.NextBatch) 99 | if err != nil { 100 | syncLog.Warnln("Failed to store next batch in database:", err) 101 | } 102 | } 103 | } 104 | 105 | func syncToTransaction(resp *mautrix.RespSync, userID id.UserID, deviceID id.DeviceID, sendOTKs bool) *appservice.Transaction { 106 | var txn appservice.Transaction 107 | if resp != nil { 108 | if len(resp.ToDevice.Events) > 0 { 109 | txn.EphemeralEvents = resp.ToDevice.Events 110 | txn.MSC2409EphemeralEvents = txn.EphemeralEvents 111 | for _, evt := range txn.EphemeralEvents { 112 | evt.ToUserID = userID 113 | evt.ToDeviceID = deviceID 114 | } 115 | } 116 | if len(resp.DeviceLists.Changed) > 0 || len(resp.DeviceLists.Left) > 0 { 117 | txn.DeviceLists = &resp.DeviceLists 118 | txn.MSC3202DeviceLists = txn.DeviceLists 119 | } 120 | if sendOTKs { 121 | txn.DeviceOTKCount = map[id.UserID]mautrix.OTKCount{ 122 | userID: resp.DeviceOTKCount, 123 | } 124 | txn.MSC3202DeviceOTKCount = txn.DeviceOTKCount 125 | } 126 | } 127 | return &txn 128 | } 129 | -------------------------------------------------------------------------------- /targets.go: -------------------------------------------------------------------------------- 1 | // mautrix-syncproxy - A /sync proxy for encrypted Matrix appservices. 2 | // Copyright (C) 2021 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 . 16 | 17 | package main 18 | 19 | import ( 20 | "context" 21 | "errors" 22 | "fmt" 23 | "runtime/debug" 24 | "sync" 25 | "sync/atomic" 26 | 27 | log "maunium.net/go/maulogger/v2" 28 | 29 | "maunium.net/go/mautrix" 30 | "maunium.net/go/mautrix/id" 31 | ) 32 | 33 | var targets = make(map[string]*SyncTarget) 34 | var targetLock sync.Mutex 35 | 36 | type SyncTarget struct { 37 | AppserviceID string `json:"appservice_id"` 38 | BotAccessToken string `json:"bot_access_token"` 39 | HSToken string `json:"hs_token"` 40 | Address string `json:"address"` 41 | UserID id.UserID `json:"user_id"` 42 | DeviceID id.DeviceID `json:"device_id"` 43 | IsProxy bool `json:"is_proxy"` 44 | 45 | NextBatch string `json:"-"` 46 | Active bool `json:"-"` 47 | 48 | client *mautrix.Client 49 | log log.Logger 50 | running bool 51 | cancel func() 52 | wg sync.WaitGroup 53 | lock sync.Mutex 54 | } 55 | 56 | func (target *SyncTarget) Upsert() error { 57 | query := ` 58 | INSERT INTO targets (appservice_id, bot_access_token, hs_token, address, user_id, device_id, is_proxy, next_batch, active) 59 | VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) 60 | ON CONFLICT (appservice_id) DO UPDATE 61 | SET bot_access_token=$2, hs_token=$3, address=$4, user_id=$5, device_id=$6, is_proxy=$7 62 | ` 63 | if db.scheme == "sqlite3" { 64 | query = "INSERT OR REPLACE INTO targets (appservice_id, bot_access_token, hs_token, address, user_id, device_id, is_proxy, next_batch, active)" 65 | } 66 | _, err := db.conn.Exec(query, target.AppserviceID, target.BotAccessToken, target.HSToken, target.Address, target.UserID, target.DeviceID, target.IsProxy, target.NextBatch, target.Active) 67 | return err 68 | } 69 | 70 | func (target *SyncTarget) SetActive(active bool) error { 71 | if target.Active == active { 72 | return nil 73 | } 74 | target.Active = active 75 | _, err := db.conn.Exec("UPDATE targets SET active=$2 WHERE appservice_id=$1", target.AppserviceID, target.Active) 76 | return err 77 | } 78 | 79 | func (target *SyncTarget) SetNextBatch(nextBatch string) error { 80 | if target.NextBatch == nextBatch { 81 | return nil 82 | } 83 | target.NextBatch = nextBatch 84 | _, err := db.conn.Exec("UPDATE targets SET next_batch=$2 WHERE appservice_id=$1", target.AppserviceID, target.NextBatch) 85 | return err 86 | } 87 | 88 | func GetOrSetTarget(appserviceID string, newTarget *SyncTarget) *SyncTarget { 89 | targetLock.Lock() 90 | defer targetLock.Unlock() 91 | target, ok := targets[appserviceID] 92 | if !ok { 93 | if newTarget != nil { 94 | targets[appserviceID] = newTarget 95 | } 96 | return nil 97 | } 98 | return target 99 | } 100 | 101 | func LoadTargets() error { 102 | res, err := db.conn.Query("SELECT appservice_id, bot_access_token, hs_token, address, is_proxy, user_id, device_id, active FROM targets") 103 | if err != nil { 104 | return fmt.Errorf("failed to query targets: %w", err) 105 | } 106 | targetLock.Lock() 107 | defer targetLock.Unlock() 108 | for res.Next() { 109 | var target SyncTarget 110 | err = res.Scan(&target.AppserviceID, &target.BotAccessToken, &target.HSToken, &target.Address, &target.IsProxy, &target.UserID, &target.DeviceID, &target.Active) 111 | if err != nil { 112 | return fmt.Errorf("failed to scan target: %w", err) 113 | } 114 | err = target.Init() 115 | if err != nil { 116 | target.log.Warnln("Failed to initialize target (startup):", err) 117 | } else { 118 | targets[target.AppserviceID] = &target 119 | } 120 | } 121 | return nil 122 | } 123 | 124 | var globalSyncID uint64 125 | 126 | const logContextKey = "log" 127 | 128 | func (target *SyncTarget) Init() error { 129 | target.log = log.Sub(fmt.Sprintf("Target-%s", target.AppserviceID)) 130 | var err error 131 | target.client, err = mautrix.NewClient(cfg.HomeserverURL, target.UserID, target.BotAccessToken) 132 | if err != nil { 133 | return fmt.Errorf("failed to create client: %w", err) 134 | } 135 | return nil 136 | } 137 | 138 | func (target *SyncTarget) Start() { 139 | syncLog := target.log.Sub(fmt.Sprintf("Sync-%d", atomic.AddUint64(&globalSyncID, 1))) 140 | if target.running { 141 | syncLog.Debugln("There seems to be an existing syncer running, stopping it first") 142 | target.Stop() 143 | } 144 | 145 | syncLog.Debugln("Locking mutex to start syncing") 146 | target.lock.Lock() 147 | target.wg = sync.WaitGroup{} 148 | target.wg.Add(1) 149 | target.running = true 150 | 151 | defer func() { 152 | target.running = false 153 | target.cancel = nil 154 | target.wg.Done() 155 | syncLog.Debugln("Unlocking mutex") 156 | target.lock.Unlock() 157 | err := recover() 158 | if err != nil { 159 | syncLog.Errorfln("Syncing panicked: %v\n%s", err, debug.Stack()) 160 | } 161 | }() 162 | 163 | if err := target.SetActive(true); err != nil { 164 | syncLog.Warnln("Failed to mark target as active:", err) 165 | } 166 | defer func() { 167 | if err := target.SetActive(false); err != nil { 168 | syncLog.Warnln("Failed to mark target as inactive:", err) 169 | } 170 | }() 171 | 172 | ctx, cancelFunc := context.WithCancel(context.WithValue(context.Background(), logContextKey, syncLog)) 173 | target.cancel = cancelFunc 174 | 175 | syncLog.Infoln("Starting syncing") 176 | err := target.sync(ctx) 177 | if errors.Is(err, context.Canceled) { 178 | syncLog.Infoln("Syncing stopped") 179 | } else if err != nil { 180 | syncLog.Errorfln("Syncing failed: %v, notifying target...", err) 181 | proxyErr := &errorRequest{ 182 | Error: ProxyErrorUnknown, 183 | Message: err.Error(), 184 | } 185 | if errors.Is(err, mautrix.MUnknownToken) { 186 | proxyErr.Error = ProxyErrorLoggedOut 187 | } 188 | err = target.tryPostTransaction(ctx, nil, proxyErr) 189 | if err != nil { 190 | syncLog.Warnln("Failed to notify target about sync error:", err) 191 | } 192 | } 193 | } 194 | 195 | func (target *SyncTarget) Stop() { 196 | if cancelFn := target.cancel; cancelFn != nil { 197 | target.log.Debugln("Stopping syncing...") 198 | cancelFn() 199 | } 200 | } 201 | --------------------------------------------------------------------------------