// Implements the h0tb0x transfer protocol func (this *PublicIdentity) Encode(stream io.Writer) error { data, err := x509.MarshalPKIXPublicKey(this.key) if err != nil { return err } return transfer.Encode(stream, data) }
// Simplified hashing for things which serialize via transfer.Encode, panics on error. func HashOf(objs ...interface{}) *Digest { h := NewHasher() err := transfer.Encode(h, objs...) if err != nil { panic(err) } return h.Finalize() }
func (this *DataMgr) downloadLoop() { this.Log.Printf("Entering download loop") this.lock.Lock() // Lock is held *except* when doing remote calls & sleeping // While I'm not closing for !this.isClosing { this.SyncMgr.Log.Printf("Looking things to download\n") // Get a object to download var key string row := this.Db.SingleQuery("SELECT key FROM Blob WHERE needs_download = 1") if !this.Db.MaybeScan(row, &key) { this.Log.Printf("Nothing to download, sleeping\n") this.download.Wait() continue } friends := this.allAdverts(key) if len(friends) == 0 { this.Log.Printf("Strangely got a download where len(friends) = 0") this.Db.Exec("UPDATE Blob SET needs_download = 0 WHERE key = ?", key) continue } friend := friends[rand.Intn(len(friends))] obj := this.getObj(key) obj.startDownload() this.writeObj(obj) this.lock.Unlock() this.Log.Printf("Doing a download of %s from %d!\n", key, friend) var send_buf bytes.Buffer transfer.Encode(&send_buf, key) tmppath := path.Join(this.incoming, crypto.RandomString()) file, _ := os.Create(tmppath) hasher := crypto.NewHasher() both := io.MultiWriter(file, hasher) tryTime := time.Now() err := this.Send(link.ServiceData, friend, &send_buf, both) if err != nil && time.Now().Sub(tryTime) < 5*time.Second { // TODO: Make this not suck time.Sleep(5 * time.Second) } if err != nil { this.Log.Printf("Download failed: %s", err) } else { this.Log.Printf("Download worked!") } this.lock.Lock() obj = this.getObj(key) obj.finishDownload(tmppath, err == nil) this.writeObj(obj) } this.lock.Unlock() this.goRoutines.Done() }
func (this *clientLooper) notifyLoop() { this.lock.Lock() // Lock is held *except* when doing remote calls & sleeping // While I'm not closing for !this.isClosing() { this.sync.Log.Printf("Looking for things to notify\n") sql := ` SELECT o.topic, o.seqno, o.key, o.value, o.type, o.author, o.priority, o.signature FROM Object o, TopicFriend tf WHERE o.topic = tf.topic AND tf.friend_id = ? AND tf.desired = 1 AND tf.requested = 1 AND o.seqno > tf.acked_seqno ORDER BY o.seqno LIMIT 100` rows := this.sync.Db.MultiQuery(sql, this.friendId) data := []dataMesg{} orm := make(map[string]int) for rows.Next() { var m dataMesg var tmp []byte this.sync.Db.Scan(rows, &m.Topic, &m.Seqno, &m.Key, &m.Value, &m.RecordType, &tmp, &m.Priority, &m.Signature) m.Author = string(tmp) orm[m.Topic] = m.Seqno data = append(data, m) } if len(data) > 0 { this.sync.Log.Printf("Doing an notify of %d rows\n", len(data)) this.lock.Unlock() var send_buf, recv_buf bytes.Buffer transfer.Encode(&send_buf, data) err := this.safeSend(link.ServiceNotify, &send_buf, &recv_buf) this.lock.Lock() if err != nil { continue } for topic, seqno := range orm { // Mark that we did the notify this.sync.Db.Exec("UPDATE TopicFriend SET acked_seqno = ? WHERE friend_id = ? AND topic = ?", seqno, this.friendId, topic) } } else { this.sync.Log.Printf("No notifies, sleeping\n") this.wakeNotify.Wait() } } this.lock.Unlock() this.goRoutines.Done() }
// Implements the h0tb0x transfer protocol func (this *LockedIdentity) Encode(stream io.Writer) error { return transfer.Encode(stream, this.impl) }
// Implements the h0tb0x transfer protocol func (this *EncryptedKey) Encode(stream io.Writer) error { return transfer.Encode(stream, this.impl) }
// Implements the h0tb0x transfer protocol func (this *SKSignature) Encode(stream io.Writer) error { return transfer.Encode(stream, this.impl) }