2024-04-18 01:52:23 +09:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-04-20 18:12:41 +09:00
|
|
|
"context"
|
2024-04-18 01:52:23 +09:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2024-04-20 21:09:53 +09:00
|
|
|
"sync"
|
2024-04-18 01:52:23 +09:00
|
|
|
"time"
|
|
|
|
|
2024-04-19 00:29:30 +09:00
|
|
|
"github.com/fumiama/terasu/dns"
|
|
|
|
"github.com/fumiama/terasu/ip"
|
2024-04-20 17:44:18 +09:00
|
|
|
ui "github.com/gizak/termui/v3"
|
2024-04-18 01:52:23 +09:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
|
|
|
|
_ "rvcmd/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate ./pckcfg.sh assets packs tools
|
|
|
|
|
|
|
|
const ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0"
|
|
|
|
|
2024-04-20 17:44:18 +09:00
|
|
|
var (
|
|
|
|
notui = false
|
|
|
|
sc screen
|
|
|
|
)
|
|
|
|
|
2024-04-18 01:52:23 +09:00
|
|
|
func main() {
|
|
|
|
ntrs := flag.Bool("notrs", false, "use standard TLS client")
|
|
|
|
dnsf := flag.String("dns", "", "custom dns.yaml")
|
2024-04-18 18:21:01 +09:00
|
|
|
cust := flag.Bool("c", false, "use custom yaml instruction")
|
2024-04-18 18:42:23 +09:00
|
|
|
force := flag.Bool("f", false, "force download even file exists")
|
2024-04-19 01:25:10 +09:00
|
|
|
wait := flag.Uint("w", 4, "connection waiting seconds")
|
2024-04-20 17:44:18 +09:00
|
|
|
flag.BoolVar(¬ui, "notui", false, "use plain text instead of TUI")
|
2024-04-18 01:52:23 +09:00
|
|
|
flag.Parse()
|
|
|
|
args := flag.Args()
|
|
|
|
if len(args) != 1 {
|
2024-04-18 02:13:23 +09:00
|
|
|
fmt.Println("Usage:", os.Args[0], "[-notrs] [-dns dns.yaml] 'target/to/download'")
|
2024-04-18 01:52:23 +09:00
|
|
|
flag.PrintDefaults()
|
|
|
|
fmt.Println(" 'target/to/download'\n like packs/general/latest")
|
|
|
|
fmt.Println("All available targets:")
|
|
|
|
fmt.Println(cmdlst.String())
|
|
|
|
return
|
|
|
|
}
|
2024-04-20 17:44:18 +09:00
|
|
|
if notui {
|
|
|
|
logrus.Infoln("RVC Models Downloader start at", time.Now().Local().Format(time.DateTime+" (MST)"))
|
|
|
|
logrus.Infof("operating system: %s, architecture: %s", runtime.GOOS, runtime.GOARCH)
|
|
|
|
logrus.Infoln("is ipv6 available:", ip.IsIPv6Available.Get())
|
|
|
|
} else {
|
|
|
|
if err := ui.Init(); err != nil {
|
|
|
|
logrus.Errorln("failed to initialize termui:", err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
2024-04-20 17:44:18 +09:00
|
|
|
defer ui.Close()
|
|
|
|
sc = newscreen()
|
|
|
|
}
|
2024-04-20 18:12:41 +09:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2024-04-20 21:09:53 +09:00
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
2024-04-20 17:44:18 +09:00
|
|
|
go func() {
|
2024-04-20 21:09:53 +09:00
|
|
|
defer wg.Done()
|
2024-04-20 17:44:18 +09:00
|
|
|
if *dnsf != "" {
|
|
|
|
f, err := os.Open(*dnsf)
|
|
|
|
if err != nil {
|
|
|
|
errorln("open custom dns file", *dnsf, "err:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
m := map[string][]string{}
|
|
|
|
err = yaml.NewDecoder(f).Decode(&m)
|
|
|
|
if err != nil {
|
|
|
|
errorln("decode custom dns file", *dnsf, "err:", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_ = f.Close()
|
|
|
|
if ip.IsIPv6Available.Get() {
|
|
|
|
dns.IPv6Servers.Add(m)
|
|
|
|
} else {
|
|
|
|
dns.IPv4Servers.Add(m)
|
|
|
|
}
|
|
|
|
infoln("custom dns file added")
|
|
|
|
}
|
|
|
|
usercfg, err := readconfig(args[0], *cust)
|
2024-04-18 01:52:23 +09:00
|
|
|
if err != nil {
|
2024-04-20 17:44:18 +09:00
|
|
|
errorln(err)
|
2024-04-18 01:52:23 +09:00
|
|
|
return
|
|
|
|
}
|
2024-04-20 18:12:41 +09:00
|
|
|
ch := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
err := usercfg.download(args[0], "", time.Second*time.Duration(*wait), *cust, !*ntrs, *force)
|
|
|
|
ch <- struct{}{}
|
|
|
|
if err != nil {
|
|
|
|
errorln(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case <-ch:
|
|
|
|
infoln("all download tasks finished.")
|
|
|
|
case <-ctx.Done():
|
|
|
|
logrus.Warnln("download canceled")
|
2024-04-18 01:52:23 +09:00
|
|
|
}
|
2024-04-20 17:44:18 +09:00
|
|
|
}()
|
2024-04-20 21:09:53 +09:00
|
|
|
if notui {
|
|
|
|
wg.Wait()
|
|
|
|
} else {
|
|
|
|
sc.show(time.Second)
|
|
|
|
}
|
2024-04-18 01:52:23 +09:00
|
|
|
}
|