Skip to content

Commit

Permalink
add js_lua middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
DashJay committed Jul 2, 2020
1 parent 7812809 commit 6049079
Show file tree
Hide file tree
Showing 16 changed files with 471 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v1.0.4:
1. add js_lua middleware.

1. 添加js_lua 中间件。

v1.0.3:
1. add middleware sub_filter.

Expand Down
7 changes: 5 additions & 2 deletions FEATURES.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
EN:
1. add middleware sub_filter.
1. add js_lua middleware.

ZH:
1. 添加中间件sub_filter。
1. 添加js_lua 中间件。



9 changes: 8 additions & 1 deletion cc/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/BurntSushi/toml"
)

var C Cfg
var Config Cfg

// proxyUrl add proxy Config to help DefaultTransport send the request to
// urls specified in the configuration
Expand Down Expand Up @@ -56,6 +56,10 @@ type Cfg struct {

// sub_filter
SubFilters map[string]SubFilter `toml:"SubFilter"`

// js lua scripts
LuaPath string `toml:"LuaPath"`
JsPath string `toml:"JSPath"`
}

// InitConfig pass in a filename and reread all config from file to cover origin value
Expand All @@ -65,6 +69,9 @@ func (c *Cfg) InitConfig(filename string) error {
if _, err = toml.DecodeFile(filename, c); err != nil {
return err
}
c.SetLogFlag()
// print config after flag was set
c.Print()
if err = c.parseProxyUrls(); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions cc/magic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package cc

const UNIQUEID = "unique_id"
37 changes: 18 additions & 19 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"superlcx/core"
)

const version = "1.0.3"
const version = "1.0.4"

