Skip to content

Commit

Permalink
Revert "Switch to fasthttp"
Browse files Browse the repository at this point in the history
This reverts commit 461d6f4.
  • Loading branch information
Xpl0itU committed Apr 8, 2024
1 parent f054096 commit 28581f7
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 150 deletions.
11 changes: 5 additions & 6 deletions certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"path"

"github.com/valyala/fasthttp"
)

var cetkData []byte
Expand All @@ -29,12 +28,12 @@ func getCert(tmdData []byte, id int, numContents uint16) ([]byte, error) {
}
}

func getDefaultCert(cancelCtx context.Context, progressReporter ProgressReporter, client *fasthttp.Client) ([]byte, error) {
func getDefaultCert(cancelCtx context.Context, progressReporter ProgressReporter, client *http.Client, buffer []byte) ([]byte, error) {
if len(cetkData) >= 0x350+0x300 {
return cetkData[0x350 : 0x350+0x300], nil
}
cetkDir := path.Join(os.TempDir(), "cetk")
if err := downloadFile(cancelCtx, progressReporter, client, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/000500101000400a/cetk", cetkDir, true); err != nil {
if err := downloadFile(cancelCtx, progressReporter, client, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/000500101000400a/cetk", cetkDir, true, buffer); err != nil {
return nil, err
}
cetkData, err := os.ReadFile(cetkDir)
Expand All @@ -52,7 +51,7 @@ func getDefaultCert(cancelCtx context.Context, progressReporter ProgressReporter
return nil, fmt.Errorf("failed to download OSv10 cetk, length: %d", len(cetkData))
}

func GenerateCert(tmdData []byte, contentCount uint16, progressReporter ProgressReporter, client *fasthttp.Client, cancelCtx context.Context) (bytes.Buffer, error) {
func GenerateCert(tmdData []byte, contentCount uint16, progressReporter ProgressReporter, client *http.Client, cancelCtx context.Context, buffer []byte) (bytes.Buffer, error) {
cert := bytes.Buffer{}

cert0, err := getCert(tmdData, 0, contentCount)
Expand All @@ -67,7 +66,7 @@ func GenerateCert(tmdData []byte, contentCount uint16, progressReporter Progress
}
cert.Write(cert1)

defaultCert, err := getDefaultCert(cancelCtx, progressReporter, client)
defaultCert, err := getDefaultCert(cancelCtx, progressReporter, client, buffer)
if err != nil {
return bytes.Buffer{}, err
}
Expand Down
27 changes: 9 additions & 18 deletions cmd/WiiUDownloader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
Expand All @@ -10,8 +11,6 @@ import (
wiiudownloader "github.com/Xpl0itU/WiiUDownloader"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"

"github.com/valyala/fasthttp"
)

func main() {
Expand Down Expand Up @@ -43,22 +42,14 @@ func main() {
logger.Fatal(err.Error())
}

client := &fasthttp.Client{
MaxConnsPerHost: 1024,
MaxIdleConnDuration: 30 * time.Second,
TLSConfig: nil,
ReadBufferSize: wiiudownloader.BUFFER_SIZE,
WriteBufferSize: wiiudownloader.BUFFER_SIZE,
MaxConnWaitTimeout: 30 * time.Second,
StreamResponseBody: true,
ConnPoolStrategy: fasthttp.LIFO,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Name: "WiiUDownloader",
DialDualStack: true,
RetryIf: func(request *fasthttp.Request) bool {
return true
},
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100

client := &http.Client{
Timeout: time.Duration(30) * time.Second,
Transport: t,
}

app.Connect("activate", func() {
Expand Down
8 changes: 4 additions & 4 deletions cmd/WiiUDownloader/mainwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/binary"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
Expand All @@ -14,7 +15,6 @@ import (
"github.com/Xpl0itU/dialog"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
"github.com/valyala/fasthttp"
"golang.org/x/sync/errgroup"
)

Expand All @@ -40,10 +40,10 @@ type MainWindow struct {
titles []wiiudownloader.TitleEntry
decryptContents bool
currentRegion uint8
client *fasthttp.Client
client *http.Client
}

func NewMainWindow(app *gtk.Application, entries []wiiudownloader.TitleEntry, logger *wiiudownloader.Logger, client *fasthttp.Client) *MainWindow {
func NewMainWindow(app *gtk.Application, entries []wiiudownloader.TitleEntry, logger *wiiudownloader.Logger, client *http.Client) *MainWindow {
gSettings, err := gtk.SettingsGetDefault()
if err != nil {
logger.Error(err.Error())
Expand Down Expand Up @@ -265,7 +265,7 @@ func (mw *MainWindow) ShowAll() {

wiiudownloader.GenerateTicket(filepath.Join(parentDir, "title.tik"), titleID, titleKey, titleVersion)

cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, mw.progressWindow, mw.client, context.Background())
cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, mw.progressWindow, http.DefaultClient, context.Background(), make([]byte, 0))
if err != nil {
return
}
Expand Down
130 changes: 58 additions & 72 deletions downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"encoding/binary"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/valyala/fasthttp"
)

const (
Expand Down Expand Up @@ -39,7 +38,7 @@ func calculateDownloadSpeed(downloaded int64, startTime, endTime time.Time) int6
return 0
}

func downloadFile(ctx context.Context, progressReporter ProgressReporter, client *fasthttp.Client, downloadURL, dstPath string, doRetries bool) error {
func downloadFile(ctx context.Context, progressReporter ProgressReporter, client *http.Client, downloadURL, dstPath string, doRetries bool, buffer []byte) error {
filePath := filepath.Base(dstPath)

startTime := time.Now()
Expand All @@ -58,104 +57,89 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client

for attempt := 1; attempt <= maxRetries; attempt++ {
isError = false
req := fasthttp.AcquireRequest()
req, err := http.NewRequestWithContext(ctx, "GET", downloadURL, nil)
if err != nil {
return err
}

req.SetRequestURI(downloadURL)
req.Header.SetMethod("GET")
req.Header.Set("Accept", "*/*")
req.Header.Set("User-Agent", "WiiUDownloader")
req.Header.Set("Connection", "Keep-Alive")
req.Header.Set("Accept-Encoding", "*")

resp := fasthttp.AcquireResponse()
resp.StreamBody = true
resp.ImmediateHeaderFlush = true

if err := client.Do(req, resp); err != nil {
req.CloseBodyStream()
resp.CloseBodyStream()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
continue
}
return fmt.Errorf("inital request error after %d attempts, status code: %s", attempt, err.Error())
resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode() != fasthttp.StatusOK {
req.CloseBodyStream()
resp.CloseBodyStream()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if resp.StatusCode != http.StatusOK {
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
continue
}
return fmt.Errorf("download error after %d attempts, status code: %d", attempt, resp.StatusCode())
resp.Body.Close()
return fmt.Errorf("download error after %d attempts, status code: %d", attempt, resp.StatusCode)
}

file, err := os.Create(dstPath)
if err != nil {
req.CloseBodyStream()
resp.CloseBodyStream()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
resp.Body.Close()
return err
}

var downloaded int64

go updateProgress(&downloaded)

customBufferedWriter, err := NewFileWriterWithProgress(file, &downloaded, progressReporter)
if err != nil {
req.CloseBodyStream()
resp.CloseBodyStream()
file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return err
}

select {
case <-ctx.Done():
req.CloseBodyStream()
resp.CloseBodyStream()
file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return ctx.Err()
default:
err := resp.BodyWriteTo(customBufferedWriter)
if err != nil && err != io.EOF {
req.CloseBodyStream()
resp.CloseBodyStream()
Loop:
for {
select {
case <-ctx.Done():
resp.Body.Close()
file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
isError = true
break
return ctx.Err()
default:
n, err := resp.Body.Read(buffer)
if err != nil && err != io.EOF {
resp.Body.Close()
file.Close()
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
isError = true
break Loop
}
return fmt.Errorf("download error after %d attempts: %+v", attempt, err)
}
return fmt.Errorf("download error after %d attempts: %+v", attempt, err)

if n == 0 {
resp.Body.Close()
file.Close()
break Loop
}

_, err = file.Write(buffer[:n])
if err != nil {
resp.Body.Close()
file.Close()
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
isError = true
break Loop
}
return fmt.Errorf("write error after %d attempts: %+v", attempt, err)
}

downloaded += int64(n)
}
}
if !isError {
req.CloseBodyStream()
resp.CloseBodyStream()
file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
break
}
}

return nil
}

func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger, client *fasthttp.Client) error {
func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger, client *http.Client) error {
tEntry := getTitleEntryFromTid(titleID)

progressReporter.SetTotalDownloaded(0)
Expand All @@ -168,8 +152,10 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
return err
}

buffer := make([]byte, BUFFER_SIZE)

tmdPath := filepath.Join(outputDir, "title.tmd")
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "tmd"), tmdPath, true); err != nil {
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "tmd"), tmdPath, true, buffer); err != nil {
if progressReporter.Cancelled() {
return nil
}
Expand All @@ -187,7 +173,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
}

tikPath := filepath.Join(outputDir, "title.tik")
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "cetk"), tikPath, false); err != nil {
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "cetk"), tikPath, false, buffer); err != nil {
if progressReporter.Cancelled() {
return nil
}
Expand Down Expand Up @@ -221,7 +207,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d

progressReporter.SetDownloadSize(int64(titleSize))

cert, err := GenerateCert(tmdData, contentCount, progressReporter, client, cancelCtx)
cert, err := GenerateCert(tmdData, contentCount, progressReporter, client, cancelCtx, buffer)
if err != nil {
if progressReporter.Cancelled() {
return nil
Expand Down Expand Up @@ -250,7 +236,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
return err
}
filePath := filepath.Join(outputDir, fmt.Sprintf("%08X.app", content.ID))
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%08X", baseURL, content.ID), filePath, true); err != nil {
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%08X", baseURL, content.ID), filePath, true, buffer); err != nil {
if progressReporter.Cancelled() {
break
}
Expand All @@ -260,7 +246,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d

if tmdData[offset+7]&0x2 == 2 {
filePath = filepath.Join(outputDir, fmt.Sprintf("%08X.h3", content.ID))
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%08X.h3", baseURL, content.ID), filePath, true); err != nil {
if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%08X.h3", baseURL, content.ID), filePath, true, buffer); err != nil {
if progressReporter.Cancelled() {
break
}
Expand Down
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@ require (
golang.org/x/crypto v0.21.0
)

require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/klauspost/compress v1.17.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
)

require (
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect
github.com/valyala/fasthttp v1.52.0
golang.org/x/sync v0.6.0
)
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d h1:2xp1BQbqcDDaik
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d/go.mod h1:peYoMncQljjNS6tZwI9WVyQB3qZS6u79/N3mBOcnd3I=
github.com/Xpl0itU/dialog v0.0.0-20230805114139-ec888310aded h1:GkBw5aNvID1+SKAD3xC5fU4EwMgOmkrvICy5NX3Rqvw=
github.com/Xpl0itU/dialog v0.0.0-20230805114139-ec888310aded/go.mod h1:Yl652wzqaetwEMJ8FnDRKBK1+CisE+PU5BGJXItbYFg=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gotk3/gotk3 v0.6.3 h1:+Ke4WkM1TQUNOlM2TZH6szqknqo+zNbX3BZWVXjSHYw=
github.com/gotk3/gotk3 v0.6.3/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI=
github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
Expand Down
Loading

0 comments on commit 28581f7

Please sign in to comment.