micro_srv.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package micro_srv
  2. import (
  3. "context"
  4. "dashoo.cn/opms_libary/dynamic"
  5. "dashoo.cn/opms_libary/gtoken"
  6. "dashoo.cn/opms_libary/mutipart"
  7. "dashoo.cn/opms_libary/request"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "github.com/rcrowley/go-metrics"
  12. "io/ioutil"
  13. "net"
  14. "os"
  15. "path"
  16. "reflect"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/gogf/gf/encoding/gbase64"
  21. "github.com/gogf/gf/encoding/gjson"
  22. "github.com/gogf/gf/frame/g"
  23. "github.com/gogf/gf/net/ghttp"
  24. "github.com/gogf/gf/text/gstr"
  25. "github.com/smallnest/rpcx/client"
  26. "github.com/smallnest/rpcx/protocol"
  27. "github.com/smallnest/rpcx/server"
  28. "github.com/smallnest/rpcx/serverplugin"
  29. "github.com/smallnest/rpcx/share"
  30. "dashoo.cn/common_definition/auth"
  31. )
  32. var (
  33. fileTransfer = reflect.TypeOf((*mutipart.FileTransfer)(nil)).Elem()
  34. )
  35. // InitMicroSrvClient 获取微服务客户端,arg为可选参数,若有必须是两个,分别是:reg string, serverAddr string
  36. func InitMicroSrvClient(serviceName, key string, args ...string) (c client.XClient) {
  37. reg := g.Config().GetString("service_registry.registry")
  38. etcdAddr := g.Config().GetString("service_registry.server-addr")
  39. if len(args) == 2 {
  40. reg = args[0]
  41. etcdAddr = args[1]
  42. }
  43. config := g.Config().GetString(key)
  44. arr := strings.Split(config, ",")
  45. srvName := arr[0]
  46. if len(arr) == 2 { // 点对点 直连
  47. d, _ := client.NewPeer2PeerDiscovery("tcp@"+arr[1], "")
  48. c = client.NewXClient(serviceName, client.Failtry, client.RandomSelect, d, client.DefaultOption)
  49. return c
  50. } else {
  51. if reg == "consul" { // 服务发现使用consul
  52. //d, _ := etcd_client.NewEtcdV3Discovery(srvName, serviceName, []string{etcdAddr}, nil)
  53. d, _ := client.NewConsulDiscovery(srvName, serviceName, []string{etcdAddr}, nil)
  54. c = client.NewXClient(serviceName, client.Failover, client.RoundRobin, d, client.DefaultOption)
  55. return c
  56. }
  57. }
  58. return nil
  59. }
  60. func CreateAndInitService(basePath string) *server.Server {
  61. srvAddr := g.Config().GetString("setting.bind-addr")
  62. fileAddr := g.Config().GetString("setting.bind-mutipart-addr")
  63. etcdAddr := g.Config().GetString("service_registry.server-addr")
  64. s := server.NewServer()
  65. if fileAddr != "" {
  66. p := server.NewStreamService(fileAddr, streamHandler, nil, 1000)
  67. s.EnableStreamService(share.StreamServiceName, p)
  68. }
  69. advertiseAddr := srvAddr
  70. if g.Config().GetBool("setting.need-advertise-addr") {
  71. advertiseAddr = g.Config().GetString("setting.advertise-addr")
  72. }
  73. g.Log().Infof("服务启动, basePath: %v, MicorSrv: %v", basePath, srvAddr)
  74. reg := g.Config().GetString("service_registry.registry")
  75. //if reg == "etcd" {
  76. // addEtcdRegistryPlugin(s, basePath, advertiseAddr, etcdAddr)
  77. //}
  78. if reg == "consul" {
  79. addConsulRegistryPlugin(s, basePath, advertiseAddr, etcdAddr)
  80. }
  81. return s
  82. }
  83. func addConsulRegistryPlugin(s *server.Server, basePath, srvAddr, consulAddr string) {
  84. r := &serverplugin.ConsulRegisterPlugin{
  85. ServiceAddress: "tcp@" + srvAddr,
  86. ConsulServers: []string{consulAddr},
  87. BasePath: basePath,
  88. Metrics: metrics.NewRegistry(),
  89. UpdateInterval: time.Minute,
  90. }
  91. err := r.Start()
  92. if err != nil {
  93. g.Log().Fatal(err)
  94. }
  95. g.Log().Infof("注册到Consul: %v, basePath: %v, MicorSrv: %v", consulAddr, basePath, srvAddr)
  96. s.Plugins.Add(r)
  97. }
  98. func streamHandler(conn net.Conn, args *share.StreamServiceArgs) {
  99. defer conn.Close()
  100. fileName := args.Meta["file_name"]
  101. suffix := path.Ext(fileName)
  102. tmpFile, err := ioutil.TempFile(os.TempDir(), "multipart-*"+suffix)
  103. if err != nil {
  104. g.Log().Error(err)
  105. return
  106. }
  107. defer os.Remove(tmpFile.Name())
  108. fileSize := args.Meta["file_size"]
  109. size, _ := strconv.Atoi(fileSize)
  110. total := 0
  111. buf := make([]byte, 4096)
  112. for {
  113. n, _ := conn.Read(buf)
  114. total += n
  115. _, err = tmpFile.Write(buf)
  116. if err != nil {
  117. g.Log().Error(err)
  118. return
  119. }
  120. //如果实际总接受字节数与客户端给的要传输字节数相等,说明传输完毕
  121. if total == size {
  122. beanName, ok := args.Meta["bean_name"]
  123. if ok {
  124. result, err := handDynamicService(beanName, tmpFile, args.Meta)
  125. if err != nil {
  126. resp := make(map[string]interface{})
  127. resp["code"] = 500
  128. resp["data"] = err.Error()
  129. result, _ = json.Marshal(resp)
  130. }
  131. conn.Write(result)
  132. } else {
  133. es := errors.New(fmt.Sprintf("Service[%s] 没有实现HandleFileTransfer接口", args.Meta["bean_name"]))
  134. g.Log().Error(es)
  135. resp := make(map[string]interface{})
  136. resp["code"] = 500
  137. resp["data"] = es.Error()
  138. result, _ := json.Marshal(resp)
  139. conn.Write(result)
  140. }
  141. break
  142. }
  143. }
  144. conn.Close()
  145. }
  146. func handDynamicService(beanName string, file *os.File, param map[string]string) ([]byte, error) {
  147. bean := dynamic.BeanFactory.GetBean(beanName)
  148. if !bean.IsValid() {
  149. ce := errors.New(fmt.Sprintf("Service[%s] 没有注册", beanName))
  150. g.Log().Error(ce)
  151. return nil, ce
  152. }
  153. typ := bean.Type()
  154. if bean.CanInterface() && typ.Implements(fileTransfer) {
  155. service := bean.Interface().(mutipart.FileTransfer)
  156. resp, err := service.HandleFileTransfer(file, param)
  157. if err != nil {
  158. ce := errors.New(fmt.Sprintf("Service[%s] 文件处理异常", beanName))
  159. g.Log().Error(ce, err)
  160. return nil, ce
  161. } else {
  162. return json.Marshal(resp)
  163. }
  164. } else {
  165. ce := errors.New(fmt.Sprintf("Service[%s] 没有实现HandleFileTransfer接口", beanName))
  166. g.Log().Error(ce)
  167. return nil, ce
  168. }
  169. }
  170. // HandleAuth 处理Auth认证
  171. func HandleAuth(ctx context.Context, req *protocol.Message, token string, authExcludePaths []string) error {
  172. path := "/" + req.ServicePath + "/" + req.ServiceMethod
  173. //g.Log().Info("reqPath: ", path)
  174. //g.Log().Info("token: ", token)
  175. if authPath(path, authExcludePaths) {
  176. req.Metadata["authExclude"] = "false"
  177. var rsp gtoken.Resp
  178. notAuthSrv := ctx.Value("NotAuthSrv")
  179. if notAuthSrv != nil && notAuthSrv.(bool) {
  180. rsp = gtoken.GFToken.ValidToken(token)
  181. } else {
  182. rsp = validToken(token)
  183. }
  184. //return errors.New("InvalidToken")
  185. if rsp.Code != 0 {
  186. return errors.New("InvalidToken")
  187. }
  188. //userInfo, err := getUserInfoFromToken(rsp)
  189. //if err!=nil{
  190. // return err
  191. //}
  192. //g.Dump(userInfo)
  193. if req.Metadata != nil {
  194. req.Metadata["userInfo"] = rsp.DataString()
  195. }
  196. return nil
  197. }
  198. return nil
  199. }
  200. // 判断路径是否需要进行认证拦截
  201. // return true 需要认证
  202. func authPath(urlPath string, authExcludePaths []string) bool {
  203. // 去除后斜杠
  204. if strings.HasSuffix(urlPath, "/") {
  205. urlPath = gstr.SubStr(urlPath, 0, len(urlPath)-1)
  206. }
  207. // 排除路径处理,到这里nextFlag为true
  208. for _, excludePath := range authExcludePaths {
  209. tmpPath := excludePath
  210. // 前缀匹配
  211. if strings.HasSuffix(tmpPath, "/*") {
  212. tmpPath = gstr.SubStr(tmpPath, 0, len(tmpPath)-2)
  213. if gstr.HasPrefix(urlPath, tmpPath) {
  214. // 前缀匹配不拦截
  215. return false
  216. }
  217. } else {
  218. // 全路径匹配
  219. if strings.HasSuffix(tmpPath, "/") {
  220. tmpPath = gstr.SubStr(tmpPath, 0, len(tmpPath)-1)
  221. }
  222. if urlPath == tmpPath {
  223. // 全路径匹配不拦截
  224. return false
  225. }
  226. }
  227. }
  228. return true
  229. }
  230. // 验证token
  231. func validToken(token string) gtoken.Resp {
  232. grsp := gtoken.Resp{}
  233. if token == "" {
  234. grsp.Code = 401
  235. grsp.Msg = "valid token empty"
  236. return grsp
  237. }
  238. authService := InitMicroSrvClient("Auth", "micro_srv.auth")
  239. defer authService.Close()
  240. rsp := &auth.Response{}
  241. err := authService.Call(context.TODO(), "ValidToken", token, rsp)
  242. if err != nil {
  243. g.Log().Error(err)
  244. grsp.Code = 401
  245. return grsp
  246. }
  247. grsp.Code = int(rsp.Code)
  248. grsp.Msg = rsp.Msg
  249. grsp.Data = rsp.Data
  250. return grsp
  251. }
  252. // IsAuthExclude 是否进行auth验证
  253. func IsAuthExclude(ctx context.Context) bool {
  254. reqMeta := ctx.Value(share.ReqMetaDataKey).(map[string]string)
  255. flag, ok := reqMeta["authExclude"]
  256. if !ok || flag == "true" {
  257. return true
  258. }
  259. return false
  260. }
  261. // GetUserInfo 从context中获取UserInfo
  262. func GetUserInfo(ctx context.Context) (request.UserInfo, error) {
  263. reqMeta := ctx.Value(share.ReqMetaDataKey).(map[string]string)
  264. userStr, ok := reqMeta["userInfo"]
  265. if !ok {
  266. return request.UserInfo{}, errors.New("不存在UserInfo数据")
  267. }
  268. userInfo, err := getUserInfoDataString(userStr)
  269. if err != nil {
  270. return request.UserInfo{}, err
  271. }
  272. return userInfo, nil
  273. }
  274. // GetTenant 从context中获取租户码
  275. func GetTenant(ctx context.Context) (string, error) {
  276. reqMeta := ctx.Value(share.ReqMetaDataKey).(map[string]string)
  277. tenant, ok := reqMeta["tenant"]
  278. if !ok {
  279. return "", errors.New("不存在租户码")
  280. }
  281. return tenant, nil
  282. }
  283. // GetReqMethod 从context中获取请求方式
  284. func GetReqMethod(ctx context.Context) (string, error) {
  285. reqMeta := ctx.Value(share.ReqMetaDataKey).(map[string]string)
  286. reqMethod, ok := reqMeta["reqMethod"]
  287. if !ok {
  288. return "", errors.New("获取请求方式异常")
  289. }
  290. return reqMethod, nil
  291. }
  292. // GetToken 从context中获取Token
  293. func GetToken(ctx context.Context) (string, error) {
  294. reqMeta := ctx.Value(share.ReqMetaDataKey).(map[string]string)
  295. token, ok := reqMeta["__AUTH"]
  296. if !ok {
  297. return "", errors.New("token获取失败")
  298. }
  299. return token, nil
  300. }
  301. // GetBrowserInfo 从context中获取ClientIP和UserAgent
  302. func GetBrowserInfo(ctx context.Context) (clientIP string, userAgent string, err error) {
  303. reqMeta := ctx.Value(share.ReqMetaDataKey).(map[string]string)
  304. clientIP, ok := reqMeta["clientIP"]
  305. if !ok {
  306. return "", "", errors.New("BrowserInfo获取失败")
  307. }
  308. userAgent, ok = reqMeta["userAgent"]
  309. if !ok {
  310. return "", "", errors.New("BrowserInfo获取失败")
  311. }
  312. userAgent, err = gbase64.DecodeToString(userAgent)
  313. return
  314. }
  315. // getUserInfoDataString 从userInfo字符串转换成对象
  316. func getUserInfoDataString(userInfoString string) (request.UserInfo, error) {
  317. var userInfo request.UserInfo
  318. //uuid := ""
  319. if j, err := gjson.DecodeToJson([]byte(userInfoString)); err != nil {
  320. g.Log().Error(err)
  321. return userInfo, err
  322. } else {
  323. j.SetViolenceCheck(true)
  324. err = j.GetStruct("data", &userInfo)
  325. if err != nil {
  326. g.Log().Error(err)
  327. return userInfo, err
  328. }
  329. }
  330. return userInfo, nil
  331. }
  332. // SetTenant 设置租户码(传统WebAPI调用使用)
  333. func SetTenant(tenant string) context.Context {
  334. metadata := map[string]string{"tenant": tenant}
  335. return context.WithValue(context.Background(), share.ReqMetaDataKey, metadata)
  336. }
  337. // SetTenantAndAuth 设置租户码和认证信息(传统WebAPI调用使用)
  338. func SetTenantAndAuth(r *ghttp.Request, client client.XClient) context.Context {
  339. // 处理Auth
  340. token := getRequestToken(r)
  341. if token != "" {
  342. client.Auth(token)
  343. }
  344. // 处理租户码
  345. tenant := request.GetTenant(r)
  346. metadata := map[string]string{"tenant": tenant}
  347. return context.WithValue(context.Background(), share.ReqMetaDataKey, metadata)
  348. }
  349. // 解析token,若无,返回空
  350. func getRequestToken(r *ghttp.Request) string {
  351. authHeader := r.Header.Get("Authorization")
  352. if authHeader != "" {
  353. parts := strings.SplitN(authHeader, " ", 2)
  354. if !(len(parts) == 2 && parts[0] == "Bearer") {
  355. return ""
  356. } else if parts[1] == "" {
  357. return ""
  358. }
  359. return parts[1]
  360. }
  361. return ""
  362. }