Пример #1
0
// RegAmbassador registers an ambassador, advertising service for the given pubkey
func (clnt *Client) RegAmbassador(mypub natrium.EdDSAPublic) (*kiricom.ServerCtx, error) {
	req := textprot.TextReq{
		Verb: "AMB_REGISTER",
		Args: []string{hex.EncodeToString(mypub)},
	}
	_, err := clnt.execCmd(req)
	if err != nil {
		return nil, err
	}

	srvctx := kiricom.NewServerCtx(8192, clnt.sok)
	return srvctx, nil
}
Пример #2
0
func (ns *NodeState) do_AMB_CONNECT(sok io.ReadWriteCloser, req textprot.TextReq) (err error) {
	if len(req.Args) == 0 {
		err = errors.New("core2core: server got truncated AMB_CONNECT")
		return
	}
	bts, err := hex.DecodeString(req.Args[0])
	if err != nil {
		return err
	}

	var kee [32]byte
	copy(kee[:], bts)

	// get the client to the other end
	ns.RLock()
	nxclnt := ns.ambState[kee]
	ns.RUnlock()
	if nxclnt == nil {
		kilog.Debug("core2core: ambassador rejecting connect to %v due to nil", kee[:])
		err = (&textprot.TextReq{
			Verb: "NOPE",
		}).WriteTo(sok)
		return
	}

	// acknowledge now
	err = (&textprot.TextReq{
		Verb: "OKAY",
	}).WriteTo(sok)
	if err != nil {
		return
	}

	// construct a server ctx
	pxsrv := kiricom.NewServerCtx(8192, sok)

	// forward the connections, don't stop
	defer sok.Close()
	defer kilog.Debug("core2core: ambassador connect for %x terminated", kee[:])
	for {
		clnt, err := pxsrv.Accept()
		if err != nil {
			kilog.Debug("core2core: ambassador failed to accept to %x: %v", kee[:], err.Error())
			return io.EOF
		}
		go func() {
			defer clnt.Close()
			rem, err := nxclnt.Dial()
			if err != nil {
				kilog.Debug("core2core: ambassador failed to forward %x: %v", kee[:], err.Error())
				return
			}
			defer rem.Close()
			go func() {
				defer clnt.Close()
				defer rem.Close()
				io.Copy(rem, clnt)
			}()
			kilog.Debug("core2core: ambassador successfully forwarded %x", kee[:])
			io.Copy(clnt, rem)
		}()
	}
}