2023-05-17 09:50:17 +09:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
2023-05-20 20:12:32 +09:00
|
|
|
|
"net/url"
|
2023-05-17 09:50:17 +09:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var client = http.Client{}
|
|
|
|
|
|
|
|
|
|
func imgproxy (w http.ResponseWriter, r *http.Request) {
|
2023-05-20 20:12:32 +09:00
|
|
|
|
// URLを確認
|
|
|
|
|
uri, err := url.Parse("https:/" + r.URL.Path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HTTPリク
|
|
|
|
|
req, err := http.NewRequest("GET", "https:/" + r.URL.Path, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Pixivかどうかの確認
|
|
|
|
|
if uri.Host == "i.pximg.net" || uri.Host == "s.pximg.net" {
|
|
|
|
|
req.Header.Set("Referer", "https://www.pixiv.net/")
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 09:50:17 +09:00
|
|
|
|
// r.URL.Pathは「/」で始まるから、「https://」じゃなくて、「https:/」となります。
|
2023-05-20 20:12:32 +09:00
|
|
|
|
img, err := client.Do(req)
|
2023-05-17 09:50:17 +09:00
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintf(w, "Error %d", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 画像だけをプロクシーする様に
|
|
|
|
|
ctype := img.Header.Get("Content-Type")
|
|
|
|
|
if ctype != "image/jpeg" && ctype != "image/png" && ctype != "image/gif" && ctype != "image/tga" && ctype != "image/targa" && ctype != "image/webp" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ヘッダー
|
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprint(img.ContentLength))
|
|
|
|
|
w.Header().Set("Content-Type", img.Header.Get("Content-Type"))
|
|
|
|
|
|
|
|
|
|
// 表示
|
|
|
|
|
if _, err = io.Copy(w, img.Body); err != nil {
|
|
|
|
|
log.Fatalf("ioエラー:%v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// もう要らない
|
|
|
|
|
img.Body.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main () {
|
|
|
|
|
http.HandleFunc("/", imgproxy)
|
|
|
|
|
http.ListenAndServe(":9810", nil)
|
|
|
|
|
}
|