allow host
This commit is contained in:
parent
7917f1fcae
commit
ae4f89cb19
9 changed files with 102 additions and 5 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -16,4 +16,6 @@
|
|||
.DS_Store
|
||||
.env*
|
||||
.tmp
|
||||
dist
|
||||
dist
|
||||
ui
|
||||
^ui/index.html
|
||||
|
|
@ -2,6 +2,9 @@ os: linux
|
|||
dist: xenial
|
||||
language: go
|
||||
|
||||
addons:
|
||||
hosts:
|
||||
- local.iqka.com
|
||||
services:
|
||||
- docker
|
||||
env:
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/kun/utils"
|
||||
"github.com/yaoapp/xiang/global"
|
||||
"github.com/yaoapp/xiang/server"
|
||||
)
|
||||
|
||||
var startCmd = &cobra.Command{
|
||||
|
|
@ -24,6 +25,6 @@ var startCmd = &cobra.Command{
|
|||
Port: global.Conf.Service.Port,
|
||||
Allows: global.Conf.Service.Allow,
|
||||
Root: "/api",
|
||||
})
|
||||
}, server.Middlewares...)
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,11 @@ package global
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
assetfs "github.com/elazarl/go-bindata-assetfs"
|
||||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/xun/capsule"
|
||||
)
|
||||
|
|
@ -13,11 +17,32 @@ const VERSION = "0.6.0"
|
|||
// DOMAIN 许可域
|
||||
const DOMAIN = "*.iqka.com"
|
||||
|
||||
// AllowHosts 解析后的许可域
|
||||
var AllowHosts = []string{}
|
||||
|
||||
// Conf 配置文件
|
||||
var Conf Config
|
||||
|
||||
// FileServer 静态服务
|
||||
var FileServer http.Handler = http.FileServer(assetFS())
|
||||
|
||||
// 初始化配置
|
||||
func init() {
|
||||
|
||||
// 解析许可Host
|
||||
domains := strings.Split(DOMAIN, "|")
|
||||
for _, domain := range domains {
|
||||
|
||||
if !strings.Contains(domain, ".") {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(domain, "*.") {
|
||||
domain = strings.TrimPrefix(domain, "*.")
|
||||
}
|
||||
AllowHosts = append(AllowHosts, domain)
|
||||
}
|
||||
|
||||
Conf = NewConfig()
|
||||
|
||||
// 数据库连接
|
||||
|
|
@ -30,3 +55,14 @@ func init() {
|
|||
// 加载数据
|
||||
Load(Conf)
|
||||
}
|
||||
|
||||
func assetFS() *assetfs.AssetFS {
|
||||
assetInfo := func(path string) (os.FileInfo, error) {
|
||||
return os.Stat(path)
|
||||
}
|
||||
for k := range _bintree.Children {
|
||||
k = "ui"
|
||||
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: k, Fallback: "index.html"}
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
|
|
|||
5
go.mod
5
go.mod
|
|
@ -4,14 +4,17 @@ go 1.16
|
|||
|
||||
require (
|
||||
github.com/caarlos0/env/v6 v6.7.1 // indirect
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1 // indirect
|
||||
github.com/gin-gonic/gin v1.7.4 // indirect
|
||||
github.com/joho/godotenv v1.3.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/spf13/cobra v1.2.1 // indirect
|
||||
github.com/stretchr/testify v1.7.0
|
||||
github.com/yaoapp/gou v0.0.0-20210906083912-fc06d8ba7e7a // indirect
|
||||
github.com/yaoapp/kun v0.6.1
|
||||
github.com/yaoapp/xun v0.5.2 // indirect
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 // indirect
|
||||
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 // indirect
|
||||
)
|
||||
|
||||
// go env -w GOPRIVATE=github.com/yaoapp/*
|
||||
|
|
|
|||
8
go.sum
8
go.sum
|
|
@ -69,6 +69,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1 h1:m0kkaHRKEu7tUIUFVwhGGGYClXvyl4RE03qmvRTNfbw=
|
||||
github.com/elazarl/go-bindata-assetfs v1.0.1/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
|
|
@ -210,6 +212,8 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u
|
|||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
|
||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
|
||||
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
|
||||
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||
|
|
@ -262,6 +266,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
|
|||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
|
||||
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=
|
||||
github.com/oklog/run v1.1.0/go.mod h1:sVPdnTZT1zYwAJeCMu2Th4T21pA3FPOQRfWjQlk7DVU=
|
||||
|
|
@ -497,6 +503,8 @@ golang.org/x/sys v0.0.0-20210903071746-97244b99971b h1:3Dq0eVHn0uaQJmPO+/aYPI/fR
|
|||
golang.org/x/sys v0.0.0-20210903071746-97244b99971b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34 h1:GkvMjFtXUmahfDtashnc1mnrCtuBVcwse5QV2lUk/tI=
|
||||
golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 h1:xrCZDmdtoloIiooiA9q0OQb9r8HejIHYoHGhGCe1pGg=
|
||||
golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ func TestCommandStart(t *testing.T) {
|
|||
// 发送请求
|
||||
request := func() (maps.MapStr, error) {
|
||||
time.Sleep(time.Microsecond * 1000)
|
||||
url := fmt.Sprintf("http://%s:%d/api/user/info/1?select=id,name", "127.0.0.1", global.Conf.Service.Port)
|
||||
url := fmt.Sprintf("http://%s:%d/api/user/info/1?select=id,name", "local.iqka.com", global.Conf.Service.Port)
|
||||
// utils.Dump(url)
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
|
|
|
|||
44
server/middleware.go
Normal file
44
server/middleware.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/yaoapp/xiang/global"
|
||||
)
|
||||
|
||||
// Middlewares 服务中间件
|
||||
var Middlewares = []gin.HandlerFunc{
|
||||
BindDomain,
|
||||
BinStatic,
|
||||
}
|
||||
|
||||
// BindDomain 绑定许可域名
|
||||
func BindDomain(c *gin.Context) {
|
||||
|
||||
for _, allow := range global.AllowHosts {
|
||||
if strings.Contains(c.Request.Host, allow) {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(403, gin.H{
|
||||
"code": 403,
|
||||
"message": fmt.Sprintf("%s is not allowed", c.Request.Host),
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// BinStatic 静态文件服务
|
||||
func BinStatic(c *gin.Context) {
|
||||
if len(c.Request.URL.Path) >= 5 && c.Request.URL.Path[0:5] == "/api/" {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 静态文件请求
|
||||
global.FileServer.ServeHTTP(c.Writer, c.Request)
|
||||
c.Abort()
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
hello world
|
||||
hello world 2
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue