package distro import ( "bufio" "fmt" "net/http" "regexp" "strings" "sync" ) const ( ubuntuMirrorsBaseUrl = "http://mirrors.ubuntu.com" ) type Ubuntu struct{} func (d Ubuntu) GetName() string { return "ubuntu" } func (d Ubuntu) GetRepos() []string { return []string{DefaultRepo} } func (d Ubuntu) FetchMirrors(repos []string, repoMirrors chan<- RepoMirror, done *sync.WaitGroup) { defer done.Done() countryMirrorsRe := regexp.MustCompile(`>([A-Z]{2}\.txt)<`) re := regexp.MustCompile(`^\S*((https?|ftp)://.*)\S*$`) resp, err := http.Get(ubuntuMirrorsBaseUrl) if err != nil { return } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { scanner := bufio.NewScanner(resp.Body) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) m := countryMirrorsRe.FindSubmatch([]byte(line)) if len(m) > 1 { url := fmt.Sprintf("%s/%s", ubuntuMirrorsBaseUrl, string(m[1])) done.Add(1) go func(u string) { defer done.Done() resp, err := http.Get(u) if err != nil { return } defer resp.Body.Close() if resp.StatusCode == http.StatusOK { scanner := bufio.NewScanner(resp.Body) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) m := re.FindSubmatch([]byte(line)) if len(m) > 1 { repoMirrors <- RepoMirror{d.GetName(), DefaultRepo, string(m[1])} } } } }(url) } } } }