Skip to content

Commit

Permalink
feat(trs): new implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Apr 18, 2024
1 parent c494c77 commit 793c9f5
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 408 deletions.
132 changes: 13 additions & 119 deletions cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@ import (
"archive/zip"
"bytes"
_ "embed"
"fmt"
"io"
"net/http"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)

type config struct {
BaseURL string `yaml:"BaseURL"`
Targets []targets `yaml:"Targets"`
}

type targets struct {
Refer string `yaml:"Refer"`
Folder string `yaml:"Folder"`
Copy []string `yaml:"Copy"`
OS string `yaml:"OS"`
Arch string `yaml:"Arch"`
}

//go:embed cfg.zip
var cfgdata []byte

Expand Down Expand Up @@ -51,113 +55,3 @@ func readconfig(path string, usecust bool) (c config, err error) {
}
return
}

type config struct {
BaseURL string `yaml:"BaseURL"`
Targets []targets `yaml:"Targets"`
}

type targets struct {
Refer string `yaml:"Refer"`
Folder string `yaml:"Folder"`
Copy []string `yaml:"Copy"`
OS string `yaml:"OS"`
Arch string `yaml:"Arch"`
}

func (c *config) download(path, prefix string, usecust, force bool) error {
for i, t := range c.Targets {
if t.Refer != "" {
refp := path[:strings.LastIndex(path, "/")+1] + t.Refer
logrus.Infof("#%s%d refer to target '%s'.", prefix, i+1, refp)
refcfg, err := readconfig(refp, usecust)
if err != nil {
return err
}
err = refcfg.download(refp, prefix+strconv.Itoa(i+1)+".", usecust, force)
if err != nil {
return err
}
continue
}
if t.OS != "" && t.OS != runtime.GOOS {
logrus.Warnf("#%s%d target required OS: %s but you are %s, skip.", prefix, i+1, t.OS, runtime.GOOS)
continue
}
if t.Arch != "" && t.Arch != runtime.GOARCH {
logrus.Warnf("#%s%d target required Arch: %s but you are %s, skip.", prefix, i+1, t.Arch, runtime.GOARCH)
continue
}
err := os.MkdirAll(t.Folder, 0755)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("#%s%d make target folder '%s'", prefix, i+1, t.Folder))
}
logrus.Infof("#%s%d open target folder '%s'.", prefix, i+1, t.Folder)
if len(t.Copy) == 0 {
logrus.Warningf("#%s%d empty copy target.", prefix, i+1)
continue
}
wg := sync.WaitGroup{}
wg.Add(len(t.Copy))
logrus.Infof("#%s%d download copy: '%v'.", prefix, i+1, t.Copy)
for j, cp := range t.Copy {
go func(i int, cp, prefix string) {
defer wg.Done()
if strings.Contains(cp, "/") { // have innner folder
infldr := t.Folder + "/" + cp[:strings.LastIndex(cp, "/")]
err := os.MkdirAll(infldr, 0755)
if err != nil {
logrus.Errorf("#%s%d make target inner folder '%s' err: %v", prefix, i+1, t.Folder, err)
return
}
logrus.Infof("#%s%d make target inner folder '%s'.", prefix, i+1, t.Folder)
}
sleep := time.Millisecond * 100 * time.Duration(i)
if sleep > time.Millisecond {
time.Sleep(sleep)
}
fname := t.Folder + "/" + cp
if !force {
if _, err := os.Stat(fname); err == nil || os.IsExist(err) {
logrus.Warnf("#%s%d skip exist file %s", prefix, i+1, fname)
return
}
}
req, err := http.NewRequest("GET", c.BaseURL+"/"+cp, nil)
if err != nil {
logrus.Errorf("#%s%d new request to %s err: %v", prefix, i+1, cp, err)
return
}
logrus.Infof("#%s%d get: %s", prefix, i+1, req.URL)
req.Header.Add("user-agent", ua)
resp, err := cli.Do(req)
if err != nil {
logrus.Errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err := errors.New(fmt.Sprintf("HTTP %d %s", resp.StatusCode, resp.Status))
logrus.Errorf("#%s%d get %s err: %v", prefix, i+1, req.URL, err)
return
}
f, err := os.Create(fname)
if err != nil {
logrus.Errorf("#%s%d create file %s err: %v", prefix, i+1, fname, err)
return
}
defer f.Close()
logrus.Infof("#%s%d writing file %s", prefix, i+1, fname)
pm := newmeter(fmt.Sprintf("#%s%d", prefix, i+1), fname, int(resp.ContentLength))
_, err = io.Copy(io.MultiWriter(f, &pm), resp.Body)
if err != nil {
logrus.Errorf("#%s%d download file %s err: %v", prefix, i+1, fname, err)
return
}
logrus.Infof("#%s%d finished download %s", prefix, i+1, fname)
}(j, cp, fmt.Sprintf("%s%d.", prefix, i+1))
}
wg.Wait()
}
return nil
}
163 changes: 0 additions & 163 deletions dns.go

This file was deleted.

12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ require (
)

require (
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e
github.com/fumiama/terasu v0.0.0-20240416061047-62d3c9f6be80
golang.org/x/net v0.24.0
github.com/fumiama/terasu v0.0.0-20240418151245-719e0c16831b
golang.org/x/sys v0.19.0
)

require golang.org/x/text v0.14.0 // indirect
require (
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 // indirect
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1 h1:g4pTnDJUW4VbJ9NvoRfUvdjDrHz/6QhfN/LoIIpICbo=
github.com/FloatTech/ttl v0.0.0-20230307105452-d6f7b2b647d1/go.mod h1:fHZFWGquNXuHttu9dUYoKuNbm3dzLETnIOnm1muSfDs=
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e h1:wR3MXQ3VbUlPKOOUwLOYgh/QaJThBTYtsl673O3lqSA=
github.com/RomiChan/syncx v0.0.0-20221202055724-5f842c53020e/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7 h1:S/ferNiehVjNaBMNNBxUjLtVmP/YWD6Yh79RfPv4ehU=
github.com/RomiChan/syncx v0.0.0-20240418144900-b7402ffdebc7/go.mod h1:vD7Ra3Q9onRtojoY5sMCLQ7JBgjUsrXDnDKyFxqpf9w=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fumiama/terasu v0.0.0-20240416061047-62d3c9f6be80 h1:O1JJZzcd5ggUw/9X8V9KxBZ9JZGWFmX/r1q2TPg+pZQ=
github.com/fumiama/terasu v0.0.0-20240416061047-62d3c9f6be80/go.mod h1:BFl0X1+rGJf8bLHl/kO+v05ryHrj/R4kyCrK89NvegA=
github.com/fumiama/terasu v0.0.0-20240418151245-719e0c16831b h1:j6DMJg+jd4HPmhQtVwtiHBM1y9XskJgWhskUvWuhFuY=
github.com/fumiama/terasu v0.0.0-20240418151245-719e0c16831b/go.mod h1:afchyfKAb7J/zvaENtYzjIEPVbwiEjJaow05zzT4usM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Loading

0 comments on commit 793c9f5

Please sign in to comment.