提交 5abd144b 作者: Steven Allen

config: update for new gateway options

License: MIT
Signed-off-by: 's avatarSteven Allen <steven@stebalien.com>
上级 3632a6d4
......@@ -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 {
......
......@@ -10,14 +10,51 @@ import (
core "github.com/ipfs/go-ipfs/core"
namesys "github.com/ipfs/go-ipfs/namesys"
config "github.com/ipfs/go-ipfs-config"
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
isd "github.com/jbenet/go-is-domain"
)
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
......
......@@ -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.4
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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论