From: qydysky Date: Sat, 9 Dec 2023 09:17:23 +0000 (+0800) Subject: 1 X-Git-Tag: v0.1.20231209091742 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=42ccb16df1231027976fd7b205444d84410ee7eb;p=front%2F.git 1 --- diff --git a/main.go b/main.go index 70755d1..4cd4daf 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,7 @@ type File interface { } // 加载 -func LoadPeriod(ctx context.Context, buf []byte, configF File, configS *Config, logger Logger) error { +func LoadPeriod(ctx context.Context, buf []byte, configF File, configS *[]Config, logger Logger) error { if e := loadConfig(buf, configF, configS); e != nil { logger.Error(`E:`, "配置加载", e) return e @@ -129,25 +129,28 @@ func Run(ctx context.Context, configSP *Config, logger Logger) { } } -func loadConfig(buf []byte, configF File, configS *Config) error { +func loadConfig(buf []byte, configF File, configS *[]Config) error { if i, e := configF.Read(buf); e != nil && !errors.Is(e, io.EOF) { return e } else if i == cap(buf) { return errors.New(`buf full`) } else { - configS.lock.Lock() - defer configS.lock.Unlock() + for i := 0; i < len(*configS); i++ { + (*configS)[i].lock.Lock() + defer (*configS)[i].lock.Unlock() + } if e := json.Unmarshal(buf[:i], configS); e != nil { return e } - - if configS.TLS.Key != "" && configS.TLS.Pub != "" { - if cert, e := tls.LoadX509KeyPair(configS.TLS.Pub, configS.TLS.Key); e != nil { - return e - } else { - configS.TLS.Config = &tls.Config{ - Certificates: []tls.Certificate{cert}, - NextProtos: []string{"h2", "http/1.1"}, + for i := 0; i < len((*configS)); i++ { + if (*configS)[i].TLS.Config == nil && (*configS)[i].TLS.Key != "" && (*configS)[i].TLS.Pub != "" { + if cert, e := tls.LoadX509KeyPair((*configS)[i].TLS.Pub, (*configS)[i].TLS.Key); e != nil { + return e + } else { + (*configS)[i].TLS.Config = &tls.Config{ + Certificates: []tls.Certificate{cert}, + NextProtos: []string{"h2", "http/1.1"}, + } } } } diff --git a/main/main.go b/main/main.go index 84140b3..0f45c05 100644 --- a/main/main.go +++ b/main/main.go @@ -49,7 +49,7 @@ func main() { ctx, cancle := pctx.WithWait(context.Background(), 0, time.Minute*2) // 获取config - configS := pfront.Config{} + configS := []pfront.Config{} configF := pfile.New(*configP, 0, true) if !configF.IsExist() { logger.L(`E:`, "配置不存在") @@ -67,7 +67,9 @@ func main() { // 测试响应 go pfront.Test(ctx, *testP, logger.Base("测试")) - go pfront.Run(ctx, &configS, logger.Base("转发")) + for i := 0; i < len(configS); i++ { + go pfront.Run(ctx, &configS[i], logger.Base(configS[i].Addr)) + } // ctrl+c退出 var interrupt = make(chan os.Signal, 2) diff --git a/main/main.json b/main/main.json index 3cb1ae0..7bb8f27 100644 --- a/main/main.json +++ b/main/main.json @@ -1,30 +1,57 @@ -{ - "addr":"0.0.0.0:9009", +[ + { + "addr": "0.0.0.0:9009", "tls": { - "pub":"cert.pem", - "key":"key.pem" + "pub": "cert.pem", + "key": "key.pem" }, "matchRule": "prefix", - "routes":[ - { - "path": "/1", - "errRedirect": true, - "back": [ - { - "name": "test", - "to": "http://127.0.0.1:13000", - "weight": 1, - "pathAdd": false, - "errBanSec": 10, - "resHeader":[ - { - "action": "set", - "key": "KEY", - "value": "asf" - } - ] - } + "routes": [ + { + "path": "/1", + "errRedirect": true, + "back": [ + { + "name": "test", + "to": "http://127.0.0.1:13000", + "weight": 1, + "pathAdd": false, + "errBanSec": 10, + "resHeader": [ + { + "action": "set", + "key": "KEY", + "value": "asf" + } ] - } + } + ] + } ] -} \ No newline at end of file + },{ + "addr": "0.0.0.0:9010", + "matchRule": "prefix", + "routes": [ + { + "path": "/2", + "errRedirect": true, + "back": [ + { + "name": "test", + "to": "http://127.0.0.1:13000", + "weight": 1, + "pathAdd": false, + "errBanSec": 10, + "resHeader": [ + { + "action": "set", + "key": "KEY", + "value": "asf" + } + ] + } + ] + } + ] + } +]