Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CORS #13

Closed
mikavilpas opened this issue Dec 17, 2022 · 7 comments
Closed

Support for CORS #13

mikavilpas opened this issue Dec 17, 2022 · 7 comments

Comments

@mikavilpas
Copy link
Contributor

Hi, I'm working on my hobby project which is a Japanese web dictionary called hare: https://github.com/sp3ctum/hare

It's nothing special but I use it in my own workflow. I'd like to add support for anki connect as well as anki connect android in order to quickly add cards when I'm reading.

Anki connect allows customizing the CORS origin list in its config, here is an example:

{
    "apiKey": null,
    "apiLogPath": null,
    "ignoreOriginList": [],
    "webBindAddress": "127.0.0.1",
    "webBindPort": 8765,
    "webCorsOriginList": [
        "http://localhost",
        "https://sakura-paris.org"
    ]
}

https://sakura-paris.org is where I run my dictionary. Without the added configuration, all requests are blocked by chrome because they target another host. This is what it looks like:
image

But after this configuration has been saved, I can access the service from chrome.
image
image

I'll start by working on anki connect desktop but I understand AnkiConnectAndroid uses the same api (roughly) so I think I might be able to support both. This, I imagine, is what yomichan does as well.

Do you think it could be possible to add support for configurable CORS origins? I could maybe submit a PR for this but I'm not exactly sure how to do it yet.

@KamWithK
Copy link
Owner

It's probably possible I guess but I'm not sure how to do it either

@mikavilpas
Copy link
Contributor Author

Will you accept a PR for it? I might be able to implement it, but no promise on any deadlines of course.

@KamWithK
Copy link
Owner

Yeah sure

@Aquafina-water-bottle
Copy link
Contributor

Will you accept a PR for it? I might be able to implement it, but no promise on any deadlines of course.

I was actually playing around with this earlier to see if I could get it work with my Anki template within Ankidroid. I believe this is what you're looking for:

diff --git a/app/src/main/java/com/kamwithk/ankiconnectandroid/routing/RouteHandler.java b/app/src/main/java/com/kamwithk/ankiconnectandroid/routing/RouteHandler.java
index 908622e..fd3e2c1 100644
--- a/app/src/main/java/com/kamwithk/ankiconnectandroid/routing/RouteHandler.java
+++ b/app/src/main/java/com/kamwithk/ankiconnectandroid/routing/RouteHandler.java
@@ -52,6 +52,14 @@ public class RouteHandler extends RouterNanoHTTPD.DefaultHandler {
             e.printStackTrace();
         }
 
-        return apiHandler.chooseAPI(files.get("postData"), session.getParameters());
+        NanoHTTPD.Response rep = null;
+        if (files.get("postData") == null) {
+            rep = newFixedLengthResponse("Ankiconnect Android is running.");
+        } else {
+            rep = apiHandler.chooseAPI(files.get("postData"), session.getParameters());
+        }
+
+        rep.addHeader("Access-Control-Allow-Origin", "http://localhost");
+        return rep;
     }

This hard codes the localhost header into the program, but I never looked into how to make a GUI for it. I'll let you pick it up from here if you're interested! @sp3ctum

@Aquafina-water-bottle
Copy link
Contributor

I should also mention @sp3ctum that the most recent PR I made (#16) implements a basic settings section on the toolbar if you want to work from there.

@mikavilpas
Copy link
Contributor Author

Cool, that looks great @Aquafina-water-bottle , should be a nice place to add some kind of "Allowed hosts/sites" setting section.

I played around with your code snippet and was able to get it to work with the headers below. There are still some issues with some requests that I haven't solved so this shouldn't be considered a final solution by any means.

        rep.addHeader("Access-Control-Allow-Origin", "*"); // will need configuration, probably not good to allow all origins
        rep.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        rep.addHeader("Access-Control-Allow-Methods ", "GET, POST, PUT, OPTIONS");

Also I opened some issues for some minor things here in this repo.

I am in the process of changing jobs in the coming weeks and don't have the means to finish this right now.. But I still need it done later for my app, so I think I'll take a look at this once things become somewhat settled down.

@KamWithK
Copy link
Owner

KamWithK commented Feb 1, 2023

It's been a while since the last message was sent here so I'm going to close it for now
Feel free to reopen this issue at any time though 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants