More golint fixing
This commit is contained in:
parent
8349ff1b3c
commit
447b1b30fb
|
@ -6,10 +6,12 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Application is a facade over a CwtchPeer that provides some wrapping logic.
|
||||||
type Application struct {
|
type Application struct {
|
||||||
Peer *peer.CwtchPeer
|
Peer *peer.CwtchPeer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewProfile creates a new CwtchPeer with a given name.
|
||||||
func (app *Application) NewProfile(name string, filename string) error {
|
func (app *Application) NewProfile(name string, filename string) error {
|
||||||
profile := peer.NewCwtchPeer(name)
|
profile := peer.NewCwtchPeer(name)
|
||||||
app.Peer = profile
|
app.Peer = profile
|
||||||
|
@ -25,6 +27,7 @@ func (app *Application) NewProfile(name string, filename string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetProfile loads an existing profile from the given filename.
|
||||||
func (app *Application) SetProfile(filename string) error {
|
func (app *Application) SetProfile(filename string) error {
|
||||||
profile, err := peer.LoadCwtchPeer(filename)
|
profile, err := peer.LoadCwtchPeer(filename)
|
||||||
app.Peer = profile
|
app.Peer = profile
|
||||||
|
@ -39,6 +42,7 @@ func (app *Application) SetProfile(filename string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PeerRequest attempts to setup peer relationship with the given onion address.
|
||||||
func (app *Application) PeerRequest(onion string) {
|
func (app *Application) PeerRequest(onion string) {
|
||||||
app.Peer.PeerWithOnion(onion)
|
app.Peer.PeerWithOnion(onion)
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,16 +87,15 @@ func completer(d prompt.Document) []prompt.Suggest {
|
||||||
s = append(s, prompt.Suggest{Text: contact.Onion, Description: contact.Name})
|
s = append(s, prompt.Suggest{Text: contact.Onion, Description: contact.Name})
|
||||||
}
|
}
|
||||||
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
|
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
|
||||||
} else {
|
|
||||||
s = []prompt.Suggest{}
|
|
||||||
groups := app.Peer.Profile.Groups
|
|
||||||
for _, group := range groups {
|
|
||||||
if group.Owner == "self" {
|
|
||||||
s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
|
|
||||||
}
|
}
|
||||||
|
s = []prompt.Suggest{}
|
||||||
|
groups := app.Peer.Profile.Groups
|
||||||
|
for _, group := range groups {
|
||||||
|
if group.Owner == "self" {
|
||||||
|
s = append(s, prompt.Suggest{Text: group.GroupID, Description: "Group owned by " + group.Owner + " on " + group.GroupServer})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return prompt.FilterHasPrefix(s, d.GetWordBeforeCursor(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if strings.HasPrefix(w, "accept-invite") {
|
if strings.HasPrefix(w, "accept-invite") {
|
||||||
|
@ -362,7 +361,7 @@ func main() {
|
||||||
if maxLatency < latency {
|
if maxLatency < latency {
|
||||||
maxLatency = latency
|
maxLatency = latency
|
||||||
}
|
}
|
||||||
totalMessages += 1
|
totalMessages++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,11 +14,11 @@ func checkAndGenPrivateKey(privateKeyFile string) {
|
||||||
if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) {
|
if _, err := os.Stat(privateKeyFile); os.IsNotExist(err) {
|
||||||
log.Printf("no private key found!")
|
log.Printf("no private key found!")
|
||||||
log.Printf("generating new private key...")
|
log.Printf("generating new private key...")
|
||||||
pk, pk_err := utils.GeneratePrivateKey()
|
pk, err := utils.GeneratePrivateKey()
|
||||||
if pk_err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error generating new private key: %v\n", err)
|
log.Fatalf("error generating new private key: %v\n", err)
|
||||||
}
|
}
|
||||||
err := ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
|
err = ioutil.WriteFile(privateKeyFile, []byte(utils.PrivateKeyToString(pk)), 0400)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("error writing new private key to file %s: %v\n", privateKeyFile, err)
|
log.Fatalf("error writing new private key to file %s: %v\n", privateKeyFile, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,17 +9,14 @@ import (
|
||||||
"github.com/s-rah/go-ricochet/wire/control"
|
"github.com/s-rah/go-ricochet/wire/control"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CwtchChannel implements the ChannelHandler interface for a channel of
|
// CwtchServerFetchChannel implements the ChannelHandler interface for a channel of
|
||||||
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
// type "im.cwtch.server.fetch" - this implementation only handles server side logic.
|
||||||
//
|
|
||||||
// CwtchChannel implements protocol-level sanity and state validation, but
|
|
||||||
// does not handle or acknowledge Cwtch messages. The application must provide
|
|
||||||
// a CwtchChannelHandler implementation to handle Cwtch events.
|
|
||||||
type CwtchServerFetchChannel struct {
|
type CwtchServerFetchChannel struct {
|
||||||
Handler CwtchServerFetchHandler
|
Handler CwtchServerFetchHandler
|
||||||
channel *channels.Channel
|
channel *channels.Channel
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CwtchServerFetchHandler defines the interface for interacting with this Channel
|
||||||
type CwtchServerFetchHandler interface {
|
type CwtchServerFetchHandler interface {
|
||||||
HandleFetchRequest() []*protocol.GroupMessage
|
HandleFetchRequest() []*protocol.GroupMessage
|
||||||
}
|
}
|
||||||
|
@ -78,7 +75,7 @@ func (cc *CwtchServerFetchChannel) OpenOutboundResult(err error, crm *Protocol_D
|
||||||
// NOTE: Should never be called
|
// NOTE: Should never be called
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendGroupMessage
|
// SendGroupMessages sends a batch of group messages to the client.
|
||||||
func (cc *CwtchServerFetchChannel) SendGroupMessages(gm []*protocol.GroupMessage) {
|
func (cc *CwtchServerFetchChannel) SendGroupMessages(gm []*protocol.GroupMessage) {
|
||||||
csp := &protocol.CwtchServerPacket{}
|
csp := &protocol.CwtchServerPacket{}
|
||||||
csp.GroupMessages = gm
|
csp.GroupMessages = gm
|
||||||
|
|
|
@ -9,12 +9,8 @@ import (
|
||||||
"github.com/s-rah/go-ricochet/wire/control"
|
"github.com/s-rah/go-ricochet/wire/control"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CwtchChannel implements the ChannelHandler interface for a channel of
|
// CwtchServerListenChannel implements the ChannelHandler interface for a channel of
|
||||||
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
// type "im.cwtch.server.listen" - this implementation only handles server side logic.
|
||||||
//
|
|
||||||
// CwtchChannel implements protocol-level sanity and state validation, but
|
|
||||||
// does not handle or acknowledge Cwtch messages. The application must provide
|
|
||||||
// a CwtchChannelHandler implementation to handle Cwtch events.
|
|
||||||
type CwtchServerListenChannel struct {
|
type CwtchServerListenChannel struct {
|
||||||
channel *channels.Channel
|
channel *channels.Channel
|
||||||
}
|
}
|
||||||
|
@ -73,7 +69,7 @@ func (cc *CwtchServerListenChannel) OpenOutboundResult(err error, crm *Protocol_
|
||||||
// NOTE: Should never be called
|
// NOTE: Should never be called
|
||||||
}
|
}
|
||||||
|
|
||||||
// SendGroupMessage
|
// SendGroupMessage sends a single group message to the peer
|
||||||
func (cc *CwtchServerListenChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
func (cc *CwtchServerListenChannel) SendGroupMessage(gm *protocol.GroupMessage) {
|
||||||
csp := &protocol.CwtchServerPacket{
|
csp := &protocol.CwtchServerPacket{
|
||||||
GroupMessage: gm,
|
GroupMessage: gm,
|
||||||
|
|
|
@ -10,12 +10,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CwtchChannel implements the ChannelHandler interface for a channel of
|
// CwtchServerSendChannel implements the ChannelHandler interface for a channel of
|
||||||
// type "im.ricochet.Cwtch". The channel may be inbound or outbound.
|
// type "im.cwtch.server.send - this implementation only handles server-side logic.
|
||||||
//
|
|
||||||
// CwtchChannel implements protocol-level sanity and state validation, but
|
|
||||||
// does not handle or acknowledge Cwtch messages. The application must provide
|
|
||||||
// a CwtchChannelHandler implementation to handle Cwtch events.
|
|
||||||
type CwtchServerSendChannel struct {
|
type CwtchServerSendChannel struct {
|
||||||
// Methods of Handler are called for Cwtch events on this channel
|
// Methods of Handler are called for Cwtch events on this channel
|
||||||
Handler CwtchServerSendChannelHandler
|
Handler CwtchServerSendChannelHandler
|
||||||
|
@ -23,8 +19,7 @@ type CwtchServerSendChannel struct {
|
||||||
spamguard spam.Guard
|
spamguard spam.Guard
|
||||||
}
|
}
|
||||||
|
|
||||||
// CwtchChannelHandler is implemented by an application type to receive
|
// CwtchServerSendChannelHandler defines the interface needed to interact with this channel
|
||||||
// events from a CwtchChannel.
|
|
||||||
type CwtchServerSendChannelHandler interface {
|
type CwtchServerSendChannelHandler interface {
|
||||||
HandleGroupMessage(*protocol.GroupMessage)
|
HandleGroupMessage(*protocol.GroupMessage)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,13 @@ import (
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Server encapsulates a complete, compliant Cwtch server.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Run s
|
||||||
|
// tarts a server with the given privateKey
|
||||||
|
// TODO: surface errors
|
||||||
func (s *Server) Run(privateKeyFile string) {
|
func (s *Server) Run(privateKeyFile string) {
|
||||||
cwtchserver := new(application.RicochetApplication)
|
cwtchserver := new(application.RicochetApplication)
|
||||||
|
|
||||||
|
|
|
@ -8,23 +8,26 @@ import (
|
||||||
"github.com/s-rah/go-ricochet/channels"
|
"github.com/s-rah/go-ricochet/channels"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Instance encapsulates the Ricochet application.
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
rai *application.ApplicationInstance
|
rai *application.ApplicationInstance
|
||||||
ra *application.RicochetApplication
|
ra *application.RicochetApplication
|
||||||
msi storage.MessageStoreInterface
|
msi storage.MessageStoreInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init sets up a Server Instance
|
||||||
func (si *Instance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
|
func (si *Instance) Init(rai *application.ApplicationInstance, ra *application.RicochetApplication, msi storage.MessageStoreInterface) {
|
||||||
si.rai = rai
|
si.rai = rai
|
||||||
si.ra = ra
|
si.ra = ra
|
||||||
si.msi = msi
|
si.msi = msi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleFetchRequest returns a list of all messages in the servers buffer
|
||||||
func (si *Instance) HandleFetchRequest() []*protocol.GroupMessage {
|
func (si *Instance) HandleFetchRequest() []*protocol.GroupMessage {
|
||||||
return si.msi.FetchMessages()
|
return si.msi.FetchMessages()
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleGroupMessage
|
// HandleGroupMessage takes ina group message and distributes it to all listening peers
|
||||||
func (si *Instance) HandleGroupMessage(gm *protocol.GroupMessage) {
|
func (si *Instance) HandleGroupMessage(gm *protocol.GroupMessage) {
|
||||||
si.msi.AddMessage(*gm)
|
si.msi.AddMessage(*gm)
|
||||||
go si.ra.Broadcast(func(rai *application.ApplicationInstance) {
|
go si.ra.Broadcast(func(rai *application.ApplicationInstance) {
|
||||||
|
|
|
@ -10,22 +10,26 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MessageStoreInterface defines an interface to interact with a store of cwtch messages.
|
||||||
type MessageStoreInterface interface {
|
type MessageStoreInterface interface {
|
||||||
AddMessage(protocol.GroupMessage)
|
AddMessage(protocol.GroupMessage)
|
||||||
FetchMessages() []*protocol.GroupMessage
|
FetchMessages() []*protocol.GroupMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MessageStore is a file-backed implementation of MessageStoreInterface
|
||||||
type MessageStore struct {
|
type MessageStore struct {
|
||||||
file *os.File
|
file *os.File
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
messages []*protocol.GroupMessage
|
messages []*protocol.GroupMessage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes the message store and underlying resources.
|
||||||
func (ms *MessageStore) Close() {
|
func (ms *MessageStore) Close() {
|
||||||
ms.messages = nil
|
ms.messages = nil
|
||||||
ms.file.Close()
|
ms.file.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Init sets up a MessageStore backed by filename
|
||||||
func (ms *MessageStore) Init(filename string) {
|
func (ms *MessageStore) Init(filename string) {
|
||||||
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
|
f, err := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -51,6 +55,7 @@ func (ms *MessageStore) Init(filename string) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FetchMessages returns all messages from the backing file.
|
||||||
func (ms *MessageStore) FetchMessages() (messages []*protocol.GroupMessage) {
|
func (ms *MessageStore) FetchMessages() (messages []*protocol.GroupMessage) {
|
||||||
messages = make([]*protocol.GroupMessage, len(ms.messages))
|
messages = make([]*protocol.GroupMessage, len(ms.messages))
|
||||||
ms.lock.Lock()
|
ms.lock.Lock()
|
||||||
|
@ -59,6 +64,7 @@ func (ms *MessageStore) FetchMessages() (messages []*protocol.GroupMessage) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddMessage adds a GroupMessage to the store
|
||||||
func (ms *MessageStore) AddMessage(gm protocol.GroupMessage) {
|
func (ms *MessageStore) AddMessage(gm protocol.GroupMessage) {
|
||||||
ms.lock.Lock()
|
ms.lock.Lock()
|
||||||
ms.messages = append(ms.messages, &gm)
|
ms.messages = append(ms.messages, &gm)
|
||||||
|
|
Loading…
Reference in New Issue