-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeys.go
More file actions
151 lines (125 loc) · 3.5 KB
/
Copy pathkeys.go
File metadata and controls
151 lines (125 loc) · 3.5 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"context"
"fmt"
"maps"
"net"
"os"
"os/signal"
"slices"
"sync"
"time"
"github.com/google/uuid"
"github.com/iteplenky/bedrock-pack-tools/v3/internal/lang"
"github.com/iteplenky/gophertunnel/minecraft"
"github.com/iteplenky/gophertunnel/minecraft/protocol/packet"
)
type keysTracker struct {
mu sync.Mutex
totalPacks int
keys *keyStore
cancel context.CancelFunc
connectSpinner *spinner
}
func (p *keysTracker) onPackStart(id uuid.UUID, version string, current, total int) bool {
p.mu.Lock()
p.totalPacks = total
p.mu.Unlock()
if p.connectSpinner != nil {
p.connectSpinner.stop("")
}
fmt.Printf(lang.T("keys.pack.skipped"), clearLine, current+1, total, id, version)
return false
}
func (p *keysTracker) onPacket(header packet.Header, payload []byte, src, dst net.Addr) {
if header.PacketID == packet.IDResourcePacksInfo {
p.onResourcePacksInfo(payload)
}
}
func (p *keysTracker) onResourcePacksInfo(payload []byte) {
packs, err := parseResourcePacks(payload)
if err != nil {
fmt.Fprintf(os.Stderr, lang.T("keys.warn"), err)
// Cancel anyway - ResourcePacksInfo arrives once per connection.
if p.cancel != nil {
p.cancel()
}
return
}
p.mu.Lock()
p.totalPacks = len(packs)
p.mu.Unlock()
p.keys.merge(collectKeys(packs))
if p.cancel != nil {
p.cancel()
}
}
func runKeys(args []string) error {
if len(args) < 1 {
fmt.Println(lang.T("keys.usage"))
return errUsage
}
server := args[0]
outFile := sanitizeServerAddr(server) + keysSuffix
if len(args) > 1 {
outFile = args[1]
}
fmt.Println(lang.T("keys.header.title"))
fmt.Println(lang.T("keys.header.server") + server)
fmt.Println(lang.T("keys.header.output") + outFile)
fmt.Println(lang.T("keys.header.rule"))
tokenSource, err := getTokenSource()
if err != nil {
return err
}
sigCtx, stopSignal := signal.NotifyContext(context.Background(), os.Interrupt)
defer stopSignal()
ctx, cancel := context.WithTimeout(sigCtx, dialTimeout(120*time.Second))
defer cancel()
tracker := &keysTracker{
keys: newKeyStore(outFile),
cancel: cancel,
}
dialer := minecraft.Dialer{
TokenSource: tokenSource,
DownloadResourcePack: tracker.onPackStart,
PacketFunc: tracker.onPacket,
}
fmt.Println()
tracker.connectSpinner = startSpinner(lang.T("keys.spinner.connecting") + server)
start := time.Now()
conn, err := dialer.DialContext(ctx, "raknet", server)
elapsed := time.Since(start)
tracker.connectSpinner.stop("")
keys := tracker.keys.snapshot()
keyCount := len(keys)
tracker.mu.Lock()
totalPacks := tracker.totalPacks
tracker.mu.Unlock()
if err != nil {
if keyCount > 0 {
fmt.Printf(lang.T("keys.partial.captured"), clearLine, elapsed.Seconds(), totalPacks)
printKeys(keys)
fmt.Printf(lang.T("keys.partial.saved"), keyCount, outFile)
return errPartialResult
}
return fmt.Errorf(lang.T("keys.connect.failed"), server, err)
}
defer conn.Close()
fmt.Printf(lang.T("keys.connected"), clearLine, elapsed.Seconds())
printKeys(keys)
fmt.Printf(lang.T("keys.total"), totalPacks, keyCount)
if keyCount > 0 {
fmt.Printf(lang.T("keys.saved"), keyCount, outFile)
} else {
fmt.Println(lang.T("keys.none") + outFile + lang.T("keys.none.tail"))
}
return nil
}
func printKeys(keys map[string]keyEntry) {
for _, uid := range slices.Sorted(maps.Keys(keys)) {
info := keys[uid]
fmt.Printf(lang.T("keys.entry.head"), colorYellow, colorReset, uid, info.Version, info.Name)
fmt.Printf(lang.T("keys.entry.key"), colorGreen, info.Key, colorReset)
}
}