Skip to content

Commit

Permalink
config: update for new gateway options
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Mar 19, 2019
1 parent 7afafb8 commit bb44c3c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
28 changes: 0 additions & 28 deletions core/corehttp/gateway_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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/<path>)
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
type gatewayHandler struct {
Expand Down
41 changes: 39 additions & 2 deletions core/corehttp/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit bb44c3c

Please sign in to comment.