-
Notifications
You must be signed in to change notification settings - Fork 16
/
hpp_server.go
230 lines (186 loc) · 5.64 KB
/
hpp_server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package nex
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strconv"
"github.com/PretendoNetwork/nex-go/v2/types"
)
// HPPServer represents a bare-bones HPP server
type HPPServer struct {
server *http.Server
accessKey string
libraryVersions *LibraryVersions
dataHandlers []func(packet PacketInterface)
errorEventHandlers []func(err *Error)
byteStreamSettings *ByteStreamSettings
AccountDetailsByPID func(pid *types.PID) (*Account, *Error)
AccountDetailsByUsername func(username string) (*Account, *Error)
useVerboseRMC bool
}
// RegisterServiceProtocol registers a NEX service with the HPP server
func (s *HPPServer) RegisterServiceProtocol(protocol ServiceProtocol) {
protocol.SetEndpoint(s)
s.OnData(protocol.HandlePacket)
}
// OnData adds an event handler which is fired when a new HPP request is received
func (s *HPPServer) OnData(handler func(packet PacketInterface)) {
s.dataHandlers = append(s.dataHandlers, handler)
}
// EmitError calls all the endpoints error event handlers with the provided error
func (s *HPPServer) EmitError(err *Error) {
for _, handler := range s.errorEventHandlers {
go handler(err)
}
}
func (s *HPPServer) handleRequest(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" {
w.WriteHeader(http.StatusBadRequest)
return
}
pidValue := req.Header.Get("pid")
if pidValue == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
// * The server checks that the header exists, but doesn't verify the value
token := req.Header.Get("token")
if token == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
accessKeySignature := req.Header.Get("signature1")
if accessKeySignature == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
passwordSignature := req.Header.Get("signature2")
if passwordSignature == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
pid, err := strconv.Atoi(pidValue)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
rmcRequestString := req.FormValue("file")
rmcRequestBytes := []byte(rmcRequestString)
tcpAddr, err := net.ResolveTCPAddr("tcp", req.RemoteAddr)
if err != nil {
// * Should never happen?
logger.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
client := NewHPPClient(tcpAddr, s)
client.SetPID(types.NewPID(uint64(pid)))
hppPacket, err := NewHPPPacket(client, rmcRequestBytes)
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
err = hppPacket.validateAccessKeySignature(accessKeySignature)
if err != nil {
logger.Error(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
err = hppPacket.validatePasswordSignature(passwordSignature)
if err != nil {
logger.Error(err.Error())
rmcMessage := hppPacket.RMCMessage()
// HPP returns PythonCore::ValidationError if password is missing or invalid
errorResponse := NewRMCError(s, ResultCodes.PythonCore.ValidationError)
errorResponse.CallID = rmcMessage.CallID
errorResponse.IsHPP = true
_, err = w.Write(errorResponse.Bytes())
if err != nil {
logger.Error(err.Error())
}
return
}
for _, dataHandler := range s.dataHandlers {
go dataHandler(hppPacket)
}
<-hppPacket.processed
if len(hppPacket.payload) > 0 {
_, err = w.Write(hppPacket.payload)
if err != nil {
logger.Error(err.Error())
}
}
}
// Listen starts a HPP server on a given port
func (s *HPPServer) Listen(port int) {
s.server.Addr = fmt.Sprintf(":%d", port)
err := s.server.ListenAndServe()
if err != nil {
panic(err)
}
}
// ListenSecure starts a HPP server on a given port using a secure (TLS) server
func (s *HPPServer) ListenSecure(port int, certFile, keyFile string) {
s.server.Addr = fmt.Sprintf(":%d", port)
err := s.server.ListenAndServeTLS(certFile, keyFile)
if err != nil {
panic(err)
}
}
// Send sends the packet to the packets sender
func (s *HPPServer) Send(packet PacketInterface) {
if packet, ok := packet.(*HPPPacket); ok {
packet.message.IsHPP = true
packet.payload = packet.message.Bytes()
packet.processed <- true
}
}
// LibraryVersions returns the versions that the server has
func (s *HPPServer) LibraryVersions() *LibraryVersions {
return s.libraryVersions
}
// AccessKey returns the servers sandbox access key
func (s *HPPServer) AccessKey() string {
return s.accessKey
}
// SetAccessKey sets the servers sandbox access key
func (s *HPPServer) SetAccessKey(accessKey string) {
s.accessKey = accessKey
}
// ByteStreamSettings returns the settings to be used for ByteStreams
func (s *HPPServer) ByteStreamSettings() *ByteStreamSettings {
return s.byteStreamSettings
}
// SetByteStreamSettings sets the settings to be used for ByteStreams
func (s *HPPServer) SetByteStreamSettings(byteStreamSettings *ByteStreamSettings) {
s.byteStreamSettings = byteStreamSettings
}
// UseVerboseRMC checks whether or not the endpoint uses verbose RMC
func (s *HPPServer) UseVerboseRMC() bool {
return s.useVerboseRMC
}
// EnableVerboseRMC enable or disables the use of verbose RMC
func (s *HPPServer) EnableVerboseRMC(enable bool) {
s.useVerboseRMC = enable
}
// NewHPPServer returns a new HPP server
func NewHPPServer() *HPPServer {
s := &HPPServer{
dataHandlers: make([]func(packet PacketInterface), 0),
errorEventHandlers: make([]func(err *Error), 0),
libraryVersions: NewLibraryVersions(),
byteStreamSettings: NewByteStreamSettings(),
}
mux := http.NewServeMux()
mux.HandleFunc("/hpp/", s.handleRequest)
httpServer := &http.Server{
Handler: mux,
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS11, // * The 3DS and Wii U only support up to TLS 1.1 natively
},
}
s.server = httpServer
return s
}