@@ -10,14 +10,51 @@ import (
10
10
core "github.com/ipfs/go-ipfs/core"
11
11
namesys "github.com/ipfs/go-ipfs/namesys"
12
12
13
+ config "github.com/ipfs/go-ipfs-config"
13
14
nsopts "github.com/ipfs/interface-go-ipfs-core/options/namesys"
14
15
isd "github.com/jbenet/go-is-domain"
15
16
)
16
17
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
+
17
32
// HostnameOption rewrites an incoming request based on the Host header.
18
33
func HostnameOption () ServeOption {
19
34
return func (n * core.IpfsNode , _ net.Listener , mux * http.ServeMux ) (* http.ServeMux , error ) {
20
35
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
+
21
58
mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
22
59
ctx , cancel := context .WithCancel (n .Context ())
23
60
defer cancel ()
@@ -38,7 +75,7 @@ func HostnameOption() ServeOption {
38
75
// would "just work". Should we try this?
39
76
40
77
// Is this one of our "known gateways"?
41
- if gw , ok := KnownGateways [r .Host ]; ok {
78
+ if gw , ok := knownGateways [r .Host ]; ok {
42
79
// This is a known gateway but we're not using
43
80
// the subdomain feature.
44
81
@@ -63,7 +100,7 @@ func HostnameOption() ServeOption {
63
100
// Looks like we're using subdomains.
64
101
65
102
// 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 {
67
104
68
105
// Yes, serve the request (and rewrite the path to not use subdomains).
69
106
r .URL .Path = pathPrefix + r .URL .Path
0 commit comments