Official cwtch.im peer and server implementations.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cwtch_peer.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package peer
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "git.mascherari.press/cwtch/model"
  6. "git.mascherari.press/cwtch/peer/connections"
  7. "git.mascherari.press/cwtch/peer/peer"
  8. "git.mascherari.press/cwtch/protocol"
  9. "github.com/s-rah/go-ricochet/application"
  10. "github.com/s-rah/go-ricochet/channels"
  11. "github.com/s-rah/go-ricochet/connection"
  12. "io/ioutil"
  13. "sync"
  14. )
  15. /**
  16. PeerConnectionPool
  17. Background thread running Process()
  18. Pointer to connections
  19. Move CwtchPeerChannel under peer/
  20. Write tests for Peer Channel
  21. */
  22. type CwtchPeer struct {
  23. connection.AutoConnectionHandler
  24. Profile *model.Profile
  25. mutex sync.Mutex
  26. Log chan string `json:"-"`
  27. connectionsManager *connections.Manager
  28. }
  29. func NewCwtchPeer(name string) *CwtchPeer {
  30. cp := new(CwtchPeer)
  31. cp.Profile = model.GenerateNewProfile(name)
  32. cp.Log = make(chan string)
  33. cp.connectionsManager = connections.NewConnectionsManager()
  34. cp.Init()
  35. return cp
  36. }
  37. func (cp *CwtchPeer) Save(profilefile string) error {
  38. cp.mutex.Lock()
  39. bytes, _ := json.Marshal(cp)
  40. err := ioutil.WriteFile(profilefile, bytes, 0600)
  41. cp.mutex.Unlock()
  42. return err
  43. }
  44. func LoadCwtchPeer(profilefile string) (*CwtchPeer, error) {
  45. bytes, _ := ioutil.ReadFile(profilefile)
  46. cp := new(CwtchPeer)
  47. err := json.Unmarshal(bytes, &cp)
  48. return cp, err
  49. }
  50. // AddContactRequest is the entry point for CwtchPeer relationships
  51. func (cp *CwtchPeer) PeerWithOnion(onion string) {
  52. cp.connectionsManager.ManagePeerConnection(onion, cp.Profile)
  53. }
  54. // InviteOnionToGroup kicks off the invite process
  55. func (cp *CwtchPeer) InviteOnionToGroup(onion string, groupid string) error {
  56. group := cp.Profile.GetGroupByGroupId(groupid)
  57. if group == nil {
  58. invite := group.Invite()
  59. ppc := cp.connectionsManager.GetPeerPeerConnectionForOnion(onion)
  60. ppc.SendGroupInvite(invite)
  61. }
  62. return errors.New("group id could not be found")
  63. }
  64. func (cp *CwtchPeer) ReceiveGroupMessage(server string, gm *protocol.GroupMessage) {
  65. cp.Profile.AttemptDecryption(gm.Ciphertext)
  66. }
  67. func (cp *CwtchPeer) JoinServer(onion string) {
  68. cp.connectionsManager.ManageServerConnection(onion, cp.ReceiveGroupMessage)
  69. }
  70. func (cp *CwtchPeer) SendMessageToGroup(groupid string, message string) {
  71. group := cp.Profile.GetGroupByGroupId(groupid)
  72. psc := cp.connectionsManager.GetPeerServerConnectionForOnion(group.GroupServer)
  73. ct, _ := cp.Profile.EncryptMessageToGroup(message, groupid)
  74. gm := &protocol.GroupMessage{
  75. Ciphertext: ct,
  76. }
  77. psc.SendGroupMessage(gm)
  78. }
  79. func (cp *CwtchPeer) Listen() error {
  80. cwtchpeer := new(application.RicochetApplication)
  81. l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", cp.Profile.OnionPrivateKey, 9878)
  82. if err != nil {
  83. return err
  84. }
  85. af := application.ApplicationInstanceFactory{}
  86. af.Init()
  87. af.AddHandler("im.cwtch.peer", func(rai *application.ApplicationInstance) func() channels.Handler {
  88. cpi := new(CwtchPeerInstance)
  89. cpi.Init(rai, cwtchpeer)
  90. return func() channels.Handler {
  91. cpc := new(peer.CwtchPeerChannel)
  92. cpc.Handler = &CwtchPeerHandler{Onion: rai.RemoteHostname, Peer: cp}
  93. return cpc
  94. }
  95. })
  96. cwtchpeer.Init(cp.Profile.OnionPrivateKey, af, new(application.AcceptAllContactManager))
  97. cp.Log <- "Running cwtch peer on " + l.Addr().String()
  98. cwtchpeer.Run(l)
  99. return nil
  100. }
  101. type CwtchPeerInstance struct {
  102. rai *application.ApplicationInstance
  103. ra *application.RicochetApplication
  104. }
  105. func (cpi *CwtchPeerInstance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication) {
  106. cpi.rai = rai
  107. cpi.ra = ra
  108. }
  109. type CwtchPeerHandler struct {
  110. Onion string
  111. Peer *CwtchPeer
  112. }
  113. func (cph *CwtchPeerHandler) ClientIdentity(ci *protocol.CwtchIdentity) {
  114. cph.Peer.Log <- "Received Client Identity from " + cph.Onion + " " + ci.String()
  115. cph.Peer.Profile.AddCwtchIdentity(cph.Onion, ci)
  116. }
  117. func (cph *CwtchPeerHandler) HandleGroupInvite(gci *protocol.GroupChatInvite) {
  118. cph.Peer.Profile.ProcessInvite(gci, cph.Onion)
  119. }
  120. func (cph *CwtchPeerHandler) HandleGroupMessage(gm *protocol.GroupMessage) {
  121. }