Skip to content

Commit bb44c3c

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

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
@@ -34,34 +34,6 @@ const (
3434
ipnsPathPrefix = "/ipns/"
3535
)
3636

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

core/corehttp/hostname.go

+39-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,50 @@ import (
1111
namesys "github.com/ipfs/go-ipfs/namesys"
1212

1313
isd "github.com/gxed/go-is-domain"
14+
config "github.com/ipfs/go-ipfs-config"
1415
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
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
@@ -33,7 +33,7 @@ require (
3333
github.com/ipfs/go-ipfs-chunker v0.0.1
3434
github.com/ipfs/go-ipfs-cmdkit v0.0.1
3535
github.com/ipfs/go-ipfs-cmds v0.0.2
36-
github.com/ipfs/go-ipfs-config v0.0.1
36+
github.com/ipfs/go-ipfs-config v0.0.2-0.20190315044135-de0accf5476b
3737
github.com/ipfs/go-ipfs-ds-help v0.0.1
3838
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
3939
github.com/ipfs/go-ipfs-exchange-offline v0.0.1

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,12 @@ github.com/ipfs/go-ipfs-chunker v0.0.1 h1:cHUUxKFQ99pozdahi+uSC/3Y6HeRpi9oTeUHbE
143143
github.com/ipfs/go-ipfs-chunker v0.0.1/go.mod h1:tWewYK0we3+rMbOh7pPFGDyypCtvGcBFymgY4rSDLAw=
144144
github.com/ipfs/go-ipfs-cmdkit v0.0.1 h1:X6YXEAjUljTzevE6DPUKXSqcgf+4FXzcn5B957F5MXo=
145145
github.com/ipfs/go-ipfs-cmdkit v0.0.1/go.mod h1:9FtbMdUabcSqv/G4/8WCxSLxkZxn/aZEFrxxqnVcRbg=
146-
github.com/ipfs/go-ipfs-cmds v0.0.1 h1:wPTynLMa+JImcTsPaVmrUDP8mJ3S8HQVUWixnKi7+k4=
147-
github.com/ipfs/go-ipfs-cmds v0.0.1/go.mod h1:k7I8PptE2kCJchR3ta546LRyxl4/uBYbLQHOJM0sUQ8=
148146
github.com/ipfs/go-ipfs-cmds v0.0.2 h1:wbyUvMGAsQLz8KUeYLK+Q6vX1MStR51O3a3vsgtf/Pk=
149147
github.com/ipfs/go-ipfs-cmds v0.0.2/go.mod h1:k7I8PptE2kCJchR3ta546LRyxl4/uBYbLQHOJM0sUQ8=
150148
github.com/ipfs/go-ipfs-config v0.0.1 h1:6ED08emzI1imdsAjixFi2pEyZxTVD5ECKtCOxLBx+Uc=
151149
github.com/ipfs/go-ipfs-config v0.0.1/go.mod h1:KDbHjNyg4e6LLQSQpkgQMBz6Jf4LXiWAcmnkcwmH0DU=
150+
github.com/ipfs/go-ipfs-config v0.0.2-0.20190315044135-de0accf5476b h1:hKpyLzPAA1BZ/fi4ImGr7Q+fnufIILmnMvBGDHQD2Ls=
151+
github.com/ipfs/go-ipfs-config v0.0.2-0.20190315044135-de0accf5476b/go.mod h1:KDbHjNyg4e6LLQSQpkgQMBz6Jf4LXiWAcmnkcwmH0DU=
152152
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
153153
github.com/ipfs/go-ipfs-delay v0.0.1 h1:r/UXYyRcddO6thwOnhiznIAiSvxMECGgtv35Xs1IeRQ=
154154
github.com/ipfs/go-ipfs-delay v0.0.1/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=

0 commit comments

Comments
 (0)