imgproxy/main.go

64 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"net/http"
"io"
"log"
"net/url"
)
var version = "1.0.0"
var client = http.Client{}
func imgproxy (w http.ResponseWriter, r *http.Request) {
// 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/")
}
// r.URL.Pathは「/」で始まるから、「https://」じゃなくて、「https:/」となります。
img, err := client.Do(req)
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)
}