-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
DashJay
committed
Jun 30, 2020
1 parent
492cc28
commit c801985
Showing
9 changed files
with
199 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1. 添加中间件模式 | ||
1. 添加混合模式 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1. add middleware mode for superlcx | ||
1. add blendmode. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package core | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io" | ||
"log" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
const maxReadTime = 10 * time.Second | ||
|
||
type SapBlend struct { | ||
lis net.Listener | ||
defaultUrl *url.URL | ||
*middleware | ||
} | ||
|
||
func NewSapBlend(lis net.Listener, target string, middleware string) *SapBlend { | ||
defaultUrl, err := url.Parse(fmt.Sprintf("http://%s/", target)) | ||
if err != nil { | ||
panic(fmt.Sprintf("default url [%s] parse error, detail:[%s]", defaultUrl, err)) | ||
} | ||
log.Printf("parse default url as %s", defaultUrl) | ||
b := &SapBlend{lis: lis, defaultUrl: defaultUrl, middleware: newMiddleware(middleware)} | ||
|
||
return b | ||
} | ||
|
||
func (s *SapBlend) Serve() { | ||
log.Printf("superlcx work in blend mode!") | ||
tr := http.DefaultTransport | ||
for { | ||
conn, err := s.lis.Accept() | ||
if err != nil { | ||
log.Printf("[x] listener accept error, detail:[%s]", err) | ||
} | ||
go func() { | ||
buf := bufio.NewReader(conn) | ||
conn.SetDeadline(time.Now().Add(maxReadTime)) | ||
defer conn.Close() | ||
wait := 100 * time.Microsecond | ||
for { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
req, err := http.ReadRequest(buf) | ||
if err != nil { | ||
if err == io.EOF { | ||
time.Sleep(wait) | ||
wait *= 2 | ||
continue | ||
} else { | ||
// 这里能识别已经关闭的链接 | ||
log.Printf("[-] connection over, detail:[%s]", err) | ||
return | ||
} | ||
} | ||
newReq := req.Clone(ctx) | ||
organizeUrl(newReq, s.defaultUrl) | ||
|
||
for _, reqH := range s.reqHandlers { | ||
reqH(newReq) | ||
} | ||
resp, err := tr.RoundTrip(newReq) | ||
if err != nil { | ||
log.Printf("[x] default transport req error, detail:[%s]", err) | ||
continue | ||
} | ||
for _, respH := range s.respHandlers { | ||
respH(resp) | ||
} | ||
|
||
resp.Write(conn) | ||
cancel() | ||
} | ||
}() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package core | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
|
||
"superlcx/middlewares/stdout" | ||
) | ||
|
||
func organizeUrl(req *http.Request, target *url.URL) { | ||
if _, ok := req.Header["User-Agent"]; !ok { | ||
// explicitly disable User-Agent so it's not set to default value | ||
req.Header.Set("User-Agent", "") | ||
} | ||
singleJoiningSlash := func(a, b string) string { | ||
aslash := strings.HasSuffix(a, "/") | ||
bslash := strings.HasPrefix(b, "/") | ||
switch { | ||
case aslash && bslash: | ||
return a + b[1:] | ||
case !aslash && !bslash: | ||
return a + "/" + b | ||
} | ||
return a + b | ||
} | ||
targetQuery := target.RawQuery | ||
req.URL.Scheme = target.Scheme | ||
req.URL.Host = target.Host | ||
req.URL.Path = singleJoiningSlash(target.Path, req.URL.Path) | ||
if targetQuery == "" || req.URL.RawQuery == "" { | ||
req.URL.RawQuery = targetQuery + req.URL.RawQuery | ||
} else { | ||
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery | ||
} | ||
} | ||
|
||
type middleware struct { | ||
reqHandlers []func(req *http.Request) | ||
respHandlers []func(resp *http.Response) | ||
} | ||
|
||
func newMiddleware(mid string) *middleware { | ||
middle := &middleware{ | ||
reqHandlers: []func(req *http.Request){}, | ||
respHandlers: []func(resp *http.Response){}, | ||
} | ||
if mid != "" { | ||
ms := strings.Split(mid, ",") | ||
for _, m := range ms { | ||
switch m { | ||
case "stdout": | ||
middle.RegisterMiddleware(stdout.HandleRequest, stdout.HandleResponse) | ||
default: | ||
reqH, respH := find(m) | ||
middle.RegisterMiddleware(reqH, respH) | ||
} | ||
} | ||
} | ||
return middle | ||
} | ||
|
||
func (m *middleware) RegisterMiddleware(reqH func(req *http.Request), respH func(resp *http.Response)) { | ||
m.reqHandlers = append(m.reqHandlers, reqH) | ||
m.respHandlers = append(m.respHandlers, respH) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters