Revert "Switch to fasthttp"

This reverts commit 461d6f4a84.
This commit is contained in:
Xpl0itU 2024-04-08 14:35:20 +02:00
parent f054096e64
commit 28581f79a0
7 changed files with 76 additions and 150 deletions

View file

@ -4,10 +4,9 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"net/http"
"os" "os"
"path" "path"
"github.com/valyala/fasthttp"
) )
var cetkData []byte var cetkData []byte
@ -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 { if len(cetkData) >= 0x350+0x300 {
return cetkData[0x350 : 0x350+0x300], nil return cetkData[0x350 : 0x350+0x300], nil
} }
cetkDir := path.Join(os.TempDir(), "cetk") 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 return nil, err
} }
cetkData, err := os.ReadFile(cetkDir) cetkData, err := os.ReadFile(cetkDir)
@ -52,7 +51,7 @@ func getDefaultCert(cancelCtx context.Context, progressReporter ProgressReporter
return nil, fmt.Errorf("failed to download OSv10 cetk, length: %d", len(cetkData)) 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{} cert := bytes.Buffer{}
cert0, err := getCert(tmdData, 0, contentCount) cert0, err := getCert(tmdData, 0, contentCount)
@ -67,7 +66,7 @@ func GenerateCert(tmdData []byte, contentCount uint16, progressReporter Progress
} }
cert.Write(cert1) cert.Write(cert1)
defaultCert, err := getDefaultCert(cancelCtx, progressReporter, client) defaultCert, err := getDefaultCert(cancelCtx, progressReporter, client, buffer)
if err != nil { if err != nil {
return bytes.Buffer{}, err return bytes.Buffer{}, err
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -10,8 +11,6 @@ import (
wiiudownloader "github.com/Xpl0itU/WiiUDownloader" wiiudownloader "github.com/Xpl0itU/WiiUDownloader"
"github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gtk"
"github.com/valyala/fasthttp"
) )
func main() { func main() {
@ -43,22 +42,14 @@ func main() {
logger.Fatal(err.Error()) logger.Fatal(err.Error())
} }
client := &fasthttp.Client{ t := http.DefaultTransport.(*http.Transport).Clone()
MaxConnsPerHost: 1024, t.MaxIdleConns = 100
MaxIdleConnDuration: 30 * time.Second, t.MaxConnsPerHost = 100
TLSConfig: nil, t.MaxIdleConnsPerHost = 100
ReadBufferSize: wiiudownloader.BUFFER_SIZE,
WriteBufferSize: wiiudownloader.BUFFER_SIZE, client := &http.Client{
MaxConnWaitTimeout: 30 * time.Second, Timeout: time.Duration(30) * time.Second,
StreamResponseBody: true, Transport: t,
ConnPoolStrategy: fasthttp.LIFO,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Name: "WiiUDownloader",
DialDualStack: true,
RetryIf: func(request *fasthttp.Request) bool {
return true
},
} }
app.Connect("activate", func() { app.Connect("activate", func() {

View file

@ -5,6 +5,7 @@ import (
"context" "context"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
@ -14,7 +15,6 @@ import (
"github.com/Xpl0itU/dialog" "github.com/Xpl0itU/dialog"
"github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gtk"
"github.com/valyala/fasthttp"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
@ -40,10 +40,10 @@ type MainWindow struct {
titles []wiiudownloader.TitleEntry titles []wiiudownloader.TitleEntry
decryptContents bool decryptContents bool
currentRegion uint8 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() gSettings, err := gtk.SettingsGetDefault()
if err != nil { if err != nil {
logger.Error(err.Error()) logger.Error(err.Error())
@ -265,7 +265,7 @@ func (mw *MainWindow) ShowAll() {
wiiudownloader.GenerateTicket(filepath.Join(parentDir, "title.tik"), titleID, titleKey, titleVersion) 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 { if err != nil {
return return
} }

View file

@ -6,12 +6,11 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/valyala/fasthttp"
) )
const ( const (
@ -39,7 +38,7 @@ func calculateDownloadSpeed(downloaded int64, startTime, endTime time.Time) int6
return 0 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) filePath := filepath.Base(dstPath)
startTime := time.Now() startTime := time.Now()
@ -58,49 +57,32 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
for attempt := 1; attempt <= maxRetries; attempt++ { for attempt := 1; attempt <= maxRetries; attempt++ {
isError = false 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("User-Agent", "WiiUDownloader")
req.Header.Set("Connection", "Keep-Alive") req.Header.Set("Connection", "Keep-Alive")
req.Header.Set("Accept-Encoding", "*") req.Header.Set("Accept-Encoding", "*")
resp := fasthttp.AcquireResponse() resp, err := client.Do(req)
resp.StreamBody = true if err != nil {
resp.ImmediateHeaderFlush = true return err
}
if err := client.Do(req, resp); err != nil { if resp.StatusCode != http.StatusOK {
req.CloseBodyStream()
resp.CloseBodyStream()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if doRetries && attempt < maxRetries { if doRetries && attempt < maxRetries {
time.Sleep(retryDelay) time.Sleep(retryDelay)
continue continue
} }
return fmt.Errorf("inital request error after %d attempts, status code: %s", attempt, err.Error()) resp.Body.Close()
} return fmt.Errorf("download error after %d attempts, status code: %d", attempt, resp.StatusCode)
if resp.StatusCode() != fasthttp.StatusOK {
req.CloseBodyStream()
resp.CloseBodyStream()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
continue
}
return fmt.Errorf("download error after %d attempts, status code: %d", attempt, resp.StatusCode())
} }
file, err := os.Create(dstPath) file, err := os.Create(dstPath)
if err != nil { if err != nil {
req.CloseBodyStream() resp.Body.Close()
resp.CloseBodyStream()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return err return err
} }
@ -108,46 +90,48 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
go updateProgress(&downloaded) go updateProgress(&downloaded)
customBufferedWriter, err := NewFileWriterWithProgress(file, &downloaded, progressReporter) Loop:
if err != nil { for {
req.CloseBodyStream()
resp.CloseBodyStream()
file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return err
}
select { select {
case <-ctx.Done(): case <-ctx.Done():
req.CloseBodyStream() resp.Body.Close()
resp.CloseBodyStream()
file.Close() file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
return ctx.Err() return ctx.Err()
default: default:
err := resp.BodyWriteTo(customBufferedWriter) n, err := resp.Body.Read(buffer)
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
req.CloseBodyStream() resp.Body.Close()
resp.CloseBodyStream()
file.Close() file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
if doRetries && attempt < maxRetries { if doRetries && attempt < maxRetries {
time.Sleep(retryDelay) time.Sleep(retryDelay)
isError = true isError = true
break 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 { if !isError {
req.CloseBodyStream()
resp.CloseBodyStream()
file.Close()
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(resp)
break break
} }
} }
@ -155,7 +139,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client
return nil 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) tEntry := getTitleEntryFromTid(titleID)
progressReporter.SetTotalDownloaded(0) progressReporter.SetTotalDownloaded(0)
@ -168,8 +152,10 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
return err return err
} }
buffer := make([]byte, BUFFER_SIZE)
tmdPath := filepath.Join(outputDir, "title.tmd") 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() { if progressReporter.Cancelled() {
return nil return nil
} }
@ -187,7 +173,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
} }
tikPath := filepath.Join(outputDir, "title.tik") 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() { if progressReporter.Cancelled() {
return nil return nil
} }
@ -221,7 +207,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
progressReporter.SetDownloadSize(int64(titleSize)) 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 err != nil {
if progressReporter.Cancelled() { if progressReporter.Cancelled() {
return nil return nil
@ -250,7 +236,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
return err return err
} }
filePath := filepath.Join(outputDir, fmt.Sprintf("%08X.app", content.ID)) 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() { if progressReporter.Cancelled() {
break break
} }
@ -260,7 +246,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d
if tmdData[offset+7]&0x2 == 2 { if tmdData[offset+7]&0x2 == 2 {
filePath = filepath.Join(outputDir, fmt.Sprintf("%08X.h3", content.ID)) 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() { if progressReporter.Cancelled() {
break break
} }

7
go.mod
View file

@ -9,14 +9,7 @@ require (
golang.org/x/crypto v0.21.0 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 ( require (
github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect
github.com/valyala/fasthttp v1.52.0
golang.org/x/sync v0.6.0 golang.org/x/sync v0.6.0
) )

8
go.sum
View file

@ -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/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 h1:GkBw5aNvID1+SKAD3xC5fU4EwMgOmkrvICy5NX3Rqvw=
github.com/Xpl0itU/dialog v0.0.0-20230805114139-ec888310aded/go.mod h1:Yl652wzqaetwEMJ8FnDRKBK1+CisE+PU5BGJXItbYFg= 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 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= 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 h1:+Ke4WkM1TQUNOlM2TZH6szqknqo+zNbX3BZWVXjSHYw=
github.com/gotk3/gotk3 v0.6.3/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= 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 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=

View file

@ -30,38 +30,3 @@ func doDeleteEncryptedContents(path string) error {
return nil return nil
}) })
} }
type BufferedWriter struct {
file *os.File
downloaded *int64
progressReporter ProgressReporter
buffer []byte
}
func NewFileWriterWithProgress(file *os.File, downloaded *int64, progressReporter ProgressReporter) (*BufferedWriter, error) {
return &BufferedWriter{
file: file,
downloaded: downloaded,
progressReporter: progressReporter,
buffer: make([]byte, BUFFER_SIZE),
}, nil
}
func (bw *BufferedWriter) Write(data []byte) (int, error) {
written := 0
for written < len(data) {
if bw.progressReporter.Cancelled() {
return written, nil
}
remaining := len(data) - written
toWrite := min(BUFFER_SIZE, uint64(remaining))
copy(bw.buffer, data[written:written+int(toWrite)])
n, err := bw.file.Write(bw.buffer[:toWrite])
if err != nil {
return written, err
}
written += n
*bw.downloaded += int64(n)
}
return written, nil
}