Unverified Commit cd014ba7 authored by Ola Bini's avatar Ola Bini

More refactoring and fix all outstanding TODOs

parent 40e80111
package main
import "flag"
import (
"errors"
"flag"
"regexp"
)
// Here we can have command line options later
var (
rawPort = flag.Uint("raw-port", 3242, "Port to connect to the raw server on")
rawIP = flag.String("raw-address", "localhost", "Address to connect to the raw server on")
......@@ -11,4 +14,23 @@ var (
xmppSharedSecret = flag.String("shared-secret", "changeme", "Shared secret for authenticating to the XMPP server")
xmppName = flag.String("name", "changeme", "Name for the XMPP component. Usually something like prekeys.example.org")
prekeyServerFingerprint = flag.String("fingerprint", "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "Fingerprint for the prekey server. This is expected to be 56 bytes expressed in hexadecimal - thus 112 digits")
validDomains = flag.String("valid-domains", "", "A list of all domains that are valid for users, separated by commas. An empty list is equal to no restriction.")
)
func isFingerprint(name string) bool {
res, _ := regexp.MatchString("^[0-9A-Fa-f]{112}$", name)
return res
}
func validateArguments() error {
if !isFingerprint(*prekeyServerFingerprint) {
return errors.New("fingerprint provided is not valid")
}
if *xmppName == "" || *xmppName == "changeme" {
return errors.New("invalid xmpp name given")
}
if *xmppSharedSecret == "" {
return errors.New("invalid shared secret given")
}
return nil
}
package main
import (
"fmt"
xco "github.com/sheenobu/go-xco"
)
func iqHandler(c *xco.Component, m *xco.Iq) error {
ret, _, _ := processIQ(m)
resp := xco.Iq{
Header: xco.Header{
From: m.To,
To: m.From,
ID: m.ID,
},
Type: "result",
Content: xmlToString(ret),
XMLName: m.XMLName,
}
e := c.Send(&resp)
if e != nil {
fmt.Printf("encountered error when sending response: %v\n", e)
}
return nil
}
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
......@@ -12,9 +13,10 @@ import (
func main() {
flag.Parse()
// TODO: validate fingerprint
// TODO: validate xmppName
// TODO: validate shared secret
if e := validateArguments(); e != nil {
fmt.Printf("%v\n", e)
return
}
opts := xco.Options{
Name: *xmppName,
......@@ -24,53 +26,17 @@ func main() {
c, err := xco.NewComponent(opts)
if err != nil {
// TODO: print error properly
panic(err)
}
c.MessageHandler = func(_ *xco.Component, m *xco.Message) error {
res := getPrekeyResponseFromRealServer(fmt.Sprintf("%s@%s", m.Header.From.LocalPart, m.Header.From.DomainPart), []byte(m.Body))
resp := xco.Message{
Header: xco.Header{
From: m.To,
To: m.From,
ID: m.ID,
},
Type: m.Type,
Body: string(res),
XMLName: m.XMLName,
}
c.Send(&resp)
// TODO: print error properly
return nil
fmt.Printf("error when connecting to server: %v\n", err)
return
}
c.IqHandler = func(_ *xco.Component, m *xco.Iq) error {
ret, _, _ := processIQ(m)
// TODO: print error properly
resp := xco.Iq{
Header: xco.Header{
From: m.To,
To: m.From,
ID: m.ID,
},
Type: "result",
Content: xmlToString(ret),
XMLName: m.XMLName,
}
c.Send(&resp)
// TODO: print error properly
return nil
}
c.MessageHandler = messageHandler
c.IqHandler = iqHandler
e := c.Run()
if e != nil {
// TODO: print error properly
panic(e)
err = c.Run()
if err != nil {
fmt.Printf("error when running component: %v\n", err)
return
}
}
......@@ -83,10 +49,12 @@ func getTCPAddr(ip string, port uint) *net.TCPAddr {
return addr
}
func getPrekeyResponseFromRealServer(u string, data []byte) []byte {
func getPrekeyResponseFromRealServer(u string, data []byte) ([]byte, error) {
addr := getTCPAddr(*rawIP, *rawPort)
con, _ := net.DialTCP(addr.Network(), nil, addr)
// TODO: print error properly
con, e := net.DialTCP(addr.Network(), nil, addr)
if e != nil {
return nil, e
}
defer con.Close()
toSend := []byte{}
......@@ -94,15 +62,17 @@ func getPrekeyResponseFromRealServer(u string, data []byte) []byte {
toSend = append(toSend, []byte(u)...)
toSend = appendShort(toSend, uint16(len(data)))
toSend = append(toSend, data...)
con.Write(toSend)
// TODO: print error properly
if _, e = con.Write(toSend); e != nil {
return nil, e
}
con.CloseWrite()
res, _ := ioutil.ReadAll(con)
// TODO: print error properly
res2, ss, _ := extractShort(res)
if uint16(len(res2)) != ss {
fmt.Printf("Unexpected length of data received\n")
return nil
res, e := ioutil.ReadAll(con)
if e != nil {
return nil, e
}
res2, ss, ok := extractShort(res)
if !ok || uint16(len(res2)) != ss {
return nil, errors.New("unexpected length of data received")
}
return res2
return res2, nil
}
package main
import (
"fmt"
"strings"
xco "github.com/sheenobu/go-xco"
)
func contains(a []string, x string) bool {
for _, n := range a {
if x == n {
return true
}
}
return false
}
func validDomain(d string) bool {
if *validDomains == "" {
return true
}
ds := strings.Split(*validDomains, ",")
return contains(ds, d)
}
func messageHandler(c *xco.Component, m *xco.Message) error {
if !validDomain(m.Header.From.DomainPart) {
fmt.Printf("Attempt to add data from unauthorized domain: %v@%v\n", m.Header.From.LocalPart, m.Header.From.DomainPart)
return nil
}
res, e := getPrekeyResponseFromRealServer(fmt.Sprintf("%s@%s", m.Header.From.LocalPart, m.Header.From.DomainPart), []byte(m.Body))
if e != nil {
fmt.Printf("encountered error when communicating with the raw server: %v\n", e)
return nil
}
resp := xco.Message{
Header: xco.Header{
From: m.To,
To: m.From,
ID: m.ID,
},
Type: m.Type,
Body: string(res),
XMLName: m.XMLName,
}
e = c.Send(&resp)
if e != nil {
fmt.Printf("encountered error when sending response: %v\n", e)
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment