Skip to content

Commit

Permalink
slim http
Browse files Browse the repository at this point in the history
  • Loading branch information
mrhaoxx committed Feb 6, 2024
1 parent c5d1330 commit 4f19c08
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
17 changes: 13 additions & 4 deletions auth/policybase.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (mgr *policyBaseAuth) HandleAuth(ctx *http.HttpCtx) AuthRet {

}

func (mgr *policyBaseAuth) HandleHTTPInternal(ctx *http.HttpCtx) http.Ret {
func (mgr *policyBaseAuth) HandleHTTPInternal(ctx *http.HttpCtx, path string) http.Ret {
cookie, _ := ctx.Req.Cookie(verfiyCookieKey)
var session *session
var user string
Expand All @@ -182,8 +182,17 @@ func (mgr *policyBaseAuth) HandleHTTPInternal(ctx *http.HttpCtx) http.Ret {
user = session.user.name
}
}
var Maindomain string
n := strings.Split(ctx.Req.Host, ".")
if len(n) >= 2 {
rawh := strings.Join(n[len(n)-2:], ".")
n = strings.Split(rawh, ":")
Maindomain = n[0]
} else {
Maindomain = ctx.Req.Host
}

path := ctx.NilLoad(http.InternalPath).(string)[len(PrefixAuth+PrefixAuthPolicy):]
path = path[len(PrefixAuth+PrefixAuthPolicy):]

r := ctx.Req.URL.Query().Get("r")
p, err := base64.URLEncoding.DecodeString(r)
Expand Down Expand Up @@ -253,7 +262,7 @@ func (mgr *policyBaseAuth) HandleHTTPInternal(ctx *http.HttpCtx) http.Ret {
ctx.SetCookie(&stdhttp.Cookie{
Name: verfiyCookieKey,
Value: session,
Domain: ctx.NilLoad(http.Maindomain).(string),
Domain: Maindomain,
Secure: true,
Path: "/",
Expires: time.Now().Add(3 * 24 * time.Hour),
Expand Down Expand Up @@ -292,7 +301,7 @@ func (mgr *policyBaseAuth) HandleHTTPInternal(ctx *http.HttpCtx) http.Ret {
ctx.SetCookie(&stdhttp.Cookie{
Name: verfiyCookieKey,
Value: "",
Domain: ctx.NilLoad(http.Maindomain).(string),
Domain: Maindomain,
Secure: true,
Path: "/",
Expires: time.Now().Add(-1 * time.Hour),
Expand Down
21 changes: 3 additions & 18 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"sync/atomic"
"time"

tcp "github.com/mrhaoxx/OpenNG/tcp"
utils "github.com/mrhaoxx/OpenNG/utils"
"github.com/mrhaoxx/OpenNG/tcp"
"github.com/mrhaoxx/OpenNG/utils"

"github.com/andybalholm/brotli"
"golang.org/x/net/http2"
Expand All @@ -35,9 +35,6 @@ type HttpCtx struct {

closing chan struct{}

utils.StoreContext
utils.SignalContext

kill func()

onClose []func(*HttpCtx)
Expand Down Expand Up @@ -68,7 +65,6 @@ func (c *HttpCtx) Close() {
}

c.Resp.Close()
c.SignalContext.Close()
close(c.closing)
}
func (c *HttpCtx) IsClosing() <-chan struct{} {
Expand All @@ -86,8 +82,6 @@ type ServiceHandler func(*HttpCtx) Ret
const (
RequestEnd Ret = false
Continue Ret = true

Killsig uint8 = 1
)

var curreq uint64
Expand Down Expand Up @@ -142,16 +136,7 @@ func (h *Midware) head(rw http.ResponseWriter, r *http.Request, conn *tcp.Conn)
closing: make(chan struct{}),
conn: conn,
}
n := strings.Split(r.Host, ".")
if len(n) >= 2 {
rawh := strings.Join(n[len(n)-2:], ".")
ctx.Store(Mainhost, rawh)
n = strings.Split(rawh, ":")
ctx.Store(Maindomain, n[0])
} else {
ctx.Store(Mainhost, r.Host)
ctx.Store(Maindomain, r.Host)
}

h.Process(ctx)
}

Expand Down
25 changes: 13 additions & 12 deletions http/midware.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type Service interface {
}
type ServiceInternal interface {
PathsInternal() utils.GroupRegexp
HandleHTTPInternal(*HttpCtx) Ret
HandleHTTPInternal(*HttpCtx, string) Ret
}

func NewHttpMidware(sni []string) *Midware {
Expand Down Expand Up @@ -279,18 +279,19 @@ func (HMW *Midware) KillRequest(rid uint64) error {
if !ok {
return errors.New("request not found")
}
ctx.Signal(Killsig, struct{}{})

ctx.kill()

return nil
}

// @RetVal tcp.ServiceHandler RedirectToTls
//
//ng:generate def func NewTCPRedirectToTls
func NewTCPRedirectToTls() tcp.ServiceHandler {
return tcp.NewServiceFunction(func(conn *tcp.Conn) tcp.SerRet {
http.Serve(utils.ConnGetSocket(conn.TopConn()), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusPermanentRedirect)
}))
return tcp.Close
})
type redirectTLS struct{}

func (redirectTLS) Handle(conn *tcp.Conn) tcp.SerRet {
http.Serve(utils.ConnGetSocket(conn.TopConn()), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.RequestURI, http.StatusPermanentRedirect)
}))
return tcp.Close
}

var Redirect2TLS = redirectTLS{}
20 changes: 10 additions & 10 deletions http/ng.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,14 @@ func ngInternalServiceHandler(RequestCtx *HttpCtx) Ret {

path := strings.TrimPrefix(RequestCtx.Req.URL.Path, PrefixNg)

RequestCtx.Store(InternalPath, path)

s := muxBufPath.Lookup(path).([]*shInternal)

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

for _, t := range s {
switch t.ServiceHandler(RequestCtx) {
switch t.ServiceInternalHandler(RequestCtx, path) {
case RequestEnd:
goto _break
case Continue:
Expand All @@ -41,7 +39,7 @@ _break:

var mux = []*shInternal{
{
ServiceHandler: func(ctx *HttpCtx) Ret {
ServiceInternalHandler: func(ctx *HttpCtx, path string) Ret {
ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
ctx.Resp.Header().Set("Cache-Control", "no-cache")
ctx.WriteString("reqid: " + strconv.Itoa(int(ctx.Id)) + "\n" +
Expand All @@ -55,7 +53,7 @@ var mux = []*shInternal{
paths: []*regexp2.Regexp{regexp2.MustCompile("^/trace$", regexp2.None)},
},
{
ServiceHandler: func(ctx *HttpCtx) Ret {
ServiceInternalHandler: func(ctx *HttpCtx, path string) Ret {
res.WriteLogo(ctx.Resp)
return RequestEnd
},
Expand All @@ -75,13 +73,15 @@ var muxBufPath = utils.NewBufferedLookup(func(s string) interface{} {
})

type shInternal struct {
ServiceHandler
ServiceInternalHandler
paths []*regexp2.Regexp
}

func addInternal(s ServiceHandler, paths []*regexp2.Regexp) {
func addInternal(s ServiceInternalHandler, paths []*regexp2.Regexp) {
mux = append(mux, &shInternal{
ServiceHandler: s,
paths: paths,
ServiceInternalHandler: s,
paths: paths,
})
}

type ServiceInternalHandler func(*HttpCtx, string) Ret
2 changes: 1 addition & 1 deletion http/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type httpproxy struct {
buf *utils.BufferedLookup
}

func (h *httpproxy) HandleHTTPInternal(ctx *HttpCtx) Ret {
func (h *httpproxy) HandleHTTPInternal(ctx *HttpCtx, path string) Ret {
_host := h.buf.Lookup(ctx.Req.Host)
var id string
if _host != nil {
Expand Down
2 changes: 1 addition & 1 deletion ui/myservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var TcpController = tcp.NewTcpController(map[string]tcp.ServiceHandler{
"knock": Knock,
"proxier": TcpProxier,
"pph": tcp.NewTCPProxyProtocolHandler(),
"rdtls": http.NewTCPRedirectToTls(),
"rdtls": http.Redirect2TLS,
"http": HttpMidware,
"det": &tcp.Detect{Dets: []tcp.Detector{
tcp.DetectTLS,
Expand Down

0 comments on commit 4f19c08

Please sign in to comment.