server.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // admin:后台管理接口
  2. package admin
  3. import (
  4. "crypto/tls"
  5. "embed"
  6. "net/http"
  7. "net/http/pprof"
  8. "github.com/arl/statsviz"
  9. "github.com/bjdgyc/anylink/base"
  10. "github.com/bjdgyc/anylink/dbdata"
  11. "github.com/gorilla/handlers"
  12. "github.com/gorilla/mux"
  13. )
  14. var UiData embed.FS
  15. // StartAdmin 开启服务
  16. func StartAdmin() {
  17. r := mux.NewRouter()
  18. r.Use(authMiddleware)
  19. r.Use(handlers.CompressHandler)
  20. // 监控检测
  21. r.HandleFunc("/status.html", func(w http.ResponseWriter, r *http.Request) {
  22. w.Write([]byte("ok"))
  23. }).Name("index")
  24. r.Handle("/", http.RedirectHandler("/ui/", http.StatusFound)).Name("index")
  25. r.PathPrefix("/ui/").Handler(
  26. // http.StripPrefix("/ui/", http.FileServer(http.Dir(base.Cfg.UiPath))),
  27. http.FileServer(http.FS(UiData)),
  28. ).Name("static")
  29. r.HandleFunc("/base/login", Login).Name("login")
  30. r.HandleFunc("/set/home", SetHome)
  31. r.HandleFunc("/set/system", SetSystem)
  32. r.HandleFunc("/set/soft", SetSoft)
  33. r.HandleFunc("/set/other", SetOther)
  34. r.HandleFunc("/set/other/edit", SetOtherEdit)
  35. r.HandleFunc("/set/other/smtp", SetOtherSmtp)
  36. r.HandleFunc("/set/other/smtp/edit", SetOtherSmtpEdit)
  37. r.HandleFunc("/set/other/audit_log", SetOtherAuditLog)
  38. r.HandleFunc("/set/other/audit_log/edit", SetOtherAuditLogEdit)
  39. r.HandleFunc("/set/audit/list", SetAuditList)
  40. r.HandleFunc("/set/audit/export", SetAuditExport)
  41. r.HandleFunc("/set/audit/act_log_list", UserActLogList)
  42. r.HandleFunc("/set/other/createcert", CreatCert)
  43. r.HandleFunc("/set/other/getcertset", GetCertSetting)
  44. r.HandleFunc("/set/other/customcert", CustomCert)
  45. r.HandleFunc("/user/list", UserList)
  46. r.HandleFunc("/user/detail", UserDetail)
  47. r.HandleFunc("/user/set", UserSet)
  48. r.HandleFunc("/user/uploaduser", UserUpload).Methods(http.MethodPost)
  49. r.HandleFunc("/user/del", UserDel)
  50. r.HandleFunc("/user/online", UserOnline)
  51. r.HandleFunc("/user/offline", UserOffline)
  52. r.HandleFunc("/user/reline", UserReline)
  53. r.HandleFunc("/user/otp_qr", UserOtpQr)
  54. r.HandleFunc("/user/ip_map/list", UserIpMapList)
  55. r.HandleFunc("/user/ip_map/detail", UserIpMapDetail)
  56. r.HandleFunc("/user/ip_map/set", UserIpMapSet)
  57. r.HandleFunc("/user/ip_map/del", UserIpMapDel)
  58. r.HandleFunc("/user/policy/list", PolicyList)
  59. r.HandleFunc("/user/policy/detail", PolicyDetail)
  60. r.HandleFunc("/user/policy/set", PolicySet)
  61. r.HandleFunc("/user/policy/del", PolicyDel)
  62. r.HandleFunc("/group/list", GroupList)
  63. r.HandleFunc("/group/names", GroupNames)
  64. r.HandleFunc("/group/names_ids", GroupNamesIds)
  65. r.HandleFunc("/group/detail", GroupDetail)
  66. r.HandleFunc("/group/set", GroupSet)
  67. r.HandleFunc("/group/del", GroupDel)
  68. r.HandleFunc("/group/auth_login", GroupAuthLogin)
  69. r.HandleFunc("/statsinfo/list", StatsInfoList)
  70. // pprof
  71. if base.Cfg.Pprof {
  72. r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline).Name("debug")
  73. r.HandleFunc("/debug/pprof/profile", pprof.Profile).Name("debug")
  74. r.HandleFunc("/debug/pprof/symbol", pprof.Symbol).Name("debug")
  75. r.HandleFunc("/debug/pprof/trace", pprof.Trace).Name("debug")
  76. r.HandleFunc("/debug/pprof", location("/debug/pprof/")).Name("debug")
  77. r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index).Name("debug")
  78. // statsviz
  79. r.Path("/debug/statsviz/ws").Name("debug").HandlerFunc(statsviz.Ws)
  80. r.PathPrefix("/debug/statsviz/").Name("debug").Handler(statsviz.Index)
  81. }
  82. base.Info("Listen admin", base.Cfg.AdminAddr)
  83. // 修复 CVE-2016-2183
  84. cipherSuites := tls.CipherSuites()
  85. selectedCipherSuites := make([]uint16, 0, len(cipherSuites))
  86. for _, s := range cipherSuites {
  87. selectedCipherSuites = append(selectedCipherSuites, s.ID)
  88. }
  89. if tlscert, _, err := dbdata.ParseCert(); err != nil {
  90. base.Fatal("证书加载失败", err)
  91. } else {
  92. dbdata.LoadCertificate(tlscert)
  93. }
  94. // 设置tls信息
  95. tlsConfig := &tls.Config{
  96. NextProtos: []string{"http/1.1"},
  97. MinVersion: tls.VersionTLS12,
  98. CipherSuites: selectedCipherSuites,
  99. GetCertificate: func(chi *tls.ClientHelloInfo) (*tls.Certificate, error) {
  100. return dbdata.GetCertificateBySNI(chi.ServerName)
  101. },
  102. }
  103. srv := &http.Server{
  104. Addr: base.Cfg.AdminAddr,
  105. Handler: r,
  106. TLSConfig: tlsConfig,
  107. }
  108. err := srv.ListenAndServeTLS("", "")
  109. if err != nil {
  110. base.Fatal(err)
  111. }
  112. }
  113. func location(url string) http.HandlerFunc {
  114. return func(w http.ResponseWriter, r *http.Request) {
  115. w.Header().Set("Location", url)
  116. w.WriteHeader(http.StatusFound)
  117. }
  118. }