示例#1
0
文件: ipboh.go 项目: jamesunger/ipboh
func clientHandlerIndex(w http.ResponseWriter, ctx context.Context, n *core.IpfsNode, baseurl, targethash string, verbose bool) {
	target, err := peer.IDB58Decode(targethash)
	if err != nil {
		http.Error(w, fmt.Sprintf("%s", err), 500)
	}

	entrylist := getEntryList(n, target)

	if verbose {
		fmt.Fprintln(w, "<html><body><p><a href=\"/\">(default list)</a></p><p><pre><ul style=\"list-style: none;\">")
		for i := len(entrylist.Entries) - 1; i >= 0; i-- {
			ts := entrylist.Entries[i].Timestamp.Format("2006-01-02T15:04")
			fmt.Fprintf(w, "<li><a href=\"/cat?hash=%s&target=%s\">%s</a> %s %s <a href=\"/cat?hash=%s&target=%s\">%s</a></li>", entrylist.Entries[i].Hash, targethash, entrylist.Entries[i].Hash, ts, bytefmt.ByteSize(uint64(entrylist.Entries[i].Size)), entrylist.Entries[i].Name, targethash, entrylist.Entries[i].Name)
		}
		fmt.Fprintln(w, "</ul></pre></p></body></html>")
	} else {

		seen := make(map[string]bool)
		hidelist := getHideList(targethash, baseurl, entrylist)

		fmt.Fprintln(w, "<html><body><p><a href=\"/?verbose=true\">(verbose list)</a></p><p><pre><ul style=\"list-style: none;\">")
		for i := len(entrylist.Entries) - 1; i >= 0; i-- {
			_, exists := seen[entrylist.Entries[i].Name]
			_, existsh := hidelist[entrylist.Entries[i].Name]
			if !exists && !existsh {
				fmt.Fprintf(w, "<li><a href=\"/cat?hash=%s&target=%s\">%s</a> <a href=\"/cat?hash=%s&target=%s\">%s</a></li>", entrylist.Entries[i].Hash, targethash, entrylist.Entries[i].Hash, entrylist.Entries[i].Name, targethash, entrylist.Entries[i].Name)
			}
			seen[entrylist.Entries[i].Name] = true
		}
		fmt.Fprintln(w, "</ul></pre></p></body></html>")
	}
	return
}
示例#2
0
// ParseMultiaddr parses a multiaddr into an IPFSAddr
func ParseMultiaddr(m ma.Multiaddr) (a IPFSAddr, err error) {
	// never panic.
	defer func() {
		if r := recover(); r != nil {
			log.Debug("recovered from panic: ", r)
			a = nil
			err = ErrInvalidAddr
		}
	}()

	if m == nil {
		return nil, ErrInvalidAddr
	}

	// make sure it's an ipfs addr
	parts := ma.Split(m)
	if len(parts) < 1 {
		return nil, ErrInvalidAddr
	}
	ipfspart := parts[len(parts)-1] // last part
	if ipfspart.Protocols()[0].Code != ma.P_IPFS {
		return nil, ErrInvalidAddr
	}

	// make sure ipfs id parses as a peer.ID
	peerIdParts := path.SplitList(ipfspart.String())
	peerIdStr := peerIdParts[len(peerIdParts)-1]
	id, err := peer.IDB58Decode(peerIdStr)
	if err != nil {
		return nil, err
	}

	return ipfsAddr{ma: m, id: id}, nil
}
示例#3
0
文件: ipboh.go 项目: jamesunger/ipboh
func clientHandlerSync(w http.ResponseWriter, ctx context.Context, n *core.IpfsNode, dspath string, targethash string, reloadindex chan *Entry) {
	target, err := peer.IDB58Decode(targethash)
	if err != nil {
		panic(err)
	}

	curentries := loadIndex(n, ctx, dspath)
	entrymap := make(map[string]bool)
	if len(curentries.Entries) != 0 {
		for i := range curentries.Entries {
			key := fmt.Sprintf("%v", curentries.Entries[i])
			//fmt.Println(key)
			//entrymap[curentries.Entries[i].Hash] = curentries.Entries[i]
			entrymap[key] = true
		}
	}

	fmt.Fprintln(w, "Syncing...", target)
	entrylist := getEntryList(n, target)

	for i := range entrylist.Entries {
		fmt.Fprintln(w, "Downloading ", entrylist.Entries[i].Name)
		reader, err := coreunix.Cat(ctx, n, entrylist.Entries[i].Hash)
		if err != nil {
			panic(err)
		}
		ioutil.ReadAll(reader)

		if len(curentries.Entries) != 0 {
			_, ok := entrymap[fmt.Sprintf("%v", entrylist.Entries[i])]
			if ok {
				fmt.Fprintln(w, "Already have", entrylist.Entries[i].Hash)
			} else {
				fmt.Fprintln(w, "Appending", entrylist.Entries[i].Hash)
				curentries.Entries = append(curentries.Entries, entrylist.Entries[i])
			}
		}

		//FIXME: potential data corruption because it could collide with the main startserver thread running
		err = pin(n, ctx, entrylist.Entries[i].Hash)
		if err != nil {
			panic(err)
		}
		fmt.Fprintln(w, "Pinned", entrylist.Entries[i].Hash, entrylist.Entries[i].Name)
	}

	if len(curentries.Entries) != 0 {
		saveIndex(curentries, dspath)
	} else {
		saveIndex(entrylist, dspath)
	}

	fmt.Fprintln(w, "Sync complete.")
	reloadindex <- &Entry{}
}
示例#4
0
文件: ipboh.go 项目: jamesunger/ipboh
func clientHandlerLs(w http.ResponseWriter, n *core.IpfsNode, targethash string) {
	target, err := peer.IDB58Decode(targethash)
	if err != nil {
		http.Error(w, fmt.Sprintf("%s", err), 500)
	}

	entrylist := getEntryList(n, target)
	elbytes, err := json.Marshal(entrylist)
	//fmt.Println("ls request sending ", string(elbytes))
	if err != nil {
		http.Error(w, fmt.Sprintf("Error marshaling json:", err), 500)
		return
	}
	w.Write(elbytes)
}
示例#5
0
文件: ipboh.go 项目: jamesunger/ipboh
func clientHandlerAdd(w http.ResponseWriter, rdr io.Reader, n *core.IpfsNode, targethash string) {
	target, err := peer.IDB58Decode(targethash)
	if err != nil {
		http.Error(w, fmt.Sprintf("%s", err), 500)
	}

	//fmt.Println("Dialing...", targethash)
	con, err := MyDial(n, target, "/pack/add")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer con.Close()

	_, err = io.Copy(con, rdr)
	if err != nil {
		http.Error(w, fmt.Sprintf("Error adding file:", err), 500)
		return
	}
}
示例#6
0
func TestIDMatches(t *testing.T) {
	for _, g := range good {
		a, err := ParseString(g)
		if err != nil {
			t.Error("failed to parse", g, err)
			continue
		}

		sp := path.SplitList(g)
		sid := sp[len(sp)-1]
		id, err := peer.IDB58Decode(sid)
		if err != nil {
			t.Error("failed to parse", sid, err)
			continue
		}

		if a.ID() != id {
			t.Error("not equal", a.ID(), id)
		}
	}
}
示例#7
0
文件: ipboh.go 项目: jamesunger/ipboh
func clientHandlerCat(ctx context.Context, w http.ResponseWriter, n *core.IpfsNode, hash, targethash string) {
	target, err := peer.IDB58Decode(targethash)
	if err != nil {
		http.Error(w, fmt.Sprintf("%s", err), 500)
	}

	// FIXME: validate this in case there is a 46 len name!
	foundhash := false
	w.Header().Set("Content-Disposition", fmt.Sprintf("filename=\"%s\"", hash))
	if len(hash) != 46 {
		entrylist := getEntryList(n, target)
		//fmt.Println(entrylist)
		for i := len(entrylist.Entries) - 1; i >= 0; i-- {
			if entrylist.Entries[i].Name == hash {
				hash = entrylist.Entries[i].Hash
				foundhash = true
				break
			}
		}
	} else {
		foundhash = true
	}

	if !foundhash {
		http.Error(w, "No entry found.", 500)
		return
	}

	reader, err := coreunix.Cat(ctx, n, hash)
	if err != nil {
		panic(err)
	}

	_, err = io.Copy(w, reader)
	if err != nil {
		http.Error(w, fmt.Sprintf("Error reading or writing entry:", err), 500)
		return
	}
}
示例#8
0
文件: query.go 项目: Kubuxu/go-ipfs
func (qe *QueryEvent) UnmarshalJSON(b []byte) error {
	temp := struct {
		ID        string
		Type      int
		Responses []*peer.PeerInfo
		Extra     string
	}{}
	err := json.Unmarshal(b, &temp)
	if err != nil {
		return err
	}
	if len(temp.ID) > 0 {
		pid, err := peer.IDB58Decode(temp.ID)
		if err != nil {
			return err
		}
		qe.ID = pid
	}
	qe.Type = QueryEventType(temp.Type)
	qe.Responses = temp.Responses
	qe.Extra = temp.Extra
	return nil
}
示例#9
0
文件: stat.go 项目: Kubuxu/go-ipfs
			return
		}

		tstr, tfound, err := req.Option("proto").String()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		if pfound && tfound {
			res.SetError(errors.New("please only specify peer OR protocol"), cmds.ErrClient)
			return
		}

		var pid peer.ID
		if pfound {
			checkpid, err := peer.IDB58Decode(pstr)
			if err != nil {
				res.SetError(err, cmds.ErrNormal)
				return
			}
			pid = checkpid
		}

		interval := time.Second
		timeS, found, err := req.Option("interval").String()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}
		if found {
			v, err := time.ParseDuration(timeS)
示例#10
0
文件: dht.go 项目: Kubuxu/go-ipfs
		cmds.BoolOption("verbose", "v", "Write debug information."),
	},
	Run: func(req cmds.Request, res cmds.Response) {
		n, err := req.InvocContext().GetNode()
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		dht, ok := n.Routing.(*ipdht.IpfsDHT)
		if !ok {
			res.SetError(ErrNotDHT, cmds.ErrNormal)
			return
		}

		pid, err := peer.IDB58Decode(req.Arguments()[0])
		if err != nil {
			res.SetError(err, cmds.ErrNormal)
			return
		}

		outChan := make(chan interface{})
		res.SetOutput((<-chan interface{})(outChan))

		events := make(chan *notif.QueryEvent)
		ctx := notif.RegisterForQueryEvents(req.Context(), events)

		go func() {
			defer close(outChan)
			for v := range events {
				outChan <- v