Fixing Linting, Vetting & Formatting Issues

This commit is contained in:
Sarah Jamie Lewis 2019-01-09 14:33:33 -08:00
parent 90231b0be9
commit b05567fd81
18 changed files with 55 additions and 29 deletions

View File

@ -16,7 +16,7 @@ pipeline:
commands:
- go list ./... | xargs go vet
#-set_exit_status too many lint fails for :(
- go list ./... | xargs golint
- go list ./... | grep -v "/wire/" | grep -v "/examples/" | grep -v "/application" | xargs golint -set_exit_status
units-tests:
image: golang
commands:

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ go-ricochet-coverage.out
*~
*.out
.idea
.reviewboardrc

View File

@ -1,13 +1,22 @@
package application
// AcceptAllContactHandler is a pass through Contact Handler. It is currently only used by the integration test.
// TODO: DEPRECATE
type AcceptAllContactHandler struct{}
// ContactRequest returns "Pending" for everything
func (aach *AcceptAllContactHandler) ContactRequest(name string, message string) string {
return "Pending"
}
// ContactRequestRejected is a noop
func (aach *AcceptAllContactHandler) ContactRequestRejected() {
}
// ContactRequestAccepted is a noop
func (aach *AcceptAllContactHandler) ContactRequestAccepted() {
}
// ContactRequestError is a noop
func (aach *AcceptAllContactHandler) ContactRequestError() {
}

View File

