當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Device.PublicKey方法代碼示例

本文整理匯總了Golang中github.com/emersion/go-kdeconnect/network.Device.PublicKey方法的典型用法代碼示例。如果您正苦於以下問題:Golang Device.PublicKey方法的具體用法?Golang Device.PublicKey怎麽用?Golang Device.PublicKey使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/emersion/go-kdeconnect/network.Device的用法示例。


在下文中一共展示了Device.PublicKey方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handleDevice

func (e *Engine) handleDevice(device *network.Device) {
	e.devices[device.Id] = device

	if i := e.getKnownDevice(device); i != -1 {
		device.Paired = true
		//device.PublicKey = e.config.KnownDevices[i].PublicKey
	}

	select {
	case e.Joins <- device:
	default:
	}

	go device.Listen()
	go e.sendIdentity(device)

	for pkg := range device.Incoming {
		if pkg == nil {
			continue
		}

		// Decrypt package if encrypted
		if pkg.Type == protocol.EncryptedType {
			var err error
			pkg, err = pkg.Body.(*protocol.Encrypted).Decrypt(e.config.PrivateKey)
			if err != nil {
				log.Println("Cannot decrypt package:", err)
				continue
			}
		}

		if pkg.Type == protocol.PairType {
			pair := pkg.Body.(*protocol.Pair)

			if pair.Pair {
				// Remote asks pairing
				device.PairRequestReceived = true

				if len(pair.PublicKey) > 0 {
					// Remote sent its public key
					pub := &crypto.PublicKey{}

					if err := pub.Unmarshal([]byte(pair.PublicKey)); err != nil {
						log.Println("Cannot parse public key:", err)
						break
					}
					log.Println("Received public key")

					device.PublicKey = pub
				}

				if device.PairRequestSent {
					// We asked for pairing first, remote accepted
					log.Println("Device accepted pairing")

					e.PairDevice(device)
				} else {
					log.Println("Device requested pairing")

					select {
					case e.RequestsPairing <- device:
					default:
					}
				}
			} else {
				log.Println("Device requested unpairing")

				device.Paired = false
				e.UnpairDevice(device)
			}
		} else if pkg.Type == protocol.IdentityType {
			setDeviceIdentity(device, pkg.Body.(*protocol.Identity))
		} else {
			err := e.handler.Handle(device, pkg)
			if err != nil {
				log.Println("Error handling package:", err, pkg.Type, string(pkg.RawBody))
			}
		}
	}

	log.Println("Closed TCP connection")

	delete(e.devices, device.Id)

	select {
	case e.Leaves <- device:
	default:
	}
}
開發者ID:emersion,項目名稱:go-kdeconnect,代碼行數:89,代碼來源:engine.go


注:本文中的github.com/emersion/go-kdeconnect/network.Device.PublicKey方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。