ankicommunity / ankicommunity-sync-server

A personal Anki sync server (so you can sync against your own server rather than AnkiWeb)
GNU Affero General Public License v3.0
885 stars 81 forks source link

Anki desktop can't connect self-hosted server while ankiDroid could. #162

Closed MacKenia closed 1 year ago

MacKenia commented 1 year ago

My country has a slow connection with the ankiweb, so I decide to self-host it to instead. However I encounter a problem.

I hosted this server on ArchLinux with original config file with changing nothing. python version: Python 3.10.10 I got the anki-sync-server from github on 07-12.

I can access the server with AnkiDroid 2.15.6, while anki desktop 2.1.65 couldn't.

This log shows when Anki Desktop connect to the server:

Traceback (most recent call last):
  File "/usr/lib/python3.10/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/home/asus/anki-sync-server/src/ankisyncd/sync_app.py", line 585, in __call__
    w = self.__wrapped__(*args, **kwargs)
  File "/home/asus/anki-sync-server/src/ankisyncd/sync_app.py", line 686, in __call__
    req.params = req.parse
  File "/home/asus/anki-sync-server/src/ankisyncd/sync_app.py", line 536, in parse
    items = re.split(repeat, body)
  File "/usr/lib/python3.10/re.py", line 230, in split
    return _compile(pattern, flags).split(string, maxsplit)
  File "/usr/lib/python3.10/re.py", line 303, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/lib/python3.10/sre_compile.py", line 788, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/lib/python3.10/sre_parse.py", line 955, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib/python3.10/sre_parse.py", line 444, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/usr/lib/python3.10/sre_parse.py", line 843, in _parse
    raise source.error("missing ), unterminated subpattern",
re.error: missing ), unterminated subpattern at position 0
[2023-07-15 18:36:47,724]:INFO:ankisyncd.http:127.0.0.1 "POST /sync/hostKey HTTP/1.1" 500 59

How could I solve this problem?

VikashKothary commented 1 year ago

Hi @MacKenia,

It looks like this line here is causing the issue for you:

https://github.com/ankicommunity/anki-sync-server/blob/35cf66d403e89b39f8a7023a367b086530334d3f/src/ankisyncd/sync_app.py#L536

I've not seen this error before and it looks like AnkiDesktop version 2.1.65 was released on 21st May 2023. I wonder if there's a change in the latest release that breaks our syncing or if it's just a issue that we've missed.

My AnkiDesktop is a bit older so I'll try and update it to see if I can reproduce this issue. If anyone else has gotten AnkiDesktop 2.1.65 working, please let me know.

In the meantime, I don't know your background but would you know how to add a print statement in the code to better understand what is causing that line to break?

MacKenia commented 1 year ago

@VikashKothary

Thanks for your reply. I add this line above the position you told me and I try again I receive this log:

repeat:b'(\xb5/\xfd\x00X\xcd\x00\x00\x98{"u":"mackenia","p}\x01\x00\x84\x9ba'
body:b'(\xb5/\xfd\x00X\xcd\x00\x00\x98{"u":"mackenia","p}\x01\x00\x84\x9ba'
Traceback (most recent call last):
  File "/usr/lib/python3.10/wsgiref/handlers.py", line 137, in run
    self.result = application(self.environ, self.start_response)
  File "/home/asus/anki-sync-server/src/ankisyncd/sync_app.py", line 586, in __call__
    w = self.__wrapped__(*args, **kwargs)
  File "/home/asus/anki-sync-server/src/ankisyncd/sync_app.py", line 687, in __call__
    req.params = req.parse
  File "/home/asus/anki-sync-server/src/ankisyncd/sync_app.py", line 537, in parse
    items = re.split(repeat, body)
  File "/usr/lib/python3.10/re.py", line 230, in split
    return _compile(pattern, flags).split(string, maxsplit)
  File "/usr/lib/python3.10/re.py", line 303, in _compile
    p = sre_compile.compile(pattern, flags)
  File "/usr/lib/python3.10/sre_compile.py", line 788, in compile
    p = sre_parse.parse(p, flags)
  File "/usr/lib/python3.10/sre_parse.py", line 955, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
  File "/usr/lib/python3.10/sre_parse.py", line 444, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
  File "/usr/lib/python3.10/sre_parse.py", line 843, in _parse
    raise source.error("missing ), unterminated subpattern",
re.error: missing ), unterminated subpattern at position 0
[2023-07-20 22:10:11,100]:INFO:ankisyncd.http:127.0.0.1 "POST /sync/hostKey HTTP/1.1" 500 59

The Code i add:

print(f"repeat:{repeat}\nbody:{body}") # <<< ADD HERE
items = re.split(repeat, body) 

This is a piece of ankiDroid's sync log:

repeat:b'--Anki-sync-boundary'
body:b'--Anki-sync-boundary\r\nContent-Disposition: form-data; name="v"\r\n\r\nankidroid,2.15.6,android:13:MI 9\r\n--Anki-sync-boundary\r\nContent-Disposition: form-data; name="k"\r\n\r\n03ea215ee842558c1e74239c0948fb41\r\n--Anki-sync-boundary\r\nContent-Disposition: form-data; name="c"\r\n\r\n1\r\n--Anki-sync-boundary\r\nContent-Disposition: form-data; name="data"; filename="data"\r\nContent-Type: application/octet-stream\r\n\r\n\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00\xab\xae\x05\x00C\xbf\xa6\xa3\x02\x00\x00\x00\r\n--Anki-sync-boundary--\r\n'
[2023-07-20 22:19:18,909]:INFO:ankisyncd.CollectionThread[mackenia]:Running begin(*[], **{'skey': '60fb8a6c'})
[2023-07-20 22:19:18,910]:INFO:ankisyncd.http:127.0.0.1 "POST /msync/begin HTTP/1.1" 200 52

And I also tried lower version like 2.1.63 and the problem stills there.

VikashKothary commented 1 year ago

I feel like this issue might be related to this issue: https://github.com/ankicommunity/anki-sync-server/issues/158.

There seems to be a new sync protocol that this server doesn't support yet. So I would suggest either using a AnkiDesktop version before 2.1.57 to see if that fixes your issue.

The issue above also linked to the new official sync server which might interest you.

Let me know what you end up doing while I try to investigate this further.

raywoocn commented 1 year ago

Try to downgrade to 2.1.56.

sarsamurmu commented 1 year ago

You can try the built-in sync server - https://docs.ankiweb.net/sync-server.html

MacKenia commented 1 year ago

@sarsamurmu Thanks, It really solve my problem!