2016-12-02 18:33:39 +00:00
|
|
|
package ipfscluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-12-05 15:24:41 +00:00
|
|
|
"net"
|
2016-12-02 18:33:39 +00:00
|
|
|
"net/http"
|
2017-01-23 17:38:59 +00:00
|
|
|
"strconv"
|
2016-12-02 18:33:39 +00:00
|
|
|
"strings"
|
2016-12-15 13:07:19 +00:00
|
|
|
"sync"
|
2016-12-22 10:31:09 +00:00
|
|
|
"time"
|
2016-12-02 18:33:39 +00:00
|
|
|
|
2017-01-25 11:14:39 +00:00
|
|
|
rpc "github.com/hsanjuan/go-libp2p-gorpc"
|
2016-12-16 11:40:28 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2017-01-23 17:38:59 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
2016-12-02 18:33:39 +00:00
|
|
|
)
|
|
|
|
|
2016-12-22 10:31:09 +00:00
|
|
|
// IPFS Proxy settings
|
|
|
|
var (
|
|
|
|
// maximum duration before timing out read of the request
|
|
|
|
IPFSProxyServerReadTimeout = 5 * time.Second
|
|
|
|
// maximum duration before timing out write of the response
|
|
|
|
IPFSProxyServerWriteTimeout = 10 * time.Second
|
|
|
|
// server-side the amount of time a Keep-Alive connection will be
|
|
|
|
// kept idle before being reused
|
|
|
|
IPFSProxyServerIdleTimeout = 60 * time.Second
|
|
|
|
)
|
|
|
|
|
2016-12-02 18:33:39 +00:00
|
|
|
// IPFSHTTPConnector implements the IPFSConnector interface
|
|
|
|
// and provides a component which does two tasks:
|
|
|
|
//
|
|
|
|
// On one side, it proxies HTTP requests to the configured IPFS
|
|
|
|
// daemon. It is able to intercept these requests though, and
|
|
|
|
// perform extra operations on them.
|
|
|
|
//
|
|
|
|
// On the other side, it is used to perform on-demand requests
|
|
|
|
// against the configured IPFS daemom (such as a pin request).
|
|
|
|
type IPFSHTTPConnector struct {
|
|
|
|
ctx context.Context
|
2017-01-23 22:58:04 +00:00
|
|
|
nodeAddr ma.Multiaddr
|
|
|
|
proxyAddr ma.Multiaddr
|
2016-12-02 18:33:39 +00:00
|
|
|
destHost string
|
|
|
|
destPort int
|
|
|
|
listenAddr string
|
|
|
|
listenPort int
|
2016-12-23 18:35:37 +00:00
|
|
|
|
|
|
|
handlers map[string]func(http.ResponseWriter, *http.Request)
|
|
|
|
|
|
|
|
rpcClient *rpc.Client
|
|
|
|
rpcReady chan struct{}
|
2016-12-05 15:24:41 +00:00
|
|
|
|
2016-12-09 19:54:46 +00:00
|
|
|
listener net.Listener
|
|
|
|
server *http.Server
|
|
|
|
|
2016-12-15 13:07:19 +00:00
|
|
|
shutdownLock sync.Mutex
|
|
|
|
shutdown bool
|
|
|
|
wg sync.WaitGroup
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ipfsError struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:07:19 +00:00
|
|
|
type pinLsResp struct {
|
|
|
|
Keys map[string]struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-02 18:33:39 +00:00
|
|
|
// NewIPFSHTTPConnector creates the component and leaves it ready to be started
|
2016-12-15 18:08:46 +00:00
|
|
|
func NewIPFSHTTPConnector(cfg *Config) (*IPFSHTTPConnector, error) {
|
2016-12-09 19:54:46 +00:00
|
|
|
ctx := context.Background()
|
2017-01-23 17:38:59 +00:00
|
|
|
destHost, err := cfg.IPFSNodeAddr.ValueForProtocol(ma.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
destPortStr, err := cfg.IPFSNodeAddr.ValueForProtocol(ma.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
destPort, err := strconv.Atoi(destPortStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listenAddr, err := cfg.IPFSProxyAddr.ValueForProtocol(ma.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listenPortStr, err := cfg.IPFSProxyAddr.ValueForProtocol(ma.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
listenPort, err := strconv.Atoi(listenPortStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-05 15:24:41 +00:00
|
|
|
l, err := net.Listen("tcp", fmt.Sprintf("%s:%d",
|
2017-01-23 17:38:59 +00:00
|
|
|
listenAddr, listenPort))
|
2016-12-05 15:24:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-12-09 19:54:46 +00:00
|
|
|
|
|
|
|
smux := http.NewServeMux()
|
|
|
|
s := &http.Server{
|
2016-12-22 10:31:09 +00:00
|
|
|
ReadTimeout: IPFSProxyServerReadTimeout,
|
|
|
|
WriteTimeout: IPFSProxyServerWriteTimeout,
|
|
|
|
// IdleTimeout: IPFSProxyServerIdleTimeout, // TODO Go 1.8
|
2016-12-09 19:54:46 +00:00
|
|
|
Handler: smux,
|
|
|
|
}
|
|
|
|
s.SetKeepAlivesEnabled(true) // A reminder that this can be changed
|
|
|
|
|
2016-12-02 18:33:39 +00:00
|
|
|
ipfs := &IPFSHTTPConnector{
|
2017-01-23 22:58:04 +00:00
|
|
|
ctx: ctx,
|
|
|
|
nodeAddr: cfg.IPFSProxyAddr,
|
|
|
|
proxyAddr: cfg.IPFSNodeAddr,
|
|
|
|
|
2017-01-23 17:38:59 +00:00
|
|
|
destHost: destHost,
|
|
|
|
destPort: destPort,
|
|
|
|
listenAddr: listenAddr,
|
|
|
|
listenPort: listenPort,
|
2016-12-02 18:33:39 +00:00
|
|
|
handlers: make(map[string]func(http.ResponseWriter, *http.Request)),
|
2016-12-23 18:35:37 +00:00
|
|
|
rpcReady: make(chan struct{}, 1),
|
2016-12-05 15:24:41 +00:00
|
|
|
listener: l,
|
2016-12-09 19:54:46 +00:00
|
|
|
server: s,
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 19:54:46 +00:00
|
|
|
smux.HandleFunc("/", ipfs.handle)
|
|
|
|
|
2016-12-15 13:07:19 +00:00
|
|
|
ipfs.run()
|
2016-12-02 18:33:39 +00:00
|
|
|
return ipfs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will run a custom handler if we have one for a URL.Path, or
|
|
|
|
// otherwise just proxy the requests.
|
|
|
|
func (ipfs *IPFSHTTPConnector) handle(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if customHandler, ok := ipfs.handlers[r.URL.Path]; ok {
|
|
|
|
customHandler(w, r)
|
|
|
|
} else {
|
|
|
|
ipfs.defaultHandler(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// defaultHandler just proxies the requests
|
|
|
|
func (ipfs *IPFSHTTPConnector) defaultHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
newURL := *r.URL
|
|
|
|
newURL.Host = fmt.Sprintf("%s:%d", ipfs.destHost, ipfs.destPort)
|
|
|
|
newURL.Scheme = "http"
|
|
|
|
|
|
|
|
proxyReq, err := http.NewRequest(r.Method, newURL.String(), r.Body)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error creating proxy request: ", err)
|
|
|
|
http.Error(w, "error forwarding request", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(proxyReq)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("error forwarding request: ", err)
|
|
|
|
http.Error(w, "error forwaring request", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set response headers
|
|
|
|
for k, v := range resp.Header {
|
|
|
|
for _, s := range v {
|
|
|
|
w.Header().Add(k, s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// And body
|
|
|
|
io.Copy(w, resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ipfs *IPFSHTTPConnector) run() {
|
|
|
|
// This launches the proxy
|
2016-12-15 13:07:19 +00:00
|
|
|
ipfs.wg.Add(1)
|
2016-12-02 18:33:39 +00:00
|
|
|
go func() {
|
2016-12-15 13:07:19 +00:00
|
|
|
defer ipfs.wg.Done()
|
2016-12-09 19:54:46 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
ipfs.ctx = ctx
|
2016-12-23 18:35:37 +00:00
|
|
|
|
|
|
|
<-ipfs.rpcReady
|
|
|
|
|
2017-01-23 22:58:04 +00:00
|
|
|
logger.Infof("IPFS Proxy: %s -> %s",
|
|
|
|
ipfs.proxyAddr,
|
|
|
|
ipfs.nodeAddr)
|
2016-12-09 19:54:46 +00:00
|
|
|
err := ipfs.server.Serve(ipfs.listener)
|
2016-12-15 13:07:19 +00:00
|
|
|
if err != nil && !strings.Contains(err.Error(), "closed network connection") {
|
|
|
|
logger.Error(err)
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-12-23 18:35:37 +00:00
|
|
|
// SetClient makes the component ready to perform RPC
|
|
|
|
// requests.
|
|
|
|
func (ipfs *IPFSHTTPConnector) SetClient(c *rpc.Client) {
|
|
|
|
ipfs.rpcClient = c
|
|
|
|
ipfs.rpcReady <- struct{}{}
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops any listeners and stops the component from taking
|
|
|
|
// any requests.
|
|
|
|
func (ipfs *IPFSHTTPConnector) Shutdown() error {
|
2016-12-15 13:07:19 +00:00
|
|
|
ipfs.shutdownLock.Lock()
|
|
|
|
defer ipfs.shutdownLock.Unlock()
|
|
|
|
|
|
|
|
if ipfs.shutdown {
|
|
|
|
logger.Debug("already shutdown")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-15 13:19:41 +00:00
|
|
|
logger.Info("stopping IPFS Proxy")
|
2016-12-15 13:07:19 +00:00
|
|
|
|
2016-12-23 18:35:37 +00:00
|
|
|
close(ipfs.rpcReady)
|
2016-12-09 19:54:46 +00:00
|
|
|
ipfs.server.SetKeepAlivesEnabled(false)
|
2016-12-05 15:24:41 +00:00
|
|
|
ipfs.listener.Close()
|
2016-12-15 13:07:19 +00:00
|
|
|
|
|
|
|
ipfs.wg.Wait()
|
|
|
|
ipfs.shutdown = true
|
2016-12-02 18:33:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pin performs a pin request against the configured IPFS
|
|
|
|
// daemon.
|
|
|
|
func (ipfs *IPFSHTTPConnector) Pin(hash *cid.Cid) error {
|
2017-01-25 17:07:19 +00:00
|
|
|
pinStatus, err := ipfs.PinLsCid(hash)
|
2016-12-05 14:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-25 17:07:19 +00:00
|
|
|
if !pinStatus.IsPinned() {
|
2016-12-02 18:33:39 +00:00
|
|
|
path := fmt.Sprintf("pin/add?arg=%s", hash)
|
|
|
|
_, err = ipfs.get(path)
|
2016-12-19 17:35:24 +00:00
|
|
|
if err == nil {
|
|
|
|
logger.Info("IPFS Pin request succeeded: ", hash)
|
|
|
|
}
|
2016-12-02 18:33:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-01-23 22:58:04 +00:00
|
|
|
logger.Debug("IPFS object is already pinned: ", hash)
|
2016-12-02 18:33:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-28 15:25:24 +00:00
|
|
|
// Unpin performs an unpin request against the configured IPFS
|
2016-12-02 18:33:39 +00:00
|
|
|
// daemon.
|
|
|
|
func (ipfs *IPFSHTTPConnector) Unpin(hash *cid.Cid) error {
|
2017-01-25 17:07:19 +00:00
|
|
|
pinStatus, err := ipfs.PinLsCid(hash)
|
2016-12-05 14:30:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-25 17:07:19 +00:00
|
|
|
if pinStatus.IsPinned() {
|
2016-12-02 18:33:39 +00:00
|
|
|
path := fmt.Sprintf("pin/rm?arg=%s", hash)
|
|
|
|
_, err := ipfs.get(path)
|
2016-12-19 17:35:24 +00:00
|
|
|
if err == nil {
|
|
|
|
logger.Info("IPFS Unpin request succeeded:", hash)
|
|
|
|
}
|
2016-12-02 18:33:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2016-12-07 16:21:29 +00:00
|
|
|
|
2017-01-23 22:58:04 +00:00
|
|
|
logger.Debug("IPFS object is already unpinned: ", hash)
|
2016-12-02 18:33:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-25 17:07:19 +00:00
|
|
|
func parseIPFSPinType(t string) IPFSPinStatus {
|
|
|
|
switch {
|
|
|
|
case t == "indirect":
|
|
|
|
return IPFSPinStatusIndirect
|
|
|
|
case t == "direct":
|
|
|
|
return IPFSPinStatusDirect
|
|
|
|
case t == "recursive":
|
|
|
|
return IPFSPinStatusRecursive
|
|
|
|
default:
|
|
|
|
return IPFSPinStatusBug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PinLs performs a "pin ls" request against the configured IPFS daemon and
|
|
|
|
// returns a map of cid strings and their status.
|
|
|
|
func (ipfs *IPFSHTTPConnector) PinLs() (map[string]IPFSPinStatus, error) {
|
|
|
|
body, err := ipfs.get("pin/ls")
|
|
|
|
|
|
|
|
// Some error talking to the daemon
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp pinLsResp
|
|
|
|
err = json.Unmarshal(body, &resp)
|
2016-12-07 16:21:29 +00:00
|
|
|
if err != nil {
|
2017-01-25 17:07:19 +00:00
|
|
|
logger.Error("parsing pin/ls response")
|
|
|
|
logger.Error(string(body))
|
|
|
|
return nil, err
|
2016-12-07 16:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:07:19 +00:00
|
|
|
statusMap := make(map[string]IPFSPinStatus)
|
|
|
|
for k, v := range resp.Keys {
|
|
|
|
statusMap[k] = parseIPFSPinType(v.Type)
|
2016-12-07 16:21:29 +00:00
|
|
|
}
|
2017-01-25 17:07:19 +00:00
|
|
|
return statusMap, nil
|
2016-12-07 16:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:07:19 +00:00
|
|
|
// PinLsCid performs a "pin ls <hash> "request and returns IPFSPinStatus for
|
|
|
|
// that hash.
|
|
|
|
func (ipfs *IPFSHTTPConnector) PinLsCid(hash *cid.Cid) (IPFSPinStatus, error) {
|
2016-12-02 18:33:39 +00:00
|
|
|
lsPath := fmt.Sprintf("pin/ls?arg=%s", hash)
|
|
|
|
body, err := ipfs.get(lsPath)
|
2016-12-05 14:30:11 +00:00
|
|
|
|
|
|
|
// Network error, daemon down
|
|
|
|
if body == nil && err != nil {
|
2017-01-25 17:07:19 +00:00
|
|
|
return IPFSPinStatusError, err
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
2016-12-05 14:30:11 +00:00
|
|
|
// Pin not found likely here
|
|
|
|
if err != nil { // Not pinned
|
2017-01-25 17:07:19 +00:00
|
|
|
return IPFSPinStatusUnpinned, nil
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 17:07:19 +00:00
|
|
|
var resp pinLsResp
|
2016-12-02 18:33:39 +00:00
|
|
|
err = json.Unmarshal(body, &resp)
|
|
|
|
if err != nil {
|
2017-01-25 17:07:19 +00:00
|
|
|
logger.Error("parsing pin/ls?arg=cid response:")
|
2016-12-08 16:24:38 +00:00
|
|
|
logger.Error(string(body))
|
2017-01-25 17:07:19 +00:00
|
|
|
return IPFSPinStatusError, err
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
pinObj, ok := resp.Keys[hash.String()]
|
|
|
|
if !ok {
|
2017-01-25 17:07:19 +00:00
|
|
|
return IPFSPinStatusError, errors.New("expected to find the pin in the response")
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
2017-01-25 17:07:19 +00:00
|
|
|
|
|
|
|
return parseIPFSPinType(pinObj.Type), nil
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get performs the heavy lifting of a get request against
|
|
|
|
// the IPFS daemon.
|
|
|
|
func (ipfs *IPFSHTTPConnector) get(path string) ([]byte, error) {
|
2016-12-15 13:19:41 +00:00
|
|
|
logger.Debugf("getting %s", path)
|
2016-12-02 18:33:39 +00:00
|
|
|
url := fmt.Sprintf("%s/%s",
|
|
|
|
ipfs.apiURL(),
|
|
|
|
path)
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
2016-12-15 13:19:41 +00:00
|
|
|
logger.Error("error getting:", err)
|
2016-12-02 18:33:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-12-15 13:19:41 +00:00
|
|
|
logger.Errorf("error reading response body: %s", err)
|
2016-12-02 18:33:39 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ipfsErr ipfsError
|
|
|
|
decodeErr := json.Unmarshal(body, &ipfsErr)
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
var msg string
|
|
|
|
if decodeErr == nil {
|
2016-12-15 13:07:19 +00:00
|
|
|
msg = fmt.Sprintf("IPFS unsuccessful: %d: %s",
|
2016-12-02 18:33:39 +00:00
|
|
|
resp.StatusCode, ipfsErr.Message)
|
|
|
|
} else {
|
2016-12-15 13:07:19 +00:00
|
|
|
msg = fmt.Sprintf("IPFS-get unsuccessful: %d: %s",
|
2016-12-02 18:33:39 +00:00
|
|
|
resp.StatusCode, body)
|
|
|
|
}
|
2016-12-15 13:07:19 +00:00
|
|
|
logger.Warning(msg)
|
2016-12-05 14:30:11 +00:00
|
|
|
return body, errors.New(msg)
|
2016-12-02 18:33:39 +00:00
|
|
|
}
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// apiURL is a short-hand for building the url of the IPFS
|
|
|
|
// daemon API.
|
|
|
|
func (ipfs *IPFSHTTPConnector) apiURL() string {
|
|
|
|
return fmt.Sprintf("http://%s:%d/api/v0",
|
|
|
|
ipfs.destHost,
|
|
|
|
ipfs.destPort)
|
|
|
|
}
|