From 1ed052ac63fff89a7661457a02c9a4fdf5291455 Mon Sep 17 00:00:00 2001 From: potsables Date: Fri, 28 Sep 2018 18:09:23 -0700 Subject: [PATCH] pkg: extract dccd --- dccd/dccd.go | 77 ----------------------------------------------- dccd/dccd_test.go | 43 -------------------------- dccd/utils.go | 40 ------------------------ 3 files changed, 160 deletions(-) delete mode 100644 dccd/dccd.go delete mode 100644 dccd/dccd_test.go delete mode 100644 dccd/utils.go diff --git a/dccd/dccd.go b/dccd/dccd.go deleted file mode 100644 index 0b462662e..000000000 --- a/dccd/dccd.go +++ /dev/null @@ -1,77 +0,0 @@ -package dccd - -/* - - DCCD is a utility used to request a particular content hash from all known IPFS gateways. - The intended purpose is to spread content throughout the IPFS network cache. - Additional features will be requesting content from user specified nodes as well, allowing for additional cache dispersion - - The initial idea is taken from `ipfg.sh` whose developer is Joss Brown (pseud.): https://github.com/JayBrown - Source code for that script is in the `scripts` folder -*/ - -import ( - "errors" - "fmt" - "time" - - ipfsapi "github.com/RTradeLtd/go-ipfs-api" -) - -type DCCDManager struct { - Shell *ipfsapi.Shell - Gateways map[int]string - TimeOut time.Duration -} - -// NewDCCDManager establishes our initial connection to our local IPFS node -func NewDCCDManager(connectionURL string, timeout time.Duration) *DCCDManager { - if connectionURL == "" { - // load a default api - connectionURL = "localhost:5001" - } - c := generateClient(timeout) - shell := ipfsapi.NewShellWithClient(connectionURL, c) - manager := &DCCDManager{Shell: shell} - manager.ParseGateways() - return manager -} - -func (dc *DCCDManager) ParseGateways() { - indexes := make(map[int]string) - for k, v := range GateArrays { - indexes[k] = v - } - dc.Gateways = indexes -} - -func (dc *DCCDManager) ReconnectShell(connectionURL string) error { - if connectionURL == "" { - return errors.New("please provide a valid connection url") - } - c := generateClient(dc.TimeOut) - shell := ipfsapi.NewShellWithClient(connectionURL, c) - dc.Shell = shell - return nil -} - -func (dc *DCCDManager) DisperseContentWithShell(contentHash string) (map[string]bool, error) { - m := make(map[string]bool) - for _, v := range GateArrays { - err := dc.ReconnectShell(v) - - r, err := dc.Shell.CatGet(contentHash) - if err != nil { - m[v] = false - fmt.Println("dispersal failed for host ", v) - continue - } - err = r.Close() - if err != nil { - fmt.Println("failed to close handler ", err) - } - fmt.Println("dispersal suceeded for host ", v) - m[v] = true - } - return m, nil -} diff --git a/dccd/dccd_test.go b/dccd/dccd_test.go deleted file mode 100644 index 323dc3152..000000000 --- a/dccd/dccd_test.go +++ /dev/null @@ -1,43 +0,0 @@ -package dccd_test - -import ( - "fmt" - "testing" - "time" - - "github.com/RTradeLtd/Temporal/dccd" -) - -var testHash = "Qmbu7x6gJbsKDcseQv66pSbUcAA3Au6f7MfTYVXwvBxN2K" - -func TestDispersal(t *testing.T) { - manager := dccd.NewDCCDManager("", 100*time.Second) - resp, err := manager.DisperseContentWithShell(testHash) - if err != nil { - t.Fatal(err) - } - fmt.Println(resp) -} - -func TestDCCD(t *testing.T) { - manager := dccd.NewDCCDManager("", 100*time.Second) - // Parse gateway array - manager.ParseGateways() - dispersals, err := manager.DisperseContentWithShell(testHash) - if err != nil { - t.Fatal(err) - } - var failureCount int - var successCount int - for k, v := range dispersals { - if v == false { - fmt.Printf("dispersal for %s failed\n", k) - failureCount++ - continue - } - fmt.Printf("dispersal for %s passed", k) - successCount++ - } - fmt.Println("Number of failures", failureCount) - fmt.Println("Number of successes", successCount) -} diff --git a/dccd/utils.go b/dccd/utils.go deleted file mode 100644 index b05b9213f..000000000 --- a/dccd/utils.go +++ /dev/null @@ -1,40 +0,0 @@ -package dccd - -import ( - "net/http" - "time" -) - -const ( - g1 = "https://ipfs.io" - g2 = "https://gateway.ipfs.io" - g3 = "https://ipfs.infura.io" - g4 = "https://rx14.co.uk" - g5 = "https://xmine128.tk" - g6 = "https://upload.global" - g7 = "https://ipfs.jes.xxx" - g8 = "https://catalunya.network" - g9 = "https://siderus.io" - g10 = "https://www.eternum.io" - g11 = "https://hardbin.com" - g12 = "https://ipfs.macholibre.org" - g13 = "https://ipfs.works" - g14 = "https://ipfs.work" - g15 = "https://ipfs.wa.hle.rs" - g16 = "https://api.wisdom.sh" - g17 = "https://gateway.blocksec.com" - g18 = "https://ipfs.renehsz.com" - numGateways = 18 -) - -var GateArrays = []string{g1, g2, g3, g4, g5, g6, g7, g8, g9, g10, g11, g12, g13, g14, g15, g16, g17, g18} - -func generateClient(timeout time.Duration) *http.Client { - return &http.Client{ - Transport: &http.Transport{ - Proxy: http.ProxyFromEnvironment, - DisableKeepAlives: true, - }, - Timeout: timeout, - } -}