@ -7,6 +7,8 @@ import (
// AcceptAllContactManager implements the contact manager interface an presumes
// all connections are allowed.
// It is currently used by the Cwtch Server.
// TODO Deprecate
type AcceptAllContactManager struct {
}
@ -15,11 +17,12 @@ func (aacm *AcceptAllContactManager) LookupContact(hostname string, publicKey rs
return true, true
}
// LookupContact returns that a contact is known and allowed to communicate for all cases.
// LookupContactV3 returns that a contact is known and allowed to communicate for all cases.
func (aacm *AcceptAllContactManager) LookupContactV3(hostname string, publicKey ed25519.PublicKey) (allowed, known bool) {
return true, true
}
// ContactRequest accepts every single Contact Request
func (aacm *AcceptAllContactManager) ContactRequest(name string, message string) string {
return "Accepted"
}

View File

@ -74,6 +74,7 @@ func (ra *RicochetApplication) handleConnection(conn net.Conn) {
ra.lock.Unlock()
}
// HandleApplicationInstance delegates handling of a given ApplicationInstance to the Application.
func (ra *RicochetApplication) HandleApplicationInstance(rai *ApplicationInstance) {
ra.lock.Lock()
ra.instances = append(ra.instances, rai)
@ -103,6 +104,7 @@ func (ra *RicochetApplication) Open(onionAddress string, requestMessage string)
return rai, nil
}
// Broadcast performs the given function do() over all application instance (all connected peers)
func (ra *RicochetApplication) Broadcast(do func(rai *ApplicationInstance)) {
ra.lock.Lock()
for _, rai := range ra.instances {
@ -121,6 +123,7 @@ func (ra *RicochetApplication) Shutdown() {
ra.lock.Unlock()
}
// ConnectionCount returns the number of concurrent connections to the application
func (ra *RicochetApplication) ConnectionCount() int {
return len(ra.instances)
}

View File

@ -12,7 +12,7 @@ type ApplicationInstance struct {
RemoteHostname string
}
// ApplicationInstanceFactory
// ApplicationInstanceFactory generates ApplicationInstances on a specific connection.
type ApplicationInstanceFactory struct {
handlerMap map[string]func(*ApplicationInstance) func() channels.Handler
}
@ -27,6 +27,7 @@ func (af *ApplicationInstanceFactory) AddHandler(ctype string, chandler func(*Ap
af.handlerMap[ctype] = chandler
}
// GetHandlers returns all handlers
func (af *ApplicationInstanceFactory) GetHandlers() []string {
keys := make([]string, len(af.handlerMap))
@ -39,6 +40,7 @@ func (af *ApplicationInstanceFactory) GetHandlers() []string {
return keys
}
// GetHandler returns a set handler for the channel type.
func (af *ApplicationInstanceFactory) GetHandler(ctype string) func(*ApplicationInstance) func() channels.Handler {
return af.handlerMap[ctype]
}

View File

@ -8,8 +8,8 @@ import (
"git.openprivacy.ca/openprivacy/libricochet-go/utils"
"git.openprivacy.ca/openprivacy/libricochet-go/wire/auth/3edh"
"git.openprivacy.ca/openprivacy/libricochet-go/wire/control"
"golang.org/x/crypto/ed25519"
"github.com/golang/protobuf/proto"
"golang.org/x/crypto/ed25519"
"testing"
)
@ -18,7 +18,7 @@ func TestServer3DHAuthChannel(t *testing.T) {
cc := new(channels.Channel)
cc.ID = 1
closed := false
cc.CloseChannel = func() {closed=true}
cc.CloseChannel = func() { closed = true }
clientChannel := new(outbound.Client3DHAuthChannel)
pub, priv, _ := ed25519.GenerateKey(rand.Reader)
cid := identity.InitializeV3("", &priv, &pub)
@ -34,7 +34,7 @@ func TestServer3DHAuthChannel(t *testing.T) {
sid := identity.InitializeV3("", &priv, &pub)
s3dhchannel.ServerIdentity = sid
clientChannel.ServerHostname = utils.GetTorV3Hostname(pub)
cr, _ := s3dhchannel.OpenInbound(cc, packet.GetOpenChannel())
cr, _ := s3dhchannel.OpenInbound(cc, packet.GetOpenChannel())
proto.Unmarshal(cr, packet)
if packet.GetChannelResult() != nil {

View File

@ -27,4 +27,4 @@ type Handler interface {
OnOpenChannelRequest(ctype string) (channels.Handler, error)
GetSupportedChannelTypes() []string
}
}

View File

@ -37,6 +37,7 @@ func HandleInboundConnection(c *Connection) *InboundConnectionHandler {
// true to accept authentication and allow the connection to continue, and also returns a
// boolean indicating whether the contact is known and recognized. Unknown contacts will
// assume they are required to send a contact request before any other activity.
// TODO: Deprecate
func (ich *InboundConnectionHandler) ProcessAuthAsServer(identity identity.Identity, sach func(hostname string, publicKey rsa.PublicKey) (allowed, known bool)) error {
if !identity.Initialized() {
@ -91,11 +92,11 @@ func (ich *InboundConnectionHandler) ProcessAuthAsServer(identity identity.Ident
return err
}
// ProcessAuthAsServer blocks until authentication has succeeded, failed, or the
// ProcessAuthAsV3Server blocks until authentication has succeeded, failed, or the
// connection is closed. A non-nil error is returned in all cases other than successful
// and accepted authentication.
//
// ProcessAuthAsServer cannot be called at the same time as any other call to a Process
// ProcessAuthAsV3Server cannot be called at the same time as any other call to a Process
// function. Another Process function must be called after this function successfully
// returns to continue handling connection events.
//

View File

@ -33,6 +33,7 @@ func HandleOutboundConnection(c *Connection) *OutboundConnectionHandler {
// For successful authentication, the `known` return value indicates whether the peer
// accepts us as a known contact. Unknown contacts will generally need to send a contact
// request before any other activity.
// TODO; Deprecate
func (och *OutboundConnectionHandler) ProcessAuthAsClient(identity identity.Identity) (bool, error) {
if !identity.Initialized() {
@ -90,11 +91,11 @@ func (och *OutboundConnectionHandler) ProcessAuthAsClient(identity identity.Iden
return false, utils.ServerRejectedClientConnectionError
}
// ProcessAuthAs3DGClient blocks until authentication has succeeded or failed with the
// ProcessAuthAsV3Client blocks until authentication has succeeded or failed with the
// provided identity, or the connection is closed. A non-nil error is returned in all
// cases other than successful authentication.
//
// ProcessAuthAsClient cannot be called at the same time as any other call to a Porcess
// ProcessAuthAsV3Client cannot be called at the same time as any other call to a Process
// function. Another Process function must be called after this function successfully
// returns to continue handling connection events.
//

View File

@ -34,7 +34,7 @@ func Initialize(name string, pk *rsa.PrivateKey) Identity {
return Identity{name, pk, nil, nil}
}
// Initialize is a courtesy function for initializing an Identity in-code.
// InitializeV3 is a courtesy function for initializing a V3 Identity in-code.
func InitializeV3(name string, pk *ed25519.PrivateKey, pubk *ed25519.PublicKey) Identity {
return Identity{name, nil, pk, pubk}
}
@ -66,6 +66,7 @@ func (i *Identity) PublicKeyBytes() []byte {
return publicKeyBytes
}
// EDH performs a diffie helman operation on this identities private key with the given public key.
func (i *Identity) EDH(key ed25519.PublicKey) []byte {
secret := utils.EDH(*i.edpk, key)
return secret[:]
@ -73,11 +74,10 @@ func (i *Identity) EDH(key ed25519.PublicKey) []byte {
// Hostname provides the onion address associated with this Identity.
func (i *Identity) Hostname() string {
if i.edpk != nil {
return utils.GetTorV3Hostname(*i.edpubk)
} else {
if i.pk != nil {
return utils.GetTorHostname(i.PublicKeyBytes())
}
return utils.GetTorV3Hostname(*i.edpubk)
}
// Sign produces a cryptographic signature using this Identities private key.

View File

@ -166,7 +166,7 @@ func Errorf(format string, v ...interface{}) {
std.Printf(LevelError, format, v...)
}
// Degubln outputs the variables at the Debug level
// Debugln outputs the variables at the Debug level
func Debugln(v ...interface{}) {
std.Println(LevelDebug, v...)
}

View File

@ -71,7 +71,7 @@ func (bot *ChatEchoBot) ChatMessage(messageID uint32, when time.Time, message st
log.Infof("ChatMessage(from: %v, %v", bot.rai.RemoteHostname, message)
bot.Messages.Add(bot.rai.RemoteHostname, bot.onion, message)
SendMessage(bot.rai, strconv.Itoa(bot.n)+" witty response")
bot.n += 1
bot.n++
return true
}
@ -193,7 +193,7 @@ func TestApplicationIntegration(t *testing.T) {
SendMessage(alicei, "Hello Bob!")
if err != nil {
log.Errorf("Error dialing from Alice to Bob: ", err)
log.Errorf("Error dialing from Alice to Bob: %v", err)
os.Exit(1)
}

View File

@ -1,7 +1,9 @@
#!/bin/sh
echo "Checking code quality (you want to see no output here)"
echo ""
echo "Formatting:"
gofmt -s -w -l .
echo "Vetting:"
go list ./... | xargs go vet
@ -9,4 +11,7 @@ go list ./... | xargs go vet
echo ""
echo "Linting:"
go list ./... | xargs golint
# Ignore wire packages as they are autogenerated
# Ignore examples as they are illustrative
# TODO Consider Renaming ApplicationInstance and ApplicationInstanceFactory to remove the last grep
go list ./... | grep -v "/wire/" | grep -v "/examples/" | grep -v "/application" | xargs golint

View File

@ -46,6 +46,7 @@ func EDH(privateKey ed25519.PrivateKey, remotePublicKey ed25519.PublicKey) [32]b
return secret
}
// GeneratePrivateKeyV3 cryptographically creats a new ed25519 key pair.
func GeneratePrivateKeyV3() (ed25519.PublicKey, ed25519.PrivateKey, error) {
return ed25519.GenerateKey(rand.Reader)
}

View File

@ -101,7 +101,7 @@ func (mb *MessageBuilder) Open3EDHAuthenticationChannel(channelID int32, pubkey
return ret
}
// ConfirmAuthChannel constructs a message to acknowledge a previous open channel operation.
// Confirm3EDHAuthChannel constructs a message to acknowledge a previous open channel operation.
func (mb *MessageBuilder) Confirm3EDHAuthChannel(channelID int32, pubkey [32]byte, ephemeralKey [32]byte) []byte {
cr := &Protocol_Data_Control.ChannelResult{
ChannelIdentifier: proto.Int32(channelID),
@ -122,7 +122,7 @@ func (mb *MessageBuilder) Confirm3EDHAuthChannel(channelID int32, pubkey [32]byt
return ret
}
// DHProof constructs a proof message with the given public key and signature.
// Proof3DH constructs a proof message with the given public key and signature.
func (mb *MessageBuilder) Proof3DH(proofBytes []byte) []byte {
proof := &Protocol_Data_Auth_TripleEDH.Proof{
Proof: proofBytes,
@ -234,7 +234,7 @@ func (mb *MessageBuilder) Proof(publicKeyBytes []byte, signatureBytes []byte) []
return ret
}
// AuthResult constructs a response to a Proof
// AuthResult3DH constructs a response to a Proof
func (mb *MessageBuilder) AuthResult3DH(accepted bool, isKnownContact bool) []byte {
// Construct a Result Message
result := &Protocol_Data_Auth_TripleEDH.Result{

View File

@ -33,7 +33,8 @@ func expandKey(pri ed25519.PrivateKey) string {
return base64.StdEncoding.EncodeToString(h[:])
}
const V3HostnameLength = 56
// V3HostnameLength is the length of a Tor V3 Onion Address (without the .onion suffix)
const V3HostnameLength = 56
// Hidden service version
const version = byte(0x03)
@ -65,7 +66,7 @@ func GetTorV3Hostname(pub ed25519.PublicKey) string {
func IsValidHostname(address string) bool {
if len(address) == V3HostnameLength {
data, err := base32.StdEncoding.DecodeString(strings.ToUpper(address))
if err ==nil {
if err == nil {
pubkey := data[0:ed25519.PublicKeySize]
if GetTorV3Hostname(ed25519.PublicKey(pubkey)) == address {
return true
@ -73,4 +74,4 @@ func IsValidHostname(address string) bool {
}
}
return false
}
}

View File

@ -44,9 +44,8 @@ func TestGetTorHostname(t *testing.T) {
}
}
func TestV3(t *testing.T) {
pub,_,_ := ed25519.GenerateKey(rand.Reader)
pub, _, _ := ed25519.GenerateKey(rand.Reader)
hostname := GetTorV3Hostname(pub)
if !IsValidHostname(hostname) {
t.Errorf("Generated V3 Hostname was invalid")
@ -55,4 +54,4 @@ func TestV3(t *testing.T) {
if IsValidHostname(hostname[0:34]) {
t.Errorf("Invalid V3 Hostname was marked valid")
}
}
}