コミット
498043a6a7
|
@ -5,8 +5,15 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func stripurl (url string) string {
|
||||
res := strings.ReplaceAll(url, "?", "")
|
||||
res = strings.ReplaceAll(res, "=", "")
|
||||
return res
|
||||
}
|
||||
|
||||
func getpage (url string, path string) {
|
||||
curl, err := http.Get(url)
|
||||
if err != nil {
|
||||
|
|
8
main.go
8
main.go
|
@ -31,7 +31,8 @@ func main () {
|
|||
return
|
||||
} else {
|
||||
if checkprefix(args[1]) {
|
||||
exist := checkexist(args[1], cnf.datapath)
|
||||
eurl := stripurl(args[1])
|
||||
exist := checkexist(eurl, cnf.datapath)
|
||||
var confirm string
|
||||
if len(exist) > 0 {
|
||||
fmt.Println("このページが既に保存されているみたいです。")
|
||||
|
@ -42,10 +43,9 @@ func main () {
|
|||
fmt.Scanf("%s", &confirm)
|
||||
}
|
||||
if len(exist) == 0 || confirm == "y" || confirm == "Y" {
|
||||
path := mkdirs(args[1], cnf.datapath)
|
||||
path := mkdirs(eurl, cnf.datapath)
|
||||
getpage(args[1], path)
|
||||
// TODO: ページの確認
|
||||
scanpage(path, args[1], cnf.datapath)
|
||||
scanpage(path, eurl, cnf.datapath)
|
||||
fmt.Println(cnf.domain + strings.Replace(path, cnf.datapath, "", 1))
|
||||
}
|
||||
return
|
||||
|
|
172
scanpage.go
172
scanpage.go
|
@ -5,16 +5,17 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"io"
|
||||
"regexp"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func scanpage (path string, domain string, thisdomain string) error {
|
||||
fn, err := os.ReadFile(path + "/index.html")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("ファイルを開けられなかった:")
|
||||
return err
|
||||
}
|
||||
|
||||
/* 削除 */
|
||||
|
@ -24,74 +25,131 @@ func scanpage (path string, domain string, thisdomain string) error {
|
|||
var video = regexp.MustCompile(`(<video.*</video>)`).ReplaceAllString(string(audio), "")
|
||||
var iframe = regexp.MustCompile(`(<iframe.*</iframe>)`).ReplaceAllString(string(video), "")
|
||||
/* 追加ダウンロード+ローカル化 */
|
||||
var ass = regexp.MustCompile(`(<img.*src="|<meta.*content="|<link.*href=")(.*\.)(png|webm|jpg|jpeg|gif|css|js)`)
|
||||
var ass = regexp.MustCompile(`(<img.*src=['"]|<meta.*content=['"]|<link.*href=['"])(.*\.)(png|webm|jpg|jpeg|gif|css|js|ico|svg|tiff|woff2)(\?[^'"]*)?`)
|
||||
|
||||
spath := "static/"
|
||||
if !strings.HasSuffix(path, "/") {
|
||||
spath = "/" + spath
|
||||
}
|
||||
spath = path + spath
|
||||
err1 := os.Mkdir(spath, 0755)
|
||||
if err1 != nil {
|
||||
fmt.Println(err1)
|
||||
return errors.New("失敗:")
|
||||
err = os.Mkdir(spath, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
repmap := make(map[string]string)
|
||||
|
||||
for _, cssx := range ass.FindAllString(iframe, -1) {
|
||||
s := regexp.MustCompile(`(.*src="|.*content="|.*href=")`).Split(cssx, -1)
|
||||
ss := regexp.MustCompile(`(".*)`).Split(s[1], -1)
|
||||
if strings.HasPrefix(ss[0], "http://") || strings.HasPrefix(ss[0], "https://") {
|
||||
// TODO
|
||||
} else {
|
||||
fss := strings.Split(ss[0], "/")
|
||||
filename := fss[len(fss)-1]
|
||||
s := regexp.MustCompile(`(.*src=['"]|.*content=['"]|.*href=['"])`).Split(cssx, -1)
|
||||
ss := regexp.MustCompile(`(['"].*)`).Split(s[1], -1)
|
||||
|
||||
if filename == "" {
|
||||
continue
|
||||
}
|
||||
f, err := os.Create(spath + filename)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("2. 作成失敗:")
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
af := domain + ss[0]
|
||||
if !strings.HasPrefix(ss[0], "/") {
|
||||
af = domain + "/" + ss[0]
|
||||
}
|
||||
i, err := http.Get(af)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("2. ダウンロードに失敗:")
|
||||
}
|
||||
defer i.Body.Close()
|
||||
if strings.HasSuffix(filename, "css") || strings.HasSuffix(filename, "js") {
|
||||
body, err := io.ReadAll(i.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("2. 読込エラー:")
|
||||
}
|
||||
|
||||
_, err2 := f.WriteString(string(body))
|
||||
if err2 != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("2. ファイル書込エラー:")
|
||||
}
|
||||
} else {
|
||||
_, err = io.Copy(f, i.Body)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("2. コピーに失敗:")
|
||||
}
|
||||
}
|
||||
|
||||
iframe = strings.Replace(iframe, ss[0], "/static/" + filename, -1)
|
||||
if strings.HasPrefix(ss[0], "//") {
|
||||
ss[0] = "https:" + ss[0]
|
||||
}
|
||||
|
||||
fss := strings.Split(ss[0], "/")
|
||||
assdom := ""
|
||||
filename := fss[len(fss)-1]
|
||||
|
||||
if strings.HasPrefix(ss[0], "http://") || strings.HasPrefix(ss[0], "https://") {
|
||||
assdom = fss[2]
|
||||
}
|
||||
|
||||
asspath := path + "/static/" + assdom
|
||||
err = os.MkdirAll(asspath, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if filename == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(ss[0], "http://") || strings.HasPrefix(ss[0], "https://") {
|
||||
err = dlres(ss[0], filepath.Join(asspath, filename))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
af := domain
|
||||
if strings.HasPrefix(ss[0], "/") {
|
||||
af = af + ss[0]
|
||||
} else {
|
||||
af = af + "/" + ss[0]
|
||||
}
|
||||
err = dlres(af, filepath.Join(asspath, filename))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
repmap[ss[0]] = filepath.Join("/static", assdom, filename)
|
||||
if assdom == "" {
|
||||
repmap[ss[0]] = filepath.Join("/static", filename)
|
||||
}
|
||||
|
||||
err := os.WriteFile(path + "/index.html", []byte(iframe), 0644)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("書込に失敗")
|
||||
return errors.New("2. ダウンロードに失敗:")
|
||||
}
|
||||
}
|
||||
|
||||
for ourl, lurl := range repmap {
|
||||
aurl := strings.ReplaceAll(path, thisdomain, "") + stripver(lurl)
|
||||
iframe = strings.ReplaceAll(iframe, ourl, aurl)
|
||||
}
|
||||
|
||||
err = os.WriteFile(path + "/index.html", []byte(iframe), 0644)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return errors.New("書込に失敗")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func stripver (durl string) string {
|
||||
u, err := url.Parse(durl)
|
||||
if err != nil {
|
||||
fmt.Println("エラー:", err)
|
||||
return ""
|
||||
}
|
||||
u.RawQuery = ""
|
||||
return u.Path
|
||||
}
|
||||
|
||||
func dlres (durl string, dest string) error {
|
||||
// ダウンロード
|
||||
res, err := http.Get(stripver(durl))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
dest = stripver(dest)
|
||||
|
||||
// ファイルを作成
|
||||
f, err := os.Create(dest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// ファイルを書き込む
|
||||
if strings.HasSuffix(dest, "css") || strings.HasSuffix(dest, "js") {
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err2 := f.WriteString(string(body))
|
||||
if err2 != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
_, err = io.Copy(f, res.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
124
srv.go
124
srv.go
|
@ -35,27 +35,83 @@ func initloc (r *http.Request) string {
|
|||
}
|
||||
}
|
||||
|
||||
func serv (cnf Config, port int) {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
|
||||
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
|
||||
func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
|
||||
return func (w http.ResponseWriter, r *http.Request) {
|
||||
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
|
||||
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
|
||||
|
||||
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(200)
|
||||
buf, _ := json.MarshalIndent(&Stat{Url: cnf.domain, Ver: version}, "", " ")
|
||||
_, _ = w.Write(buf)
|
||||
})
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
lang := initloc(r)
|
||||
pth := r.URL.Path
|
||||
|
||||
data.Lan = lang
|
||||
ftmpl[0] = cnf.webpath + "/view/index.html"
|
||||
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/archive") {
|
||||
data.Tit = getloc("top", lang)
|
||||
if r.Method == "POST" {
|
||||
err := r.ParseForm()
|
||||
if err != nil { fmt.Println(err) }
|
||||
|
||||
// クッキー
|
||||
if r.PostForm.Get("langchange") != "" {
|
||||
cookie, err := r.Cookie("lang")
|
||||
if err != nil || cookie.Value == "ja" {
|
||||
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "en", MaxAge: 31536000, Path: "/"})
|
||||
} else {
|
||||
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
|
||||
}
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
if r.PostForm.Get("hozonsite") != "" {
|
||||
fmt.Println("sasa")
|
||||
url := r.PostForm.Get("hozonsite")
|
||||
fmt.Println(url)
|
||||
// HTTPかHTTPSじゃない場合
|
||||
if !checkprefix(url) {
|
||||
data.Err = getloc("errfuseiurl", lang)
|
||||
ftmpl[0] = cnf.webpath + "/view/404.html"
|
||||
} else {
|
||||
//if r.PostForm.Get("sosin") != "" {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
tmpl.Execute(w, data)
|
||||
}
|
||||
}
|
||||
|
||||
func apiHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
|
||||
return func (w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
w.WriteHeader(200)
|
||||
buf, _ := json.MarshalIndent(&Stat{Url: cnf.domain, Ver: version}, "", " ")
|
||||
_, _ = w.Write(buf)
|
||||
}
|
||||
}
|
||||
|
||||
func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
|
||||
return func (w http.ResponseWriter, r *http.Request) {
|
||||
ftmpl := []string{cnf.webpath + "/view/index.html", cnf.webpath + "/view/header.html", cnf.webpath + "/view/footer.html"}
|
||||
data := &Page{Ver: version, Ves: strings.ReplaceAll(version, ".", "")}
|
||||
lang := initloc(r)
|
||||
|
||||
data.Lan = lang
|
||||
ftmpl[0] = cnf.webpath + "/view/index.html"
|
||||
tmpl := template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
path := strings.TrimPrefix(r.URL.Path, "/archive/")
|
||||
|
||||
if strings.Contains(path, "/static/") {
|
||||
if !strings.HasSuffix(path, ".css") && !strings.HasSuffix(path, ".png") && !strings.HasSuffix(path, ".jpeg") && !strings.HasSuffix(path, ".jpg") && !strings.HasSuffix(path, ".webm") && !strings.HasSuffix(path, ".gif") && !strings.HasSuffix(path, ".js") {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
fpath := cnf.datapath + "/archive/" + path
|
||||
http.ServeFile(w, r, fpath)
|
||||
} else {
|
||||
pth := r.URL.Path
|
||||
if !strings.HasSuffix(pth, "/") && !strings.HasSuffix(pth, "index.html") {
|
||||
pth += "/index.html"
|
||||
} else if strings.HasSuffix(pth, "/") && !strings.HasSuffix(pth, "index.html") {
|
||||
|
@ -72,40 +128,16 @@ func serv (cnf Config, port int) {
|
|||
tmpl = template.Must(template.ParseFiles(cnf.webpath + "/view/archive.html"))
|
||||
tmpl.Execute(w, data)
|
||||
data = nil
|
||||
} else {
|
||||
data.Tit = getloc("top", lang)
|
||||
if r.Method == "POST" {
|
||||
err := r.ParseForm()
|
||||
if err != nil { fmt.Println(err) }
|
||||
|
||||
// クッキー
|
||||
if r.PostForm.Get("langchange") != "" {
|
||||
cookie, err := r.Cookie("lang")
|
||||
if err != nil || cookie.Value == "ja" {
|
||||
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "en", MaxAge: 31536000, Path: "/"})
|
||||
} else {
|
||||
http.SetCookie(w, &http.Cookie {Name: "lang", Value: "ja", MaxAge: 31536000, Path: "/"})
|
||||
}
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
|
||||
if r.PostForm.Get("hozonsite") != "" {
|
||||
url := r.PostForm.Get("hozonsite")
|
||||
// HTTPかHTTPSじゃない場合
|
||||
if !checkprefix(url) {
|
||||
data.Err = getloc("errfuseiurl", lang)
|
||||
ftmpl[0] = cnf.webpath + "/view/404.html"
|
||||
} else {
|
||||
//if r.PostForm.Get("sosin") != "" {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tmpl = template.Must(template.ParseFiles(ftmpl[0], ftmpl[1], ftmpl[2]))
|
||||
tmpl.Execute(w, data)
|
||||
})
|
||||
func serv (cnf Config, port int) {
|
||||
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
|
||||
|
||||
http.HandleFunc("/api/", apiHandler(cnf))
|
||||
http.HandleFunc("/archive/", archiveHandler(cnf))
|
||||
http.HandleFunc("/", siteHandler(cnf))
|
||||
|
||||
fmt.Println(fmt.Sprint("http://127.0.0.1:", port, " でサーバーを実行中。終了するには、CTRL+Cを押して下さい。"))
|
||||
http.ListenAndServe(fmt.Sprint(":", port), nil)
|
||||
|
|
読み込み中…
新しいイシューから参照