-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
221 lines (186 loc) · 5.07 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/gomodule/redigo/redis"
"github.com/kataras/iris/v12"
)
// Some const
const (
recaptchaURL = "https://recaptcha.net/recaptcha/api/siteverify"
)
// RecaptchaResponse is the struct of json recv from recaptcha.net
type RecaptchaResponse struct {
Success bool `json:"success"`
ChallengeTs time.Time `json:"challenge_ts"`
Hostname string `json:"hostname"`
Score float64 `json:"score"`
Action string `json:"action"`
}
// Info read from cli
var (
runAddress = flag.String("address", "0.0.0.0", "Pastebin Listen Port")
runPort = flag.String("port", "80", "Pastebin Bind IP")
useRecaptcha = flag.Bool("userecaptcha", false, "Use Google Recaptcha or not")
recaptchaSecret = flag.String("secretkey", "", "Recaptcha Secret Key")
recaptchaPublic = flag.String("publickey", "", "Recaptcha site key")
recaptchaScore = flag.Float64("recaptcharate", 0.6, "Recaptcha verify score")
redisAddress = flag.String("redisadd", "127.0.0.1", "RedisIP")
redisPort = flag.String("redisport", "6379", "RedisPort")
)
// Create Global var
var (
app *iris.Application
redisClient redis.Conn
err error
)
func init() {
flag.Parse()
//Create redis client
redisClient, err = redis.Dial("tcp", *redisAddress+":"+*redisPort)
if err != nil {
fmt.Println("Can't not connect to redis.")
os.Exit(2)
}
// Create Iris app
app = iris.New()
// ViewRegister
app.RegisterView(iris.HTML("./public", ".html").Reload(true))
// Static assets Handler
app.HandleDir("/css", "./public/css")
app.HandleDir("/js", "./public/js")
app.HandleDir("/img", "./public/img")
}
func main() {
// Method: GET
// Main Webpage
app.Handle("GET", "/", mainPageHandler)
// Method: POST
// Recv User input
app.Post("/paste", inputPageHandler)
// Method: GET
// Show Paste data
app.Get("/{id:string}", pasteDataHandler)
// Method: GET
// Show RAW data(actully not raw now)
app.Get("/raw/{id:string}", rawDataHandler)
// http://localhost:8964
// http://localhost:8964/paste
// http://localhost:8964/css
// http://localhost:8964/{id:string}
app.Run(iris.Addr(*runAddress+":"+*runPort), iris.WithoutServerError(iris.ErrServerClosed))
}
// Method: GET
// Main Webpage
func mainPageHandler(ctx iris.Context) {
if *useRecaptcha {
ctx.ViewData("recaptchaPublic",*recaptchaPublic)
ctx.View("input.html")
} else {
ctx.View("input_no_recaptcha.html")
}
}
// Method: POST
// Recv User input
func inputPageHandler(ctx iris.Context) {
// Verify with recaptcha
if !verify(ctx) {
ctx.View("error.html")
return
}
text := ctx.FormValue("text")
if len(text) > 81920 {
return
}
// check duration vaild
duration := ctx.FormValue("duration")
expire, err := strconv.Atoi(duration)
if err != nil || expire < 0 || expire > 18000 {
return
}
// Generate an ID with md5[0:6]
textMd5 := md5.New()
io.WriteString(textMd5, text)
textID := (hex.EncodeToString(textMd5.Sum(nil)))[0:6]
app.Logger().Infof("IP:%s Send a paste %s", ctx.RemoteAddr(), textID)
redisClient.Do("SET", textID, text, "ex", strconv.Itoa(expire))
ctx.ViewData("id", textID)
ctx.Redirect(textID, 302)
}
// Method: GET
// Show Paste data
func pasteDataHandler(ctx iris.Context) {
textID := ctx.Params().GetStringDefault("id", "")
v, err := redis.String(redisClient.Do("GET", strings.ToLower(textID)))
if err != nil {
ctx.Redirect("", 302)
} else {
ctx.ViewData("id", textID)
ctx.ViewData("content", v)
ctx.ViewData("domain", ctx.GetReferrer().URL)
ctx.View("result.html")
}
}
// Method: GET
// Show RAW data(actully not raw now)
func rawDataHandler(ctx iris.Context) {
textID := ctx.Params().GetStringDefault("id", "")
v, err := redis.String(redisClient.Do("GET", strings.ToLower(textID)))
if err != nil {
ctx.ViewData("id", textID)
} else {
// Exist XSS attack
ctx.Writef(v)
}
}
// Verify by myself but not iris
// www.google.com is not available in some region like china mainland
func verify(ctx iris.Context) bool {
if !*useRecaptcha {
return true
}
// Makeup URL
verifyURL, _ := url.Parse(recaptchaURL)
arg := verifyURL.Query()
arg.Set("secret", *recaptchaSecret)
arg.Set("response", ctx.FormValue("g-recaptcha-response"))
verifyURL.RawQuery = arg.Encode()
// Send to recaptcha verigy server
recv, err := http.Get(verifyURL.String())
if err != nil {
app.Logger().Infof("Can't to recaptcha server.")
return false
}
// Get json
result, err := ioutil.ReadAll(recv.Body)
recv.Body.Close()
if err != nil {
fmt.Println(err)
app.Logger().Infof("Connection of recaptcha server seems incorrect")
return false
}
fmt.Println(string(result))
// Unmarshal Json to Struct
var reRes RecaptchaResponse
err = json.Unmarshal(result, &reRes)
if err != nil {
fmt.Println(err)
app.Logger().Infof("Connection of recaptcha server seems incorrect")
}
// If verify secceed and user score >= recaptchaScore then return true
if reRes.Success && reRes.Score >= *recaptchaScore {
return true
}
return false
}