var (
showVersion bool
Expand All @@ -29,18 +29,19 @@ func init() {
defer func() {
err := recover()
if err != nil {
fmt.Print(err)
flag.PrintDefaults()
os.Exit(-1)
}
}()
flag.BoolVar(&showVersion, "v", false, "show version and about then exit.")
flag.StringVar(&configFile, "c", "", "load config from")
flag.IntVar(&C.ListenPort, "l", 8080, "listen port")
flag.IntVar(&C.PPROFPort, "pp", 8999, "pprof port")
flag.StringVar(&C.DefaultTarget, "host", "0.0.0.0:8081", "target host:port.")
flag.StringVar(&C.Mode, "m", "proxy", "run mode <proxy|copy|blend>.")
flag.StringVar(&C.Middleware, "M", "", "middleware, comma separated if more than one, eg: --M stdout,dumps")
flag.StringVar(&C.LogFlag, "log", "t", "l -> line of code, d -> date, t -> time, order doesn't matter")
flag.IntVar(&Config.ListenPort, "l", 8080, "listen port")
flag.IntVar(&Config.PPROFPort, "pp", 8999, "pprof port")
flag.StringVar(&Config.DefaultTarget, "host", "0.0.0.0:8081", "target host:port.")
flag.StringVar(&Config.Mode, "m", "proxy", "run mode <proxy|copy|blend>.")
flag.StringVar(&Config.Middleware, "M", "", "middleware, comma separated if more than one, eg: --M stdout,dumps")
flag.StringVar(&Config.LogFlag, "log", "t", "l -> line of code, d -> date, t -> time, order doesn't matter")
flag.Parse()
if showVersion {
fmt.Printf(`
Expand All @@ -55,44 +56,42 @@ Superlcx [%s], a tool kit for port transfer with middlewares!
`, version)
os.Exit(0)
}

if configFile != "" {
err := C.InitConfig(configFile)
err := Config.InitConfig(configFile)
if err != nil {
panic(err)
}
}
C.SetLogFlag()
// print config after flag was set
C.Print()

if C.ListenPort < 1 || C.ListenPort > 65535 {
if Config.ListenPort < 1 || Config.ListenPort > 65535 {
panic("[x] Listen Port Invalid")
}
checkHost(C.DefaultTarget)
checkHost(Config.DefaultTarget)
}

func main() {
// Buried point for debug
go func() {
http.ListenAndServe(fmt.Sprintf(":%d", C.PPROFPort), nil)
http.ListenAndServe(fmt.Sprintf(":%d", Config.PPROFPort), nil)
}()

go showMemLog()

// start listen
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", C.ListenPort))
lis, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", Config.ListenPort))
if err != nil {
panic(err)
}
switch C.Mode {
switch Config.Mode {
case "proxy":
c := core.NewSapProxy(lis, C)
c := core.NewSapProxy(lis, Config)
c.Serve()
case "copy":
c := core.NewSapCopy(lis, C)
c := core.NewSapCopy(lis, Config)
c.Serve()
case "blend":
c := core.NewSapBlend(lis, C)
c := core.NewSapBlend(lis, Config)
c.Serve()
default:
flag.PrintDefaults()
Expand Down
4 changes: 3 additions & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ ListenPort = 8081
DefaultTarget = "0.0.0.0:8080"
PPROFPort = 8999
LogFlag = "ltd"
Middleware = "sub_filter,c_header,stdout"
Middleware = "sub_filter,c_header,stdout,js_lua"
Mode = "blend"
LuaPath="./middlewares/js_lua/sub_custom.lua"
JsPath="./middlewares/js_lua/sub_custom.js"

# pass_proxy work on blend and proxy mode
[ProxyUrls]
Expand Down
11 changes: 9 additions & 2 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import (
"net/url"
"strings"

"github.com/google/uuid"

"superlcx/cc"
"superlcx/middlewares/c_header"
"superlcx/middlewares/js_lua"
"superlcx/middlewares/stdout"
"superlcx/middlewares/sub_filter"
)
Expand All @@ -29,8 +32,8 @@ func organizeUrl(req *http.Request, defaultT *url.URL) {
return a + b
}
var target *url.URL = nil
if cc.C.ProxyUrls != nil && len(cc.C.ProxyUrls) > 0 {
for _, proxyUrl := range cc.C.ProxyUrls {
if cc.Config.ProxyUrls != nil && len(cc.Config.ProxyUrls) > 0 {
for _, proxyUrl := range cc.Config.ProxyUrls {
if proxyUrl.Re.MatchString(req.URL.RequestURI()) {
target = proxyUrl.U
break
Expand All @@ -50,6 +53,8 @@ func organizeUrl(req *http.Request, defaultT *url.URL) {
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}

req.Header.Add(cc.UNIQUEID, uuid.New().String())
}

type middleware struct {
Expand All @@ -73,6 +78,8 @@ func newMiddleware(mid string) *middleware {
middle.RegisterMiddleware(c_header.HandleRequest, c_header.HandleResponse)
case "sub_filter":
middle.RegisterMiddleware(sub_filter.HandleRequest, sub_filter.HandleResponse)
case "js_lua":
middle.RegisterMiddleware(js_lua.HandleRequest, js_lua.HandleResponse)
default:
reqH, respH := find(m)
middle.RegisterMiddleware(reqH, respH)
Expand Down
16 changes: 16 additions & 0 deletions docs/middleware.CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,19 @@ newReq, err := http.ReadRequest(bufio.NewReader(bytes.NewReader(content)))
Repl='<script src="/js/jquery.min.js"></script></head>' # 替换为
Path="/" # 指定url(支持正则表达式)
```


### js_lua
使用解释型语言来保守的扩展功能,是作为频繁更新的另一选择。
该程序采用了如下两个模块库,来实现lua和js的调用
```
"github.com/robertkrimen/otto"
"github.com/yuin/gopher-lua"
```
配置如下
```toml
Middleware = "js_lua"
LuaPath="./middlewares/js_lua/sub_custom.lua"
JsPath="./middlewares/js_lua/sub_custom.js"
```
只有在middleware中添加了该脚本,并且在LuaPath和JsPath不为空时,才会载入脚本。
18 changes: 18 additions & 0 deletions docs/middleware.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ The middleware is the same as the sub_filter function in NGINX. For the specifie
Repl='<script src="/js/jquery.min.js"></script></head>' # what will be replace if matched
Path="/" # specific url(Support for regular expressions)
```

### js_lua
Using interpreted languages to conservatively extend functionality is another alternative for frequent updates.
The program USES the following two libraries to implement invoke of Lua and Js.
```
"github.com/robertkrimen/otto"
"github.com/yuin/gopher-lua"
```

Configuration is as follows

```toml
Middleware = "js_lua"
LuaPath="./middlewares/js_lua/sub_custom.lua"
JsPath="./middlewares/js_lua/sub_custom.js"
```

This two scripts will be added as middleware only when LuaPath and JsPath are not empty and middleware js_lua was added.
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ module superlcx

go 1.14

require github.com/BurntSushi/toml v0.3.1
require (
github.com/BurntSushi/toml v0.3.1
github.com/google/uuid v1.1.1
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff
github.com/yuin/gopher-lua v0.0.0-20200603152657-dc2b0ca8b37e
gopkg.in/sourcemap.v1 v1.0.5 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff h1:+6NUiITWwE5q1KO6SAfUX918c+Tab0+tGAM/mtdlUyA=
github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspoSXJTIpemEonrMDFq6XzwHYYgToXWj5eRX1OtY=
github.com/yuin/gopher-lua v0.0.0-20200603152657-dc2b0ca8b37e h1:oIpIX9VKxSCFrfjsKpluGbNPBGq9iNnT9crH781j9wY=
github.com/yuin/gopher-lua v0.0.0-20200603152657-dc2b0ca8b37e/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ=
golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
8 changes: 4 additions & 4 deletions middlewares/c_header/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

func HandleRequest(req *http.Request) {
if C.CustomHeaders == nil || len(C.CustomHeaders) == 0 {
if Config.CustomHeaders == nil || len(Config.CustomHeaders) == 0 {
return
}
for k, v := range C.CustomHeaders {
for k, v := range Config.CustomHeaders {
if strings.HasPrefix(k, "req") {
// log.Printf("add header kv to req k:[%s],v:[%s]", v.Key, v.Value)
req.Header.Set(v.Key, v.Value)
Expand All @@ -20,10 +20,10 @@ func HandleRequest(req *http.Request) {
}

func HandleResponse(resp *http.Response) {
if C.CustomHeaders == nil || len(C.CustomHeaders) == 0 {
if Config.CustomHeaders == nil || len(Config.CustomHeaders) == 0 {
return
}
for k, v := range C.CustomHeaders {
for k, v := range Config.CustomHeaders {
if strings.HasPrefix(k, "resp") {
// log.Printf("add header kv to resp k:[%s],v:[%s]", v.Key, v.Value)
resp.Header.Set(v.Key, v.Value)
Expand Down
Loading

0 comments on commit 6049079

Please sign in to comment.