@@ -118,6 +118,16 @@ struct PushFramesResult {
118118 baton : Option < String > ,
119119}
120120
121+ #[ derive( Debug , Clone ) ]
122+ pub struct EncryptionContext {
123+ /// The base64-encoded key for the encryption, sent on every request.
124+ pub key_16_bytes_base64_encoded : String ,
125+ /// Whether the pushed frames are already encrypted.
126+ pub push_is_encrypted : bool ,
127+ /// Whether to request the server to decrypt the pulled frames.
128+ pub decrypt_pull : bool ,
129+ }
130+
121131pub struct SyncContext {
122132 db_path : String ,
123133 client : hyper:: Client < ConnectorService , Body > ,
@@ -133,6 +143,8 @@ pub struct SyncContext {
133143 /// whenever sync is called very first time, we will call the remote server
134144 /// to get the generation information and sync the db file if needed
135145 initial_server_sync : bool ,
146+ /// The encryption context for the sync.
147+ remote_encryption : Option < EncryptionContext > ,
136148}
137149
138150impl SyncContext {
@@ -141,6 +153,7 @@ impl SyncContext {
141153 db_path : String ,
142154 sync_url : String ,
143155 auth_token : Option < String > ,
156+ remote_encryption : Option < EncryptionContext > ,
144157 ) -> Result < Self > {
145158 let client = hyper:: client:: Client :: builder ( ) . build :: < _ , hyper:: Body > ( connector) ;
146159
@@ -163,6 +176,7 @@ impl SyncContext {
163176 durable_generation : 0 ,
164177 durable_frame_num : 0 ,
165178 initial_server_sync : false ,
179+ remote_encryption,
166180 } ;
167181
168182 if let Err ( e) = me. read_metadata ( ) . await {
@@ -303,6 +317,16 @@ impl SyncContext {
303317 None => { }
304318 }
305319
320+ if let Some ( remote_encryption) = & self . remote_encryption {
321+ if remote_encryption. decrypt_pull {
322+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
323+ }
324+ if remote_encryption. push_is_encrypted {
325+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
326+ }
327+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
328+ }
329+
306330 let req = req. body ( body. clone ( ) . into ( ) ) . expect ( "valid body" ) ;
307331
308332 let res = self
@@ -414,6 +438,16 @@ impl SyncContext {
414438 None => { }
415439 }
416440
441+ if let Some ( remote_encryption) = & self . remote_encryption {
442+ if remote_encryption. decrypt_pull {
443+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
444+ }
445+ if remote_encryption. push_is_encrypted {
446+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
447+ }
448+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
449+ }
450+
417451 let req = req. body ( Body :: empty ( ) ) . expect ( "valid request" ) ;
418452
419453 let res = self
@@ -577,6 +611,16 @@ impl SyncContext {
577611 req = req. header ( "Authorization" , auth_token) ;
578612 }
579613
614+ if let Some ( remote_encryption) = & self . remote_encryption {
615+ if remote_encryption. decrypt_pull {
616+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
617+ }
618+ if remote_encryption. push_is_encrypted {
619+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
620+ }
621+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
622+ }
623+
580624 let req = req. body ( Body :: empty ( ) ) . expect ( "valid request" ) ;
581625
582626 let res = self
@@ -673,6 +717,16 @@ impl SyncContext {
673717 req = req. header ( "Authorization" , auth_token) ;
674718 }
675719
720+ if let Some ( remote_encryption) = & self . remote_encryption {
721+ if remote_encryption. decrypt_pull {
722+ req = req. header ( "x-turso-decrypt-response" , "true" ) ;
723+ }
724+ if remote_encryption. push_is_encrypted {
725+ req = req. header ( "x-turso-encrypted-request" , "true" ) ;
726+ }
727+ req = req. header ( "x-turso-encryption-key" , remote_encryption. key_16_bytes_base64_encoded . as_str ( ) ) ;
728+ }
729+
676730 let req = req. body ( Body :: empty ( ) ) . expect ( "valid request" ) ;
677731
678732 let ( res, http_duration) =
0 commit comments