Cleaning up error handling

Getting rid of lots of FATALs, now they can be handled by the calling
function.
This commit is contained in:
Sarah Jamie Lewis 2016-01-04 19:53:10 -08:00
parent 97f5982754
commit 04f99b298f
1 changed files with 43 additions and 46 deletions

View File

@ -41,7 +41,7 @@ const (
// Ricochet is a protocol to conducting anonymous IM.
type Ricochet struct {
conn net.Conn
privateKey *pem.Block
privateKey *rsa.PrivateKey
logger *log.Logger
channelState map[int]int
channel chan RicochetMessage
@ -89,7 +89,9 @@ func (r *Ricochet) Init(filename string, debugLog bool) {
r.logger.Print("No valid PEM data found")
}
r.privateKey = block
r.privateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)
r.handleFatal(err, "Private key can't be decoded")
r.channelState = make(map[int]int)
r.channel = make(chan RicochetMessage)
}
@ -105,12 +107,10 @@ func (r *Ricochet) Connect(from string, to string) error {
toAddr := strings.Split(to, "|")
tcpAddr, err := net.ResolveTCPAddr("tcp", toAddr[0])
if err != nil {
r.logger.Fatal("Cannot Resolve TCP Address ", err)
return errors.New("Cannot Resolve Local TCP Address")
}
r.conn, err = net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
r.logger.Fatal("Cannot Dial TCP Address ", err)
return errors.New("Cannot Dial Local TCP Address")
}
r.logger.Print("Connected to " + to + " as " + toAddr[1])
@ -120,7 +120,6 @@ func (r *Ricochet) Connect(from string, to string) error {
r.logger.Print("Connecting to ", to+".onion:9878")
conn, err := dialSocksProxy("", to+".onion:9878")
if err != nil {
r.logger.Fatal("Cannot Dial Remove Address ", err)
return errors.New("Cannot Dial Remote Ricochet Address")
}
r.conn = conn
@ -144,7 +143,7 @@ func (r *Ricochet) Connect(from string, to string) error {
data, err := proto.Marshal(pc)
if err != nil {
r.logger.Fatal("Cannot Marshal Open Channel Message: ", err)
return errors.New("Cannot Marshal Open Channel Message")
}
r.sendPacket(data, 0)
@ -175,21 +174,18 @@ func (r *Ricochet) Connect(from string, to string) error {
hmac := mac.Sum(nil)
r.logger.Print("Got HMAC: ", hmac)
privateKey, err := x509.ParsePKCS1PrivateKey(r.privateKey.Bytes)
r.handleFatal(err, "Private key can't be decoded")
// DER Encode the Public Key
publickeybytes, err := asn1.Marshal(rsa.PublicKey{
N: privateKey.PublicKey.N,
E: privateKey.PublicKey.E,
N: r.privateKey.PublicKey.N,
E: r.privateKey.PublicKey.E,
})
signature, _ := rsa.SignPKCS1v15(nil, privateKey, crypto.SHA256, hmac)
signature, _ := rsa.SignPKCS1v15(nil, r.privateKey, crypto.SHA256, hmac)
signatureBytes := make([]byte, 128)
copy(signatureBytes[:], signature[:])
r.logger.Print("Signature Length: ", len(signatureBytes))
r.logger.Print("Public Key Length: ", len(publickeybytes), ", Bit Size: ", privateKey.PublicKey.N.BitLen())
r.logger.Print("Public Key Length: ", len(publickeybytes), ", Bit Size: ", r.privateKey.PublicKey.N.BitLen())
// Construct a Proof Message
proof := &Protocol_Data_AuthHiddenService.Proof{
@ -241,7 +237,7 @@ func (r *Ricochet) OpenChannel(channelType string, id int) error {
// SendContactRequest initiates a contact request to the server.
// Prerequisites:
// * Must have Previously issued a successful Connect()
func (r *Ricochet) SendContactRequest(nick string, message string) {
func (r *Ricochet) SendContactRequest(nick string, message string) error {
// Construct a Contact Request Channel
oc := &Protocol_Data_Control.OpenChannel{
ChannelIdentifier: proto.Int32(3),
@ -260,11 +256,11 @@ func (r *Ricochet) SendContactRequest(nick string, message string) {
data, err := proto.Marshal(pc)
if err != nil {
r.logger.Fatal("Cannot Marshal Open Channel Message: ", err)
return errors.New("Cannot Marshal Open Channel Message")
}
r.sendPacket(data, 0)
return nil
}
// SendMessage sends a Chat Message (message) to a give Channel (channel).
@ -286,7 +282,7 @@ func (r *Ricochet) SendMessage(message string, channel int) {
}
// negotiateVersion Perform version negotiation with the connected host.
func (r *Ricochet) negotiateVersion() {
func (r *Ricochet) negotiateVersion() error {
version := make([]byte, 4)
version[0] = 0x49
version[1] = 0x4D
@ -297,14 +293,15 @@ func (r *Ricochet) negotiateVersion() {
res, err := r.recv()
if len(res) != 1 || err != nil {
r.logger.Fatal("Failed Version Negotiating: ", res, err)
return errors.New("Failed Version Negotiating")
}
if res[0] != 1 {
r.logger.Fatal("Failed Version Negotiating - Invalid Version ", res)
return errors.New("Failed Version Negotiating - Invalid Version ")
}
r.logger.Print("Successfully Negotiated Version ", res[0])
return nil
}
// sendPacket places the data into a structure needed for the client to
@ -319,7 +316,6 @@ func (r *Ricochet) sendPacket(data []byte, channel int) {
fmt.Fprintf(r.conn, "%s", header)
}
// Listen blocks and waits for a new message to arrive from the connected user
// once a message has arrived, it returns the message and the channel it occured
// on, else it returns an error.
@ -372,7 +368,7 @@ func (r *Ricochet) ListenAndWait() error {
if err != nil {
r.logger.Printf("Failed to decode control packet, discarding")
break;
break
}
// Automatically accept new channels
@ -388,6 +384,7 @@ func (r *Ricochet) ListenAndWait() error {
}
data, err := proto.Marshal(pc)
// TODO we should set up some kind of error channel.
r.handleFatal(err, "error marshalling control protocol")
r.logger.Printf("Client Opening Channel: %d\n", message.ControlPacket.GetOpenChannel().GetChannelIdentifier())
@ -416,7 +413,7 @@ func (r *Ricochet) ListenAndWait() error {
message, err := r.decodePacket(packet, DATA)
if err != nil {
r.logger.Printf("Failed to decode data packet, discarding")
break;
break
}
r.channel <- message
}
@ -445,7 +442,7 @@ func (r *Ricochet) decodePacket(packet RicochetData, t MessageType) (rm Ricochet
}
if err != nil {
r.logger.Fatal("Error Unmarshalling Response", err)
return rm, errors.New("Error Unmarshalling Response")
}
return rm, err
}