2018-03-09 20:44:13 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.mascherari.press/cwtch/server/fetch"
|
|
|
|
"git.mascherari.press/cwtch/server/listen"
|
|
|
|
"git.mascherari.press/cwtch/server/send"
|
|
|
|
"git.mascherari.press/cwtch/storage"
|
|
|
|
"github.com/s-rah/go-ricochet/application"
|
|
|
|
"github.com/s-rah/go-ricochet/channels"
|
|
|
|
"github.com/s-rah/go-ricochet/utils"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2018-05-16 21:11:04 +00:00
|
|
|
// Server encapsulates a complete, compliant Cwtch server.
|
2018-03-09 20:44:13 +00:00
|
|
|
type Server struct {
|
|
|
|
}
|
|
|
|
|
2018-05-16 21:11:04 +00:00
|
|
|
// Run s
|
|
|
|
// tarts a server with the given privateKey
|
|
|
|
// TODO: surface errors
|
2018-03-09 20:44:13 +00:00
|
|
|
func (s *Server) Run(privateKeyFile string) {
|
|
|
|
cwtchserver := new(application.RicochetApplication)
|
|
|
|
|
2018-04-30 17:01:08 +00:00
|
|
|
pk, err := utils.LoadPrivateKeyFromFile(privateKeyFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error reading private key file: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-03-09 20:44:13 +00:00
|
|
|
l, err := application.SetupOnion("127.0.0.1:9051", "tcp4", "", pk, 9878)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error setting up onion service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
af := application.ApplicationInstanceFactory{}
|
|
|
|
af.Init()
|
|
|
|
ms := new(storage.MessageStore)
|
|
|
|
ms.Init("cwtch.messages")
|
|
|
|
af.AddHandler("im.cwtch.server.listen", func(rai *application.ApplicationInstance) func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
si := new(Instance)
|
2018-03-09 20:44:13 +00:00
|
|
|
si.Init(rai, cwtchserver, ms)
|
|
|
|
return func() channels.Handler {
|
|
|
|
cslc := new(listen.CwtchServerListenChannel)
|
|
|
|
return cslc
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
af.AddHandler("im.cwtch.server.fetch", func(rai *application.ApplicationInstance) func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
si := new(Instance)
|
2018-03-09 20:44:13 +00:00
|
|
|
si.Init(rai, cwtchserver, ms)
|
|
|
|
return func() channels.Handler {
|
|
|
|
cssc := new(fetch.CwtchServerFetchChannel)
|
|
|
|
cssc.Handler = si
|
|
|
|
return cssc
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
af.AddHandler("im.cwtch.server.send", func(rai *application.ApplicationInstance) func() channels.Handler {
|
2018-03-14 22:23:35 +00:00
|
|
|
si := new(Instance)
|
2018-03-09 20:44:13 +00:00
|
|
|
si.Init(rai, cwtchserver, ms)
|
|
|
|
return func() channels.Handler {
|
|
|
|
cssc := new(send.CwtchServerSendChannel)
|
|
|
|
cssc.Handler = si
|
|
|
|
return cssc
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
cwtchserver.Init(pk, af, new(application.AcceptAllContactManager))
|
2018-03-30 21:16:51 +00:00
|
|
|
log.Printf("cwtch server running on cwtch:%s", l.Addr().String()[0:16])
|
2018-03-09 20:44:13 +00:00
|
|
|
cwtchserver.Run(l)
|
|
|
|
}
|