Skip to content

Commit

Permalink
Fix download geo resources
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthecoderpro authored Nov 5, 2023
1 parent 171e428 commit a2f9bc2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/sagernet/gvisor v0.0.0-20230930141345-5fef6f2e17ab
github.com/sagernet/quic-go v0.0.0-20231008035953-32727fef9460
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691
github.com/sagernet/sing v0.2.16-0.20231028125948-afcc9cb766c2
github.com/sagernet/sing v0.2.16-0.20231105072849-64e91d772616
github.com/sagernet/sing-dns v0.1.10
github.com/sagernet/sing-mux v0.1.4-0.20231105044304-ae2745a33479
github.com/sagernet/sing-quic v0.1.3-0.20231026034240-fa3d997246b6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691 h1:5Th31OC6yj8byL
github.com/sagernet/reality v0.0.0-20230406110435-ee17307e7691/go.mod h1:B8lp4WkQ1PwNnrVMM6KyuFR20pU8jYBD+A4EhJovEXU=
github.com/sagernet/sing v0.0.0-20220817130738-ce854cda8522/go.mod h1:QVsS5L/ZA2Q5UhQwLrn0Trw+msNd/NPGEhBKR/ioWiY=
github.com/sagernet/sing v0.1.8/go.mod h1:jt1w2u7lJQFFSGLiRrRIs5YWmx4kAPfWuOejuDW9qMk=
github.com/sagernet/sing v0.2.16-0.20231028125948-afcc9cb766c2 h1:PW18IgRodvppd09d4mewYM3Hedu3PtFERN8yOqkTVk0=
github.com/sagernet/sing v0.2.16-0.20231028125948-afcc9cb766c2/go.mod h1:AhNEHu0GXrpqkuzvTwvC8+j2cQUU/dh+zLEmq4C99pg=
github.com/sagernet/sing v0.2.16-0.20231105072849-64e91d772616 h1:zIm6f7NQR8zhkAaoCIe8sL9Rp3HAkfoF+FGb4XEN3SY=
github.com/sagernet/sing v0.2.16-0.20231105072849-64e91d772616/go.mod h1:AhNEHu0GXrpqkuzvTwvC8+j2cQUU/dh+zLEmq4C99pg=
github.com/sagernet/sing-dns v0.1.10 h1:iIU7nRBlUYj+fF2TaktGIvRiTFFrHwSMedLQsvlTZCI=
github.com/sagernet/sing-dns v0.1.10/go.mod h1:vtUimtf7Nq9EdvD5WTpfCr69KL1M7bcgOVKiYBiAY/c=
github.com/sagernet/sing-mux v0.1.4-0.20231105044304-ae2745a33479 h1:h6ANTA5wbP5BSqbjOT7s1OKLZgdsLqiXO564KQqY2i4=
Expand Down
30 changes: 18 additions & 12 deletions route/router_geo_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,6 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error {
filemanager.MkdirAll(r.ctx, parentDir, 0o755)
}

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
defer saveFile.Close()

httpClient := &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
Expand All @@ -182,7 +176,16 @@ func (r *Router) downloadGeoIPDatabase(savePath string) error {
return err
}
defer response.Body.Close()

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
_, err = io.Copy(saveFile, response.Body)
saveFile.Close()
if err != nil {
filemanager.Remove(r.ctx, savePath)
}
return err
}

Expand All @@ -209,12 +212,6 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
filemanager.MkdirAll(r.ctx, parentDir, 0o755)
}

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
defer saveFile.Close()

httpClient := &http.Client{
Transport: &http.Transport{
ForceAttemptHTTP2: true,
Expand All @@ -234,7 +231,16 @@ func (r *Router) downloadGeositeDatabase(savePath string) error {
return err
}
defer response.Body.Close()

saveFile, err := filemanager.Create(r.ctx, savePath)
if err != nil {
return E.Cause(err, "open output file: ", downloadURL)
}
_, err = io.Copy(saveFile, response.Body)
saveFile.Close()
if err != nil {
filemanager.Remove(r.ctx, savePath)
}
return err
}

Expand Down

0 comments on commit a2f9bc2

Please sign in to comment.