2018-10-13 14:27:03 +00:00
|
|
|
package ipfsproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httputil"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2018-12-18 14:44:11 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/adder/adderutils"
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
"github.com/ipfs/ipfs-cluster/rpcutil"
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
mux "github.com/gorilla/mux"
|
2018-10-13 14:27:03 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log"
|
2019-03-01 17:46:27 +00:00
|
|
|
path "github.com/ipfs/go-path"
|
2019-06-14 10:41:11 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
2018-11-04 03:27:09 +00:00
|
|
|
rpc "github.com/libp2p/go-libp2p-gorpc"
|
2018-10-13 14:27:03 +00:00
|
|
|
madns "github.com/multiformats/go-multiaddr-dns"
|
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
2019-03-01 17:46:27 +00:00
|
|
|
|
|
|
|
"go.opencensus.io/plugin/ochttp"
|
|
|
|
"go.opencensus.io/plugin/ochttp/propagation/tracecontext"
|
|
|
|
"go.opencensus.io/trace"
|
2018-10-13 14:27:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// DNSTimeout is used when resolving DNS multiaddresses in this module
|
|
|
|
var DNSTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
var logger = logging.Logger("ipfsproxy")
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
// Server offers an IPFS API, hijacking some interesting requests
|
2018-10-13 14:27:03 +00:00
|
|
|
// and forwarding the rest to the ipfs daemon
|
|
|
|
// it proxies HTTP requests to the configured IPFS
|
|
|
|
// daemon. It is able to intercept these requests though, and
|
|
|
|
// perform extra operations on them.
|
2018-11-01 10:24:05 +00:00
|
|
|
type Server struct {
|
2018-10-13 14:27:03 +00:00
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
config *Config
|
|
|
|
nodeScheme string
|
|
|
|
nodeAddr string
|
2018-10-13 14:27:03 +00:00
|
|
|
|
|
|
|
rpcClient *rpc.Client
|
|
|
|
rpcReady chan struct{}
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
listener net.Listener // proxy listener
|
|
|
|
server *http.Server // proxy server
|
|
|
|
ipfsRoundTripper http.RoundTripper // allows to talk to IPFS
|
2018-10-13 14:27:03 +00:00
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
ipfsHeadersStore sync.Map
|
2018-12-18 14:44:11 +00:00
|
|
|
|
2018-10-13 14:27:03 +00:00
|
|
|
shutdownLock sync.Mutex
|
|
|
|
shutdown bool
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipfsError struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipfsPinType struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipfsPinLsResp struct {
|
|
|
|
Keys map[string]ipfsPinType
|
|
|
|
}
|
|
|
|
|
|
|
|
type ipfsPinOpResp struct {
|
|
|
|
Pins []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// From https://github.com/ipfs/go-ipfs/blob/master/core/coreunix/add.go#L49
|
|
|
|
type ipfsAddResp struct {
|
|
|
|
Name string
|
|
|
|
Hash string `json:",omitempty"`
|
|
|
|
Bytes int64 `json:",omitempty"`
|
|
|
|
Size string `json:",omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
// New returns and ipfs Proxy component
|
|
|
|
func New(cfg *Config) (*Server, error) {
|
2018-10-13 14:27:03 +00:00
|
|
|
err := cfg.Validate()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeMAddr := cfg.NodeAddr
|
|
|
|
// dns multiaddresses need to be resolved first
|
|
|
|
if madns.Matches(nodeMAddr) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), DNSTimeout)
|
|
|
|
defer cancel()
|
|
|
|
resolvedAddrs, err := madns.Resolve(ctx, cfg.NodeAddr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
nodeMAddr = resolvedAddrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
_, nodeAddr, err := manet.DialArgs(nodeMAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-12 18:51:21 +00:00
|
|
|
proxyNet, proxyAddr, err := manet.DialArgs(cfg.ListenAddr)
|
2018-10-13 14:27:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l, err := net.Listen(proxyNet, proxyAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
nodeScheme := "http"
|
|
|
|
if cfg.NodeHTTPS {
|
|
|
|
nodeScheme = "https"
|
|
|
|
}
|
|
|
|
nodeHTTPAddr := fmt.Sprintf("%s://%s", nodeScheme, nodeAddr)
|
2018-10-13 14:27:03 +00:00
|
|
|
proxyURL, err := url.Parse(nodeHTTPAddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-27 04:03:15 +00:00
|
|
|
var handler http.Handler
|
2019-01-10 19:03:59 +00:00
|
|
|
router := mux.NewRouter()
|
2018-06-27 04:03:15 +00:00
|
|
|
handler = router
|
|
|
|
|
|
|
|
if cfg.Tracing {
|
|
|
|
handler = &ochttp.Handler{
|
|
|
|
IsPublicEndpoint: true,
|
|
|
|
Propagation: &tracecontext.HTTPFormat{},
|
|
|
|
Handler: router,
|
|
|
|
StartOptions: trace.StartOptions{SpanKind: trace.SpanKindServer},
|
|
|
|
FormatSpanName: func(req *http.Request) string {
|
|
|
|
return "proxy:" + req.Host + ":" + req.URL.Path + ":" + req.Method
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-13 14:27:03 +00:00
|
|
|
s := &http.Server{
|
2018-12-12 18:51:21 +00:00
|
|
|
ReadTimeout: cfg.ReadTimeout,
|
|
|
|
WriteTimeout: cfg.WriteTimeout,
|
|
|
|
ReadHeaderTimeout: cfg.ReadHeaderTimeout,
|
|
|
|
IdleTimeout: cfg.IdleTimeout,
|
2018-06-27 04:03:15 +00:00
|
|
|
Handler: handler,
|
2019-03-07 05:28:06 +00:00
|
|
|
MaxHeaderBytes: cfg.MaxHeaderBytes,
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// See: https://github.com/ipfs/go-ipfs/issues/5168
|
|
|
|
// See: https://github.com/ipfs/ipfs-cluster/issues/548
|
|
|
|
// on why this is re-enabled.
|
2018-11-01 10:24:05 +00:00
|
|
|
s.SetKeepAlivesEnabled(true) // A reminder that this can be changed
|
2018-10-13 14:27:03 +00:00
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
reverseProxy := httputil.NewSingleHostReverseProxy(proxyURL)
|
|
|
|
reverseProxy.Transport = http.DefaultTransport
|
2018-10-13 14:27:03 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy := &Server{
|
2019-01-10 19:03:59 +00:00
|
|
|
ctx: ctx,
|
|
|
|
config: cfg,
|
|
|
|
cancel: cancel,
|
|
|
|
nodeAddr: nodeHTTPAddr,
|
|
|
|
nodeScheme: nodeScheme,
|
|
|
|
rpcReady: make(chan struct{}, 1),
|
|
|
|
listener: l,
|
|
|
|
server: s,
|
|
|
|
ipfsRoundTripper: reverseProxy.Transport,
|
2018-12-18 14:44:11 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
// Ideally, we should only intercept POST requests, but
|
|
|
|
// people may be calling the API with GET or worse, PUT
|
|
|
|
// because IPFS has been allowing this traditionally.
|
|
|
|
// The main idea here is that we do not intercept
|
|
|
|
// OPTIONS requests (or HEAD).
|
|
|
|
hijackSubrouter := router.
|
|
|
|
Methods(http.MethodPost, http.MethodGet, http.MethodPut).
|
|
|
|
PathPrefix("/api/v0").
|
|
|
|
Subrouter()
|
|
|
|
|
|
|
|
// Add hijacked routes
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/add/{arg}").
|
|
|
|
HandlerFunc(slashHandler(proxy.pinHandler)).
|
|
|
|
Name("PinAddSlash") // supports people using the API wrong.
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/add").
|
|
|
|
HandlerFunc(proxy.pinHandler).
|
|
|
|
Name("PinAdd")
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/rm/{arg}").
|
|
|
|
HandlerFunc(slashHandler(proxy.unpinHandler)).
|
|
|
|
Name("PinRmSlash") // supports people using the API wrong.
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/rm").
|
|
|
|
HandlerFunc(proxy.unpinHandler).
|
|
|
|
Name("PinRm")
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/ls/{arg}").
|
|
|
|
HandlerFunc(slashHandler(proxy.pinLsHandler)).
|
|
|
|
Name("PinLsSlash") // supports people using the API wrong.
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/ls").
|
|
|
|
HandlerFunc(proxy.pinLsHandler).
|
|
|
|
Name("PinLs")
|
2019-04-29 14:36:40 +00:00
|
|
|
hijackSubrouter.
|
|
|
|
Path("/pin/update").
|
|
|
|
HandlerFunc(proxy.pinUpdateHandler).
|
|
|
|
Name("PinUpdate")
|
2019-01-10 19:03:59 +00:00
|
|
|
hijackSubrouter.
|
|
|
|
Path("/add").
|
|
|
|
HandlerFunc(proxy.addHandler).
|
|
|
|
Name("Add")
|
|
|
|
hijackSubrouter.
|
|
|
|
Path("/repo/stat").
|
|
|
|
HandlerFunc(proxy.repoStatHandler).
|
|
|
|
Name("RepoStat")
|
|
|
|
|
|
|
|
// Everything else goes to the IPFS daemon.
|
|
|
|
router.PathPrefix("/").Handler(reverseProxy)
|
2018-11-01 10:24:05 +00:00
|
|
|
|
|
|
|
go proxy.run()
|
|
|
|
return proxy, nil
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetClient makes the component ready to perform RPC
|
|
|
|
// requests.
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) SetClient(c *rpc.Client) {
|
|
|
|
proxy.rpcClient = c
|
|
|
|
proxy.rpcReady <- struct{}{}
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops any listeners and stops the component from taking
|
|
|
|
// any requests.
|
2018-06-27 04:03:15 +00:00
|
|
|
func (proxy *Server) Shutdown(ctx context.Context) error {
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy.shutdownLock.Lock()
|
|
|
|
defer proxy.shutdownLock.Unlock()
|
2018-10-13 14:27:03 +00:00
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
if proxy.shutdown {
|
2018-10-13 14:27:03 +00:00
|
|
|
logger.Debug("already shutdown")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Info("stopping IPFS Proxy")
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy.cancel()
|
|
|
|
close(proxy.rpcReady)
|
|
|
|
proxy.server.SetKeepAlivesEnabled(false)
|
|
|
|
proxy.listener.Close()
|
2018-10-13 14:27:03 +00:00
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy.wg.Wait()
|
|
|
|
proxy.shutdown = true
|
2018-10-13 14:27:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-14 10:37:42 +00:00
|
|
|
// launches proxy when we receive the rpcReady signal.
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) run() {
|
|
|
|
<-proxy.rpcReady
|
2018-10-13 14:27:03 +00:00
|
|
|
|
|
|
|
// Do not shutdown while launching threads
|
2018-11-01 10:24:05 +00:00
|
|
|
// -- prevents race conditions with proxy.wg.
|
|
|
|
proxy.shutdownLock.Lock()
|
|
|
|
defer proxy.shutdownLock.Unlock()
|
2018-10-13 14:27:03 +00:00
|
|
|
|
|
|
|
// This launches the proxy
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy.wg.Add(1)
|
2018-10-13 14:27:03 +00:00
|
|
|
go func() {
|
2018-11-01 10:24:05 +00:00
|
|
|
defer proxy.wg.Done()
|
2018-10-13 14:27:03 +00:00
|
|
|
logger.Infof(
|
|
|
|
"IPFS Proxy: %s -> %s",
|
2018-12-12 18:51:21 +00:00
|
|
|
proxy.config.ListenAddr,
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy.config.NodeAddr,
|
2018-10-13 14:27:03 +00:00
|
|
|
)
|
2018-11-01 10:24:05 +00:00
|
|
|
err := proxy.server.Serve(proxy.listener) // hangs here
|
2018-10-13 14:27:03 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "closed network connection") {
|
|
|
|
logger.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
// ipfsErrorResponder writes an http error response just like IPFS would.
|
2019-05-02 09:32:13 +00:00
|
|
|
func ipfsErrorResponder(w http.ResponseWriter, errMsg string, code int) {
|
2018-10-13 14:27:03 +00:00
|
|
|
res := ipfsError{errMsg}
|
|
|
|
resBytes, _ := json.Marshal(res)
|
2019-05-02 09:32:13 +00:00
|
|
|
if code > 0 {
|
|
|
|
w.WriteHeader(code)
|
|
|
|
} else {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2018-10-13 14:27:03 +00:00
|
|
|
w.Write(resBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) pinOpHandler(op string, w http.ResponseWriter, r *http.Request) {
|
2019-01-10 19:03:59 +00:00
|
|
|
proxy.setHeaders(w.Header(), r)
|
2018-12-18 14:44:11 +00:00
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
arg := r.URL.Query().Get("arg")
|
2019-03-01 17:46:27 +00:00
|
|
|
p, err := path.ParsePath(arg)
|
2018-10-13 14:27:03 +00:00
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "Error parsing IPFS Path: "+err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:46:27 +00:00
|
|
|
pinPath := &api.PinPath{Path: p.String()}
|
|
|
|
var pin api.Pin
|
2018-11-01 10:24:05 +00:00
|
|
|
err = proxy.rpcClient.Call(
|
2018-10-13 14:27:03 +00:00
|
|
|
"",
|
|
|
|
"Cluster",
|
|
|
|
op,
|
2019-03-01 17:46:27 +00:00
|
|
|
pinPath,
|
|
|
|
&pin,
|
2018-10-13 14:27:03 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := ipfsPinOpResp{
|
2019-03-01 17:46:27 +00:00
|
|
|
Pins: []string{pin.Cid.String()},
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
resBytes, _ := json.Marshal(res)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(resBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) pinHandler(w http.ResponseWriter, r *http.Request) {
|
2019-03-01 17:46:27 +00:00
|
|
|
proxy.pinOpHandler("PinPath", w, r)
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) unpinHandler(w http.ResponseWriter, r *http.Request) {
|
2019-03-01 17:46:27 +00:00
|
|
|
proxy.pinOpHandler("UnpinPath", w, r)
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) pinLsHandler(w http.ResponseWriter, r *http.Request) {
|
2019-01-10 19:03:59 +00:00
|
|
|
proxy.setHeaders(w.Header(), r)
|
2018-12-18 14:44:11 +00:00
|
|
|
|
2018-10-13 14:27:03 +00:00
|
|
|
pinLs := ipfsPinLsResp{}
|
|
|
|
pinLs.Keys = make(map[string]ipfsPinType)
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
arg := r.URL.Query().Get("arg")
|
|
|
|
if arg != "" {
|
2018-10-13 14:27:03 +00:00
|
|
|
c, err := cid.Decode(arg)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
var pin api.Pin
|
2018-11-01 10:24:05 +00:00
|
|
|
err = proxy.rpcClient.Call(
|
2018-10-13 14:27:03 +00:00
|
|
|
"",
|
|
|
|
"Cluster",
|
|
|
|
"PinGet",
|
2019-02-27 17:04:35 +00:00
|
|
|
c,
|
2018-10-13 14:27:03 +00:00
|
|
|
&pin,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, fmt.Sprintf("Error: path '%s' is not pinned", arg), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
2019-02-27 17:04:35 +00:00
|
|
|
pinLs.Keys[pin.Cid.String()] = ipfsPinType{
|
2018-10-13 14:27:03 +00:00
|
|
|
Type: "recursive",
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-27 17:04:35 +00:00
|
|
|
pins := make([]*api.Pin, 0)
|
2018-11-01 10:24:05 +00:00
|
|
|
err := proxy.rpcClient.Call(
|
2018-10-13 14:27:03 +00:00
|
|
|
"",
|
|
|
|
"Cluster",
|
|
|
|
"Pins",
|
|
|
|
struct{}{},
|
|
|
|
&pins,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, pin := range pins {
|
2019-02-27 17:04:35 +00:00
|
|
|
pinLs.Keys[pin.Cid.String()] = ipfsPinType{
|
2018-10-13 14:27:03 +00:00
|
|
|
Type: "recursive",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resBytes, _ := json.Marshal(pinLs)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(resBytes)
|
|
|
|
}
|
|
|
|
|
2019-04-29 14:36:40 +00:00
|
|
|
func (proxy *Server) pinUpdateHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx, span := trace.StartSpan(r.Context(), "ipfsproxy/pinUpdateHandler")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
proxy.setHeaders(w.Header(), r)
|
|
|
|
|
|
|
|
// Check that we have enough arguments and mimic ipfs response when not
|
|
|
|
q := r.URL.Query()
|
|
|
|
args := q["arg"]
|
|
|
|
if len(args) == 0 {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "argument \"from-path\" is required", http.StatusBadRequest)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if len(args) == 1 {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "argument \"to-path\" is required", http.StatusBadRequest)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
unpin := !(q.Get("unpin") == "false")
|
|
|
|
from := args[0]
|
|
|
|
to := args[1]
|
|
|
|
|
|
|
|
// Parse paths (we will need to resolve them)
|
|
|
|
pFrom, err := path.ParsePath(from)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "error parsing \"from-path\" argument: "+err.Error(), -1)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pTo, err := path.ParsePath(to)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "error parsing \"to-path\" argument: "+err.Error(), -1)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the FROM argument
|
|
|
|
var fromCid cid.Cid
|
|
|
|
err = proxy.rpcClient.CallContext(
|
|
|
|
ctx,
|
|
|
|
"",
|
2019-05-04 20:36:10 +00:00
|
|
|
"IPFSConnector",
|
|
|
|
"Resolve",
|
2019-04-29 14:36:40 +00:00
|
|
|
pFrom.String(),
|
|
|
|
&fromCid,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:40:29 +00:00
|
|
|
// Do a PinPath setting PinUpdate
|
|
|
|
pinPath := &api.PinPath{Path: pTo.String()}
|
|
|
|
pinPath.PinUpdate = fromCid
|
2019-04-29 14:36:40 +00:00
|
|
|
|
2019-07-12 14:40:29 +00:00
|
|
|
var pin api.Pin
|
|
|
|
err = proxy.rpcClient.Call(
|
2019-04-29 14:36:40 +00:00
|
|
|
"",
|
|
|
|
"Cluster",
|
|
|
|
"PinPath",
|
2019-07-12 14:40:29 +00:00
|
|
|
pinPath,
|
|
|
|
&pin,
|
2019-04-29 14:36:40 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If unpin != "false", unpin the FROM argument
|
|
|
|
// (it was already resolved).
|
Improve pin/unpin method signatures (#843)
* Improve pin/unpin method signatures:
These changes the following Cluster Go API methods:
* -> Cluster.Pin(ctx, cid, options) (pin, error)
* -> Cluster.Unpin(ctx, cid) (pin, error)
* -> Cluster.PinPath(ctx, path, opts) (pin,error)
Pin and Unpin now return the pinned object.
The signature of the methods now matches that of the API Client, is clearer as
to what options the user can set and is aligned with PinPath, UnpinPath, which
returned pin methods.
The REST API now returns the Pinned/Unpinned object rather than 204-Accepted.
This was necessary for a cleaner pin/update approach, which I'm working on in
another branch.
Most of the changes here are updating tests to the new signatures
* Adapt load-balancing client to new Pin/Unpin signatures
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
2019-07-22 13:39:11 +00:00
|
|
|
var pinObj api.Pin
|
2019-04-29 14:36:40 +00:00
|
|
|
if unpin {
|
|
|
|
err = proxy.rpcClient.CallContext(
|
|
|
|
ctx,
|
|
|
|
"",
|
|
|
|
"Cluster",
|
|
|
|
"Unpin",
|
2019-07-12 14:40:29 +00:00
|
|
|
api.PinCid(fromCid),
|
Improve pin/unpin method signatures (#843)
* Improve pin/unpin method signatures:
These changes the following Cluster Go API methods:
* -> Cluster.Pin(ctx, cid, options) (pin, error)
* -> Cluster.Unpin(ctx, cid) (pin, error)
* -> Cluster.PinPath(ctx, path, opts) (pin,error)
Pin and Unpin now return the pinned object.
The signature of the methods now matches that of the API Client, is clearer as
to what options the user can set and is aligned with PinPath, UnpinPath, which
returned pin methods.
The REST API now returns the Pinned/Unpinned object rather than 204-Accepted.
This was necessary for a cleaner pin/update approach, which I'm working on in
another branch.
Most of the changes here are updating tests to the new signatures
* Adapt load-balancing client to new Pin/Unpin signatures
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
2019-07-22 13:39:11 +00:00
|
|
|
&pinObj,
|
2019-04-29 14:36:40 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2019-04-29 14:36:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := ipfsPinOpResp{
|
2019-07-12 14:40:29 +00:00
|
|
|
Pins: []string{fromCid.String(), pin.Cid.String()},
|
2019-04-29 14:36:40 +00:00
|
|
|
}
|
|
|
|
resBytes, _ := json.Marshal(res)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(resBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) addHandler(w http.ResponseWriter, r *http.Request) {
|
2019-01-10 19:03:59 +00:00
|
|
|
proxy.setHeaders(w.Header(), r)
|
2018-12-18 14:44:11 +00:00
|
|
|
|
2018-10-13 14:27:03 +00:00
|
|
|
reader, err := r.MultipartReader()
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "error reading request: "+err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
if q.Get("only-hash") == "true" {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "only-hash is not supported when adding to cluster", -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unpin := q.Get("pin") == "false"
|
|
|
|
|
|
|
|
// Luckily, most IPFS add query params are compatible with cluster's
|
|
|
|
// /add params. We can parse most of them directly from the query.
|
|
|
|
params, err := api.AddParamsFromQuery(q)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, "error parsing options:"+err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
trickle := q.Get("trickle")
|
|
|
|
if trickle == "true" {
|
|
|
|
params.Layout = "trickle"
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Warningf("Proxy/add does not support all IPFS params. Current options: %+v", params)
|
|
|
|
|
|
|
|
outputTransform := func(in *api.AddedOutput) interface{} {
|
|
|
|
r := &ipfsAddResp{
|
|
|
|
Name: in.Name,
|
2019-03-06 18:48:25 +00:00
|
|
|
Hash: in.Cid.String(),
|
2018-10-13 14:27:03 +00:00
|
|
|
Bytes: int64(in.Bytes),
|
|
|
|
}
|
|
|
|
if in.Size != 0 {
|
|
|
|
r.Size = strconv.FormatUint(in.Size, 10)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := adderutils.AddMultipartHTTPHandler(
|
2018-11-01 10:24:05 +00:00
|
|
|
proxy.ctx,
|
|
|
|
proxy.rpcClient,
|
2018-10-13 14:27:03 +00:00
|
|
|
params,
|
|
|
|
reader,
|
|
|
|
w,
|
|
|
|
outputTransform,
|
|
|
|
)
|
|
|
|
|
|
|
|
// any errors have been sent as Trailer
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !unpin {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unpin because the user doesn't want to pin
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
Improve pin/unpin method signatures (#843)
* Improve pin/unpin method signatures:
These changes the following Cluster Go API methods:
* -> Cluster.Pin(ctx, cid, options) (pin, error)
* -> Cluster.Unpin(ctx, cid) (pin, error)
* -> Cluster.PinPath(ctx, path, opts) (pin,error)
Pin and Unpin now return the pinned object.
The signature of the methods now matches that of the API Client, is clearer as
to what options the user can set and is aligned with PinPath, UnpinPath, which
returned pin methods.
The REST API now returns the Pinned/Unpinned object rather than 204-Accepted.
This was necessary for a cleaner pin/update approach, which I'm working on in
another branch.
Most of the changes here are updating tests to the new signatures
* Adapt load-balancing client to new Pin/Unpin signatures
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
2019-07-22 13:39:11 +00:00
|
|
|
var pinObj api.Pin
|
2018-11-01 10:24:05 +00:00
|
|
|
err = proxy.rpcClient.CallContext(
|
|
|
|
proxy.ctx,
|
2018-10-13 14:27:03 +00:00
|
|
|
"",
|
|
|
|
"Cluster",
|
|
|
|
"Unpin",
|
2019-02-27 17:04:35 +00:00
|
|
|
root,
|
Improve pin/unpin method signatures (#843)
* Improve pin/unpin method signatures:
These changes the following Cluster Go API methods:
* -> Cluster.Pin(ctx, cid, options) (pin, error)
* -> Cluster.Unpin(ctx, cid) (pin, error)
* -> Cluster.PinPath(ctx, path, opts) (pin,error)
Pin and Unpin now return the pinned object.
The signature of the methods now matches that of the API Client, is clearer as
to what options the user can set and is aligned with PinPath, UnpinPath, which
returned pin methods.
The REST API now returns the Pinned/Unpinned object rather than 204-Accepted.
This was necessary for a cleaner pin/update approach, which I'm working on in
another branch.
Most of the changes here are updating tests to the new signatures
* Adapt load-balancing client to new Pin/Unpin signatures
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
* cluster.go: Fix typo
Co-Authored-By: Kishan Sagathiya <kishansagathiya@gmail.com>
2019-07-22 13:39:11 +00:00
|
|
|
&pinObj,
|
2018-10-13 14:27:03 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
w.Header().Set("X-Stream-Error", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
func (proxy *Server) repoStatHandler(w http.ResponseWriter, r *http.Request) {
|
2019-01-10 19:03:59 +00:00
|
|
|
proxy.setHeaders(w.Header(), r)
|
2018-12-18 14:44:11 +00:00
|
|
|
|
2018-11-03 14:54:15 +00:00
|
|
|
peers := make([]peer.ID, 0)
|
2018-11-01 10:24:05 +00:00
|
|
|
err := proxy.rpcClient.Call(
|
2018-10-13 14:27:03 +00:00
|
|
|
"",
|
2019-05-04 20:36:10 +00:00
|
|
|
"Consensus",
|
|
|
|
"Peers",
|
2018-10-13 14:27:03 +00:00
|
|
|
struct{}{},
|
|
|
|
&peers,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2019-05-02 09:32:13 +00:00
|
|
|
ipfsErrorResponder(w, err.Error(), -1)
|
2018-10-13 14:27:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
ctxs, cancels := rpcutil.CtxsWithCancel(proxy.ctx, len(peers))
|
2018-10-13 14:27:03 +00:00
|
|
|
defer rpcutil.MultiCancel(cancels)
|
|
|
|
|
2019-02-27 17:04:35 +00:00
|
|
|
repoStats := make([]*api.IPFSRepoStat, len(peers), len(peers))
|
2018-10-13 14:27:03 +00:00
|
|
|
repoStatsIfaces := make([]interface{}, len(repoStats), len(repoStats))
|
|
|
|
for i := range repoStats {
|
2019-02-27 18:43:29 +00:00
|
|
|
repoStats[i] = &api.IPFSRepoStat{}
|
2019-02-27 17:04:35 +00:00
|
|
|
repoStatsIfaces[i] = repoStats[i]
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 10:24:05 +00:00
|
|
|
errs := proxy.rpcClient.MultiCall(
|
2018-10-13 14:27:03 +00:00
|
|
|
ctxs,
|
|
|
|
peers,
|
2019-05-04 20:36:10 +00:00
|
|
|
"IPFSConnector",
|
|
|
|
"RepoStat",
|
2018-10-13 14:27:03 +00:00
|
|
|
struct{}{},
|
|
|
|
repoStatsIfaces,
|
|
|
|
)
|
|
|
|
|
|
|
|
totalStats := api.IPFSRepoStat{}
|
|
|
|
|
|
|
|
for i, err := range errs {
|
|
|
|
if err != nil {
|
2019-05-09 19:23:49 +00:00
|
|
|
if rpc.IsAuthorizationError(err) {
|
|
|
|
logger.Debug(err)
|
|
|
|
continue
|
|
|
|
}
|
2018-10-13 14:27:03 +00:00
|
|
|
logger.Errorf("%s repo/stat errored: %s", peers[i], err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalStats.RepoSize += repoStats[i].RepoSize
|
|
|
|
totalStats.StorageMax += repoStats[i].StorageMax
|
|
|
|
}
|
|
|
|
|
|
|
|
resBytes, _ := json.Marshal(totalStats)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(resBytes)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-10 19:03:59 +00:00
|
|
|
// slashHandler returns a handler which converts a /a/b/c/<argument> request
|
2019-01-11 12:36:56 +00:00
|
|
|
// into an /a/b/c/<argument>?arg=<argument> one. And uses the given origHandler
|
2019-01-10 19:03:59 +00:00
|
|
|
// for it. Our handlers expect that arguments are passed in the ?arg query
|
|
|
|
// value.
|
|
|
|
func slashHandler(origHandler http.HandlerFunc) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
warnMsg := "You are using an undocumented form of the IPFS API. "
|
2018-10-13 14:27:03 +00:00
|
|
|
warnMsg += "Consider passing your command arguments"
|
|
|
|
warnMsg += "with the '?arg=' query parameter"
|
2019-01-10 19:03:59 +00:00
|
|
|
logger.Error(warnMsg)
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
arg := vars["arg"]
|
|
|
|
|
|
|
|
// IF we needed to modify the request path, we could do
|
|
|
|
// something along these lines. This is not the case
|
|
|
|
// at the moment. We just need to set the query argument.
|
|
|
|
//
|
|
|
|
// route := mux.CurrentRoute(r)
|
|
|
|
// path, err := route.GetPathTemplate()
|
|
|
|
// if err != nil {
|
|
|
|
// // I'd like to panic, but I don' want to kill a full
|
|
|
|
// // peer just because of a buggy use.
|
|
|
|
// logger.Critical("BUG: wrong use of slashHandler")
|
|
|
|
// origHandler(w, r) // proceed as nothing
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// fixedPath := strings.TrimSuffix(path, "/{arg}")
|
|
|
|
// r.URL.Path = url.PathEscape(fixedPath)
|
|
|
|
// r.URL.RawPath = fixedPath
|
|
|
|
|
|
|
|
q := r.URL.Query()
|
|
|
|
q.Set("arg", arg)
|
|
|
|
r.URL.RawQuery = q.Encode()
|
|
|
|
origHandler(w, r)
|
2018-10-13 14:27:03 +00:00
|
|
|
}
|
|
|
|
}
|