2020-03-07 13:37:26 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2020-03-12 17:17:53 +01:00
|
|
|
"sync"
|
2020-03-07 13:37:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
dir := flag.String("dir", "", "directory.")
|
|
|
|
flag.Parse()
|
|
|
|
files, err := ioutil.ReadDir(*dir)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
articles := make([]string, 0)
|
|
|
|
for _, i := range files {
|
|
|
|
abs, err := filepath.Abs(filepath.Join(*dir, i.Name()))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
articles = append(articles, abs)
|
|
|
|
}
|
2020-03-12 17:17:53 +01:00
|
|
|
var wg sync.WaitGroup
|
2020-03-07 13:37:26 +01:00
|
|
|
for _, a := range articles {
|
2020-03-12 17:17:53 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func(a string, wg *sync.WaitGroup) {
|
|
|
|
defer wg.Done()
|
2020-03-07 13:37:26 +01:00
|
|
|
data, err := ioutil.ReadFile(a)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
reIframe := regexp.MustCompile(`(?U)(<iframe\s.+>)`)
|
2020-03-12 17:17:53 +01:00
|
|
|
for _, m := range reIframe.FindAllString(string(data), -1) {
|
|
|
|
reSrc := regexp.MustCompile(`(?U)src="http://(.+)"`)
|
|
|
|
for _, s := range reSrc.FindAllStringSubmatch(m, -1) {
|
|
|
|
httpsUrl := fmt.Sprintf("https://%s\n", s[1])
|
|
|
|
log.Printf("%s => %s", fmt.Sprintf("http://%s", s[1]), httpsUrl)
|
|
|
|
data = reSrc.ReplaceAll(data, []byte(`src="https://$1"`))
|
|
|
|
}
|
2020-03-07 13:37:26 +01:00
|
|
|
}
|
2020-03-12 17:17:53 +01:00
|
|
|
if err := ioutil.WriteFile(a, []byte(data), 0644); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}(a, &wg)
|
2020-03-07 13:37:26 +01:00
|
|
|
}
|
2020-03-12 17:17:53 +01:00
|
|
|
|
|
|
|
wg.Wait()
|
2020-03-07 13:37:26 +01:00
|
|
|
}
|