Skip to content

Commit 6509e39

Browse files
committed
config: update for new gateway options
License: MIT Signed-off-by: Steven Allen <[email protected]>
1 parent ab79a3c commit 6509e39

File tree

4 files changed

+42
-33
lines changed

4 files changed

+42
-33
lines changed

core/corehttp/gateway_handler.go

-28
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,6 @@ const (
3131
ipnsPathPrefix = "/ipns/"
3232
)
3333

34-
type GatewaySpec struct {
35-
// PathPrefixes list the set of path prefixes that should be handled by
36-
// the gateway logic.
37-
PathPrefixes []string
38-
39-
// UseSubdomains indicates whether or not this gateway uses subdomains
40-
// for IPFS resources instead of paths. That is: http://CID.ipfs.GATEWAY/...
41-
//
42-
// If this flag is set, any /ipns/$id and/or /ipfs/$id paths in PathPrefixes
43-
// will be permanently redirected to http://$id.[ipns|ipfs].$gateway/.
44-
//
45-
// We do not support using both paths and subdomains for a single domain
46-
// for security reasons.
47-
UseSubdomains bool
48-
}
49-
50-
var DefaultGatewaySpec = GatewaySpec{
51-
PathPrefixes: []string{ipfsPathPrefix, ipnsPathPrefix, "/api/"},
52-
UseSubdomains: false,
53-
}
54-
55-
// TODO(steb): Configurable
56-
var KnownGateways = map[string]GatewaySpec{
57-
"ipfs.io": DefaultGatewaySpec,
58-
"gateway.ipfs.io": DefaultGatewaySpec,
59-
"localhost:8080": DefaultGatewaySpec,
60-
}
61-
6234
// gatewayHandler is a HTTP handler that serves IPFS objects (accessible by default at /ipfs/<path>)
6335
// (it serves requests like GET /ipfs/QmVRzPKPzNtSrEzBFm2UZfxmPAgnaLke4DMcerbsGGSaFe/link)
6436
type gatewayHandler struct {

core/corehttp/hostname.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,51 @@ import (
1010
core "github.com/ipfs/go-ipfs/core"
1111
namesys "github.com/ipfs/go-ipfs/namesys"
1212

13+
config "github.com/ipfs/go-ipfs-config"
1314
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
1415
isd "github.com/jbenet/go-is-domain"
1516
)
1617

18+
var defaultGatewaySpec = config.GatewaySpec{
19+
PathPrefixes: []string{ipfsPathPrefix, ipnsPathPrefix, "/api/"},
20+
UseSubdomains: false,
21+
}
22+
23+
var defaultKnownGateways = map[string]config.GatewaySpec{
24+
"ipfs.io": defaultGatewaySpec,
25+
"gateway.ipfs.io": defaultGatewaySpec,
26+
"dweb.link": config.GatewaySpec{
27+
PathPrefixes: []string{ipfsPathPrefix, ipnsPathPrefix},
28+
UseSubdomains: true,
29+
},
30+
}
31+
1732
// HostnameOption rewrites an incoming request based on the Host header.
1833
func HostnameOption() ServeOption {
1934
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
2035
childMux := http.NewServeMux()
36+
37+
cfg, err := n.Repo.Config()
38+
if err != nil {
39+
return nil, err
40+
}
41+
knownGateways := make(
42+
map[string]config.GatewaySpec,
43+
len(defaultKnownGateways)+len(cfg.Gateway.PublicGateways),
44+
)
45+
for host, gw := range defaultKnownGateways {
46+
knownGateways[host] = gw
47+
}
48+
for host, gw := range cfg.Gateway.PublicGateways {
49+
if gw == nil {
50+
// Allows the user to remove gateways but _also_
51+
// allows us to continuously update the list.
52+
delete(knownGateways, host)
53+
} else {
54+
knownGateways[host] = *gw
55+
}
56+
}
57+
2158
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
2259
ctx, cancel := context.WithCancel(n.Context())
2360
defer cancel()
@@ -38,7 +75,7 @@ func HostnameOption() ServeOption {
3875
// would "just work". Should we try this?
3976

4077
// Is this one of our "known gateways"?
41-
if gw, ok := KnownGateways[r.Host]; ok {
78+
if gw, ok := knownGateways[r.Host]; ok {
4279
// This is a known gateway but we're not using
4380
// the subdomain feature.
4481

@@ -63,7 +100,7 @@ func HostnameOption() ServeOption {
63100
// Looks like we're using subdomains.
64101

65102
// Again, is this a known gateway that supports subdomains?
66-
if gw, ok := KnownGateways[host]; ok && gw.UseSubdomains {
103+
if gw, ok := knownGateways[host]; ok && gw.UseSubdomains {
67104

68105
// Yes, serve the request (and rewrite the path to not use subdomains).
69106
r.URL.Path = pathPrefix + r.URL.Path

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ require (
3030
github.com/ipfs/go-ipfs-blockstore v0.1.1
3131
github.com/ipfs/go-ipfs-chunker v0.0.3
3232
github.com/ipfs/go-ipfs-cmds v0.1.1
33-
github.com/ipfs/go-ipfs-config v0.2.0
33+
github.com/ipfs/go-ipfs-config v0.2.1-0.20200110091747-69d023fb5309
3434
github.com/ipfs/go-ipfs-ds-help v0.0.1
3535
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
3636
github.com/ipfs/go-ipfs-exchange-offline v0.0.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ github.com/ipfs/go-ipfs-chunker v0.0.3 h1:UuXhKoxvxl/vGhie+WXFRZYCwpZjbKF2SzD1Tv
210210
github.com/ipfs/go-ipfs-chunker v0.0.3/go.mod h1:RkGJorerOQNTDPgmX7HtJ5YzVQqaIYdzI/hrCHty5Kc=
211211
github.com/ipfs/go-ipfs-cmds v0.1.1 h1:H9/BLf5rcsULHMj/x8gC0e5o+raYhqk1OQsfzbGMNM4=
212212
github.com/ipfs/go-ipfs-cmds v0.1.1/go.mod h1:k1zMXcOLtljA9iAnZHddbH69yVm5+weRL0snmMD/rK0=
213-
github.com/ipfs/go-ipfs-config v0.2.0 h1:c5UtyIRVdeSYRP2I1ZR167e9XJs5x8vepeo2w4I4+KU=
214-
github.com/ipfs/go-ipfs-config v0.2.0/go.mod h1:vEXbHHFPMCGCVhgv8oye4QF75kQ/gyZ3/tBdMMZFAps=
213+
github.com/ipfs/go-ipfs-config v0.2.1-0.20200110091747-69d023fb5309 h1:bMQ/X8L/X2ah7oiHMemjrxIL3iPdfsCepPmqkDCtQAQ=
214+
github.com/ipfs/go-ipfs-config v0.2.1-0.20200110091747-69d023fb5309/go.mod h1:vEXbHHFPMCGCVhgv8oye4QF75kQ/gyZ3/tBdMMZFAps=
215215
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
216216
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
217217
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=

0 commit comments

Comments
 (0)