diff --git a/core/corehttp/gateway_handler.go b/core/corehttp/gateway_handler.go index 23cf501f0e6..3e07e551df1 100644 --- a/core/corehttp/gateway_handler.go +++ b/core/corehttp/gateway_handler.go @@ -34,34 +34,6 @@ const ( ipnsPathPrefix = "/ipns/" ) -type GatewaySpec struct { - // PathPrefixes list the set of path prefixes that should be handled by - // the gateway logic. - PathPrefixes []string - - // UseSubdomains indicates whether or not this gateway uses subdomains - // for IPFS resources instead of paths. That is: http://CID.ipfs.GATEWAY/... - // - // If this flag is set, any /ipns/$id and/or /ipfs/$id paths in PathPrefixes - // will be permanently redirected to http://$id.[ipns|ipfs].$gateway/. - // - // We do not support using both paths and subdomains for a single domain - // for security reasons. - UseSubdomains bool -} - -var DefaultGatewaySpec = GatewaySpec{ - PathPrefixes: []string{ipfsPathPrefix, ipnsPathPrefix, "/api/"}, - UseSubdomains: false, -} - -// TODO(steb): Configurable -var KnownGateways = map[string]GatewaySpec{ - "ipfs.io": DefaultGatewaySpec, - "gateway.ipfs.io": DefaultGatewaySpec, - "localhost:8080": DefaultGatewaySpec, -} - // gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/) // (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link) type gatewayHandler struct { diff --git a/core/corehttp/hostname.go b/core/corehttp/hostname.go index c517d3161f8..9ec2134da20 100644 --- a/core/corehttp/hostname.go +++ b/core/corehttp/hostname.go @@ -11,13 +11,50 @@ import ( namesys "github.com/ipfs/go-ipfs/namesys" isd "github.com/gxed/go-is-domain" + config "github.com/ipfs/go-ipfs-config" nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys" ) +var defaultGatewaySpec = config.GatewaySpec{ + PathPrefixes: []string{ipfsPathPrefix, ipnsPathPrefix, "/api/"}, + UseSubdomains: false, +} + +var defaultKnownGateways = map[string]config.GatewaySpec{ + "ipfs.io": defaultGatewaySpec, + "gateway.ipfs.io": defaultGatewaySpec, + "dweb.link": config.GatewaySpec{ + PathPrefixes: []string{ipfsPathPrefix, ipnsPathPrefix}, + UseSubdomains: true, + }, +} + // HostnameOption rewrites an incoming request based on the Host header. func HostnameOption() ServeOption { return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { childMux := http.NewServeMux() + + cfg, err := n.Repo.Config() + if err != nil { + return nil, err + } + knownGateways := make( + map[string]config.GatewaySpec, + len(defaultKnownGateways)+len(cfg.Gateway.PublicGateways), + ) + for host, gw := range defaultKnownGateways { + knownGateways[host] = gw + } + for host, gw := range cfg.Gateway.PublicGateways { + if gw == nil { + // Allows the user to remove gateways but _also_ + // allows us to continuously update the list. + delete(knownGateways, host) + } else { + knownGateways[host] = *gw + } + } + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithCancel(n.Context()) defer cancel() @@ -38,7 +75,7 @@ func HostnameOption() ServeOption { // would "just work". Should we try this? // Is this one of our "known gateways"? - if gw, ok := KnownGateways[r.Host]; ok { + if gw, ok := knownGateways[r.Host]; ok { // This is a known gateway but we're not using // the subdomain feature. @@ -63,7 +100,7 @@ func HostnameOption() ServeOption { // Looks like we're using subdomains. // Again, is this a known gateway that supports subdomains? - if gw, ok := KnownGateways[host]; ok && gw.UseSubdomains { + if gw, ok := knownGateways[host]; ok && gw.UseSubdomains { // Yes, serve the request (and rewrite the path to not use subdomains). r.URL.Path = pathPrefix + r.URL.Path diff --git a/go.mod b/go.mod index a2c682c724f..be4662cfe4b 100644 --- a/go.mod +++ b/go.mod @@ -33,7 +33,7 @@ require ( github.com/ipfs/go-ipfs-chunker v0.0.1 github.com/ipfs/go-ipfs-cmdkit v0.0.1 github.com/ipfs/go-ipfs-cmds v0.0.2 - github.com/ipfs/go-ipfs-config v0.0.1 + github.com/ipfs/go-ipfs-config v0.0.2-0.20190315044135-de0accf5476b github.com/ipfs/go-ipfs-ds-help v0.0.1 github.com/ipfs/go-ipfs-exchange-interface v0.0.1 github.com/ipfs/go-ipfs-exchange-offline v0.0.1 diff --git a/go.sum b/go.sum index a443bf2a44a..667b197c776 100644 --- a/go.sum +++ b/go.sum @@ -143,12 +143,12 @@ github.com/ipfs/go-ipfs-chunker v0.0.1 h1:cHUUxKFQ99pozdahi+uSC/3Y6HeRpi9oTeUHbE github.com/ipfs/go-ipfs-chunker v0.0.1/go.mod h1:tWewYK0we3+rMbOh7pPFGDyypCtvGcBFymgY4rSDLAw= github.com/ipfs/go-ipfs-cmdkit v0.0.1 h1:X6YXEAjUljTzevE6DPUKXSqcgf+4FXzcn5B957F5MXo= github.com/ipfs/go-ipfs-cmdkit v0.0.1/go.mod h1:9FtbMdUabcSqv/G4/8WCxSLxkZxn/aZEFrxxqnVcRbg= -github.com/ipfs/go-ipfs-cmds v0.0.1 h1:wPTynLMa+JImcTsPaVmrUDP8mJ3S8HQVUWixnKi7+k4= -github.com/ipfs/go-ipfs-cmds v0.0.1/go.mod h1:k7I8PptE2kCJchR3ta546LRyxl4/uBYbLQHOJM0sUQ8= github.com/ipfs/go-ipfs-cmds v0.0.2 h1:wbyUvMGAsQLz8KUeYLK+Q6vX1MStR51O3a3vsgtf/Pk= github.com/ipfs/go-ipfs-cmds v0.0.2/go.mod h1:k7I8PptE2kCJchR3ta546LRyxl4/uBYbLQHOJM0sUQ8= github.com/ipfs/go-ipfs-config v0.0.1 h1:6ED08emzI1imdsAjixFi2pEyZxTVD5ECKtCOxLBx+Uc= github.com/ipfs/go-ipfs-config v0.0.1/go.mod h1:KDbHjNyg4e6LLQSQpkgQMBz6Jf4LXiWAcmnkcwmH0DU= +github.com/ipfs/go-ipfs-config v0.0.2-0.20190315044135-de0accf5476b h1:hKpyLzPAA1BZ/fi4ImGr7Q+fnufIILmnMvBGDHQD2Ls= +github.com/ipfs/go-ipfs-config v0.0.2-0.20190315044135-de0accf5476b/go.mod h1:KDbHjNyg4e6LLQSQpkgQMBz6Jf4LXiWAcmnkcwmH0DU= github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw= github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ= github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=