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:
@ -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)
|
||||||
}
|
}
|
||||||
|
29
core/corehttp/ipns_hostname.go
Normal file
29
core/corehttp/ipns_hostname.go
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user