Skip to content

Commit

Permalink
config,http: reorg gateway settings
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Nov 21, 2023
1 parent ff05726 commit 71c4834
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
22 changes: 14 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,29 @@ type (
Bucket string `yaml:"bucket"`
}

// HTTPGateway contains the configuration for the IPFS HTTP gateway
HTTPGateway struct {
ListenAddress string `yaml:"ListenAddress"`
RedirectPathStyle bool `yaml:"redirectPathStyle"`
// RemoteFetch contains settings for enabling/disabling remote IPFS block
// fetching.
RemoteFetch struct {
// Enabled indicates whether remote IPFS block fetching is enabled.
// If false, all served IPFS blocks must be pinned locally.
Enabled bool `yaml:"enabled"`
// Allowlist contains the CIDs of blocks that are allowed to be
// fetched remotely. If empty, all blocks are allowed.
AllowList []cid.Cid `yaml:"allowlist"`
}

Fetch struct {
AllowRemote bool `yaml:"allowRemote"`
AllowList []cid.Cid `yaml:"allowList"`
// HTTPGateway contains the configuration for the IPFS HTTP gateway
HTTPGateway struct {
ListenAddress string `yaml:"ListenAddress"`
RedirectPathStyle bool `yaml:"redirectPathStyle"`
Fetch RemoteFetch `yaml:"fetch"`
}

// IPFS contains the configuration for the IPFS node
IPFS struct {
PrivateKey string `yaml:"privateKey"`
ListenAddresses []string `yaml:"listenAddresses"`
AnnounceAddresses []string `yaml:"announceAddresses"`
Fetch Fetch `yaml:"fetch"`
Gateway HTTPGateway `yaml:"gateway"`
}

Expand Down
10 changes: 6 additions & 4 deletions http/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func redirectPathCID(w http.ResponseWriter, r *http.Request, c cid.Cid, path []s
}

func (is *ipfsGatewayServer) allowRemoteFetch(c cid.Cid) bool {
if !is.config.IPFS.Fetch.AllowRemote {
if !is.config.IPFS.Gateway.Fetch.Enabled {
return false // deny all
} else if len(is.config.IPFS.Fetch.AllowList) == 0 {
} else if len(is.config.IPFS.Gateway.Fetch.AllowList) == 0 {
return true // allow all
}

// allowlist check
for _, match := range is.config.IPFS.Fetch.AllowList {
for _, match := range is.config.IPFS.Gateway.Fetch.AllowList {
if c.Equals(match) {
return true
}
Expand Down Expand Up @@ -117,13 +117,15 @@ func (is *ipfsGatewayServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
is.log.Info("downloading from ipfs", zap.Stringer("cid", c))
r, err := is.ipfs.DownloadCID(ctx, c, path)
if err != nil {
http.Error(w, "", http.StatusInternalServerError)
http.Error(w, "", http.StatusNotFound)
is.log.Error("failed to download cid", zap.Error(err))
return
}
defer r.Close()

io.Copy(w, r)
} else if errors.Is(err, sia.ErrNotFound) {
http.Error(w, "", http.StatusNotFound)
} else if err != nil {
http.Error(w, "", http.StatusInternalServerError)
is.log.Error("failed to get block", zap.Error(err))
Expand Down

0 comments on commit 71c4834

Please sign in to comment.