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
Fix #382: Extract headers from IPFS API requests and apply them to hijacked ones. #623
Conversation
889c1f8
to
df98ffe
Compare
…cked ones. This commit makes the proxy extract useful fixed headers (like CORS) from the IPFS daemon API responses and then apply them to the responses from hijacked endpoints like /add or /repo/stat. It does this by caching a list of headers from the first IPFS API response which has them. If we have not performed any proxied request or managed to obtain the headers we're interested in, this will try triggering a request to "/api/v0/version" to obtain them first. This should fix the issues with using Cluster proxy with IPFS Companion and Chrome. License: MIT Signed-off-by: Hector Sanjuan <code@hector.link>
df98ffe
to
862c1eb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR looks good except a minor concerns about header checking
@@ -1,4 +1,4 @@ | |||
package ipfscluster | |||
package version |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would have been nice to have a separate commit for creating the version package. But let it be as it is, it would be overkill to go back and spit the commit.
func (proxy *Server) isIPFSHeadersKnown() bool { | ||
_, ok := proxy.ipfsHeaders.Load(ipfsHeaderList[0]) | ||
return ok | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(From looking at this and references of isIPFSHeadersKnown
) It assumes that if Server
header is present, remaining headers are present as well. Shouldn't we check other headers? Depending we need to check for other headers or not the name can be changed to areIPFSHeadersKnown
or isServerHeaderPresent
.
} | |
func (proxy *Server) areIPFSHeadersKnown() bool { | |
for _, header := range ipfsHeaderList { | |
if _, ok := proxy.ipfsHeaders.Load(header); !ok { | |
return !ok | |
} | |
} | |
return true | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a Go unwritten rule saying that boolean functions start with is
? @lanzafame ?
Shouldn't we check other headers?
We can assume that if we learned about Server (or any one of them) we have learned about the rest. But maybe worth checking the whole list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a Go unwritten rule saying that boolean functions start with is?
There is this:
"The Go Programming Language" by Donovan and Kernighan recommends in Chapter 6 that
Functions that merely access or modify internal values of a type..are called getters and setters. > However, when naming a getter method we usually omit the Get prefix. This Preference for brevity extends to all methods not just field accessors, and to other reductant prefixes as well, such as Fetch, Find and Lookup
But I wouldn't apply that to is
. Though in this particular case, Known
is enough of an indicator that this is a boolean predicate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On trying it out, I don't see response headers with proxy endpoints, however I do see expected response headers( as in ipfsHeaderList
) with ipfs endpoints. Can you check and confirm if you see the same thing?
kishan@kishansagathiya:~/$ curl -X GET http://localhost:9096/api/v0/pin/ls -v
Note: Unnecessary use of -X or --request, GET is already inferred.
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 9096 (#0)
> GET /api/v0/pin/ls HTTP/1.1
> Host: localhost:9096
> User-Agent: curl/7.58.0
> Accept: */*
>
* Connection #0 to host localhost left intact
��f#[L��{m-�P�p9��Ѫ��#3���B�%���)��S"�~��kishan@kishansagathiya:~/$
@kishansagathiya that is the wrong port number. The proxy api is on 9095. 9096 is the libp2p swarm/cluster-api port. |
|
My bad, this is what confused me. Will have to modify that docker-compose file 😆 |
License: MIT Signed-off-by: Hector Sanjuan <code@hector.link>
And do it by only loading them from the sync.Map once. License: MIT Signed-off-by: Hector Sanjuan <code@hector.link>
Ok thanks for the feedback. I made it in a way that we only load the headers once from the sync.Map for every request. And if we don't have all the headers we want we will try to call /version to get them. Ready on my side. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Trick used in last commit to get headers from sync.Map.
I will leave merging for you
This commit makes the proxy extract useful fixed headers (like CORS) from
the IPFS daemon API responses and then apply them to the responses
from hijacked endpoints like /add or /repo/stat.
It does this by caching a list of headers from the first IPFS API
response which has them. If we have not performed any proxied request or
managed to obtain the headers we're interested in, this will try triggering a
request to "/api/v0/version" to obtain them first.
This should fix the issues with using Cluster proxy with IPFS Companion and
Chrome.
License: MIT
Signed-off-by: Hector Sanjuan code@hector.link