-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
116 lines (94 loc) · 2.32 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"bytes"
"crypto/des"
"encoding/base64"
"github.com/gin-gonic/gin"
"io/ioutil"
"net/http"
"net/url"
"os"
)
var ZHINSTA_CRYPT_KEY = os.Getenv("ZHINSTA_CRYPT_KEY")
func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(ciphertext, padtext...)
}
func PKCS5UnPadding(origData []byte) []byte {
length := len(origData)
unpadding := int(origData[length-1])
return origData[:(length - unpadding)]
}
func ZeroPadding(ciphertext []byte, blockSize int) []byte {
padding := blockSize - len(ciphertext)%blockSize
padtext := bytes.Repeat([]byte{0}, padding)
return append(ciphertext, padtext...)
}
func ZeroUnPadding(origData []byte) []byte {
return bytes.TrimFunc(origData,
func(r rune) bool {
return r == rune(0)
})
}
func urlDecrypt(encoded string) (decoded string, err error) {
key := []byte(ZHINSTA_CRYPT_KEY)
src, err := base64.URLEncoding.DecodeString(encoded)
if err != nil {
return "", err
}
cipher, err := des.NewCipher(key)
if err != nil {
println("new des failed!")
return "", err
}
out := make([]byte, len(src))
dst := out
bs := cipher.BlockSize()
for len(src) > 0 {
cipher.Decrypt(dst, src[:bs])
src = src[bs:]
dst = dst[bs:]
}
return string(PKCS5UnPadding(out)), nil
}
func newProxyHandler(c *gin.Context) {
if c.Request.Header.Get("Referer") != "http://www.zhinsta.com/" {
c.String(406, "instersting ...")
return
}
encodedUrl := c.Params.ByName("url")
picUrl, err := urlDecrypt(encodedUrl)
if err != nil {
print(err)
c.String(406, "invalid url in decrypt")
return
}
insUrl, err := url.Parse("http://" + string(picUrl))
if err != nil {
c.String(406, "invalid url")
return
}
if !isHostAllowed(insUrl.Host) {
c.String(406, "invalid url")
return
}
resp, err := http.Get(insUrl.String())
defer resp.Body.Close()
if err != nil {
c.String(502, "instagram error")
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.String(500, "read error")
}
// c.Writer.Header().Set("Cache-Control", "max-age=604800")
c.Writer.Header().Set("Expires", "Fri, 15 May 2015 12:10:16 GMT")
c.Data(200, resp.Header.Get("Content-Type"), body)
}
func main() {
r := gin.Default()
r.GET("/:url", newProxyHandler)
r.Run(":8000")
}