Skip to content

Commit

Permalink
Support WebRTC communication
Browse files Browse the repository at this point in the history
Signed-off-by: billfort <fxbao@hotmail.com>
  • Loading branch information
billfort committed Apr 17, 2024
1 parent c9773e8 commit 8474cae
Show file tree
Hide file tree
Showing 14 changed files with 870 additions and 262 deletions.
81 changes: 78 additions & 3 deletions api/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"strings"

"github.com/nknorg/nkn/v2/api/common/errcode"
"github.com/nknorg/nkn/v2/api/webrtc"
"github.com/nknorg/nkn/v2/block"
"github.com/nknorg/nkn/v2/chain"
"github.com/nknorg/nkn/v2/common"
Expand Down Expand Up @@ -412,12 +416,13 @@ func getVersion(s Serverer, params map[string]interface{}, ctx context.Context)
return respPacking(errcode.SUCCESS, config.Version)
}

func NodeInfo(wsAddr, rpcAddr string, pubkey, id []byte) map[string]string {
func NodeInfo(wsAddr, rpcAddr string, pubkey, id []byte, sdp string) map[string]string {
nodeInfo := make(map[string]string)
nodeInfo["addr"] = wsAddr
nodeInfo["rpcAddr"] = rpcAddr
nodeInfo["pubkey"] = hex.EncodeToString(pubkey)
nodeInfo["id"] = hex.EncodeToString(id)
nodeInfo["sdp"] = sdp
return nodeInfo
}

Expand All @@ -444,7 +449,7 @@ func getWsAddr(s Serverer, params map[string]interface{}, ctx context.Context) m
return respPacking(errcode.INTERNAL_ERROR, err.Error())
}

return respPacking(errcode.SUCCESS, NodeInfo(wsAddr, rpcAddr, pubkey, id))
return respPacking(errcode.SUCCESS, NodeInfo(wsAddr, rpcAddr, pubkey, id, ""))
}

func getWssAddr(s Serverer, params map[string]interface{}, ctx context.Context) map[string]interface{} {
Expand All @@ -467,7 +472,7 @@ func getWssAddr(s Serverer, params map[string]interface{}, ctx context.Context)
return respPacking(errcode.INTERNAL_ERROR, err.Error())
}

return respPacking(errcode.SUCCESS, NodeInfo(wsAddr, rpcAddr, pubkey, id))
return respPacking(errcode.SUCCESS, NodeInfo(wsAddr, rpcAddr, pubkey, id, ""))
}

// getBalanceByAddr gets balance by address
Expand Down Expand Up @@ -953,6 +958,75 @@ func findSuccessorAddr(s Serverer, params map[string]interface{}, ctx context.Co
return respPacking(errcode.SUCCESS, addrs[0])
}

// getPeerAddr get a node address
// params: {"address":<address>}
// return: {"resultOrData":<result>|<error data>, "error":<errcode>}
func getPeerAddr(s Serverer, params map[string]interface{}, ctx context.Context) map[string]interface{} {
if len(params) < 1 {
return RespPacking("length of params is less than 1", errcode.INVALID_PARAMS)
}

str, ok := params["address"].(string)
if !ok {
return RespPacking("address should be a string", errcode.INTERNAL_ERROR)
}

clientID, _, _, err := address.ParseClientAddress(str)
if err != nil {
return RespPacking(err.Error(), errcode.INTERNAL_ERROR)
}

wsAddr, rpcAddr, pubkey, id, err := s.GetNetNode().FindWsAddr(clientID)
if err != nil {
return RespPacking(err.Error(), errcode.INTERNAL_ERROR)
}

n := s.GetNetNode()
if n == nil {
return nil
}

if n.GetWsAddr() == wsAddr {
offer := params["offer"].(string)
peer := webrtc.NewPeer(config.Parameters.StunList)

err = peer.Answer(offer)
if err != nil {
return RespPacking(err.Error(), errcode.INTERNAL_ERROR)
}
answer := <-peer.OnSdp

return RespPacking(NodeInfo(wsAddr, rpcAddr, pubkey, id, answer), errcode.SUCCESS)
}

reqAddr := (&url.URL{Scheme: "http", Host: rpcAddr}).String()
reqBody, err := json.Marshal(map[string]interface{}{
"id": "nkn-sdk-go",
"method": "getpeeraddr",
"params": params,
})
if err != nil {
return nil
}
req, err := http.NewRequestWithContext(ctx, "POST", reqAddr, bytes.NewBuffer(reqBody))
if err != nil {
return nil
}
client := &http.Client{}

resp, err := client.Do(req)
if err != nil {
return nil
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil
}

return RespPacking(NodeInfo(wsAddr, rpcAddr, pubkey, id, string(b)), errcode.SUCCESS)
}

var InitialAPIHandlers = map[string]APIHandler{
"getlatestblockhash": {Handler: getLatestBlockHash, AccessCtrl: BIT_JSONRPC},
"getblock": {Handler: getBlock, AccessCtrl: BIT_JSONRPC},
Expand Down Expand Up @@ -983,4 +1057,5 @@ var InitialAPIHandlers = map[string]APIHandler{
"findsuccessoraddr": {Handler: findSuccessorAddr, AccessCtrl: BIT_JSONRPC},
"findsuccessoraddrs": {Handler: findSuccessorAddrs, AccessCtrl: BIT_JSONRPC},
"getregistrant": {Handler: getRegistrant, AccessCtrl: BIT_JSONRPC},
"getpeeraddr": {Handler: getPeerAddr, AccessCtrl: BIT_JSONRPC},
}
Loading

0 comments on commit 8474cae

Please sign in to comment.