-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntcpclient.go
151 lines (128 loc) · 2.84 KB
/
ntcpclient.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
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 (
"encoding/json"
"fmt"
"io"
"net"
"github.com/vikebot/vbcore"
"github.com/vikebot/vbgs/vbge"
"go.uber.org/zap"
)
type ntcpclient struct {
Out io.Writer
Log *zap.Logger
Authenticated bool
UserID int
Crypt *vbcore.CryptoService
CurType string
SDK string
SDKLink string
OS string
Player *vbge.Player
IP string
PureIP string
LoginDone bool
ClienthelloDone bool
AgreeconnDone bool
IsEncrypted bool
StartPc uint32
Pc uint32
}
func newNtcpclient(ip net.Addr, w io.Writer, ctx *zap.Logger) *ntcpclient {
pureip := ip.String()
if addr, ok := ip.(*net.TCPAddr); ok {
pureip = addr.IP.String()
}
return &ntcpclient{
Log: ctx,
IP: ip.String(),
PureIP: pureip,
Out: w,
CurType: "unknown",
}
}
type typePacket struct {
Type *string `json:"type"`
Pc *uint32 `json:"pc"`
Obj *interface{} `json:"obj"`
}
type defaultResponse struct {
Type string `json:"type"`
Pc *uint32 `json:"pc,omitempty"`
Error *string `json:"error"`
}
func newDefaultResponse(c *ntcpclient, err *string) defaultResponse {
dr := defaultResponse{
Type: c.CurType,
Error: err,
}
if c.IsEncrypted {
c.Pc++
dr.Pc = &c.Pc
}
return dr
}
type defaultObjResponse struct {
Type string `json:"type"`
Pc *uint32 `json:"pc,omitempty"`
Error *string `json:"error"`
Obj interface{} `json:"obj"`
}
func newDefaultObjResponse(c *ntcpclient, d interface{}) defaultObjResponse {
dr := defaultObjResponse{
Type: c.CurType,
Error: nil,
Obj: d,
}
if c.IsEncrypted {
c.Pc++
dr.Pc = &c.Pc
}
return dr
}
func (c *ntcpclient) MgmtWrite(d interface{}) {
buf, err := json.Marshal(d)
if err != nil {
c.Log.Warn("failed to marshal interface", zap.Error(err))
return
}
// Precreate debug message (only print it if encryption succeeds)
pkt := string(buf)
// Encrypt
if c.IsEncrypted && !envDisableCrypt {
cipher, err := c.Crypt.EncryptBase64(buf)
if err != nil {
c.Log.Error("encrypting buffer failed", zap.Error(err))
return
}
buf = cipher
}
c.Log.Debug("sent",
zap.String("packet", pkt),
zap.Uint32("seqnr", c.Pc-c.StartPc))
buf = append(buf, '\n')
_, err = c.Out.Write(buf)
if err != nil {
c.Log.Warn("sending failed", zap.Error(err))
}
}
func (c *ntcpclient) RespondNil() {
c.MgmtWrite(newDefaultResponse(c, nil))
}
func (c *ntcpclient) Respond(errorText string) {
c.MgmtWrite(newDefaultResponse(c, &errorText))
}
func (c *ntcpclient) RespondFmt(format string, a ...interface{}) {
c.Respond(fmt.Sprintf(format, a...))
}
func (c *ntcpclient) RespondObj(d interface{}) {
c.MgmtWrite(newDefaultObjResponse(c, d))
}
func (c *ntcpclient) InitAes(key []byte) error {
cs, err := vbcore.NewCryptoService(key)
if err != nil {
return err
}
c.Crypt = cs
return nil
}