Skip to content

Commit

Permalink
feat: adding more details to errorpage
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhaoxx committed Apr 22, 2024
1 parent 2a3144a commit 74731b5
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion auth/html/login_permission_denied.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<small>{{.user}} :</small><br>
<small>You do not have the permission to continue this request</small><br>
</p></div>
<p> <a href="login?r={{.r}}" style="color: black;">Refresh</a> or <a href="tmplogin?r={{.r}}" style="color: black;">Temp Login</a> or <a href="logout?r={{.r}}" style="color: black;">Logout</a></p>
<p> <a href="login?r={{.r}}" style="color: black;">Refresh</a> or <a href="logout?r={{.r}}" style="color: black;">Logout</a></p>
{{end}}
7 changes: 4 additions & 3 deletions auth/policybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,14 +332,15 @@ func (mgr *policyBaseAuth) HandleHTTPCgi(ctx *http.HttpCtx, path string) http.Re
return http.RequestEnd
case "/pwd":
if session != nil {
ctx.Resp.RefreshRedirectPage(http.StatusConflict, truepath, "You've already logged in as "+session.user.name, 2)
// ctx.Resp.RefreshRedirectPage(http.StatusConflict, truepath, "You've already logged in as "+session.user.name, 1)
ctx.Redirect(truepath, http.StatusFound)
} else {
if ctx.Req.Method == "POST" {
//get username & password
ctx.Req.ParseForm()
var userl, passl = ctx.Req.PostForm.Get("username"), ctx.Req.PostForm.Get("password")
if userl == "" || passl == "" {
ctx.Resp.ErrorPage(http.StatusBadRequest, "Username or password missing")
ctx.Resp.RefreshRedirectPage(http.StatusBadRequest, "login?r="+r, "Username or password missing", 3)
return http.RequestEnd
}

Expand All @@ -364,7 +365,7 @@ func (mgr *policyBaseAuth) HandleHTTPCgi(ctx *http.HttpCtx, path string) http.Re
// if it doesn't, the server would move it back
} else {
time.Sleep(200 * time.Millisecond) // Sleep 200ms to avoid being cracked
ctx.Resp.RefreshRedirectPage(http.StatusUnauthorized, "login?r="+r, "Username or password error", 1)
ctx.Resp.RefreshRedirectPage(http.StatusUnauthorized, "login?r="+r, "Username or password error", 3)

log.Println("%", "!", userl, "r"+strconv.FormatUint(ctx.Id, 10), ctx.Req.RemoteAddr)
}
Expand Down
2 changes: 1 addition & 1 deletion http/cgi.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (mid *Midware) ngCgi(RequestCtx *HttpCtx, RequestPath *string) {
s := mid.bufferedLookupForCgi.Lookup(path).([]*CgiStruct)

if len(s) == 0 {
RequestCtx.Resp.ErrorPage(StatusNotFound, "The requested URL "+RequestCtx.Req.RequestURI+"("+path+")"+" was not found on this server.")
RequestCtx.Resp.ErrorPage(StatusNotFound, "The requested URL "+RequestCtx.Req.RequestURI+" was not found on this server.")
}

for _, t := range s {
Expand Down
6 changes: 3 additions & 3 deletions http/forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func StdForwardProxy(ctx *HttpCtx) Ret {
if ctx.Req.Method == "CONNECT" {
server, err := net.Dial("tcp", ctx.Req.RequestURI)
if err != nil {
ctx.Resp.ErrorPage(http.StatusBadRequest, fmt.Sprintf("Dial: %v", err))
ctx.Resp.ErrorPage(http.StatusBadRequest, fmt.Sprintf("Forward: Dial: %v", err))
return RequestEnd
}
defer server.Close()

if ctx.Req.ProtoMajor == 0 || ctx.Req.ProtoMajor == 1 {
localconn, _, err := ctx.Resp.Hijack()
if err != nil {
ctx.Resp.ErrorPage(http.StatusBadRequest, fmt.Sprintf("Hijack: %v", err))
ctx.Resp.ErrorPage(http.StatusBadRequest, fmt.Sprintf("Forward: Hijack: %v", err))
return RequestEnd
}

Expand All @@ -67,7 +67,7 @@ func StdForwardProxy(ctx *HttpCtx) Ret {
resp, err := http.DefaultTransport.RoundTrip(ctx.Req)

if err != nil {
ctx.Resp.ErrorPage(http.StatusBadRequest, fmt.Sprintf("%v", err))
ctx.Resp.ErrorPage(http.StatusBadRequest, fmt.Sprintf("Forward: %v", err))
return RequestEnd
}
defer resp.Body.Close()
Expand Down
25 changes: 21 additions & 4 deletions http/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ var confirm_rawhtml string
func (rw *NgResponseWriter) ErrorPage(code int, err string) {
rw.Header().Add("Content-Type", "text/html; charset=utf-8")
rw.WriteHeader(code)
errorpage_template.Execute(rw, err)
errorpage_template.Execute(rw, struct {
MSG string
RID string
RIP string
// CID string
CODE string
UTC string
// ELA string
}{
MSG: err,
CODE: strconv.Itoa(code),
RID: strconv.FormatUint(rw.ctx.Id, 10),
RIP: rw.ctx.RemoteIP,
// CID: strconv.FormatUint(rw.ctx.conn.Id, 10),
UTC: rw.ctx.starttime.UTC().Format("2006-01-02 15:04:05 UTC"),
// ELA: time.Since(rw.ctx.starttime).String(),
})
}

func (rw *NgResponseWriter) InfoPage(code int, info string) {
Expand All @@ -41,9 +57,10 @@ func (rw *NgResponseWriter) RefreshRedirectPage(code int, url string, msg string
rw.Header().Add("Content-Type", "text/html; charset=utf-8")
rw.WriteHeader(code)
redirecting_template.Execute(rw, struct {
URL string
MSG string
}{URL: url, MSG: msg})
URL string
MSG string
TIME int
}{URL: url, MSG: msg, TIME: time})
}

func (rw *NgResponseWriter) ConfrimPage(code int, url string, msg string) {
Expand Down
10 changes: 7 additions & 3 deletions http/html/error.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<head>
<meta charset="UTF-8">
<title>Error - NetGATE</title>
<title>{{.CODE}} - NetGATE</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

<body style="text-align: center">
<img src="/ng-cgi/logo" alt="logo" style="padding-top: 3%;margin: 0 auto;" width="20%" height="20%"><br>
<br>
<p>ERROR</p>
<div><p><strong>{{.}}</a></strong></p></div>
<p>ERROR {{.CODE}}</p>
<div><p><strong>{{.MSG}}</a></strong></p></div>

<div style="position: fixed; bottom: 0; width: 100%;color: #929292;font-family: monospace; font-weight: lighter;">
RequestID: {{.RID}} · {{.RIP}} · {{.UTC}}
</div>
</body>

</html>
2 changes: 1 addition & 1 deletion http/html/redirecting.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<img src="/ng-cgi/logo" alt="logo" style="padding-top: 3%;margin: 0 auto;" width="20%" height="20%"><br>
<br>
<p><strong>{{.MSG}}</strong></p>
<div><p><small><a style="color: black;" href="{{.URL}}">You are being redirected.</a></small></p></div>
<div><p><small><a style="color: black;" href="{{.URL}}">You are being redirected in {{.TIME}} seconds.</a></small></p></div>
</body>

</html>
9 changes: 9 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type HttpCtx struct {
Id uint64
starttime time.Time

RemoteIP string

Req *http.Request
Resp *NgResponseWriter

Expand Down Expand Up @@ -125,6 +127,8 @@ func (h *Midware) head(rw http.ResponseWriter, r *http.Request, conn *tcp.Conn)

c, kill := context.WithCancel(r.Context())

ip, _, _ := net.SplitHostPort(r.RemoteAddr)

ctx := &HttpCtx{
Req: r.WithContext(c),
Resp: ngrw,
Expand All @@ -133,8 +137,11 @@ func (h *Midware) head(rw http.ResponseWriter, r *http.Request, conn *tcp.Conn)
kill: kill,
closing: make(chan struct{}),
conn: conn,
RemoteIP: ip,
}

ngrw.ctx = ctx

h.Process(ctx)
}

Expand All @@ -150,6 +157,8 @@ type NgResponseWriter struct {
writtenBytes uint64 // before compression

init sync.Once

ctx *HttpCtx
}

func (w *NgResponseWriter) Write(b []byte) (byt int, e error) {
Expand Down
24 changes: 13 additions & 11 deletions ui/myservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,22 @@ func LoadCfg(cfgs []byte) error {
}

var prik []gossh.Signer
for _, key := range cfg.SSH.PrivateKeys {
s, err := gossh.ParsePrivateKey([]byte(key))
if err != nil {
log.Println("sys", "ssh", err)
os.Exit(-1)
if len(cfg.SSH.PrivateKeys) > 0 {
for _, key := range cfg.SSH.PrivateKeys {
s, err := gossh.ParsePrivateKey([]byte(key))
if err != nil {
log.Println("sys", "ssh", err)
os.Exit(-1)
}
ak := gossh.MarshalAuthorizedKey(s.PublicKey())
log.Println("sys", "ssh", "Found private key with authorized key", string(ak[:len(ak)-1]), "fingerprint", gossh.FingerprintSHA256(s.PublicKey()))
prik = append(prik, s)

}
ak := gossh.MarshalAuthorizedKey(s.PublicKey())
log.Println("sys", "ssh", "Found private key with authorized key", string(ak[:len(ak)-1]), "fingerprint", gossh.FingerprintSHA256(s.PublicKey()))
prik = append(prik, s)
var sshs = ssh.NewSSHController(prik, cfg.SSH.Banner, pba.SSHAuthPwd, pba.SSHAuthPubKey)

builtinTcpServices["ssh"] = sshs
}
var sshs = ssh.NewSSHController(prik, cfg.SSH.Banner, pba.SSHAuthPwd, pba.SSHAuthPubKey)

builtinTcpServices["ssh"] = sshs

watcher, err := fsnotify.NewWatcher()

Expand Down

0 comments on commit 74731b5

Please sign in to comment.