2022-06-15 14:49:31 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-03-01 11:53:17 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2022-06-15 14:49:31 +00:00
|
|
|
"testing"
|
2022-10-12 12:51:13 +00:00
|
|
|
"time"
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2023-03-01 11:53:17 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-15 14:49:31 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2023-03-01 11:53:17 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/images"
|
2023-10-13 12:25:34 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2023-03-21 12:22:46 +00:00
|
|
|
"github.com/status-im/status-go/server/servertest"
|
2022-06-15 14:49:31 +00:00
|
|
|
)
|
|
|
|
|
2022-10-12 12:51:13 +00:00
|
|
|
const (
|
2023-03-01 11:53:17 +00:00
|
|
|
waitTime = 50 * time.Millisecond
|
|
|
|
customPortForTests = 1337
|
|
|
|
defaultPortForTests = 80
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseURL = "https://127.0.0.1"
|
|
|
|
baseURLWithCustomPort = fmt.Sprintf("%s:%d", baseURL, customPortForTests)
|
|
|
|
baseURLWithDefaultPort = fmt.Sprintf("%s:%d", baseURL, defaultPortForTests)
|
2022-10-12 12:51:13 +00:00
|
|
|
)
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func TestServerURLSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ServerURLSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerURLSuite struct {
|
|
|
|
suite.Suite
|
2023-03-21 12:22:46 +00:00
|
|
|
servertest.TestKeyComponents
|
|
|
|
servertest.TestLoggerComponents
|
2022-06-15 14:49:31 +00:00
|
|
|
|
2022-09-27 22:59:02 +00:00
|
|
|
server *MediaServer
|
2023-03-01 11:53:17 +00:00
|
|
|
serverForQR *MediaServer
|
2022-09-27 22:59:02 +00:00
|
|
|
serverNoPort *MediaServer
|
2022-10-12 12:51:13 +00:00
|
|
|
testStart time.Time
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 12:51:13 +00:00
|
|
|
func (s *ServerURLSuite) SetupTest() {
|
2022-06-15 15:36:17 +00:00
|
|
|
s.SetupKeyComponents(s.T())
|
2022-10-21 15:19:46 +00:00
|
|
|
s.SetupLoggerComponents()
|
2022-06-15 15:36:17 +00:00
|
|
|
|
2023-03-01 11:53:17 +00:00
|
|
|
mediaServer, err := NewMediaServer(nil, nil, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.serverForQR = mediaServer
|
|
|
|
|
|
|
|
err = s.serverForQR.Start()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
s.server = &MediaServer{Server: Server{
|
2023-08-22 16:18:14 +00:00
|
|
|
hostname: LocalHostIP.String(),
|
2022-10-21 15:19:46 +00:00
|
|
|
portManger: newPortManager(s.Logger, nil),
|
2022-06-15 14:49:31 +00:00
|
|
|
}}
|
2023-03-01 11:53:17 +00:00
|
|
|
err = s.server.SetPort(customPortForTests)
|
2022-10-12 10:00:14 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort = &MediaServer{Server: Server{
|
2023-08-22 16:18:14 +00:00
|
|
|
hostname: LocalHostIP.String(),
|
2022-10-21 15:19:46 +00:00
|
|
|
portManger: newPortManager(s.Logger, nil),
|
2022-06-15 14:49:31 +00:00
|
|
|
}}
|
2022-10-12 12:51:13 +00:00
|
|
|
go func() {
|
|
|
|
time.Sleep(waitTime)
|
2023-03-01 11:53:17 +00:00
|
|
|
s.serverNoPort.port = defaultPortForTests
|
2022-10-12 12:51:13 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
s.testStart = time.Now()
|
|
|
|
}
|
|
|
|
|
2022-10-12 14:09:28 +00:00
|
|
|
// testNoPort takes two strings and compares expects them both to be equal
|
|
|
|
// then compares ServerURLSuite.testStart to the current time
|
|
|
|
// the difference must be greater than waitTime.
|
|
|
|
// This is caused by the ServerURLSuite.SetupTest waiting waitTime before unlocking the portWait sync.Mutex
|
2022-10-12 12:51:13 +00:00
|
|
|
func (s *ServerURLSuite) testNoPort(expected string, actual string) {
|
|
|
|
s.Require().Equal(expected, actual)
|
2023-01-13 17:12:46 +00:00
|
|
|
s.Require().Greater(time.Since(s.testStart), waitTime)
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeBaseURL() {
|
2023-03-01 11:53:17 +00:00
|
|
|
s.Require().Equal(baseURLWithCustomPort, s.server.MakeBaseURL().String())
|
|
|
|
s.testNoPort(baseURLWithDefaultPort, s.serverNoPort.MakeBaseURL().String())
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeImageServerURL() {
|
2023-03-01 11:53:17 +00:00
|
|
|
s.Require().Equal(baseURLWithCustomPort+"/messages/", s.server.MakeImageServerURL())
|
|
|
|
s.testNoPort(baseURLWithDefaultPort+"/messages/", s.serverNoPort.MakeImageServerURL())
|
2022-06-15 14:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeImageURL() {
|
|
|
|
s.Require().Equal(
|
2023-03-01 11:53:17 +00:00
|
|
|
baseURLWithCustomPort+"/messages/images?messageId=0x10aded70ffee",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.server.MakeImageURL("0x10aded70ffee"))
|
2023-02-15 08:14:00 +00:00
|
|
|
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2023-03-01 11:53:17 +00:00
|
|
|
baseURLWithDefaultPort+"/messages/images?messageId=0x10aded70ffee",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeImageURL("0x10aded70ffee"))
|
|
|
|
}
|
|
|
|
|
URL unfurling (initial implementation) (#3471)
This is the initial implementation for the new URL unfurling requirements. The
most important one is that only the message sender will pay the privacy cost for
unfurling and extracting metadata from websites. Once the message is sent, the
unfurled data will be stored at the protocol level and receivers will just
profit and happily decode the metadata to render it.
Further development of this URL unfurling capability will be mostly guided by
issues created on clients. For the moment in status-mobile:
https://github.com/status-im/status-mobile/labels/url-preview
- https://github.com/status-im/status-mobile/issues/15918
- https://github.com/status-im/status-mobile/issues/15917
- https://github.com/status-im/status-mobile/issues/15910
- https://github.com/status-im/status-mobile/issues/15909
- https://github.com/status-im/status-mobile/issues/15908
- https://github.com/status-im/status-mobile/issues/15906
- https://github.com/status-im/status-mobile/issues/15905
### Terminology
In the code, I've tried to stick to the word "unfurl URL" to really mean the
process of extracting metadata from a website, sort of lower level. I use "link
preview" to mean a higher level structure which is enriched by unfurled data.
"link preview" is also how designers refer to it.
### User flows
1. Carol needs to see link previews while typing in the chat input field. Notice
from the diagram nothing is persisted and that status-go endpoints are
essentially stateless.
```
#+begin_src plantuml :results verbatim
Client->>Server: Call wakuext_getTextURLs
Server-->>Client: Normalized URLs
Client->>Client: Render cached unfurled URLs
Client->>Server: Unfurl non-cached URLs.\nCall wakuext_unfurlURLs
Server->>Website: Fetch metadata
Website-->>Server: Metadata (thumbnail URL, title, etc)
Server->>Website: Fetch thumbnail
Server->>Website: Fetch favicon
Website-->>Server: Favicon bytes
Website-->>Server: Thumbnail bytes
Server->>Server: Decode & process images
Server-->>Client: Unfurled data (thumbnail data URI, etc)
#+end_src
```
```
,------. ,------. ,-------.
|Client| |Server| |Website|
`--+---' `--+---' `---+---'
| Call wakuext_getTextURLs | |
| ---------------------------------------> |
| | |
| Normalized URLs | |
| <- - - - - - - - - - - - - - - - - - - - |
| | |
|----. | |
| | Render cached unfurled URLs | |
|<---' | |
| | |
| Unfurl non-cached URLs. | |
| Call wakuext_unfurlURLs | |
| ---------------------------------------> |
| | |
| | Fetch metadata |
| | ------------------------------------>
| | |
| | Metadata (thumbnail URL, title, etc)|
| | <- - - - - - - - - - - - - - - - - -
| | |
| | Fetch thumbnail |
| | ------------------------------------>
| | |
| | Fetch favicon |
| | ------------------------------------>
| | |
| | Favicon bytes |
| | <- - - - - - - - - - - - - - - - - -
| | |
| | Thumbnail bytes |
| | <- - - - - - - - - - - - - - - - - -
| | |
| |----. |
| | | Decode & process images |
| |<---' |
| | |
| Unfurled data (thumbnail data URI, etc)| |
| <- - - - - - - - - - - - - - - - - - - - |
,--+---. ,--+---. ,---+---.
|Client| |Server| |Website|
`------' `------' `-------'
```
2. Carol sends the text message with link previews in the RPC request
wakuext_sendChatMessages. status-go assumes the link previews are good
because it can't and shouldn't attempt to re-unfurl them.
```
#+begin_src plantuml :results verbatim
Client->>Server: Call wakuext_sendChatMessages
Server->>Server: Transform link previews to\nbe proto-marshalled
Server->DB: Write link previews serialized as JSON
Server-->>Client: Updated message response
#+end_src
```
```
,------. ,------. ,--.
|Client| |Server| |DB|
`--+---' `--+---' `+-'
| Call wakuext_sendChatMessages| |
| -----------------------------> |
| | |
| |----. |
| | | Transform link previews to |
| |<---' be proto-marshalled |
| | |
| | |
| | Write link previews serialized as JSON|
| | -------------------------------------->
| | |
| Updated message response | |
| <- - - - - - - - - - - - - - - |
,--+---. ,--+---. ,+-.
|Client| |Server| |DB|
`------' `------' `--'
```
3. The message was sent over waku and persisted locally in Carol's device. She
should now see the link previews in the chat history. There can be many link
previews shared by other chat members, therefore it is important to serve the
assets via the media server to avoid overloading the ReactNative bridge with
lots of big JSON payloads containing base64 encoded data URIs (maybe this
concern is meaningless for desktop). When a client is rendering messages with
link previews, they will have the field linkPreviews, and the thumbnail URL
will point to the local media server.
```
#+begin_src plantuml :results verbatim
Client->>Server: GET /link-preview/thumbnail (media server)
Server->>DB: Read from user_messages.unfurled_links
Server->Server: Unmarshal JSON
Server-->>Client: HTTP Content-Type: image/jpeg/etc
#+end_src
```
```
,------. ,------. ,--.
|Client| |Server| |DB|
`--+---' `--+---' `+-'
| GET /link-preview/thumbnail (media server)| |
| ------------------------------------------> |
| | |
| | Read from user_messages.unfurled_links|
| | -------------------------------------->
| | |
| |----. |
| | | Unmarshal JSON |
| |<---' |
| | |
| HTTP Content-Type: image/jpeg/etc | |
| <- - - - - - - - - - - - - - - - - - - - - |
,--+---. ,--+---. ,+-.
|Client| |Server| |DB|
`------' `------' `--'
```
### Some limitations of the current implementation
The following points will become separate issues in status-go that I'll work on
over the next couple weeks. In no order of importance:
- Improve how multiple links are fetched; retries on failure and testing how
unfurling behaves around the timeout limits (deterministically, not by making
real HTTP calls as I did). https://github.com/status-im/status-go/issues/3498
- Unfurl favicons and store them in the protobuf too.
- For this PR, I added unfurling support only for websites with OpenGraph
https://ogp.me/ meta tags. Other unfurlers will be implemented on demand. The
next one will probably be for oEmbed https://oembed.com/, the protocol
supported by YouTube, for example.
- Resize and/or compress thumbnails (and favicons). Often times, thumbnails are
huge for the purposes of link previews. There is already support for
compressing JPEGs in status-go, but I prefer to work with compression in a
separate PR because I'd like to also solve the problem for PNGs (probably
convert them to JPEGs, plus compress them). This would be a safe choice for
thumbnails, favicons not so much because transparency is desirable.
- Editing messages is not yet supported.
- I haven't coded any artificial limit on the number of previews or on the size
of the thumbnail payload. This will be done in a separate issue. I have heard
the ideal solution may be to split messages into smaller chunks of ~125 KiB
because of libp2p, but that might be too complicated at this stage of the
product (?).
- Link preview deletion.
- For the moment, OpenGraph metadata is extracted by requesting data for the
English language (and fallback to whatever is available). In the future, we'll
want to unfurl by respecting the user's local device language. Some websites,
like GoDaddy, are already localized based on the device's IP, but many aren't.
- The website's description text should be limited by a certain number of
characters, especially because it's outside our control. Exactly how much has
not been decided yet, so it'll be done separately.
- URL normalization can be tricky, so I implemented only the basics to help with
caching. For example, the url https://status.im and HTTPS://status.im are
considered identical. Also, a URL is considered valid for unfurling if its TLD
exists according to publicsuffix.EffectiveTLDPlusOne. This was essential,
otherwise the default Go url.Parse approach would consider many invalid URLs
valid, and thus the server would waste resources trying to unfurl the
unfurleable.
### Other requirements
- If the message is edited, the link previews should reflect the edited text,
not the original one. This has been aligned with the design team as well.
- If the website's thumbnail or the favicon can't be fetched, just ignore them.
The only mandatory piece of metadata is the website's title and URL.
- Link previews in clients should be generated in near real-time, that is, as
the user types, previews are updated. In mobile this performs very well, and
it's what other clients like WhatsApp, Telegram, and Facebook do.
### Decisions
- While the user typing in the input field, the client is constantly (debounced)
asking status-go to parse the text and extract normalized URLs and then the
client checks if they're already in its in-memory cache. If they are, no RPC
call is made. I chose this approach to achieve the best possible performance
in mobile and avoid the whole RPC overhead, since the chat experience is
already not smooth enough. The mobile client uses URLs as cache keys in a
hashmap, i.e. if the key is present, it means the preview is readily available
(naive, but good enough for now). This decision also gave me more flexibility
to find the best UX at this stage of the feature.
- Due to the requirement that users should be able to see independent loading
indicators for each link preview, when status-go can't unfurl a URL, it
doesn't return it in the response.
- As an initial implementation, I added the BLOB column unfurled_links to the
user_messages table. The preview data is then serialized as JSON before being
stored in this column. I felt that creating a separate table and the related
code for this initial PR would be inconvenient. Is that reasonable to you?
Once things stabilize I can create a proper table if we want to avoid this
kind of solution with serialized columns.
2023-05-18 18:43:06 +00:00
|
|
|
func (s *ServerURLSuite) TestServer_MakeLinkPreviewThumbnailURL() {
|
|
|
|
s.Require().Equal(
|
|
|
|
baseURLWithCustomPort+"/link-preview/thumbnail?message-id=99&url=https%3A%2F%2Fgithub.com",
|
|
|
|
s.server.MakeLinkPreviewThumbnailURL("99", "https://github.com"))
|
|
|
|
|
|
|
|
s.testNoPort(
|
|
|
|
baseURLWithDefaultPort+"/link-preview/thumbnail?message-id=99&url=https%3A%2F%2Fgithub.com",
|
|
|
|
s.serverNoPort.MakeLinkPreviewThumbnailURL("99", "https://github.com"))
|
|
|
|
}
|
|
|
|
|
2023-10-13 12:25:34 +00:00
|
|
|
func (s *ServerURLSuite) TestServer_MakeStatusLinkPreviewThumbnailURL() {
|
|
|
|
s.Require().Equal(
|
|
|
|
baseURLWithCustomPort+"/status-link-preview/thumbnail?image-id=contact-icon&message-id=99&url=https%3A%2F%2Fstatus.app",
|
|
|
|
s.server.MakeStatusLinkPreviewThumbnailURL("99", "https://status.app", common.MediaServerContactIcon))
|
|
|
|
|
|
|
|
s.testNoPort(
|
|
|
|
baseURLWithDefaultPort+"/status-link-preview/thumbnail?image-id=contact-icon&message-id=99&url=https%3A%2F%2Fstatus.app",
|
|
|
|
s.serverNoPort.MakeStatusLinkPreviewThumbnailURL("99", "https://status.app", common.MediaServerContactIcon))
|
|
|
|
}
|
|
|
|
|
2022-06-15 14:49:31 +00:00
|
|
|
func (s *ServerURLSuite) TestServer_MakeAudioURL() {
|
|
|
|
s.Require().Equal(
|
2023-03-01 11:53:17 +00:00
|
|
|
baseURLWithCustomPort+"/messages/audio?messageId=0xde1e7ebee71e",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.server.MakeAudioURL("0xde1e7ebee71e"))
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2023-03-01 11:53:17 +00:00
|
|
|
baseURLWithDefaultPort+"/messages/audio?messageId=0xde1e7ebee71e",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeAudioURL("0xde1e7ebee71e"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerURLSuite) TestServer_MakeStickerURL() {
|
|
|
|
s.Require().Equal(
|
2023-03-01 11:53:17 +00:00
|
|
|
baseURLWithCustomPort+"/ipfs?hash=0xdeadbeef4ac0",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.server.MakeStickerURL("0xdeadbeef4ac0"))
|
2022-10-12 12:51:13 +00:00
|
|
|
s.testNoPort(
|
2023-03-01 11:53:17 +00:00
|
|
|
baseURLWithDefaultPort+"/ipfs?hash=0xdeadbeef4ac0",
|
2022-06-15 14:49:31 +00:00
|
|
|
s.serverNoPort.MakeStickerURL("0xdeadbeef4ac0"))
|
|
|
|
}
|
2023-03-01 11:53:17 +00:00
|
|
|
|
|
|
|
// TestQRCodeGeneration tests if we provide all the correct parameters to the media server
|
|
|
|
// do we get a valid QR code or not as part of the response payload.
|
|
|
|
// we have stored a generated QR code in tests folder, and we compare their bytes.
|
|
|
|
func (s *ServerURLSuite) TestQRCodeGeneration() {
|
|
|
|
|
|
|
|
qrURL := "https://github.com/status-im/status-go/pull/3154"
|
|
|
|
generatedURL := base64.StdEncoding.EncodeToString([]byte(qrURL))
|
|
|
|
generatedURL = s.serverForQR.MakeQRURL(generatedURL, "false", "2", "200", "", "")
|
|
|
|
|
|
|
|
u, err := url.Parse(generatedURL)
|
|
|
|
if err != nil {
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme == "" || u.Host == "" {
|
|
|
|
s.Require().Failf("generatedURL is not a valid URL: %s", generatedURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
serverCert := s.serverForQR.cert
|
|
|
|
serverCertBytes := serverCert.Certificate[0]
|
|
|
|
|
|
|
|
certPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: serverCertBytes})
|
|
|
|
|
|
|
|
rootCAs, err := x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = rootCAs.AppendCertsFromPEM(certPem)
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
2023-03-28 03:45:54 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
RootCAs: rootCAs,
|
2023-03-01 11:53:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{Transport: tr}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, generatedURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_ = resp.Body.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
s.Require().Failf("Unexpected response status code: %d", fmt.Sprint(resp.StatusCode))
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NotEmpty(payload)
|
|
|
|
|
|
|
|
expectedPayload, err := images.Asset("_assets/tests/qr/defaultQR.png")
|
|
|
|
require.Equal(s.T(), payload, expectedPayload)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
//(siddarthkay) un-comment code block below to generate the file in tests folder
|
|
|
|
//f, err := os.Create("image.png")
|
|
|
|
//if err != nil {
|
|
|
|
// s.Require().NoError(err)
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
//defer f.Close()
|
|
|
|
//_, err = f.Write(payload)
|
|
|
|
//
|
|
|
|
//if err != nil {
|
|
|
|
// s.Require().NoError(err)
|
|
|
|
//}
|
|
|
|
}
|