-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
82 lines (69 loc) · 1.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"net"
xco "github.com/sheenobu/go-xco"
)
func main() {
flag.Parse()
if e := validateArguments(); e != nil {
fmt.Printf("%v\n", e)
return
}
opts := xco.Options{
Name: *xmppName,
SharedSecret: *xmppSharedSecret,
Address: joinHostPort(*xmppIP, *xmppPort),
}
c, err := xco.NewComponent(opts)
if err != nil {
fmt.Printf("error when connecting to server: %v\n", err)
return
}
c.MessageHandler = messageHandler
c.IqHandler = iqHandler
err = c.Run()
if err != nil {
fmt.Printf("error when running component: %v\n", err)
return
}
}
func joinHostPort(ip string, port uint) string {
return net.JoinHostPort(ip, fmt.Sprintf("%d", port))
}
func getTCPAddr(ip string, port uint) *net.TCPAddr {
addr, _ := net.ResolveTCPAddr("tcp", joinHostPort(ip, port))
return addr
}
func getPrekeyResponseFromRealServer(u string, data []byte) ([]byte, error) {
addr := getTCPAddr(*rawIP, *rawPort)
con, e := net.DialTCP(addr.Network(), nil, addr)
if e != nil {
return nil, e
}
defer con.Close()
toSend := []byte{}
toSend = appendShort(toSend, uint16(len(u)))
toSend = append(toSend, []byte(u)...)
toSend = appendShort(toSend, uint16(len(data)))
toSend = append(toSend, data...)
if _, e = con.Write(toSend); e != nil {
return nil, e
}
con.CloseWrite()
res, e := ioutil.ReadAll(con)
if e != nil {
return nil, e
}
if len(res) == 0 {
return nil, errors.New("server closed connection without sending any data - this probably happened because of a malformed request")
}
res2, ss, ok := extractShort(res)
if !ok || uint16(len(res2)) != ss {
return nil, errors.New("unexpected length of data received")
}
return res2, nil
}