Split responses into templates

This commit is contained in:
Spotlight 2020-07-28 04:24:02 -05:00
parent ce7afd94ec
commit 9088111c4e
No known key found for this signature in database
GPG Key ID: 874AA355B3209BDC
4 changed files with 88 additions and 144 deletions

78
ecs.go
View File

@ -56,28 +56,14 @@ func ecsHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(CDS)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<CheckDeviceStatusResponse xmlns="urn:ecs.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<Balance>
custom := fmt.Sprintf(`<Balance>
<Amount>2018</Amount>
<Currency>POINTS</Currency>
</Balance>
<ForceSyncTime>0</ForceSyncTime>
<ExtTicketTime>%s</ExtTicketTime>
<SyncTime>%s</SyncTime>
</CheckDeviceStatusResponse>
</soapenv:Body>
</soapenv:Envelope>`, CDS.Version, CDS.DeviceID, CDS.MessageID, timestamp, timestamp, timestamp)
<SyncTime>%s</SyncTime>`, timestamp, timestamp)
fmt.Fprint(w, formatSuccess("ecs", action, CDS.Version, CDS.DeviceID, CDS.MessageID, custom))
case "NotifiedETicketsSynced":
fmt.Println("NETS")
@ -90,21 +76,7 @@ func ecsHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(NETS)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<NotifyETicketsSyncedResponse xmlns="urn:ecs.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
</NotifyETicketsSyncedResponse>
</soapenv:Body>
</soapenv:Envelope>`, NETS.Version, NETS.DeviceID, NETS.MessageID, timestamp)
fmt.Fprint(w, formatSuccess("ecs", action, NETS.Version, NETS.DeviceID, NETS.MessageID, ""))
case "ListETickets":
fmt.Println("LET")
@ -117,24 +89,10 @@ func ecsHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(LET)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ListETicketsResponse xmlns="urn:ecs.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<ForceSyncTime>0</ForceSyncTime>
custom := fmt.Sprintf(`<ForceSyncTime>0</ForceSyncTime>
<ExtTicketTime>%s</ExtTicketTime>
<SyncTime>%s</SyncTime>
</ListETicketsResponse>
</soapenv:Body>
</soapenv:Envelope>`, LET.Version, LET.DeviceID, LET.MessageID, timestamp, timestamp, timestamp)
<SyncTime>%s</SyncTime>`, timestamp, timestamp)
fmt.Fprint(w, formatSuccess("ecs", action, LET.Version, LET.DeviceID, LET.MessageID, custom))
case "PurchaseTitle":
fmt.Println("PT")
@ -147,19 +105,7 @@ func ecsHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(PT)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<PurchaseTitleResponse xmlns="urn:ecs.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<Balance>
custom := fmt.Sprintf(`<Balance>
<Amount>2018</Amount>
<Currency>POINTS</Currency>
</Balance>
@ -172,13 +118,11 @@ func ecsHandler(w http.ResponseWriter, r *http.Request) {
<ETickets>00000000</ETickets>
<Certs>00000000</Certs>
<Certs>00000000</Certs>
<TitleId>00000000</TitleId>
</PurchaseTitleResponse>
</soapenv:Body>
</soapenv:Envelope>`, PT.Version, PT.DeviceID, PT.MessageID, timestamp, timestamp, timestamp)
<TitleId>00000000</TitleId>`, timestamp, timestamp)
fmt.Fprint(w, formatSuccess("ecs", action, PT.Version, PT.DeviceID, PT.MessageID, custom))
default:
fmt.Fprintf(w, "WiiSOAP can't handle this. Try again later or actually use a Wii instead of a computer.")
fmt.Fprint(w, "WiiSOAP can't handle this. Try again later or actually use a Wii instead of a computer.")
return
}

82
ias.go
View File

