Skip to content

Commit 8f3c624

Browse files
committed
update
1 parent 1de4017 commit 8f3c624

File tree

10 files changed

+37
-22
lines changed

10 files changed

+37
-22
lines changed

blog_default.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ addr = ":8888"
88
assets = "ROOT/assets"
99
images = "/tmp/images"
1010
db_file = "/tmp/blog.db"
11+
# affect remote addr
12+
proxy_mode = true
1113
[common.logging]
1214
roll_size = 10 # MB
1315
roll_interval = 720 # hour

conf/conf.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ type Blacklist struct {
3131
}
3232

3333
type Common struct {
34-
Addr string `toml:"addr"`
35-
Assets string `toml:"assets"`
36-
Images string `toml:"images"`
37-
DbFile string `toml:"db_file"`
38-
Logging Logging `toml:"logging"`
34+
Addr string `toml:"addr"`
35+
Assets string `toml:"assets"`
36+
Images string `toml:"images"`
37+
DbFile string `toml:"db_file"`
38+
Logging Logging `toml:"logging"`
39+
ProxyMode bool `toml:"proxy_mode"`
3940
}
4041

4142
type Logging struct {

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func main() {
5353

5454
access := middleware.NewAccess()
5555

56-
r := routers.New(mgr)
56+
r := routers.New(mgr, cfg.Common.ProxyMode)
5757
r.ServeFile(cfg.Service.Assets, cfg.Common.Assets)
5858
r.AddCb(access)
5959

middleware/access.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ func NewAccess() *Access {
2020
}
2121

2222
func (a *Access) Serve(c *routers.Context) {
23-
logging.Info("ACCESS: %s %s %s", c.Req.RemoteAddr, c.Req.Method, c.Req.URL.String())
23+
logging.Info("ACCESS: %s %s %s", c.RemoteAddr(), c.Req.Method, c.Req.URL.String())
2424
}

routers/context.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"io/ioutil"
1515
"net/http"
1616
"strconv"
17+
"strings"
1718
)
1819

1920
const (
@@ -23,11 +24,12 @@ const (
2324
)
2425

2526
type Context struct {
26-
Req *http.Request
27-
Resp http.ResponseWriter
28-
param *paramImpl
29-
mgr *session.SessionManager
30-
next bool
27+
Req *http.Request
28+
Resp http.ResponseWriter
29+
param *paramImpl
30+
mgr *session.SessionManager
31+
next bool
32+
proxyMode bool
3133
}
3234

3335
func (c *Context) simpleResponse(mime string, code int, data []byte) {
@@ -86,3 +88,11 @@ func (c *Context) Next() {
8688
func (c *Context) Abort() {
8789
c.next = false
8890
}
91+
92+
func (c *Context) RemoteAddr() string {
93+
if c.proxyMode {
94+
return c.Req.Header.Get("X-Real-IP")
95+
} else {
96+
return strings.Split(c.Req.RemoteAddr, ":")[0]
97+
}
98+
}

routers/routers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ type Router struct {
2424
trie map[string]*node
2525
lru *LRU
2626
mgr *session.SessionManager
27+
proxyMode bool
2728
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
2829
NotFound func(w http.ResponseWriter, r *http.Request)
2930
callbacks []IHandler
3031
}
3132

32-
func New(mgr *session.SessionManager) *Router {
33+
func New(mgr *session.SessionManager, proxyMode bool) *Router {
3334
return &Router{
3435
trie: make(map[string]*node),
3536
lru: NewLRU(100, 50<<20), // cache max 100 items total 50MB
3637
mgr: mgr,
38+
proxyMode: proxyMode,
3739
callbacks: make([]IHandler, 0),
3840
NotFound: http.NotFound,
3941
}
@@ -56,7 +58,7 @@ func (r *Router) dispatch(w http.ResponseWriter, req *http.Request) {
5658
if handlers == nil {
5759
r.NotFound(w, req)
5860
} else {
59-
ctx := &Context{req, w, param, r.mgr, true}
61+
ctx := &Context{req, w, param, r.mgr, true, r.proxyMode}
6062
for _, cb := range r.callbacks {
6163
cb.Serve(ctx)
6264
if !ctx.next {

service/editor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (a *Editor) Put(c *routers.Context) {
9898
if err != nil {
9999
c.Json(http.StatusBadRequest, newError(-1, err.Error()))
100100
} else {
101-
a.Update(c.Req.URL.Query().Get("id"))
101+
a.Update(c.GetParam("id"))
102102
c.Json(http.StatusOK, newError(0, ""))
103103
}
104104
}
@@ -117,7 +117,7 @@ func (a *Editor) Put(c *routers.Context) {
117117
if err != nil {
118118
c.Json(http.StatusBadRequest, newError(-2, err.Error()))
119119
} else {
120-
a.Update(c.Req.URL.Query().Get("id"))
120+
a.Update(c.GetParam("id"))
121121
c.Json(http.StatusOK, newError(0, ""))
122122
}
123123
}

service/home.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func (h *Home) Serve(c *routers.Context) {
127127
}
128128

129129
func (h *Home) formatTime(arg interface{}) string {
130-
return arg.(time.Time).Format("2006 01-02 15:04:05")
130+
return arg.(time.Time).Format("2006-01-02 15:04:05")
131131
}
132132

133133
func (h *Home) Update(arg interface{}) {

service/login.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"blog/model"
1515
"blog/routers"
1616
"net/http"
17-
"strings"
1817
"sync"
1918
"time"
2019
)
@@ -130,8 +129,9 @@ func (l *Login) Serve(c *routers.Context) {
130129
}
131130

132131
func (l *Login) HandlePost(c *routers.Context) {
133-
remoteIp := strings.Split(c.Req.RemoteAddr, ":")[0]
132+
remoteIp := c.RemoteAddr()
134133
if !l.bl.valid(remoteIp) {
134+
logging.Warn("too many attempts: %s", remoteIp)
135135
c.Json(http.StatusBadRequest, newError(-1, "fuck you!"))
136136
return
137137
}

session/session.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (m *SessionManager) newSession(w http.ResponseWriter, r *http.Request) (ISe
188188
sid := buildId()
189189
session, e := m.backend.Init(sid)
190190
if e != nil {
191-
logging.Info("SessionManager::newSession: %s for: %s", e.Error(), r.RemoteAddr)
191+
logging.Info("SessionManager::newSession: %s", e.Error())
192192
}
193193
cookie := http.Cookie{Name: m.name, Value: url.QueryEscape(sid), Path: "/", HttpOnly: true, MaxAge: int(m.maxAge)}
194194
http.SetCookie(w, &cookie)
@@ -206,11 +206,11 @@ func (m *SessionManager) StartSession(w http.ResponseWriter, r *http.Request) (s
206206
} else {
207207
sid, e := url.QueryUnescape(cookie.Value)
208208
if e != nil {
209-
logging.Error("unescape cookie: %s, for: %s", e.Error(), r.RemoteAddr)
209+
logging.Error("unescape cookie: %s", e.Error())
210210
}
211211
session, e = m.backend.Get(sid)
212212
if e != nil {
213-
logging.Info("session id is not exists(maybe a previous seesion), create a new one for: %s", r.RemoteAddr)
213+
logging.Info("session id is not exists(maybe a previous session), create a new one")
214214
session, _ = m.newSession(w, r)
215215
}
216216
}

0 commit comments

Comments
 (0)