diff --git a/main.go b/main.go index 3528069..2d65d10 100644 --- a/main.go +++ b/main.go @@ -21,13 +21,14 @@ import ( "database/sql" "encoding/xml" "fmt" - _ "github.com/go-sql-driver/mysql" "io/ioutil" "log" "net/http" "os" "strconv" "time" + + _ "github.com/go-sql-driver/mysql" ) const ( @@ -40,15 +41,13 @@ const ( // CheckError makes error handling not as ugly and inefficient. func CheckError(e error) { if e != nil { - log.Fatal("WiiSOAP forgot how to drive and crashed! Reason: ", e) + log.Fatal("WiiSOAP forgot how to drive and suddenly crashed! Reason: ", e) } } func main() { - // Initial Start. - fmt.Println("WiiSOAP 0.2.4 Kawauso") - fmt.Println("Reading the Config...") + fmt.Println("WiiSOAP 0.2.4 Kawauso\nReading the Config...") // Check the Config. configfile, err := os.Open("./config.xml") @@ -63,8 +62,7 @@ func main() { fmt.Println("Initializing core...") // Start SQL. - db, err := sql.Open("mysql", - CON.SQLUser+":"+CON.SQLPass+"@tcp("+CON.SQLPort+")/"+CON.SQLDB) + db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s)/%s)", CON.SQLUser, CON.SQLPass, CON.SQLPort, CON.SQLDB)) CheckError(err) defer db.Close() @@ -72,8 +70,7 @@ func main() { CheckError(err) // Start the HTTP server. - fmt.Println("Starting HTTP connection (" + CON.Port + ")...") - fmt.Println("Not using the usual port for HTTP? Be sure to use a proxy, otherwise the Wii can't connect!") + fmt.Printf("Starting HTTP connection (%s)...\nNot using the usual port for HTTP? Be sure to use a proxy, otherwise the Wii can't connect!", CON.Port) http.HandleFunc("/", handler) // each request calls handler log.Fatal(http.ListenAndServe(CON.Port, nil)) @@ -81,7 +78,6 @@ func main() { } func handler(w http.ResponseWriter, r *http.Request) { - // Get a sexy new timestamp to use. timestampnano := strconv.FormatInt(time.Now().UTC().Unix(), 10) timestamp := timestampnano + "000" @@ -89,8 +85,7 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Println("-=Incoming request!=-") body, err := ioutil.ReadAll(r.Body) if err != nil { - http.Error(w, "Error reading request body...", - http.StatusInternalServerError) + http.Error(w, "Error reading request body...", http.StatusInternalServerError) } // The switch converts the HTTP Body of the request into a string. There is no need to convert the cases to byte format. @@ -99,48 +94,43 @@ func handler(w http.ResponseWriter, r *http.Request) { // TODO: Update the responses so that they query the SQL Database for the proper information (e.g. Device Code, Token, etc). case "CheckDeviceStatus": - fmt.Println("CDS.") CDS := CDS{} - err = xml.Unmarshal([]byte(body), &CDS) - if err != nil { + if err = xml.Unmarshal(body, &CDS); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "You need to POST some SOAP from WSC if you wanna get some, honey. ;)") return } fmt.Println(CDS) fmt.Println("The request is valid! Responding...") - fmt.Fprintf(w, ` - + fmt.Fprintf(w, ` - - - `+CDS.Version+` - `+CDS.DeviceId+` - `+CDS.MessageId+` - `+timestamp+` - 0 - false - - 2018 - POINTS - - 0 - `+timestamp+` - `+timestamp+` - - -`) + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + + %s + %s + %s + %s + 0 + false + + 2018 + POINTS + + 0 + %s + %s + + +`, CDS.Version, CDS.DeviceID, CDS.MessageID, timestamp, timestamp, timestamp) fmt.Println("Delivered response!") case "NotifiedETicketsSynced": - fmt.Println("NETS") NETS := NETS{} - err = xml.Unmarshal([]byte(body), &NETS) - if err != nil { + if err = xml.Unmarshal(body, &NETS); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "This is a disgusting request, but 20 dollars is 20 dollars. ;)") fmt.Printf("error: %v", err) @@ -150,27 +140,25 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Println("The request is valid! Responding...") fmt.Fprintf(w, ` - - - `+NETS.Version+` - `+NETS.DeviceId+` - `+NETS.MessageId+` - `+timestamp+` - 0 - false - - -`) + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + + %s + %s + %s + %s + 0 + false + + +`, NETS.Version, NETS.DeviceID, NETS.MessageID, timestamp) fmt.Println("Delivered response!") case "ListETickets": - fmt.Println("LET") LET := LET{} - err = xml.Unmarshal([]byte(body), &LET) - if err != nil { + if err = xml.Unmarshal(body, &LET); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "This is a disgusting request, but 20 dollars is 20 dollars. ;)") fmt.Printf("error: %v", err) @@ -179,183 +167,173 @@ func handler(w http.ResponseWriter, r *http.Request) { fmt.Println(LET) fmt.Println("The request is valid! Responding...") fmt.Fprintf(w, ` - - - - `+LET.Version+` - `+LET.DeviceId+` - `+LET.MessageId+` - `+timestamp+` - 0 - false - 0 - `+timestamp+` - `+timestamp+` - - -`) + + + + %s + %s + %s + %s + 0 + false + 0 + %s + %s + + +`, LET.Version, LET.DeviceID, LET.MessageID, timestamp, timestamp, timestamp) fmt.Println("Delivered response!") case "PurchaseTitle": - fmt.Println("PT") PT := PT{} - err = xml.Unmarshal([]byte(body), &PT) - if err != nil { + if err = xml.Unmarshal(body, &PT); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "if you wanna fun time, its gonna cost ya extra sweetie. ;)") - fmt.Printf("error: %v", err) + fmt.Printf("Error: %s", err.Error()) return } fmt.Println(PT) fmt.Println("The request is valid! Responding...") fmt.Fprintf(w, ` - - - `+PT.Version+` - `+PT.DeviceId+` - `+PT.MessageId+` - `+timestamp+` - 0 - false - - 2018 - POINTS - - - 00000000 - `+timestamp+` - PURCHGAME - - `+timestamp+` - 00000000 - 00000000 - 00000000 - 00000000 - - -`) + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + + %s + %s + %s + %s + 0 + false + + 2018 + POINTS + + + 00000000 + %s + PURCHGAME + + %s + 00000000 + 00000000 + 00000000 + 00000000 + + +`, PT.Version, PT.DeviceID, PT.MessageID, timestamp, timestamp, timestamp) fmt.Println("Delivered response!") case "CheckRegistration": - fmt.Println("CR.") CR := CR{} - err = xml.Unmarshal([]byte(body), &CR) - if err != nil { + if err = xml.Unmarshal(body, &CR); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "not good enough for me. ;)") - fmt.Printf("error: %v", err) + fmt.Printf("Error: %s", err.Error()) return } fmt.Println(CR) fmt.Println("The request is valid! Responding...") fmt.Fprintf(w, ` + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> - `+CR.Version+` - `+CR.DeviceId+` - `+CR.DeviceId+` - `+timestamp+` + %s + %s + %s + %s 0 false - `+CR.SerialNo+` + %s R -`) +`, CR.Version, CR.DeviceID, CR.DeviceID, timestamp, CR.SerialNo) fmt.Println("Delivered response!") case "GetRegistrationInfo": - fmt.Println("GRI.") GRI := GRI{} - err = xml.Unmarshal([]byte(body), &GRI) - if err != nil { + if err = xml.Unmarshal(body, &GRI); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "how dirty. ;)") - fmt.Printf("error: %v", err) + fmt.Printf("Error: %s", err.Error()) return } fmt.Println(GRI) fmt.Println("The request is valid! Responding...") fmt.Fprintf(w, ` - - - `+GRI.Version+` - `+GRI.DeviceId+` - `+GRI.MessageId+` - `+timestamp+` - 0 - false - `+GRI.AccountId+` - 00000000 - false - `+GRI.Country+` - - 0000000000000000 - R - POINTS - - -`) + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> + + + %s + %s + %s + %s + 0 + false + %s + 00000000 + false + %s + + 0000000000000000 + R + POINTS + + +`, GRI.Version, GRI.DeviceID, GRI.MessageID, timestamp, GRI.AccountID, GRI.Country) fmt.Println("Delivered response!") case "Register": - fmt.Println("REG.") REG := REG{} - err = xml.Unmarshal([]byte(body), ®) - if err != nil { + if err = xml.Unmarshal(body, ®); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "disgustingly invalid. ;)") - fmt.Printf("error: %v", err) + fmt.Printf("Error: %s", err.Error()) return } fmt.Println(REG) fmt.Println("The request is valid! Responding...") fmt.Fprintf(w, ` + xmlns:xsd="http://www.w3.org/2001/XMLSchema" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> - `+REG.Version+` - `+REG.DeviceId+` - `+REG.MessageId+` - `+timestamp+` + %s + %s + %s + %s 0 false - `+REG.AccountId+` + %s 00000000 - `+REG.Country+` + %s 00000000 -`) +`, REG.Version, REG.DeviceID, REG.MessageID, timestamp, REG.AccountID, REG.Country) fmt.Println("Delivered response!") case "Unregister": - fmt.Println("UNR.") UNR := UNR{} - err = xml.Unmarshal([]byte(body), &UNR) - if err != nil { + if err = xml.Unmarshal(body, &UNR); os.IsExist(err) { fmt.Println("...or not. Bad or incomplete request. (End processing.)") fmt.Fprint(w, "how abnormal... ;)") - fmt.Printf("error: %v", err) + fmt.Printf("Error: %s", err.Error()) return } fmt.Println(UNR) @@ -364,20 +342,19 @@ func handler(w http.ResponseWriter, r *http.Request) { - `+UNR.Version+` - `+UNR.DeviceId+` - `+UNR.MessageId+` - `+timestamp+` + %s + %s + %s + %s 0 false -`) +`, UNR.Version, UNR.DeviceID, UNR.MessageID, timestamp) fmt.Println("Delivered response!") default: fmt.Fprintf(w, "WiiSOAP can't handle this. Try again later or actually use a Wii instead of a computer.") - } // TODO: Add NUS and CAS SOAP to the case list.