forked from mcstatus-io/mcutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy_vote.go
111 lines (85 loc) · 2.17 KB
/
legacy_vote.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package mcutil
import (
"bufio"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"net"
"strings"
"time"
"github.com/mcstatus-io/mcutil/v2/options"
)
// SendLegacyVote sends a legacy Votifier vote to the specified Minecraft server
func SendLegacyVote(ctx context.Context, host string, port uint16, opts options.LegacyVote) error {
e := make(chan error, 1)
go func() {
e <- sendLegacyVote(host, port, opts)
}()
select {
case <-ctx.Done():
if v := ctx.Err(); v != nil {
return v
}
return errors.New("context finished before server sent response")
case v := <-e:
return v
}
}
func sendLegacyVote(host string, port uint16, opts options.LegacyVote) error {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), opts.Timeout)
if err != nil {
return err
}
defer conn.Close()
r := bufio.NewReader(conn)
if err = conn.SetDeadline(time.Now().Add(opts.Timeout)); err != nil {
return err
}
// Handshake packet
// https://github.com/NuVotifier/NuVotifier/wiki/Technical-QA#protocol-v1-deprecated
{
data, err := r.ReadBytes('\n')
if err != nil {
return err
}
split := strings.Split(string(data[:len(data)-1]), " ")
if !strings.HasPrefix(split[1], "1") {
return fmt.Errorf("vote: unknown server Votifier version: %s", split[1])
}
}
// Vote packet
// https://github.com/NuVotifier/NuVotifier/wiki/Technical-QA#protocol-v1-deprecated
{
block, _ := pem.Decode([]byte(fmt.Sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----", opts.PublicKey)))
if block == nil {
return errors.New("invalid public key value")
}
key, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
publicKey, ok := key.(*rsa.PublicKey)
if !ok {
return fmt.Errorf("parsed invalid key type: %T", key)
}
payload := fmt.Sprintf(
"VOTE\n%s\n%s\n%s\n%s",
opts.ServiceName,
opts.Username,
opts.IPAddress,
opts.Timestamp.Format(time.RFC3339),
)
encryptedPayload, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, []byte(payload))
if err != nil {
return err
}
if _, err = conn.Write(encryptedPayload); err != nil {
return err
}
}
return nil
}