Skip to content

Commit 25856ff

Browse files
author
Brian Tiger Chow
committed
Merge pull request ipfs/kubo#741 from jbenet/feat/blocklist
add blocklist to gateway executable This commit was moved from ipfs/kubo@6599756
2 parents 9e615a2 + d9b0edd commit 25856ff

File tree

2 files changed

+71
-9
lines changed

2 files changed

+71
-9
lines changed

gateway/core/corehttp/gateway.go

+57-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,30 @@ package corehttp
22

33
import (
44
"net/http"
5+
"sync"
56

67
core "github.com/jbenet/go-ipfs/core"
78
)
89

9-
func GatewayOption(writable bool) ServeOption {
10+
// Gateway should be instantiated using NewGateway
11+
type Gateway struct {
12+
Config GatewayConfig
13+
}
14+
15+
type GatewayConfig struct {
16+
BlockList *BlockList
17+
Writable bool
18+
}
19+
20+
func NewGateway(conf GatewayConfig) *Gateway {
21+
return &Gateway{
22+
Config: conf,
23+
}
24+
}
25+
26+
func (g *Gateway) ServeOption() ServeOption {
1027
return func(n *core.IpfsNode, mux *http.ServeMux) error {
11-
gateway, err := newGatewayHandler(n, writable)
28+
gateway, err := newGatewayHandler(n, g.Config)
1229
if err != nil {
1330
return err
1431
}
@@ -17,3 +34,41 @@ func GatewayOption(writable bool) ServeOption {
1734
return nil
1835
}
1936
}
37+
38+
func GatewayOption(writable bool) ServeOption {
39+
g := NewGateway(GatewayConfig{
40+
Writable: writable,
41+
BlockList: &BlockList{},
42+
})
43+
return g.ServeOption()
44+
}
45+
46+
// Decider decides whether to Allow string
47+
type Decider func(string) bool
48+
49+
type BlockList struct {
50+
51+
mu sync.RWMutex
52+
d Decider
53+
}
54+
55+
func (b *BlockList) ShouldAllow(s string) bool {
56+
b.mu.RLock()
57+
d := b.d
58+
b.mu.RUnlock()
59+
if d == nil {
60+
return true
61+
}
62+
return d(s)
63+
}
64+
65+
// SetDecider atomically swaps the blocklist's decider
66+
func (b *BlockList) SetDecider(d Decider) {
67+
b.mu.Lock()
68+
b.d = d
69+
b.mu.Unlock()
70+
}
71+
72+
func (b *BlockList) ShouldBlock(s string) bool {
73+
return !b.ShouldAllow(s)
74+
}

gateway/core/corehttp/gateway_handler.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ type directoryItem struct {
5050
type gatewayHandler struct {
5151
node *core.IpfsNode
5252
dirList *template.Template
53-
writable bool
53+
config GatewayConfig
5454
}
5555

56-
func newGatewayHandler(node *core.IpfsNode, writable bool) (*gatewayHandler, error) {
56+
func newGatewayHandler(node *core.IpfsNode, conf GatewayConfig) (*gatewayHandler, error) {
5757
i := &gatewayHandler{
5858
node: node,
59-
writable: writable,
59+
config: conf,
6060
}
6161
err := i.loadTemplate()
6262
if err != nil {
@@ -125,18 +125,20 @@ func (i *gatewayHandler) NewDagReader(nd *dag.Node) (uio.ReadSeekCloser, error)
125125
return uio.NewDagReader(i.node.Context(), nd, i.node.DAG)
126126
}
127127

128+
// TODO(btc): break this apart into separate handlers using a more expressive
129+
// muxer
128130
func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
129-
if i.writable && r.Method == "POST" {
131+
if i.config.Writable && r.Method == "POST" {
130132
i.postHandler(w, r)
131133
return
132134
}
133135

134-
if i.writable && r.Method == "PUT" {
136+
if i.config.Writable && r.Method == "PUT" {
135137
i.putHandler(w, r)
136138
return
137139
}
138140

139-
if i.writable && r.Method == "DELETE" {
141+
if i.config.Writable && r.Method == "DELETE" {
140142
i.deleteHandler(w, r)
141143
return
142144
}
@@ -147,7 +149,7 @@ func (i *gatewayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
147149
}
148150

149151
errmsg := "Method " + r.Method + " not allowed: "
150-
if !i.writable {
152+
if !i.config.Writable {
151153
w.WriteHeader(http.StatusMethodNotAllowed)
152154
errmsg = errmsg + "read only access"
153155
} else {
@@ -164,6 +166,11 @@ func (i *gatewayHandler) getHandler(w http.ResponseWriter, r *http.Request) {
164166

165167
urlPath := r.URL.Path
166168

169+
if i.config.BlockList != nil && i.config.BlockList.ShouldBlock(urlPath) {
170+
w.WriteHeader(http.StatusNotFound)
171+
return
172+
}
173+
167174
nd, p, err := i.ResolvePath(ctx, urlPath)
168175
if err != nil {
169176
if err == routing.ErrNotFound {

0 commit comments

Comments
 (0)