blog/tools/iframehttps/main.go

54 lines
1.2 KiB
Go
Raw Permalink Normal View History

2020-03-07 13:37:26 +01:00
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"path/filepath"
"regexp"
"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)
}
var wg sync.WaitGroup
2020-03-07 13:37:26 +01:00
for _, a := range articles {
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.+>)`)
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
}
if err := ioutil.WriteFile(a, []byte(data), 0644); err != nil {
log.Fatal(err)
}
}(a, &wg)
2020-03-07 13:37:26 +01:00
}
wg.Wait()
2020-03-07 13:37:26 +01:00
}