@ -22,8 +22,6 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
func iasHandler(w http.ResponseWriter, r *http.Request) {
@ -31,10 +29,6 @@ func iasHandler(w http.ResponseWriter, r *http.Request) {
action := r.Header.Get("SOAPAction")
action = parseAction(action, "ias")
// Get a sexy new timestamp to use.
timestampNano := strconv.FormatInt(time.Now().UTC().Unix(), 10)
timestamp := timestampNano + "000"
fmt.Println("[!] Incoming IAS request.")
body, err := ioutil.ReadAll(r.Body)
if err != nil {
@ -57,23 +51,9 @@ func iasHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(CR)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<CheckRegistrationResponse xmlns="urn:ias.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<OriginalSerialNumber>%s</OriginalSerialNumber>
<DeviceStatus>R</DeviceStatus>
</CheckRegistrationResponse>
</soapenv:Body>
</soapenv:Envelope>`, CR.Version, CR.DeviceID, CR.DeviceID, timestamp, CR.SerialNo)
custom := fmt.Sprintf(`<OriginalSerialNumber>%s</OriginalSerialNumber>
<DeviceStatus>R</DeviceStatus>`, CR.SerialNo)
fmt.Fprint(w, formatSuccess("ias", action, CR.Version, CR.DeviceID, CR.MessageID, custom))
case "GetRegistrationInfo":
fmt.Println("GRI.")
@ -86,29 +66,15 @@ func iasHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(GRI)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<GetRegistrationInfoResponse xmlns="urn:ias.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<AccountId>%s</AccountId>
custom := fmt.Sprintf(`<AccountId>%s</AccountId>
<DeviceToken>00000000</DeviceToken>
<DeviceTokenExpired>false</DeviceTokenExpired>
<Country>%s</Country>
<ExtAccountId></ExtAccountId>
<DeviceCode>0000000000000000</DeviceCode>
<DeviceStatus>R</DeviceStatus>
<Currency>POINTS</Currency>
</GetRegistrationInfoResponse>
</soapenv:Body>
</soapenv:Envelope>`, GRI.Version, GRI.DeviceID, GRI.MessageID, timestamp, GRI.AccountID, GRI.Country)
<Currency>POINTS</Currency>`, GRI.AccountID, GRI.Country)
fmt.Fprint(w, formatSuccess("ias", action, GRI.Version, GRI.DeviceID, GRI.MessageID, custom))
case "Register":
fmt.Println("REG.")
@ -121,26 +87,12 @@ func iasHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(REG)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<RegisterResponse xmlns="urn:ias.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
<AccountId>%s</AccountId>
custom := fmt.Sprintf(`<AccountId>%s</AccountId>
<DeviceToken>00000000</DeviceToken>
<Country>%s</Country>
<ExtAccountId></ExtAccountId>
<DeviceCode>00000000</DeviceCode>
</RegisterResponse>
</soapenv:Body>
</soapenv:Envelope>`, REG.Version, REG.DeviceID, REG.MessageID, timestamp, REG.AccountID, REG.Country)
<DeviceCode>00000000</DeviceCode>`, REG.AccountID, REG.Country)
fmt.Fprint(w, formatSuccess("ias", action, REG.Version, REG.DeviceID, REG.MessageID, custom))
case "Unregister":
fmt.Println("UNR.")
@ -153,22 +105,10 @@ func iasHandler(w http.ResponseWriter, r *http.Request) {
}
fmt.Println(UNR)
fmt.Println("The request is valid! Responding...")
fmt.Fprintf(w, `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<UnregisterResponse xmlns="urn:ias.wsapi.broadon.com">
<Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>0</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>
</UnregisterResponse>
</soapenv:Body>
</soapenv:Envelope>`, UNR.Version, UNR.DeviceID, UNR.MessageID, timestamp)
fmt.Fprint(w, formatSuccess("ias", action, UNR.Version, UNR.DeviceID, UNR.MessageID, ""))
default:
fmt.Fprintf(w, "WiiSOAP can't handle this. Try again later or actually use a Wii instead of a computer.")
fmt.Fprint(w, "WiiSOAP can't handle this. Try again later or actually use a Wii instead of a computer.")
return
}

23
main.go
View File

@ -29,10 +29,25 @@ import (
)
const (
// Header is a generic XML header suitable for use with the output of Marshal.
// This is not automatically added to any output of this package,
// it is provided as a convenience.
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
// Header is the base format of a SOAP response with string substitutions available.
// All XML constants must be treated as temporary until a proper XPath solution is investigated.
Header = `<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<%sResponse xmlns="%s">` + "\n"
// Template describes common fields across all requests, for easy replication.
Template = ` <Version>%s</Version>
<DeviceId>%s</DeviceId>
<MessageId>%s</MessageId>
<TimeStamp>%s</TimeStamp>
<ErrorCode>%d</ErrorCode>
<ServiceStandbyMode>false</ServiceStandbyMode>` + "\n"
// Footer is the base format of a closing envelope in SOAP.
Footer = ` </%sResponse>
</soapenv:Body>
</soapenv:Envelope>`
)
// checkError makes error handling not as ugly and inefficient.

View File

@ -17,13 +17,19 @@
package main
import "strings"
import (
"fmt"
"strconv"
"strings"
"time"
)
// namespaceForType returns the expected XML namespace format for a service.
func namespaceForType(service string) string {
return "urn:" + service + ".wsapi.broadon.com"
}
// Expected contents are along the lines of "urn:ecs.wsapi.broadon.com/CheckDeviceStatus"
// parseAction interprets contents along the lines of "urn:ecs.wsapi.broadon.com/CheckDeviceStatus".
func parseAction(original string, service string) string {
prefix := namespaceForType(service) + "/"
stripped := strings.Replace(original, prefix, "", 1)
@ -35,3 +41,42 @@ func parseAction(original string, service string) string {
return stripped
}
}
// formatHeader formats a response type and the proper service.
func formatHeader(responseType string, service string) string {
return fmt.Sprintf(Header, responseType, namespaceForType(service))
}
// formatTemplate inserts common, cross-requests values into every request.
func formatTemplate(version string, deviceId string, messageId string, errorCode int) string {
// Get a sexy new timestamp to use.
timestampNano := strconv.FormatInt(time.Now().UTC().Unix(), 10)
timestamp := timestampNano + "000"
return fmt.Sprintf(Template, version, deviceId, messageId, timestamp, errorCode)
}
// formatFooter formats the closing tags of any SOAP request per previous response type.
func formatFooter(responseType string) string {
return fmt.Sprintf(Footer, responseType)
}
// formatForNamespace mangles together several variables throughout a SOAP request.
func formatForNamespace(service string, responseType string, version string, deviceId string, messageId string, errorCode int, extraContents string) string {
return fmt.Sprintf("%s%s%s%s",
formatHeader(responseType, service),
formatTemplate(version, deviceId, messageId, errorCode),
"\t\t"+extraContents,
formatFooter(responseType),
)
}
// formatSuccess returns a standard SOAP response with a positive error code, and additional contents.
func formatSuccess(service string, responseType string, version string, deviceId string, messageId string, extraContents string) string {
return formatForNamespace(service, responseType, version, deviceId, messageId, 0, extraContents)
}
// formatError returns a standard SOAP response with an error code.
func formatError(service string, responseType string, version string, deviceId string, messageId string, errorCode int) string {
return formatForNamespace(service, responseType, version, deviceId, messageId, errorCode, "")
}