1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-09-10 09:52:20 +08:00

gateway: attempt to resolve hostname to ipfs path

This allows someone to host a static site by pointing a TXT record at their
content in IPFS, and a CNAME record at an IPFS gateway.

Note that such a setup technically violates RFC1912 (section 2.4; "A CNAME
record is not allowed to coexist with any other data."), but tends to work in
practice.

We may want to consider changing the DNS->IPFS resolution scheme to allow this
scenario to be RFC-compliant (e.g. store the mapping on a well-known subdomain
to allow CNAME records on the domain itself).

License: MIT
Signed-off-by: Kevin Wallace <kevin@pentabarf.net>
This commit is contained in:
Kevin Wallace
2015-02-02 22:55:23 -08:00
parent fbd76ebb5b
commit 084cdc3ed8
2 changed files with 33 additions and 1 deletions

View File

@ -178,7 +178,10 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
if gatewayMaddr != nil { if gatewayMaddr != nil {
go func() { go func() {
var opts = []corehttp.ServeOption{corehttp.GatewayOption(writable)} var opts = []corehttp.ServeOption{
corehttp.IPNSHostnameOption(),
corehttp.GatewayOption(writable),
}
if rootRedirect != nil { if rootRedirect != nil {
opts = append(opts, rootRedirect) opts = append(opts, rootRedirect)
} }

View File

@ -0,0 +1,29 @@
package corehttp
import (
"net/http"
"strings"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/core"
)
// IPNSHostnameOption rewrites an incoming request if its Host: header contains
// an IPNS name.
// The rewritten request points at the resolved name on the gateway handler.
func IPNSHostnameOption() ServeOption {
return func(n *core.IpfsNode, mux *http.ServeMux) (*http.ServeMux, error) {
childMux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithCancel(n.Context())
defer cancel()
host := strings.SplitN(r.Host, ":", 2)[0]
if k, err := n.Namesys.Resolve(ctx, host); err == nil {
r.URL.Path = "/ipfs/" + k.Pretty() + r.URL.Path
}
childMux.ServeHTTP(w, r)
})
return childMux, nil
}
}